Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #222031: remove ties and spanners when removing induvidual notes within a chord #3564

Merged
merged 1 commit into from
Jun 2, 2018
Merged
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
10 changes: 2 additions & 8 deletions libmscore/chord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,7 @@ void Chord::add(Element* e)
}
if (!found)
_notes.push_back(note);
if (note->tieFor()) {
if (note->tieFor()->endNote())
note->tieFor()->endNote()->setTieBack(note->tieFor());
}
note->connectTiedNotes();
if (voice() && measure() && note->visible())
measure()->setHasVoices(staffIdx(), true);
}
Expand Down Expand Up @@ -529,10 +526,7 @@ void Chord::remove(Element* e)
auto i = std::find(_notes.begin(), _notes.end(), note);
if (i != _notes.end()) {
_notes.erase(i);
if (note->tieFor()) {
if (note->tieFor()->endNote())
note->tieFor()->endNote()->setTieBack(0);
}
note->disconnectTiedNotes();
for (Spanner* s : note->spannerBack())
note->removeSpannerBack(s);
for (Spanner* s : note->spannerFor())
Expand Down
30 changes: 30 additions & 0 deletions libmscore/note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3122,6 +3122,36 @@ std::vector<Note*> Note::tiedNotes() const
return notes;
}

//---------------------------------------------------------
// disconnectTiedNotes
//---------------------------------------------------------

void Note::disconnectTiedNotes()
{
if (tieBack() && tieBack()->startNote()) {
tieBack()->startNote()->remove(tieBack());
}
if (tieFor() && tieFor()->endNote()) {
tieFor()->endNote()->setTieBack(0);
}
}

//---------------------------------------------------------
// connectTiedNotes
//---------------------------------------------------------

void Note::connectTiedNotes()
{
if (tieBack()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible that tieBack() returns nullptr? If it is, whether it is a corrupted logic or usual case?
If it is a corrupted logic, it is better not to check nullptr since it allows finding the corrupted place faster thanks to the crash. It is better for development to have crashes on invalid program state, than checking nullptr and skipping such corrupted program state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, note may have only a forward tie or no ties at all, and in those cases tieBack() will return nullptr. I see nothing wrong about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is calling connect/disconnect valid in the case of nullptr ties?

Copy link
Contributor Author

@dmitrio95 dmitrio95 Apr 2, 2018

Choose a reason for hiding this comment

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

Yes, the purpose of these member functions is just to ensure that all ties that should be connected to the given note will be properly connected (or disconnected) with this note and those notes that should be bound to it with the ties. The note is not required to have any of those ties, and if it doesn't have them then these functions will just do nothing.

In fact, these functions are based on the code that was placed in undo.cpp and chord.cpp to handle ties connection/disconnection when adding/deleting a single note. I moved it to separate functions to make note deletion process more unified (which was the purpose of the whole commit) and reduce code duplication, and made some changes to make it suitable for more general case that that of deletion and addition of the note to the chord. Although my "generalization" may be not so good, the discussed checks on existence of backward and forward ties were present in the previous version of this code too (see, e.g., this), so I have not added anything when assuming that the note in the considered cases may have just one tie or no ties.

tieBack()->setEndNote(this);
if (tieBack()->startNote())
tieBack()->startNote()->add(tieBack());
}
if (tieFor() && tieFor()->endNote()) {
tieFor()->endNote()->setTieBack(tieFor());
}
}

//---------------------------------------------------------
// accidentalType
//---------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions libmscore/note.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ class Note final : public Element {
void setTieBack(Tie* t) { _tieBack = t; }
Note* firstTiedNote() const;
const Note* lastTiedNote() const;
void disconnectTiedNotes();
void connectTiedNotes();

Chord* chord() const { return (Chord*)parent(); }
void setChord(Chord* a) { setParent((Element*)a); }
Expand Down
45 changes: 28 additions & 17 deletions libmscore/undo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,26 @@ const char* AddElement::name() const
return buffer;
}

//---------------------------------------------------------
// removeNote
// Helper function for RemoveElement class
//---------------------------------------------------------

static void removeNote(const Note* note)
{
Score* score = note->score();
if (note->tieFor() && note->tieFor()->endNote())
score->undo(new RemoveElement(note->tieFor()));
if (note->tieBack())
score->undo(new RemoveElement(note->tieBack()));
for (Spanner* s : note->spannerBack()) {
score->undo(new RemoveElement(s));
}
for (Spanner* s : note->spannerFor()) {
score->undo(new RemoveElement(s));
}
}

//---------------------------------------------------------
// RemoveElement
//---------------------------------------------------------
Expand All @@ -582,19 +602,15 @@ RemoveElement::RemoveElement(Element* e)
score->undo(new RemoveElement(tremolo));
}
for (const Note* note : chord->notes()) {
if (note->tieFor() && note->tieFor()->endNote())
score->undo(new RemoveElement(note->tieFor()));
if (note->tieBack())
score->undo(new RemoveElement(note->tieBack()));
for (Spanner* s : note->spannerBack()) {
score->undo(new RemoveElement(s));
}
for (Spanner* s : note->spannerFor()) {
score->undo(new RemoveElement(s));
}
removeNote(note);
}
}
}
else if (element->isNote()) {
// Removing an individual note within a chord
const Note* note = toNote(element);
removeNote(note);
}
}

//---------------------------------------------------------
Expand All @@ -621,10 +637,7 @@ void RemoveElement::undo(EditData*)
if (element->isChord()) {
Chord* chord = toChord(element);
for (Note* note : chord->notes()) {
if (note->tieBack())
note->tieBack()->setEndNote(note);
if (note->tieFor() && note->tieFor()->endNote())
note->tieFor()->endNote()->setTieBack(note->tieFor());
note->connectTiedNotes();
}
}
undoAddTuplet(toChordRest(element));
Expand All @@ -648,9 +661,7 @@ void RemoveElement::redo(EditData*)
if (element->isChord()) {
Chord* chord = toChord(element);
for (Note* note : chord->notes()) {
if (note->tieFor() && note->tieFor()->endNote()) {
note->tieFor()->endNote()->setTieBack(0);
}
note->disconnectTiedNotes();
}
}
}
Expand Down