Skip to content

Commit e8f5523

Browse files
committed
Fix
1 parent 67f8616 commit e8f5523

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

src/engraving/dom/instrchange.cpp

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "score.h"
3535
#include "segment.h"
3636
#include "staff.h"
37-
37+
#include "../editing/editclef.h"
3838
#include "log.h"
3939

4040
using namespace mu;
@@ -82,8 +82,15 @@ InstrumentChange::~InstrumentChange()
8282
void 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

8996
void InstrumentChange::setupInstrument(const Instrument* instrument)
@@ -99,19 +106,44 @@ 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+
if (!measure || !segment()) continue;
121+
122+
EngravingItem* element = rtick().isZero()
123+
? static_cast<EngravingItem*>(measure)
124+
: static_cast<EngravingItem*>(segment());
125+
126+
score()->undoChangeClef(part->staff(i), element, newClefType, true);
127+
128+
// locate the clef just created and set both CP/TP variants for this staff
129+
Fraction tick = tickStart;
130+
track_idx_t track = part->staff(i)->idx() * VOICES;
131+
Measure* m = score()->tick2measure(tick);
132+
if (!m) continue;
133+
Fraction rt = rtick().isZero() ? m->ticks() : rtick();
134+
Segment* seg = m->findSegment(SegmentType::Clef, rt);
135+
if (!seg) seg = m->findSegment(SegmentType::HeaderClef, rt);
136+
if (!seg) continue;
137+
Clef* created = toClef(seg->element(track));
138+
if (!created) continue;
139+
140+
ClefType cp = _concertClefs[i];
141+
ClefType tp = _transposingClefs[i];
142+
143+
score()->undo(new ChangeClefType(created, cp, tp));
114144
}
145+
}
146+
115147

116148
// Change key signature if necessary. CAUTION: not necessary in case of octave-transposing!
117149
if ((v.chromatic - oldV.chromatic) % 12) {
@@ -213,4 +245,13 @@ engraving::PropertyValue InstrumentChange::propertyDefault(Pid propertyId) const
213245
return TextBase::propertyDefault(propertyId);
214246
}
215247
}
248+
249+
ClefType InstrumentChange::clefTypeForCurrentLayout(size_t staffIndex) const
250+
{
251+
bool isConcert = score()->style().styleB(Sid::concertPitch);
252+
return isConcert
253+
? _concertClefs[staffIndex]
254+
: _transposingClefs[staffIndex];
255+
}
256+
216257
}

src/engraving/dom/instrchange.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ class InstrumentChange final : public TextBase
6767

6868
bool placeMultiple() const override { return false; }
6969

70+
ClefType clefTypeForCurrentLayout(size_t staffIndex) const;
71+
72+
std::vector<ClefType> _concertClefs;
73+
std::vector<ClefType> _transposingClefs;
74+
7075
private:
7176

7277
Instrument* m_instrument = nullptr; // Staff holds ownership if part of score

0 commit comments

Comments
 (0)