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 #25299: Allow navigating to grace notes when inputting fingerings #26033

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions src/engraving/dom/chord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2553,6 +2553,21 @@ Chord* Chord::graceNoteAt(size_t idx) const
return m_graceNotes.at(idx);
}

//---------------------------------------------------------
// allGraceChordsOfMainChord
// returns a list containing all grace notes (chords) attached to the main chord and the main chord itself, in order
//---------------------------------------------------------
std::vector<Chord*> Chord::allGraceChordsOfMainChord()
{
Chord* mainChord = isGrace() ? toChord(explicitParent()) : this;
std::vector<Chord*> chords = { mainChord };
GraceNotesGroup gnBefore = mainChord->graceNotesBefore();
GraceNotesGroup gnAfter = mainChord->graceNotesAfter();
Comment on lines +2564 to +2565
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
GraceNotesGroup gnBefore = mainChord->graceNotesBefore();
GraceNotesGroup gnAfter = mainChord->graceNotesAfter();
const GraceNotesGroup& gnBefore = mainChord->graceNotesBefore();
const GraceNotesGroup& gnAfter = mainChord->graceNotesAfter();

chords.insert(chords.begin(), gnBefore.begin(), gnBefore.end());
chords.insert(chords.end(), gnAfter.begin(), gnAfter.end());
return chords;
}

//---------------------------------------------------------
// setShowStemSlashInAdvance
//---------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/engraving/dom/chord.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class Chord final : public ChordRest

const std::vector<Chord*>& graceNotes() const { return m_graceNotes; }
std::vector<Chord*>& graceNotes() { return m_graceNotes; }
std::vector<Chord*> allGraceChordsOfMainChord();

GraceNotesGroup& graceNotesBefore(bool filterUnplayable = false) const;
GraceNotesGroup& graceNotesAfter(bool filterUnplayable = false) const;
Expand Down
62 changes: 42 additions & 20 deletions src/notation/internal/notationinteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5920,16 +5920,32 @@ void NotationInteraction::navigateToNearText(MoveDirection direction)
Note* origNote = toNote(op);
Chord* ch = origNote->chord();
const std::vector<Note*>& notes = ch->notes();

// first, try going to prev/next note in the current chord
if (origNote != (back ? notes.back() : notes.front())) {
// find prev/next note in same chord
for (auto it = notes.cbegin(); it != notes.cend(); ++it) {
if (*it == origNote) {
el = back ? *(it + 1) : *(it - 1);
break;
}
}
} else {
// find prev/next chord in another voice
}

// next, try going to next/prev grace note chord in the same group as the current
std::vector<Chord*> chordList = ch->allGraceChordsOfMainChord();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
std::vector<Chord*> chordList = ch->allGraceChordsOfMainChord();
const std::vector<Chord*> chordList = ch->allGraceChordsOfMainChord();


if (!el && ch != (back ? chordList.front() : chordList.back())) {
for (auto it = chordList.begin(); it != chordList.end(); ++it) {
if (*it == ch) {
Chord* targetChord = back ? *(it - 1) : *(it + 1);
el = back ? targetChord->notes().front() : targetChord->notes().back();
break;
}
}
}
Comment on lines +5937 to +5945
Copy link
Contributor

Choose a reason for hiding this comment

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

We can make this more concise with some std methods:

Suggested change
if (!el && ch != (back ? chordList.front() : chordList.back())) {
for (auto it = chordList.begin(); it != chordList.end(); ++it) {
if (*it == ch) {
Chord* targetChord = back ? *(it - 1) : *(it + 1);
el = back ? targetChord->notes().front() : targetChord->notes().back();
break;
}
}
}
if (!el && ch != (back ? chordList.front() : chordList.back())) {
auto it = std::find(chordList.begin(), chordList.end(), ch);
if (it != chordList.end()) {
const Chord* targetChord = back ? *std::prev(it) : *std::next(it);
el = back ? targetChord->notes().front() : targetChord->notes().back();
}
}

...
Arguably it would be a bit more readable if we swapped those final ternary operators with a simple if/else - the tradeoff is that the nesting gets a bit painful. I'll leave it up to you:

if (back) {
    const Chord* targetChord = *std::prev(it);
    el = targetChord->notes().front();
} else {
    const Chord* targetChord = *std::next(it);
    el = targetChord->notes().back();
}

...
As a general note, if this method gets any more complicated I think we should consider splitting it up into lambdas or helpers. Early returns would help a lot with readability here.


// next, try going to prev/next chord in another voice
if (!el) {
Segment* seg = ch->segment();
if (!seg) {
LOGD("navigateToNearText: no segment");
Expand All @@ -5941,31 +5957,37 @@ void NotationInteraction::navigateToNearText(MoveDirection direction)
for (int track = sTrack; back ? (track >= eTrack) : (track <= eTrack); track += inc) {
EngravingItem* e = seg->element(track);
if (e && e->isChord()) {
el = back ? toChord(e)->notes().front() : toChord(e)->notes().back();
std::vector<Chord*> targetChordList = toChord(e)->allGraceChordsOfMainChord();
Chord* targetChord = back ? targetChordList.back() : targetChordList.front();
el = back ? targetChord->notes().front() : targetChord->notes().back();
break;
}
}
}

// find chord in prev/next segments
if (!el) {
seg = back ? seg->prev1(SegmentType::ChordRest) : seg->next1(SegmentType::ChordRest);
sTrack = back ? maxTrack : minTrack;
eTrack = back ? minTrack : maxTrack;
while (seg) {
for (int track = sTrack; back ? (track >= eTrack) : (track <= eTrack); track += inc) {
EngravingItem* e = seg->element(track);
if (e && e->isChord()) {
el = back ? toChord(e)->notes().front() : toChord(e)->notes().back();
break;
}
}

if (el) {
// finally, try going to chord in prev/next segments
if (!el) {
Segment* seg = ch->segment();
seg = back ? seg->prev1(SegmentType::ChordRest) : seg->next1(SegmentType::ChordRest);
int sTrack = back ? maxTrack : minTrack;
int eTrack = back ? minTrack : maxTrack;
int inc = back ? -1 : 1;
while (seg) {
for (int track = sTrack; back ? (track >= eTrack) : (track <= eTrack); track += inc) {
EngravingItem* e = seg->element(track);
if (e && e->isChord()) {
std::vector<Chord*> targetChordList = toChord(e)->allGraceChordsOfMainChord();
Chord* targetChord = back ? targetChordList.back() : targetChordList.front();
el = back ? targetChord->notes().front() : targetChord->notes().back();
break;
}
}

seg = back ? seg->prev1(SegmentType::ChordRest) : seg->next1(SegmentType::ChordRest);
if (el) {
break;
}

seg = back ? seg->prev1(SegmentType::ChordRest) : seg->next1(SegmentType::ChordRest);
}
}
} else if (op->isSegment()) {
Expand Down