3434#include " score.h"
3535#include " segment.h"
3636#include " staff.h"
37-
3837#include " log.h"
3938
4039using namespace mu ;
@@ -82,8 +81,15 @@ InstrumentChange::~InstrumentChange()
8281void InstrumentChange::setInstrument (const Instrument& i)
8382{
8483 *m_instrument = i;
85- // delete _instrument;
86- // _instrument = new Instrument(i);
84+ // Number of staves for this instrument
85+ size_t n = i.cleffTypeCount ();
86+ _concertClefs.resize (n);
87+ _transposingClefs.resize (n);
88+ for (size_t s = 0 ; s < n; ++s) {
89+ ClefTypeList ct = i.clefType (s);
90+ _concertClefs[s] = ct.concertClef ;
91+ _transposingClefs[s] = ct.transposingClef ;
92+ }
8793}
8894
8995void InstrumentChange::setupInstrument (const Instrument* instrument)
@@ -101,17 +107,26 @@ void InstrumentChange::setupInstrument(const Instrument* instrument)
101107
102108 // change the clef for each staff
103109 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- }
110+ ClefType oldClefType = concPitch ? part->instrument (tickStart)->clefType (i).concertClef
111+ : part->instrument (tickStart)->clefType (i).transposingClef ;
112+ ClefTypeList ct = instrument->clefType (i);
113+ ClefType newClefType = concPitch ? ct.concertClef : ct.transposingClef ;
114+ if (ClefInfo::symId (oldClefType) != ClefInfo::symId (newClefType)) {
115+ EngravingItem* element = rtick ().isZero () ? toEngravingItem (findMeasure ()) : toEngravingItem (this );
116+ score ()->undoChangeClef (part->staff (i), element, newClefType, true );
114117 }
118+ }
119+
120+ for (size_t i = 0 ; i < part->nstaves (); i++) {
121+ Staff* staff = part->staff (i);
122+
123+ ClefType oldClef = staff->clef (tickStart);
124+ ClefType newClef = clefTypeForCurrentLayout (i);
125+
126+ if (ClefInfo::symId (oldClef) != ClefInfo::symId (newClef)) {
127+ score ()->undoChangeClef (staff, this , newClef, true );
128+ }
129+ }
115130
116131 // Change key signature if necessary. CAUTION: not necessary in case of octave-transposing!
117132 if ((v.chromatic - oldV.chromatic ) % 12 ) {
@@ -213,4 +228,13 @@ engraving::PropertyValue InstrumentChange::propertyDefault(Pid propertyId) const
213228 return TextBase::propertyDefault (propertyId);
214229 }
215230}
231+
232+ ClefType InstrumentChange::clefTypeForCurrentLayout (size_t staffIndex) const
233+ {
234+ bool isConcert = score ()->style ().styleB (Sid::concertPitch);
235+ return isConcert
236+ ? _concertClefs[staffIndex]
237+ : _transposingClefs[staffIndex];
238+ }
239+
216240}
0 commit comments