3434#include " score.h"
3535#include " segment.h"
3636#include " staff.h"
37-
37+ # include " ../editing/editclef.h "
3838#include " log.h"
3939
4040using namespace mu ;
@@ -82,8 +82,15 @@ InstrumentChange::~InstrumentChange()
8282void InstrumentChange::setInstrument (const Instrument& i)
8383{
8484 *m_instrument = i;
85- // delete _instrument;
86- // _instrument = new Instrument(i);
85+ // Number of staves for this instrument
86+ size_t n = i.cleffTypeCount ();
87+ _concertClefs.resize (n);
88+ _transposingClefs.resize (n);
89+ for (size_t s = 0 ; s < n; ++s) {
90+ ClefTypeList ct = i.clefType (s);
91+ _concertClefs[s] = ct.concertClef ;
92+ _transposingClefs[s] = ct.transposingClef ;
93+ }
8794}
8895
8996void InstrumentChange::setupInstrument (const Instrument* instrument)
@@ -99,19 +106,41 @@ void InstrumentChange::setupInstrument(const Instrument* instrument)
99106 Interval v = instrument->transpose ();
100107 bool concPitch = style ().styleB (Sid::concertPitch);
101108
102- // change the clef for each staff
103- for (size_t i = 0 ; i < part->nstaves (); i++) {
104- ClefType oldClefType = concPitch ? part->instrument (tickStart)->clefType (i).concertClef
105- : part->instrument (tickStart)->clefType (i).transposingClef ;
106- ClefType newClefType = concPitch ? instrument->clefType (i).concertClef
107- : instrument->clefType (i).transposingClef ;
108- // Introduce cleff change only if the new clef *symbol* is different from the old one
109- if (ClefInfo::symId (oldClefType) != ClefInfo::symId (newClefType)) {
110- // If instrument change is at the start of a measure, use the measure as the element, as this will place the instrument change before the barline.
111- EngravingItem* element = rtick ().isZero () ? toEngravingItem (findMeasure ()) : toEngravingItem (this );
112- score ()->undoChangeClef (part->staff (i), element, newClefType, true );
113- }
109+ // change the clef for each staff
110+ for (size_t i = 0 ; i < part->nstaves (); i++) {
111+ ClefType oldClefType = concPitch
112+ ? part->instrument (tickStart)->clefType (i).concertClef
113+ : part->instrument (tickStart)->clefType (i).transposingClef ;
114+
115+ ClefTypeList ct = instrument->clefType (i);
116+ ClefType newClefType = concPitch ? ct.concertClef : ct.transposingClef ;
117+
118+ if (ClefInfo::symId (oldClefType) != ClefInfo::symId (newClefType)) {
119+ Measure* measure = segment ()->measure ();
120+ EngravingItem* element = rtick ().isZero () ? static_cast <EngravingItem*>(measure) : static_cast <EngravingItem*>(this );
121+
122+ score ()->undoChangeClef (part->staff (i), element, newClefType, true );
123+
124+ // locate the clef just created and set both CP/TP variants for this staff
125+ Fraction tick = tickStart;
126+ track_idx_t track = part->staff (i)->idx () * VOICES;
127+ Measure* m = score ()->tick2measure (tick);
128+ if (!m) continue ;
129+ Fraction rt = rtick ().isZero () ? m->ticks () : rtick ();
130+ Segment* seg = m->findSegment (SegmentType::Clef, rt);
131+ if (!seg) seg = m->findSegment (SegmentType::HeaderClef, rt);
132+ if (!seg) continue ;
133+ Clef* created = toClef (seg->element (track));
134+ if (!created) continue ;
135+
136+ // use InstrumentChange stored variants
137+ ClefType cp = concertClefType (i);
138+ ClefType tp = transposingClefType (i);
139+
140+ score ()->undo (new ChangeClefType (created, cp, tp));
114141 }
142+ }
143+
115144
116145 // Change key signature if necessary. CAUTION: not necessary in case of octave-transposing!
117146 if ((v.chromatic - oldV.chromatic ) % 12 ) {
@@ -213,4 +242,13 @@ engraving::PropertyValue InstrumentChange::propertyDefault(Pid propertyId) const
213242 return TextBase::propertyDefault (propertyId);
214243 }
215244}
245+
246+ ClefType InstrumentChange::clefTypeForCurrentLayout (size_t staffIndex) const
247+ {
248+ bool isConcert = score ()->style ().styleB (Sid::concertPitch);
249+ return isConcert
250+ ? _concertClefs[staffIndex]
251+ : _transposingClefs[staffIndex];
252+ }
253+
216254}
0 commit comments