Skip to content

Commit

Permalink
Merge pull request musescore#14804 from RomanPudashkin/change_instrum…
Browse files Browse the repository at this point in the history
…ent_fix

[MU4] Fix musescore#14722: Incorrect transposition when changing from one transposing instrument to another
  • Loading branch information
RomanPudashkin authored Nov 30, 2022
2 parents d7adc67 + f00f544 commit ac3a095
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/engraving/libmscore/chordrest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,19 @@ EngravingItem* ChordRest::drop(EditData& data)
if (part()->instruments().find(tick().ticks()) != part()->instruments().end()) {
LOGD() << "InstrumentChange already exists at tick = " << tick().ticks();
delete e;
return 0;
return nullptr;
} else {
InstrumentChange* ic = toInstrumentChange(e);
ic->setParent(segment());
ic->setTrack(trackZeroVoice(track()));
Instrument* instr = part()->instrument(tick());
ic->setInstrument(instr);

const Instrument* instr = part()->instrument(tick());
IF_ASSERT_FAILED(instr) {
delete e;
return nullptr;
}

ic->setInstrument(*instr);
score()->undoAddElement(ic);
return e;
}
Expand Down
1 change: 1 addition & 0 deletions src/engraving/libmscore/part.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ using namespace mu;
using namespace mu::engraving;

namespace mu::engraving {
const Fraction Part::MAIN_INSTRUMENT_TICK = Fraction(-1, 1);
//---------------------------------------------------------
// Part
//---------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions src/engraving/libmscore/part.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class Part final : public EngravingObject
friend class compat::Read206;

public:
static const Fraction MAIN_INSTRUMENT_TICK;

Part(Score* score = nullptr);
void initFromInstrTemplate(const InstrumentTemplate*);

Expand Down
1 change: 1 addition & 0 deletions src/instrumentsscene/view/instrumentsettingsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void InstrumentSettingsModel::load(const QVariant& instrument)
QVariantMap map = instrument.toMap();
m_instrumentKey.partId = ID(map["partId"]);
m_instrumentKey.instrumentId = map["instrumentId"].toString();
m_instrumentKey.tick = Part::MAIN_INSTRUMENT_TICK;

const Part* part = notationParts()->part(m_instrumentKey.partId);
if (!part) {
Expand Down
2 changes: 1 addition & 1 deletion src/notation/internal/masternotationparts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void MasterNotationParts::replaceInstrument(const InstrumentKey& instrumentKey,
startGlobalEdit();

const Part* part = partModifiable(instrumentKey.partId);
bool isMainInstrument = part && isMainInstrumentForPart(instrumentKey.instrumentId, part);
bool isMainInstrument = part && isMainInstrumentForPart(instrumentKey, part);

NotationParts::replaceInstrument(instrumentKey, newInstrument);

Expand Down
2 changes: 1 addition & 1 deletion src/notation/internal/notationparts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ void NotationParts::replaceInstrument(const InstrumentKey& instrumentKey, const

startEdit();

if (isMainInstrumentForPart(instrumentKey.instrumentId, part)) {
if (isMainInstrumentForPart(instrumentKey, part)) {
mu::engraving::Interval oldTranspose = part->instrument()->transpose();

QString newInstrumentPartName = formatInstrumentTitle(newInstrument.trackName(), newInstrument.trait());
Expand Down
4 changes: 2 additions & 2 deletions src/notation/notationtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ struct InstrumentKey
Fraction tick = mu::engraving::Fraction(0, 1);
};

inline bool isMainInstrumentForPart(const QString& instrumentId, const Part* part)
inline bool isMainInstrumentForPart(const InstrumentKey& instrumentKey, const Part* part)
{
return instrumentId == part->instrumentId();
return instrumentKey.instrumentId == part->instrumentId() && instrumentKey.tick == Part::MAIN_INSTRUMENT_TICK;
}

inline QString formatInstrumentTitle(const QString& instrumentName, const InstrumentTrait& trait)
Expand Down
9 changes: 7 additions & 2 deletions src/notation/view/widgets/editstaff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,17 @@ void EditStaff::setStaff(Staff* s, const Fraction& tick)
Part* part = m_orgStaff->part();
mu::engraving::Score* score = part->score();

m_instrument = *part->instrument(tick);
auto it = mu::findLessOrEqual(part->instruments(), tick.ticks());
if (it == part->instruments().cend()) {
return;
}

m_instrument = *it->second;
m_orgInstrument = m_instrument;

m_instrumentKey.instrumentId = m_instrument.id();
m_instrumentKey.partId = part->id();
m_instrumentKey.tick = tick;
m_instrumentKey.tick = Fraction::fromTicks(it->first);

m_staff = engraving::Factory::createStaff(part);
mu::engraving::StaffType* stt = m_staff->setStaffType(Fraction(0, 1), *m_orgStaff->staffType(Fraction(0, 1)));
Expand Down

0 comments on commit ac3a095

Please sign in to comment.