Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/engraving/dom/instrchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,20 @@
Interval oldV = part->instrument(tickStart)->transpose();
Interval oldKv = staff()->transpose(tickStart);
Interval v = instrument->transpose();
bool concPitch = style().styleB(Sid::concertPitch);

Check warning on line 100 in src/engraving/dom/instrchange.cpp

View workflow job for this annotation

GitHub Actions / windows_x64

'concPitch': local variable is initialized but not referenced

Check warning on line 100 in src/engraving/dom/instrchange.cpp

View workflow job for this annotation

GitHub Actions / build (linux_arm64)

unused variable ‘concPitch’ [-Wunused-variable]

Check warning on line 100 in src/engraving/dom/instrchange.cpp

View workflow job for this annotation

GitHub Actions / build (linux_x64)

unused variable ‘concPitch’ [-Wunused-variable]

// change the clef for each staff
for (size_t i = 0; i < part->nstaves(); i++) {
ClefType oldClefType = concPitch ? part->instrument(tickStart)->clefType(i).concertClef
: part->instrument(tickStart)->clefType(i).transposingClef;
ClefType newClefType = concPitch ? instrument->clefType(i).concertClef
: instrument->clefType(i).transposingClef;
// Introduce cleff change only if the new clef *symbol* is different from the old one
if (ClefInfo::symId(oldClefType) != ClefInfo::symId(newClefType)) {
// 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.
ClefTypeList oldClefTypeList = part->instrument(tickStart)->clefType(i);
ClefTypeList newClefTypeList = instrument->clefType(i);
if (ClefInfo::symId(oldClefTypeList.concertClef) != ClefInfo::symId(newClefTypeList.concertClef)
|| ClefInfo::symId(oldClefTypeList.transposingClef) != ClefInfo::symId(newClefTypeList.transposingClef)) {
Comment on lines +105 to +106
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Compare full clef types instead of glyph IDs.

Line 105 and Line 106 compare only ClefInfo::symId(...). Different clef types can share the same symbol, so required IC clef updates may be skipped.

Proposed fix
-        if (ClefInfo::symId(oldClefTypeList.concertClef) != ClefInfo::symId(newClefTypeList.concertClef)
-            || ClefInfo::symId(oldClefTypeList.transposingClef) != ClefInfo::symId(newClefTypeList.transposingClef)) {
+        if (oldClefTypeList.concertClef != newClefTypeList.concertClef
+            || oldClefTypeList.transposingClef != newClefTypeList.transposingClef) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (ClefInfo::symId(oldClefTypeList.concertClef) != ClefInfo::symId(newClefTypeList.concertClef)
|| ClefInfo::symId(oldClefTypeList.transposingClef) != ClefInfo::symId(newClefTypeList.transposingClef)) {
if (oldClefTypeList.concertClef != newClefTypeList.concertClef
|| oldClefTypeList.transposingClef != newClefTypeList.transposingClef) {

int cp = static_cast<int>(newClefTypeList.concertClef);
int tp = static_cast<int>(newClefTypeList.transposingClef);
int packedData = 1000 + (cp & 0xFF) + ((tp & 0xFF) << 8);
Comment on lines +107 to +109
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Extract IC clef packing into shared constants/helper.

Line 109 uses inline 1000 and 0xFF. Please move this packing contract to a shared helper (or shared constants) used by both encode/decode sites to prevent divergence.

Refactor direction
-            int cp = static_cast<int>(newClefTypeList.concertClef);
-            int tp = static_cast<int>(newClefTypeList.transposingClef);
-            int packedData = 1000 + (cp & 0xFF) + ((tp & 0xFF) << 8);
+            constexpr int kIcClefPackOffset = 1000;
+            constexpr int kIcClefMask = 0xFF;
+            const int cp = static_cast<int>(newClefTypeList.concertClef);
+            const int tp = static_cast<int>(newClefTypeList.transposingClef);
+            const int packedData = kIcClefPackOffset + (cp & kIcClefMask) + ((tp & kIcClefMask) << 8);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
int cp = static_cast<int>(newClefTypeList.concertClef);
int tp = static_cast<int>(newClefTypeList.transposingClef);
int packedData = 1000 + (cp & 0xFF) + ((tp & 0xFF) << 8);
constexpr int kIcClefPackOffset = 1000;
constexpr int kIcClefMask = 0xFF;
const int cp = static_cast<int>(newClefTypeList.concertClef);
const int tp = static_cast<int>(newClefTypeList.transposingClef);
const int packedData = kIcClefPackOffset + (cp & kIcClefMask) + ((tp & kIcClefMask) << 8);

EngravingItem* element = rtick().isZero() ? toEngravingItem(findMeasure()) : toEngravingItem(this);
score()->undoChangeClef(part->staff(i), element, newClefType, true);
score()->undoChangeClef(part->staff(i), element, newClefTypeList.concertClef, packedData);
}
}

// Change key signature if necessary. CAUTION: not necessary in case of octave-transposing!
if ((v.chromatic - oldV.chromatic) % 12) {
for (size_t i = 0; i < part->nstaves(); i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/engraving/dom/score.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ class Score : public EngravingObject, public muse::Contextable
void undoChangeTuning(Note*, double);
void undoChangeUserMirror(Note*, DirectionH);
void undoChangeKeySig(Staff* ostaff, const Fraction& tick, KeySigEvent);
void undoChangeClef(Staff* ostaff, EngravingItem*, ClefType st, bool forInstrumentChange = false, Clef* clefToRelink = nullptr);
void undoChangeClef(Staff* ostaff, EngravingItem*, ClefType st, int forInstrumentChange = false, Clef* clefToRelink = nullptr);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Type change from bool to int with false default is semantically confusing.

While false implicitly converts to 0 in C++, using a boolean literal as the default for an int parameter obscures the intent. Additionally, the parameter name forInstrumentChange no longer accurately describes its content when it carries packed clef data.

Consider:

  1. Change the default to 0 for clarity.
  2. Rename the parameter (e.g., instrumentChangeData or clefPackedData) to reflect its dual purpose.
  3. Add a brief doc comment explaining the packing scheme (1000 + concertClef + (transposingClef << 8)), or define named constants for the magic offset.
Proposed minimal fix for default value
-    void undoChangeClef(Staff* ostaff, EngravingItem*, ClefType st, int forInstrumentChange = false, Clef* clefToRelink = nullptr);
+    void undoChangeClef(Staff* ostaff, EngravingItem*, ClefType st, int forInstrumentChange = 0, Clef* clefToRelink = nullptr);

For a more robust API, consider introducing a small struct or enum to encapsulate the instrument-change clef data instead of relying on magic-number packing in a public interface.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
void undoChangeClef(Staff* ostaff, EngravingItem*, ClefType st, int forInstrumentChange = false, Clef* clefToRelink = nullptr);
void undoChangeClef(Staff* ostaff, EngravingItem*, ClefType st, int forInstrumentChange = 0, Clef* clefToRelink = nullptr);

bool undoPropertyChanged(EngravingItem* item, Pid propId, const PropertyValue& propValue,
PropertyFlags propFlags = PropertyFlags::NOSTYLE);
void undoPropertyChanged(EngravingObject*, Pid, const PropertyValue& v, PropertyFlags ps = PropertyFlags::NOSTYLE);
Expand Down
40 changes: 27 additions & 13 deletions src/engraving/editing/edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6173,7 +6173,7 @@ void Score::updateInstrumentChangeTranspositions(KeySigEvent& key, Staff* staff,
// create a clef before element e
//---------------------------------------------------------

void Score::undoChangeClef(Staff* ostaff, EngravingItem* e, ClefType ct, bool forInstrumentChange, Clef* clefToRelink)
void Score::undoChangeClef(Staff* ostaff, EngravingItem* e, ClefType ct, int forInstrumentChange, Clef* clefToRelink)
{
IF_ASSERT_FAILED(ostaff && e) {
return;
Expand Down Expand Up @@ -6211,6 +6211,14 @@ void Score::undoChangeClef(Staff* ostaff, EngravingItem* e, ClefType ct, bool fo
Fraction tick = e->tick();
Fraction rtick = e->rtick();
bool isSmall = (st == SegmentType::Clef);
bool isIC = (forInstrumentChange >= 1000);
ClefType cp = ct;
ClefType tp = ct;
if (isIC) {
int val = forInstrumentChange - 1000;
cp = static_cast<ClefType>(static_cast<signed char>(val & 0xFF));
tp = static_cast<ClefType>(static_cast<signed char>((val >> 8) & 0xFF));
}
for (Staff* staff : ostaff->staffList()) {
if (clefToRelink && ostaff == staff) {
continue;
Expand Down Expand Up @@ -6240,7 +6248,7 @@ void Score::undoChangeClef(Staff* ostaff, EngravingItem* e, ClefType ct, bool fo

StaffType* staffType = staff->staffType(e->tick());
StaffGroup staffGroup = staffType->group();
if (ClefInfo::staffGroup(ct) != staffGroup && !forInstrumentChange) {
if (ClefInfo::staffGroup(ct) != staffGroup && !isIC) {
continue;
}

Expand All @@ -6250,17 +6258,18 @@ void Score::undoChangeClef(Staff* ostaff, EngravingItem* e, ClefType ct, bool fo
// clef type for concertPitch
//
Instrument* i = staff->part()->instrument(tick);
ClefType cp, tp;
if (i->transpose().isZero()) {
cp = ct;
tp = ct;
} else {
if (concertPitch) {
if (!isIC) {
if (i->transpose().isZero()) {
cp = ct;
tp = clef->transposingClef();
} else {
cp = clef->concertClef();
tp = ct;
} else {
if (concertPitch) {
cp = ct;
tp = clef->transposingClef();
} else {
cp = clef->concertClef();
tp = ct;
}
}
}
clef->setGenerated(false);
Expand All @@ -6286,15 +6295,20 @@ void Score::undoChangeClef(Staff* ostaff, EngravingItem* e, ClefType ct, bool fo
clef->setScore(score);
} else {
clef = Factory::createClef(score->dummy()->segment());
clef->setClefType(ct);
if (isIC) {
clef->setTransposingClef(tp);
clef->setConcertClef(cp);
} else {
clef->setClefType(ct);
}
gclef = clef;
}
clef->setTrack(track);
clef->setParent(destSeg);
clef->setIsHeader(st == SegmentType::HeaderClef);
score->doUndoAddElement(clef);
}
if (forInstrumentChange) {
if (isIC) {
clef->setForInstrumentChange(true);
}
clef->setSmall(isSmall);
Expand Down
Loading