@@ -2163,50 +2163,59 @@ Tie* Score::cmdToggleTie()
21632163 if (noteList.empty ()) {
21642164 LOGD (" no notes selected" );
21652165 return nullptr ;
2166- }
2166+ }
2167+
2168+ std::vector<Note*> tieNoteList (noteList.size ());
2169+ // Note* tieNote = nullptr;
2170+ // bool sameChord = std::all_of(noteList.begin(), noteList.end(), [&](const Note* n) { return n->chord() == chord; });
21672171
2168- bool canAddTies = false ;
2169- const size_t notes = noteList.size ();
2170- std::vector<Note*> tieNoteList (notes);
2171- Note* tieNote = nullptr ;
2172- Note* n = nullptr ;
2172+ Chord* chord = noteList.front ()->chord ();
2173+ bool someHaveExistingNextNoteToTieTo = false ; // replaces `canAddTies`
2174+ bool allHaveExistingNextNoteToTieTo = true ;
21732175
2174- bool sameChord = std::all_of (noteList.begin (), noteList.end (), [&](const Note* n) { return n->chord () == noteList[0 ]->chord (); });
2175- const bool shouldTieListSelection = !sameChord;
21762176
2177- for (size_t i = 0 ; i < notes; ++i) {
2178- n = noteList[i];
2177+ for (size_t i = 0 ; i < noteList.size (); ++i) {
2178+ Note* n = noteList[i];
2179+ if (chord && n->chord () != chord) {
2180+ chord = nullptr ;
2181+ }
21792182 if (n->tieFor ()) {
21802183 tieNoteList[i] = nullptr ;
21812184 } else {
2182- tieNote = searchTieNote (n);
2185+ Note* tieNote = searchTieNote (n);
21832186 tieNoteList[i] = tieNote;
21842187 if (tieNote) {
2185- canAddTies = true ;
2188+ someHaveExistingNextNoteToTieTo = true ;
2189+ } else {
2190+ allHaveExistingNextNoteToTieTo = false ;
21862191 }
21872192 }
21882193 }
21892194
2190- if (!tieNote && selection ().isList () && sameChord) {
2195+ const bool shouldTieListSelection = noteList.size () >= 2 && !chord;
2196+
2197+
2198+ if (chord /* i.e. all notes are in the same chord */ && !allHaveExistingNextNoteToTieTo) {
21912199 cmdAddTie ();
2192- if (notes >= 2 ) {
2193- Chord* c = n-> chord () ->next ();
2200+ if (noteList. size () >= 2 ) {
2201+ Chord* c = chord->next ();
21942202 for (Note* cn : c->notes ()) {
21952203 score ()->select (cn, SelectType::ADD);
21962204 }
21972205 }
21982206 return nullptr ;
21992207 }
22002208
2201- const TranslatableString actionName = canAddTies
2209+
2210+ const TranslatableString actionName = someHaveExistingNextNoteToTieTo
22022211 ? TranslatableString (" undoableAction" , " Add tie" )
22032212 : TranslatableString (" undoableAction" , " Remove tie" );
22042213
22052214 startCmd (actionName);
22062215
22072216 Tie* tie = nullptr ;
22082217
2209- for (size_t i = 0 ; i < notes ; ++i) {
2218+ for (size_t i = 0 ; i < noteList. size () ; ++i) {
22102219 Note* note = noteList[i];
22112220 Note* tieToNote = tieNoteList[i];
22122221
@@ -2215,7 +2224,7 @@ Tie* Score::cmdToggleTie()
22152224 }
22162225
22172226 // Tie to adjacent unselected note
2218- if (canAddTies && tieToNote) {
2227+ if (someHaveExistingNextNoteToTieTo && tieToNote) {
22192228 Note* startNote = note->tick () <= tieToNote->tick () ? note : tieToNote;
22202229 Note* endNote = startNote == tieToNote ? note : tieToNote;
22212230 tie = createAndAddTie (startNote, endNote);
@@ -2239,14 +2248,14 @@ Tie* Score::cmdToggleTie()
22392248 continue ;
22402249 }
22412250
2242- if (!shouldTieListSelection || i > notes - 2 ) {
2251+ if (!shouldTieListSelection || i > noteList. size () - 2 ) {
22432252 continue ;
22442253 }
22452254
22462255 // Tie to next appropriate note in selection
22472256 Note* note2 = nullptr ;
22482257
2249- for (size_t j = i + 1 ; j < notes ; ++j) {
2258+ for (size_t j = i + 1 ; j < noteList. size () ; ++j) {
22502259 Note* candidateNote = noteList[j];
22512260 if (!candidateNote) {
22522261 continue ;
0 commit comments