|
32 | 32 | #include "editing/editsystemlocks.h" |
33 | 33 | #include "types/typesconv.h" |
34 | 34 |
|
| 35 | +// notation |
| 36 | +#include "notation/inotation.h" |
| 37 | +#include "notation/inotationparts.h" |
| 38 | +#include "notation/notationtypes.h" |
| 39 | + |
35 | 40 | // api |
36 | 41 | #include "apistructs.h" |
37 | 42 | #include "cursor.h" |
@@ -158,6 +163,49 @@ void Score::appendPartByMusicXmlId(const QString& instrumentMusicXmlId) |
158 | 163 | score()->appendPart(t); |
159 | 164 | } |
160 | 165 |
|
| 166 | +//--------------------------------------------------------- |
| 167 | +// Score::replaceInstrument |
| 168 | +//--------------------------------------------------------- |
| 169 | + |
| 170 | +void Score::replaceInstrument(apiv1::Part* part, const QString& instrumentId) |
| 171 | +{ |
| 172 | + if (!part) { |
| 173 | + LOGW("replaceInstrument: part is null"); |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + const InstrumentTemplate* t = searchTemplate(instrumentId); |
| 178 | + if (!t) { |
| 179 | + LOGW("replaceInstrument: <%s> not found", qPrintable(instrumentId)); |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + // Get the notation interface to access INotationParts |
| 184 | + mu::notation::INotationPtr notationPtr = notation(); |
| 185 | + if (!notationPtr) { |
| 186 | + LOGW("replaceInstrument: notation is null"); |
| 187 | + return; |
| 188 | + } |
| 189 | + |
| 190 | + mu::notation::INotationPartsPtr parts = notationPtr->parts(); |
| 191 | + if (!parts) { |
| 192 | + LOGW("replaceInstrument: notation parts is null"); |
| 193 | + return; |
| 194 | + } |
| 195 | + |
| 196 | + // Create InstrumentKey for the main instrument of the part |
| 197 | + mu::notation::InstrumentKey instrumentKey; |
| 198 | + instrumentKey.partId = muse::ID(part->part()->id()); |
| 199 | + instrumentKey.instrumentId = muse::String::fromQString(part->part()->instrumentId()); |
| 200 | + instrumentKey.tick = mu::engraving::Fraction(0, 1); // Main instrument at tick 0 |
| 201 | + |
| 202 | + // Create the new Instrument from the template |
| 203 | + mu::engraving::Instrument newInstrument = mu::engraving::Instrument::fromTemplate(t); |
| 204 | + |
| 205 | + // Call replaceInstrument through the notation layer |
| 206 | + parts->replaceInstrument(instrumentKey, newInstrument); |
| 207 | +} |
| 208 | + |
161 | 209 | //--------------------------------------------------------- |
162 | 210 | // Score::firstSegment |
163 | 211 | //--------------------------------------------------------- |
|
0 commit comments