LCOV - code coverage report
Current view: top level - src/engraving/dom - chord.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 63.1 % 1521 959
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-Studio-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2021 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : 
      23              : #include "chord.h"
      24              : 
      25              : #include <cmath>
      26              : #include <vector>
      27              : 
      28              : #include "containers.h"
      29              : 
      30              : #include "../editing/addremoveelement.h"
      31              : #include "../editing/editchord.h"
      32              : #include "../editing/editnote.h"
      33              : #include "../editing/navigation.h"
      34              : 
      35              : #include "accidental.h"
      36              : #include "arpeggio.h"
      37              : #include "chordbracket.h"
      38              : #include "articulation.h"
      39              : #include "beam.h"
      40              : #include "chordline.h"
      41              : #include "dom/parenthesis.h"
      42              : #include "drumset.h"
      43              : #include "factory.h"
      44              : #include "guitarbend.h"
      45              : #include "hook.h"
      46              : #include "key.h"
      47              : #include "ledgerline.h"
      48              : #include "measure.h"
      49              : #include "mscore.h"
      50              : #include "note.h"
      51              : #include "notedot.h"
      52              : #include "noteevent.h"
      53              : #include "noteline.h"
      54              : #include "ornament.h"
      55              : #include "part.h"
      56              : #include "rest.h"
      57              : #include "score.h"
      58              : #include "segment.h"
      59              : #include "staff.h"
      60              : #include "stafftype.h"
      61              : #include "stem.h"
      62              : #include "stemslash.h"
      63              : #include "stringdata.h"
      64              : #include "system.h"
      65              : #include "tie.h"
      66              : #include "tremolosinglechord.h"
      67              : #include "tremolotwochord.h"
      68              : #include "trill.h"
      69              : #include "tuplet.h"
      70              : #include "utils.h"
      71              : 
      72              : #ifndef ENGRAVING_NO_ACCESSIBILITY
      73              : #include "accessibility/accessibleitem.h"
      74              : #endif
      75              : 
      76              : #include "log.h"
      77              : 
      78              : using namespace mu;
      79              : using namespace mu::engraving;
      80              : 
      81              : namespace mu::engraving {
      82          212 : NoteParenthesisInfo::NoteParenthesisInfo (Parenthesis* lParen, Parenthesis* rParen, std::vector<Note*> nList)
      83          212 :     : m_leftParen(lParen), m_rightParen(rParen), m_notes(nList)
      84              : {
      85          212 :     std::sort(m_notes.begin(), m_notes.end(), noteIsBefore);
      86          212 : }
      87              : 
      88          174 : NoteParenthesisInfo::~NoteParenthesisInfo()
      89              : {
      90          174 :     delete m_leftParen;
      91          174 :     delete m_rightParen;
      92          174 : }
      93              : 
      94            9 : void NoteParenthesisInfo::insertNote(Note* note)
      95              : {
      96            9 :     m_notes.insert(std::upper_bound(m_notes.begin(), m_notes.end(), note, noteIsBefore), note);
      97            9 : }
      98              : 
      99           12 : void NoteParenthesisInfo::removeNote(Note* note)
     100              : {
     101           12 :     muse::remove(m_notes, note);
     102           12 : }
     103              : 
     104              : //---------------------------------------------------------
     105              : //   upNote
     106              : //---------------------------------------------------------
     107              : 
     108      1084105 : Note* Chord::upNote() const
     109              : {
     110      1084105 :     assert(!m_notes.empty());
     111              : 
     112      1084105 :     Note* result = m_notes.back();
     113      1084105 :     const Staff* stf = staff();
     114      1084105 :     if (!stf) {
     115            0 :         return result;
     116              :     }
     117              : 
     118      1084105 :     const StaffType* st  = stf->staffTypeForElement(this);
     119      1084105 :     if (st->isDrumStaff()) {
     120        39670 :         for (Note* n : m_notes) {
     121        20449 :             if (n->line() < result->line()) {
     122            0 :                 result = n;
     123              :             }
     124              :         }
     125      1064884 :     } else if (st->isTabStaff()) {
     126         2354 :         int line = st->lines();          // start at bottom line
     127              :         int noteLine;
     128              :         // scan each note: if TAB strings are not in sequential order,
     129              :         // visual order of notes might not correspond to pitch order
     130         5311 :         for (Note* n : m_notes) {
     131         2957 :             if (n->shouldHideFret()) {
     132            2 :                 continue;
     133              :             }
     134         2955 :             noteLine = st->physStringToVisual(n->string());
     135         2955 :             if (noteLine < line) {
     136         2936 :                 line   = noteLine;
     137         2936 :                 result = n;
     138              :             }
     139              :         }
     140              :     }
     141      1084105 :     return result;
     142              : }
     143              : 
     144              : //---------------------------------------------------------
     145              : //   downNote
     146              : //---------------------------------------------------------
     147              : 
     148      1006014 : Note* Chord::downNote() const
     149              : {
     150      1006014 :     assert(!m_notes.empty());
     151              : 
     152      1006014 :     Note* result = m_notes.front();
     153      1006014 :     const Staff* stf = staff();
     154      1006014 :     if (!stf) {
     155            0 :         return result;
     156              :     }
     157              : 
     158      1006014 :     const StaffType* st  = stf->staffTypeForElement(this);
     159      1006014 :     if (st->isDrumStaff()) {
     160        28527 :         for (Note* n : m_notes) {
     161        14653 :             if (n->line() > result->line()) {
     162            0 :                 result = n;
     163              :             }
     164              :         }
     165       992140 :     } else if (st->isTabStaff()) {
     166         1328 :         int line        = 0;          // start at top line
     167              :         int noteLine;
     168              :         // scan each note: if TAB strings are not in sequential order,
     169              :         // visual order of notes might not correspond to pitch order
     170         3107 :         for (Note* n : m_notes) {
     171         1779 :             if (n->shouldHideFret()) {
     172            4 :                 continue;
     173              :             }
     174         1775 :             noteLine = st->physStringToVisual(n->string());
     175         1775 :             if (noteLine > line) {
     176         1061 :                 line = noteLine;
     177         1061 :                 result = n;
     178              :             }
     179              :         }
     180              :     }
     181      1006014 :     return result;
     182              : }
     183              : 
     184              : //---------------------------------------------------------
     185              : //   upLine / downLine
     186              : //---------------------------------------------------------
     187              : 
     188       452507 : int Chord::upLine() const
     189              : {
     190       452507 :     return onTabStaff() ? upString() * 2 : upNote()->line();
     191              : }
     192              : 
     193       401812 : int Chord::downLine() const
     194              : {
     195       401812 :     return onTabStaff() ? downString() * 2 : downNote()->line();
     196              : }
     197              : 
     198              : //---------------------------------------------------------
     199              : //   upString / downString
     200              : //
     201              : //    return the topmost / bottommost string used by chord
     202              : //    Top and bottom refer to the DRAWN position, not the position in the instrument
     203              : //    (i.e., upside-down TAB are taken into account)
     204              : //
     205              : //    If no staff, always return 0
     206              : //    If staff is not a TAB, always returns TOP and BOTTOM staff lines
     207              : //---------------------------------------------------------
     208              : 
     209          979 : int Chord::upString() const
     210              : {
     211              :     // if no staff or staff not a TAB, return 0 (=topmost line)
     212          979 :     const Staff* st = staff();
     213          979 :     if (!st) {
     214            0 :         return 0;
     215              :     }
     216              : 
     217          979 :     const StaffType* tab = st->staffTypeForElement(this);
     218          979 :     if (!tab->isTabStaff()) {
     219            0 :         return 0;
     220              :     }
     221              : 
     222          979 :     int line = tab->lines() - 1;              // start at bottom line
     223              :     int noteLine;
     224              :     // scan each note: if TAB strings are not in sequential order,
     225              :     // visual order of notes might not correspond to pitch order
     226          979 :     size_t n = m_notes.size();
     227         2221 :     for (size_t i = 0; i < n; ++i) {
     228         1242 :         noteLine = tab->physStringToVisual(m_notes.at(i)->string());
     229         1242 :         if (noteLine < line) {
     230         1068 :             line = noteLine;
     231              :         }
     232              :     }
     233          979 :     return line;
     234              : }
     235              : 
     236         2284 : int Chord::downString() const
     237              : {
     238         2284 :     const Staff* st = staff();
     239         2284 :     if (!st) {                                         // if no staff, return 0
     240            0 :         return 0;
     241              :     }
     242              : 
     243         2284 :     const StaffType* tab = st->staffTypeForElement(this);
     244         2284 :     if (!tab->isTabStaff()) {                // if staff not a TAB, return bottom line
     245            0 :         return st->lines(tick()) - 1;
     246              :     }
     247              : 
     248         2284 :     int line = 0;           // start at top line
     249              :     int noteLine;
     250         2284 :     size_t n = m_notes.size();
     251         4943 :     for (size_t i = 0; i < n; ++i) {
     252         2659 :         noteLine = tab->physStringToVisual(m_notes.at(i)->string());
     253         2659 :         if (noteLine > line) {
     254         1786 :             line = noteLine;
     255              :         }
     256              :     }
     257         2284 :     return line;
     258              : }
     259              : 
     260              : //---------------------------------------------------------
     261              : //   noteDistances
     262              : //---------------------------------------------------------
     263              : 
     264        51392 : std::vector<int> Chord::noteDistances() const
     265              : {
     266        51392 :     const StaffType* staffType = this->staffType();
     267        51392 :     assert(staffType);
     268        51392 :     bool isTabStaff = staffType->isTabStaff();
     269        51392 :     int staffMiddleLine = staffType->middleLine();
     270              : 
     271        51392 :     std::vector<int> distances;
     272        51392 :     distances.reserve(m_notes.size());
     273       108914 :     for (Note* note : m_notes) {
     274        57522 :         int noteLine = isTabStaff ? note->string() : note->line();
     275        57522 :         distances.push_back(noteLine - staffMiddleLine);
     276              :     }
     277        51392 :     return distances;
     278            0 : }
     279              : 
     280              : //---------------------------------------------------------
     281              : //   Chord
     282              : //---------------------------------------------------------
     283              : 
     284        32580 : Chord::Chord(DummyParentOr<Segment, Chord> parent)
     285        32580 :     : ChordRest(ElementType::CHORD, parent)
     286              : {
     287        32580 :     m_stem             = 0;
     288        32580 :     m_hook             = 0;
     289        32580 :     m_stemDirection    = DirectionV::AUTO;
     290        32580 :     m_arpeggio         = 0;
     291        32580 :     m_spanArpeggio     = 0;
     292        32580 :     m_noteType         = NoteType::NORMAL;
     293        32580 :     m_stemSlash        = 0;
     294        32580 :     m_noStem           = false;
     295        32580 :     m_showStemSlash    = m_noteType == NoteType::ACCIACCATURA;
     296        32580 :     m_playEventType    = PlayEventType::Auto;
     297        32580 :     m_spaceLw          = 0.;
     298        32580 :     m_spaceRw          = 0.;
     299        32580 :     m_crossMeasure     = CrossMeasure::UNKNOWN;
     300        32580 :     m_graceIndex       = 0;
     301        32580 :     m_combineVoice     = AutoOnOff::AUTO;
     302        32580 : }
     303              : 
     304         3946 : Chord::Chord(const Chord& c, bool link)
     305         3946 :     : ChordRest(c, link)
     306              : {
     307         3946 :     if (link) {
     308         1039 :         score()->undo(new Link(this, const_cast<Chord*>(&c)));
     309              :     }
     310              : 
     311         9857 :     for (Note* onote : c.m_notes) {
     312         5911 :         Note* nnote = Factory::copyNote(*onote, link);
     313         5911 :         add(nnote);
     314              :     }
     315         3969 :     for (Chord* gn : c.graceNotes()) {
     316           23 :         Chord* nc = new Chord(*gn, link);
     317           23 :         add(nc);
     318              :     }
     319         4034 :     for (Articulation* a : c.m_articulations) {      // make deep copy
     320           88 :         Articulation* na = a->clone();
     321           88 :         if (link) {
     322           27 :             score()->undo(new Link(na, a));
     323              :         }
     324           88 :         na->setOwnershipParent(this);
     325           88 :         na->setTrack(track());
     326           88 :         m_articulations.push_back(na);
     327              :     }
     328         3946 :     m_stem          = 0;
     329         3946 :     m_hook          = 0;
     330         3946 :     m_arpeggio      = 0;
     331         3946 :     m_stemSlash     = 0;
     332              : 
     333         3946 :     m_spanArpeggio   = c.m_spanArpeggio;
     334         3946 :     m_graceIndex     = c.m_graceIndex;
     335         3946 :     m_noStem         = c.m_noStem;
     336         3946 :     m_showStemSlash  = c.m_showStemSlash;
     337         3946 :     m_playEventType  = c.m_playEventType;
     338         3946 :     m_stemDirection  = c.m_stemDirection;
     339         3946 :     m_noteType       = c.m_noteType;
     340         3946 :     m_crossMeasure   = CrossMeasure::UNKNOWN;
     341         3946 :     m_combineVoice     = c.m_combineVoice;
     342              : 
     343         3946 :     if (c.m_stem) {
     344         2945 :         add(Factory::copyStem(*(c.m_stem)));
     345              :     }
     346         3946 :     if (c.m_hook) {
     347          444 :         add(new Hook(*(c.m_hook)));
     348              :     }
     349         3946 :     if (c.m_stemSlash) {
     350            0 :         add(Factory::copyStemSlash(*(c.m_stemSlash)));
     351              :     }
     352         3946 :     if (c.m_arpeggio) {
     353           22 :         Arpeggio* a = c.m_arpeggio->isChordBracket() ? new ChordBracket(*toChordBracket(c.m_arpeggio)) : new Arpeggio(*(c.m_arpeggio));
     354           22 :         add(a);
     355           22 :         if (link) {
     356           22 :             score()->undo(new Link(a, const_cast<Arpeggio*>(c.m_arpeggio)));
     357              :         }
     358              :     }
     359              : 
     360         3946 :     if (c.m_tremoloSingleChord) {
     361            2 :         TremoloSingleChord* t = Factory::copyTremoloSingleChord(*(c.m_tremoloSingleChord));
     362            2 :         if (link) {
     363            2 :             score()->undo(new Link(t, const_cast<TremoloSingleChord*>(c.m_tremoloSingleChord)));
     364              :         }
     365            2 :         add(t);
     366         3944 :     } else if (c.m_tremoloTwoChord) {
     367           14 :         if (c.m_tremoloTwoChord->chord1() == &c) {
     368            8 :             TremoloTwoChord* t = Factory::copyTremoloTwoChord(*(c.m_tremoloTwoChord));
     369            8 :             if (link) {
     370            4 :                 score()->undo(new Link(t, const_cast<TremoloTwoChord*>(c.m_tremoloTwoChord)));
     371              :             }
     372            8 :             t->setChords(this, nullptr);
     373            8 :             add(t);
     374              :         }
     375              :     }
     376              : 
     377         3950 :     for (EngravingItem* e : c.el()) {
     378            4 :         if (e->isChordLine()) {
     379            4 :             ChordLine* cl = toChordLine(e);
     380            4 :             ChordLine* ncl = Factory::copyChordLine(*cl);
     381            4 :             add(ncl);
     382            4 :             if (cl->note()) {
     383            2 :                 ncl->setNote(findNote(cl->note()->pitch()));
     384              :             }
     385            4 :             if (link) {
     386            4 :                 score()->undo(new Link(ncl, cl));
     387              :             }
     388              :         }
     389              :     }
     390              : 
     391         3950 :     for (EngravingItem* e : c.el()) {
     392            4 :         if (e->isChordBracket()) {
     393            0 :             EngravingItem* clonedChordBracket = e->linkedClone();
     394            0 :             add(clonedChordBracket);
     395              :         }
     396              :     }
     397              : 
     398         3946 :     if (!c.noteParentheses().empty()) {
     399           12 :         for (const NoteParenthesisInfo* info : c.noteParentheses()) {
     400            6 :             Parenthesis* newLeftParen = toParenthesis(info->leftParen()->clone());
     401            6 :             newLeftParen->setOwnershipParent(this);
     402            6 :             Parenthesis* newRightParen = toParenthesis(info->rightParen()->clone());
     403            6 :             newRightParen->setOwnershipParent(this);
     404              : 
     405            6 :             if (link && !info->leftParen()->generated()) {
     406            2 :                 score()->undo(new Link(newLeftParen, info->leftParen()));
     407              :             }
     408            6 :             if (link && !info->rightParen()->generated()) {
     409            2 :                 score()->undo(new Link(newRightParen, info->rightParen()));
     410              :             }
     411              : 
     412            6 :             std::vector<Note*> newNotes;
     413            6 :             newNotes.reserve(info->notes().size());
     414           12 :             for (Note* note : info->notes()) {
     415            6 :                 newNotes.push_back(findNote(note->pitch()));
     416              :             }
     417              : 
     418            6 :             m_noteParens.push_back(new NoteParenthesisInfo(newLeftParen, newRightParen, newNotes));
     419            6 :         }
     420              :     }
     421         3946 : }
     422              : 
     423              : //---------------------------------------------------------
     424              : //   undoUnlink
     425              : //---------------------------------------------------------
     426              : 
     427          179 : void Chord::undoUnlink()
     428              : {
     429          179 :     ChordRest::undoUnlink();
     430          360 :     for (Note* n : m_notes) {
     431          181 :         n->undoUnlink();
     432              :     }
     433          181 :     for (Chord* gn : graceNotes()) {
     434            2 :         gn->undoUnlink();
     435              :     }
     436          188 :     for (Articulation* a : m_articulations) {
     437            9 :         a->undoUnlink();
     438              :     }
     439              : /*      if (_glissando)
     440              :             _glissando->undoUnlink(); */
     441          179 :     if (m_arpeggio) {
     442            1 :         m_arpeggio->undoUnlink();
     443              :     }
     444              : 
     445          179 :     if (tremoloSingleChord()) {
     446            0 :         tremoloSingleChord()->undoUnlink();
     447              :     }
     448              : 
     449          179 :     for (EngravingItem* e : el()) {
     450            0 :         if (e->isChordLine()) {
     451            0 :             e->undoUnlink();
     452              :         }
     453              :     }
     454          179 : }
     455              : 
     456              : //---------------------------------------------------------
     457              : //   ~Chord
     458              : //---------------------------------------------------------
     459              : 
     460        65370 : Chord::~Chord()
     461              : {
     462        32685 :     muse::DeleteAll(m_articulations);
     463              : 
     464        32685 :     if (tremoloTwoChord()) {
     465          122 :         if (tremoloTwoChord()->chord1() == this) {
     466           62 :             tremoloTwoChord()->setChord1(nullptr);
     467           60 :         } else if (tremoloTwoChord()->chord2() == this) {
     468           60 :             tremoloTwoChord()->setChord2(nullptr);
     469              :         }
     470              :     }
     471              : 
     472        32685 :     delete m_arpeggio;
     473        32685 :     delete m_stemSlash;
     474        32685 :     delete m_stem;
     475        32685 :     delete m_hook;
     476        32685 :     muse::DeleteAll(m_ledgerLines);
     477        32685 :     muse::DeleteAll(m_graceNotes);
     478        32685 :     muse::DeleteAll(m_notes);
     479        32685 :     muse::DeleteAll(m_noteParens);
     480        65370 : }
     481              : 
     482              : #ifndef ENGRAVING_NO_ACCESSIBILITY
     483            0 : AccessibleItemPtr Chord::createAccessible()
     484              : {
     485            0 :     return std::make_shared<AccessibleItem>(this, AccessibleItem::Group);
     486              : }
     487              : 
     488              : #endif
     489              : 
     490              : //---------------------------------------------------------
     491              : //   noteHeadWidth
     492              : //---------------------------------------------------------
     493              : 
     494       214409 : double Chord::noteHeadWidth() const
     495              : {
     496       214409 :     return score()->noteHeadWidth() * mag();
     497              : }
     498              : 
     499              : //---------------------------------------------------------
     500              : //   rightEdge
     501              : //---------------------------------------------------------
     502              : 
     503            0 : double Chord::rightEdge() const
     504              : {
     505            0 :     double right = 0.0;
     506            0 :     for (Note* n : notes()) {
     507            0 :         right = std::max(right, x() + n->x() + n->bboxRightPos());
     508              :     }
     509              : 
     510            0 :     return right;
     511              : }
     512              : 
     513         5182 : TremoloType Chord::tremoloType() const
     514              : {
     515         5182 :     if (tremoloSingleChord()) {
     516          117 :         return tremoloSingleChord()->tremoloType();
     517         5065 :     } else if (tremoloTwoChord()) {
     518           22 :         return tremoloTwoChord()->tremoloType();
     519              :     } else {
     520         5043 :         return TremoloType::INVALID_TREMOLO;
     521              :     }
     522              : }
     523              : 
     524      1268143 : TremoloTwoChord* Chord::tremoloTwoChord() const
     525              : {
     526      1268143 :     return m_tremoloTwoChord;
     527              : }
     528              : 
     529       577742 : TremoloSingleChord* Chord::tremoloSingleChord() const
     530              : {
     531       577742 :     return m_tremoloSingleChord;
     532              : }
     533              : 
     534          262 : void Chord::setTremoloTwoChord(TremoloTwoChord* tr, bool applyLogic)
     535              : {
     536          262 :     if (m_tremoloTwoChord && tr && tr == m_tremoloTwoChord) {
     537           74 :         return;
     538              :     }
     539              : 
     540          188 :     if (m_tremoloTwoChord) {
     541           30 :         TDuration d;
     542           30 :         const Fraction f = ticks();
     543           30 :         if (f.numerator() > 0) {
     544           30 :             d = TDuration(f);
     545              :         } else {
     546            0 :             d = m_tremoloTwoChord->durationType();
     547            0 :             const int dots = d.dots();
     548            0 :             d = d.shift(1);
     549            0 :             d.setDots(dots);
     550              :         }
     551              : 
     552           30 :         setDurationType(d);
     553           30 :         Chord* other = m_tremoloTwoChord->chord1() == this ? m_tremoloTwoChord->chord2() : m_tremoloTwoChord->chord1();
     554           30 :         m_tremoloTwoChord = nullptr;
     555           30 :         if (other) {
     556           10 :             other->setTremoloTwoChord(nullptr);
     557              :         }
     558              :     }
     559          188 :     m_tremoloSingleChord = nullptr;
     560              : 
     561          188 :     if (tr && applyLogic) {
     562          140 :         TDuration d = tr->durationType();
     563          140 :         if (!d.isValid()) {
     564           52 :             d = durationType();
     565           52 :             const int dots = d.dots();
     566           52 :             d = d.shift(-1);
     567           52 :             d.setDots(dots);
     568           52 :             tr->setDurationType(d);
     569              :         }
     570              : 
     571          140 :         setDurationType(d);
     572          140 :         Chord* other = tr->chord1() == this ? tr->chord2() : tr->chord1();
     573          140 :         m_tremoloTwoChord = tr;
     574          140 :         if (other) {
     575          102 :             other->setTremoloTwoChord(tr);
     576              :         }
     577              :     }
     578              : 
     579          188 :     m_tremoloTwoChord = tr;
     580              : }
     581              : 
     582          166 : void Chord::setTremoloSingleChord(TremoloSingleChord* tr)
     583              : {
     584          166 :     if (m_tremoloSingleChord && tr && tr == m_tremoloSingleChord) {
     585            0 :         return;
     586              :     }
     587              : 
     588          166 :     if (m_tremoloTwoChord) {
     589            0 :         TDuration d;
     590            0 :         const Fraction f = ticks();
     591            0 :         if (f.numerator() > 0) {
     592            0 :             d = TDuration(f);
     593              :         } else {
     594            0 :             d = m_tremoloTwoChord->durationType();
     595            0 :             const int dots = d.dots();
     596            0 :             d = d.shift(1);
     597            0 :             d.setDots(dots);
     598              :         }
     599              : 
     600            0 :         setDurationType(d);
     601            0 :         Chord* other = m_tremoloTwoChord->chord1() == this ? m_tremoloTwoChord->chord2() : m_tremoloTwoChord->chord1();
     602            0 :         m_tremoloTwoChord = nullptr;
     603            0 :         if (other) {
     604            0 :             other->setTremoloTwoChord(nullptr);
     605              :         }
     606              :     }
     607              : 
     608          166 :     m_tremoloSingleChord = tr;
     609              : }
     610              : 
     611              : //---------------------------------------------------------
     612              : //   add
     613              : //---------------------------------------------------------
     614              : 
     615        82045 : void Chord::add(EngravingItem* e)
     616              : {
     617        82045 :     if (e->ownershipParent() != this) {
     618        10741 :         e->setOwnershipParent(this);
     619              :     }
     620        82045 :     e->setTrack(track());
     621        82045 :     switch (e->type()) {
     622        44058 :     case ElementType::NOTE:
     623              :     {
     624        44058 :         Note* note = toNote(e);
     625        44058 :         bool tab = staff() && staff()->isTabStaff(tick());
     626        44058 :         bool found = false;
     627              : 
     628        49637 :         for (unsigned idx = 0; idx < m_notes.size(); ++idx) {
     629        10273 :             if (tab) {
     630              :                 // _notes should be sorted by string
     631          116 :                 if (note->string() > m_notes[idx]->string()) {
     632            9 :                     m_notes.insert(m_notes.begin() + idx, note);
     633            9 :                     found = true;
     634            9 :                     break;
     635              :                 }
     636              :             } else {
     637              :                 // _notes should be sorted by line position,
     638              :                 // but it's often not yet possible since line is unknown
     639              :                 // use pitch instead, and line as a second sort criteria.
     640        10157 :                 if (note->pitch() <= m_notes[idx]->pitch()) {
     641         4685 :                     if (note->pitch() == m_notes[idx]->pitch() && note->line() >= m_notes[idx]->line()) {
     642           55 :                         m_notes.insert(m_notes.begin() + idx + 1, note);
     643              :                     } else {
     644         4630 :                         m_notes.insert(m_notes.begin() + idx, note);
     645              :                     }
     646         4685 :                     found = true;
     647         4685 :                     break;
     648              :                 }
     649              :             }
     650              :         }
     651        44058 :         if (!found) {
     652        39364 :             m_notes.push_back(note);
     653              :         }
     654        44058 :         note->connectTiedNotes();
     655        44058 :         if (voice() && measure() && note->visible()) {
     656         2696 :             measure()->setHasVoices(staffIdx(), true);
     657              :         }
     658              :     }
     659        44058 :     break;
     660           86 :     case ElementType::ARPEGGIO:
     661           86 :         m_arpeggio = toArpeggio(e);
     662           86 :         break;
     663            0 :     case ElementType::CHORD_BRACKET:
     664            0 :         addEl(e);
     665            0 :         break;
     666           41 :     case ElementType::TREMOLO_TWOCHORD:
     667           41 :         setTremoloTwoChord(item_cast<TremoloTwoChord*>(e));
     668           41 :         break;
     669           77 :     case ElementType::TREMOLO_SINGLECHORD:
     670           77 :         setTremoloSingleChord(item_cast<TremoloSingleChord*>(e));
     671           77 :         break;
     672        31872 :     case ElementType::STEM:
     673        31872 :         assert(!m_stem);
     674        31872 :         m_stem = toStem(e);
     675        31872 :         break;
     676         1781 :     case ElementType::HOOK:
     677         1781 :         m_hook = toHook(e);
     678         1781 :         break;
     679          158 :     case ElementType::CHORDLINE:
     680          158 :         addEl(e);
     681          158 :         break;
     682          142 :     case ElementType::STEM_SLASH:
     683          142 :         assert(!m_stemSlash);
     684          142 :         m_stemSlash = toStemSlash(e);
     685          142 :         break;
     686          675 :     case ElementType::CHORD:
     687              :     {
     688          675 :         Chord* gc = toChord(e);
     689          675 :         assert(gc->noteType() != NoteType::NORMAL);
     690          675 :         size_t idx = gc->graceIndex();
     691          675 :         gc->setFlag(ElementFlag::MOVABLE, true);
     692          675 :         m_graceNotes.insert(m_graceNotes.begin() + idx, gc);
     693              :     }
     694          675 :     break;
     695            0 :     case ElementType::LEDGER_LINE:
     696            0 :         ASSERT_X("Chord::add ledgerline");
     697              :         break;
     698         1289 :     case ElementType::ARTICULATION:
     699              :     case ElementType::ORNAMENT:
     700              :     case ElementType::TAPPING:
     701              :     {
     702         1289 :         Articulation* a = toArticulation(e);
     703         1289 :         if (a->layoutCloseToNote()) {
     704          417 :             if (a->isStaccato()) {
     705              :                 // staccato is always the first articulation
     706          295 :                 m_articulations.insert(m_articulations.begin(), a);
     707              :             } else {
     708          122 :                 auto i = m_articulations.begin();
     709          151 :                 while (i != m_articulations.end() && (*i)->layoutCloseToNote()) {
     710           29 :                     i++;
     711              :                 }
     712          122 :                 m_articulations.insert(i, a);
     713              :             }
     714              :         } else {
     715          872 :             m_articulations.push_back(a);
     716              :         }
     717              :     }
     718         1289 :     break;
     719         1866 :     default:
     720         1866 :         ChordRest::add(e);
     721         1866 :         return;
     722              :     }
     723              : 
     724        80179 :     e->added();
     725              : }
     726              : 
     727              : //---------------------------------------------------------
     728              : //   remove
     729              : //---------------------------------------------------------
     730              : 
     731         1347 : void Chord::remove(EngravingItem* e)
     732              : {
     733         1347 :     if (!e) {
     734            0 :         return;
     735              :     }
     736              : 
     737         1347 :     switch (e->type()) {
     738          441 :     case ElementType::NOTE:
     739              :     {
     740          441 :         Note* note = toNote(e);
     741          441 :         auto i = std::find(m_notes.begin(), m_notes.end(), note);
     742          441 :         if (i != m_notes.end()) {
     743          441 :             m_notes.erase(i);
     744          441 :             note->disconnectTiedNotes();
     745          441 :             for (Spanner* s : note->spannerBack()) {
     746            0 :                 note->removeSpannerBack(s);
     747              :             }
     748          441 :             for (Spanner* s : note->spannerFor()) {
     749            0 :                 note->removeSpannerFor(s);
     750              :             }
     751         1323 :             EditChord::removeChordParentheses(this, { note });
     752              :         } else {
     753            0 :             LOGD("Chord::remove() note %p not found!", e);
     754              :         }
     755          441 :         if (voice() && measure() && note->visible()) {
     756          100 :             measure()->checkMultiVoices(staffIdx());
     757              :         }
     758              :     }
     759          441 :     break;
     760              : 
     761            0 :     case ElementType::ARPEGGIO:
     762            0 :         if (m_spanArpeggio == m_arpeggio) {
     763            0 :             m_spanArpeggio = nullptr;
     764              :         }
     765            0 :         m_arpeggio = nullptr;
     766            0 :         break;
     767            0 :     case ElementType::CHORD_BRACKET:
     768            0 :         removeEl(e);
     769            0 :         break;
     770            1 :     case ElementType::TREMOLO_TWOCHORD:
     771            1 :         setTremoloTwoChord(nullptr);
     772            1 :         break;
     773            0 :     case ElementType::TREMOLO_SINGLECHORD:
     774            0 :         setTremoloSingleChord(nullptr);
     775            0 :         break;
     776          567 :     case ElementType::STEM:
     777          567 :         m_stem = 0;
     778          567 :         break;
     779           70 :     case ElementType::HOOK:
     780           70 :         m_hook = 0;
     781           70 :         break;
     782           11 :     case ElementType::STEM_SLASH:
     783           11 :         assert(m_stemSlash);
     784           11 :         if (m_stemSlash->selected() && score()) {
     785            0 :             score()->deselect(m_stemSlash);
     786              :         }
     787           11 :         m_stemSlash = 0;
     788           11 :         break;
     789           12 :     case ElementType::CHORDLINE:
     790           12 :         removeEl(e);
     791           12 :         break;
     792            9 :     case ElementType::CHORD:
     793              :     {
     794            9 :         auto i = std::find(m_graceNotes.begin(), m_graceNotes.end(), toChord(e));
     795            9 :         IF_ASSERT_FAILED(i != m_graceNotes.end()) {
     796            0 :             break;
     797              :         }
     798            9 :         Chord* grace = *i;
     799            9 :         grace->setGraceIndex(i - m_graceNotes.begin());
     800            9 :         m_graceNotes.erase(i);
     801              :     }
     802            9 :     break;
     803          213 :     case ElementType::ARTICULATION:
     804              :     case ElementType::ORNAMENT:
     805              :     case ElementType::TAPPING:
     806              :     {
     807          213 :         Articulation* a = toArticulation(e);
     808          213 :         if (!muse::remove(m_articulations, a)) {
     809            0 :             LOGD("Chord::remove(): articulation not found");
     810              :         }
     811              :     }
     812          213 :     break;
     813            0 :     case ElementType::PARENTHESIS: {
     814            0 :         NoteParenthesisInfo* parenInfo = findNoteParenthesisInfo(toParenthesis(e));
     815            0 :         IF_ASSERT_FAILED(parenInfo) {
     816            0 :             LOGD() << "Chord::remove(): This parenthesis does not belong to this chord";
     817            0 :             return;
     818              :         }
     819            0 :         EditChord::removeChordParentheses(this, parenInfo->notes());
     820            0 :         break;
     821              :     }
     822              :     break;
     823           23 :     default:
     824           23 :         ChordRest::remove(e);
     825           23 :         return;
     826              :     }
     827              : 
     828         1324 :     e->removed();
     829              : }
     830              : 
     831              : //---------------------------------------------------------
     832              : //   maxHeadWidth
     833              : //---------------------------------------------------------
     834              : 
     835            0 : double Chord::maxHeadWidth() const
     836              : {
     837              :     // determine max head width in chord
     838            0 :     double hw = 0;
     839            0 :     for (const Note* n : m_notes) {
     840            0 :         double t = n->headWidth();
     841            0 :         if (t > hw) {
     842            0 :             hw = t;
     843              :         }
     844              :     }
     845            0 :     return hw;
     846              : }
     847              : 
     848              : //---------------------------------------------------------
     849              : //   selectedNote
     850              : //---------------------------------------------------------
     851              : 
     852            0 : Note* Chord::selectedNote() const
     853              : {
     854            0 :     Note* note = 0;
     855            0 :     size_t n = m_notes.size();
     856            0 :     for (size_t i = 0; i < n; ++i) {
     857            0 :         Note* currentNote = m_notes.at(i);
     858            0 :         if (currentNote->selected()) {
     859            0 :             if (note) {
     860            0 :                 return 0;
     861              :             }
     862            0 :             note = currentNote;
     863              :         }
     864              :     }
     865            0 :     return note;
     866              : }
     867              : 
     868              : //---------------------------------------------------------
     869              : //   upPos
     870              : //---------------------------------------------------------
     871              : 
     872         3508 : double Chord::upPos() const
     873              : {
     874         3508 :     return upNote()->pos().y();
     875              : }
     876              : 
     877              : //---------------------------------------------------------
     878              : //   downPos
     879              : //---------------------------------------------------------
     880              : 
     881         3497 : double Chord::downPos() const
     882              : {
     883         3497 :     return downNote()->pos().y();
     884              : }
     885              : 
     886           19 : bool Chord::allNotesTiedToNext() const
     887              : {
     888           19 :     Chord* tiedChord = nullptr;
     889           31 :     for (Note* note : m_notes) {
     890           23 :         if (!note->tieFor()) {
     891           11 :             return false;
     892              :         }
     893              : 
     894           12 :         Note* endNote = note->tieFor()->endNote();
     895           12 :         Chord* endChord = endNote ? endNote->chord() : nullptr;
     896           12 :         if (!endChord) {
     897            0 :             return false;
     898              :         }
     899              : 
     900           12 :         if (!tiedChord) {
     901            8 :             tiedChord = endChord;
     902            8 :             continue;
     903              :         }
     904              : 
     905            4 :         if (endChord != tiedChord) {
     906            0 :             return false;
     907              :         }
     908              :     }
     909              : 
     910            8 :     return true;
     911              : }
     912              : 
     913         7644 : bool Chord::allElementsInvisible() const
     914              : {
     915         7716 :     for (EngravingObject* child : getChildren()) {
     916         7684 :         if (toEngravingItem(child)->visible()) {
     917         7612 :             return false;
     918              :         }
     919         7644 :     }
     920              : 
     921           32 :     for (Chord* grace : m_graceNotes) {
     922            0 :         if (!grace->allElementsInvisible()) {
     923            0 :             return false;
     924              :         }
     925              :     }
     926              : 
     927           32 :     return true;
     928              : }
     929              : 
     930              : //---------------------------------------------------------
     931              : //   processSiblings
     932              : //---------------------------------------------------------
     933              : 
     934        43771 : void Chord::processSiblings(std::function<void(EngravingItem*)> func, bool includeTemporarySiblings) const
     935              : {
     936        43771 :     if (m_hook) {
     937          665 :         func(m_hook);
     938              :     }
     939        43771 :     if (m_stem) {
     940         5944 :         func(m_stem);
     941              :     }
     942        43771 :     if (m_stemSlash) {
     943           90 :         func(m_stemSlash);
     944              :     }
     945        43771 :     if (m_arpeggio) {
     946           71 :         func(m_arpeggio);
     947              :     }
     948        43771 :     if (m_tremoloTwoChord) {
     949           16 :         func(m_tremoloTwoChord);
     950              :     }
     951        43771 :     if (m_tremoloSingleChord) {
     952            8 :         func(m_tremoloSingleChord);
     953              :     }
     954        43771 :     if (includeTemporarySiblings) {
     955        43944 :         for (LedgerLine* ll : m_ledgerLines) {
     956          174 :             func(ll);
     957              :         }
     958              :     }
     959        43940 :     for (Articulation* a : m_articulations) {
     960          169 :         func(a);
     961              :     }
     962        62991 :     for (Note* note : m_notes) {
     963        19220 :         func(note);
     964              :     }
     965        43851 :     for (Chord* chord : m_graceNotes) {    // process grace notes last, needed for correct shape calculation
     966           80 :         func(chord);
     967              :     }
     968        43771 : }
     969              : 
     970              : //---------------------------------------------------------
     971              : //   setTrack
     972              : //---------------------------------------------------------
     973              : 
     974        40280 : void Chord::setTrack(track_idx_t val)
     975              : {
     976        40280 :     ChordRest::setTrack(val);
     977        58771 :     processSiblings([val](EngravingItem* e) { e->setTrack(val); }, true);
     978        40280 : }
     979              : 
     980       112223 : bool Chord::shouldCombineVoice() const
     981              : {
     982       112223 :     return combineVoice() == AutoOnOff::ON || (combineVoice() == AutoOnOff::AUTO && style().styleB(Sid::combineVoice));
     983              : }
     984              : 
     985         1426 : bool Chord::combineVoice(const Chord* chord1, const Chord* chord2)
     986              : {
     987         1426 :     return chord1->shouldCombineVoice() && chord2->shouldCombineVoice();
     988              : }
     989              : 
     990              : //---------------------------------------------------------
     991              : //   setScore
     992              : //---------------------------------------------------------
     993              : 
     994         3490 : void Chord::setScore(Score* s)
     995              : {
     996         3490 :     ChordRest::setScore(s);
     997        11434 :     processSiblings([s](EngravingItem* e) { e->setScore(s); }, true);
     998         3490 : }
     999              : 
    1000            0 : Fraction Chord::endTickIncludingTied() const
    1001              : {
    1002            0 :     const Chord* lastTied = this;
    1003            0 :     while (lastTied) {
    1004            0 :         const Chord* next = lastTied->nextTiedChord();
    1005            0 :         if (next) {
    1006            0 :             lastTied = next;
    1007              :         } else {
    1008            0 :             break;
    1009              :         }
    1010              :     }
    1011            0 :     return lastTied->endTick();
    1012              : }
    1013              : 
    1014          217 : Chord* Chord::prev() const
    1015              : {
    1016          217 :     ChordRest* prev = Navigation::prevChordRest(const_cast<Chord*>(this));
    1017          217 :     if (prev && prev->isChord()) {
    1018          209 :         return toChord(prev);
    1019              :     }
    1020            8 :     return nullptr;
    1021              : }
    1022              : 
    1023          293 : Chord* Chord::next() const
    1024              : {
    1025          293 :     ChordRest* next = Navigation::nextChordRest(const_cast<Chord*>(this));
    1026          293 :     if (next && next->isChord()) {
    1027          226 :         return toChord(next);
    1028              :     }
    1029           67 :     return nullptr;
    1030              : }
    1031              : 
    1032         7974 : void Chord::resizeLedgerLinesTo(size_t newSize)
    1033              : {
    1034         7974 :     int ledgerLineCountDiff = static_cast<int>(newSize - m_ledgerLines.size());
    1035         7974 :     if (ledgerLineCountDiff > 0) {
    1036       208283 :         for (int i = 0; i < ledgerLineCountDiff; ++i) {
    1037       202314 :             m_ledgerLines.push_back(new LedgerLine(this));
    1038              :         }
    1039              :     } else {
    1040        22015 :         for (int i = 0; i < std::abs(ledgerLineCountDiff); ++i) {
    1041        20010 :             delete m_ledgerLines.back();
    1042        20010 :             m_ledgerLines.pop_back();
    1043              :         }
    1044              :     }
    1045              : 
    1046         7974 :     assert(m_ledgerLines.size() == newSize);
    1047         7974 : }
    1048              : 
    1049        32330 : void Chord::setBeamExtension(double extension)
    1050              : {
    1051        32330 :     if (m_stem) {
    1052        32330 :         double baseLength = m_stem->absoluteFromSpatium(m_stem->baseLength());
    1053        32330 :         m_stem->setBaseLength(std::max(Spatium::fromAbsolute(baseLength + extension, spatium()), 0.0_sp));
    1054        32330 :         m_defaultStemLength = std::max(m_defaultStemLength + extension, m_stem->absoluteFromSpatium(m_stem->baseLength()));
    1055              :     }
    1056        32330 : }
    1057              : 
    1058        91505 : bool Chord::shouldHaveStem() const
    1059              : {
    1060        91505 :     const Staff* staff = this->staff();
    1061        91505 :     const StaffType* staffType = staff ? staff->staffTypeForElement(this) : nullptr;
    1062              : 
    1063        91505 :     return !m_noStem
    1064        90771 :            && durationType().hasStem()
    1065        82420 :            && !(durationType().type() == DurationType::V_HALF && staffType && staffType->isTabStaff()
    1066          417 :                 && staffType->minimStyle() == TablatureMinimStyle::NONE)
    1067        81616 :            && !(measure() && measure()->stemless(staffIdx()))
    1068        80909 :            && !(staffType && staffType->isTabStaff() && staffType->stemless())
    1069       182276 :            && !(staff && staff->isJianpuStaff(tick()));
    1070              : }
    1071              : 
    1072        36705 : bool Chord::shouldHaveHook() const
    1073              : {
    1074        36705 :     return shouldHaveStem()
    1075        68764 :            && durationType().hooks() > 0
    1076         2243 :            && !beam()
    1077       105469 :            && !tremoloTwoChord();
    1078              : }
    1079              : 
    1080        24143 : void Chord::createStem()
    1081              : {
    1082        24143 :     Stem* stem = Factory::createStem(this);
    1083        24143 :     stem->setGenerated(true);
    1084              :     //! score()->undoAddElement calls add(), which assigns this created stem to _stem
    1085        24143 :     score()->doUndoAddElement(stem);
    1086        24143 : }
    1087              : 
    1088         6907 : void Chord::removeStem()
    1089              : {
    1090         6907 :     if (m_stem) {
    1091          567 :         score()->undoRemoveElement(m_stem);
    1092              :     }
    1093         6907 :     if (m_hook) {
    1094            0 :         score()->undoRemoveElement(m_hook);
    1095              :     }
    1096         6907 :     if (m_stemSlash) {
    1097            0 :         score()->undoRemoveElement(m_stemSlash);
    1098              :     }
    1099         6907 : }
    1100              : 
    1101         1337 : void Chord::createHook()
    1102              : {
    1103         1337 :     Hook* hook = new Hook(this);
    1104         1337 :     hook->setGenerated(true);
    1105         1337 :     score()->doUndoAddElement(hook);
    1106         1337 : }
    1107              : 
    1108              : //---------------------------------------------------------
    1109              : //    underBeam: true, if grace note is placed under a beam.
    1110              : //---------------------------------------------------------
    1111              : 
    1112            0 : bool Chord::underBeam() const
    1113              : {
    1114            0 :     if (m_noteType == NoteType::NORMAL) {
    1115            0 :         return false;
    1116              :     }
    1117            0 :     const Chord* cr = toChord(ownershipParent());
    1118            0 :     Beam* beam = cr->beam();
    1119            0 :     if (!beam || !cr->beam()->up()) {
    1120            0 :         return false;
    1121              :     }
    1122            0 :     size_t s = beam->elements().size();
    1123            0 :     if (isGraceBefore()) {
    1124            0 :         if (beam->elements()[0] != cr) {
    1125            0 :             return true;
    1126              :         }
    1127              :     }
    1128            0 :     if (isGraceAfter()) {
    1129            0 :         if (beam->elements()[s - 1] != cr) {
    1130            0 :             return true;
    1131              :         }
    1132              :     }
    1133            0 :     return false;
    1134              : }
    1135              : 
    1136              : //---------------------------------------------------------
    1137              : //   cmdUpdateNotes
    1138              : //---------------------------------------------------------
    1139              : 
    1140        66904 : void Chord::cmdUpdateNotes(AccidentalState* as, staff_idx_t staffIdx)
    1141              : {
    1142              :     // TAB_STAFF is different, as each note has to be fretted
    1143              :     // in the context of the all of the chords of the whole segment
    1144              : 
    1145        66904 :     const Staff* st = staff();
    1146        66904 :     StaffGroup staffGroup = st->staffTypeForElement(this)->group();
    1147        66904 :     if (staffGroup == StaffGroup::TAB) {
    1148         1428 :         Fraction tick = this->tick();
    1149         1428 :         const StringData* stringData = originPart()->stringData(tick, st->idx());
    1150         1488 :         for (Chord* ch : graceNotes()) {
    1151           60 :             stringData->fretChords(ch);
    1152              :         }
    1153         1428 :         stringData->fretChords(this);
    1154         1428 :         return;
    1155              :     } else {
    1156              :         // if not tablature, use instrument->useDrumset to set staffGroup (to allow pitched to unpitched in same staff)
    1157        65476 :         staffGroup = originPart()->instrument(this->tick())->useDrumset() ? StaffGroup::PERCUSSION : StaffGroup::STANDARD;
    1158              :     }
    1159              : 
    1160              :     // PITCHED_ and PERCUSSION_STAFF can go note by note
    1161              : 
    1162        65476 :     if (staffGroup == StaffGroup::STANDARD) {
    1163        64360 :         const std::vector<Chord*> gnb(graceNotesBefore());
    1164        65137 :         for (Chord* ch : gnb) {
    1165          777 :             if (ch->vStaffIdx() != staffIdx) {
    1166          135 :                 continue;
    1167              :             }
    1168          642 :             std::vector<Note*> notes(ch->notes());        // we need a copy!
    1169         1374 :             for (Note* note : notes) {
    1170          732 :                 note->updateAccidental(as);
    1171              :             }
    1172          642 :             ch->sortNotes();
    1173          642 :         }
    1174        64360 :         if (vStaffIdx() == staffIdx) {
    1175        51149 :             std::vector<Note*> lnotes(notes());      // we need a copy!
    1176       109386 :             for (Note* note : lnotes) {
    1177        58237 :                 if (note->tieBackNonPartial() && note->tpc() == note->tieBack()->startNote()->tpc()) {
    1178              :                     // same pitch
    1179         2146 :                     if (note->accidental() && note->accidental()->role() == AccidentalRole::AUTO) {
    1180              :                         // not courtesy
    1181              :                         // TODO: remove accidental only if note is not
    1182              :                         // on new system
    1183           30 :                         score()->undoRemoveElement(note->accidental());
    1184              :                     }
    1185              :                 }
    1186        58237 :                 note->updateAccidental(as);
    1187              :             }
    1188        52797 :             for (Articulation* art : m_articulations) {
    1189         1648 :                 if (!art->isOrnament()) {
    1190         1340 :                     continue;
    1191              :                 }
    1192          308 :                 Ornament* ornament = toOrnament(art);
    1193          308 :                 ornament->computeNotesAboveAndBelow(as);
    1194              :             }
    1195        52969 :             for (Spanner* spanner : startingSpanners()) {
    1196         1820 :                 if (spanner->isTrill()) {
    1197          113 :                     Ornament* ornament = toTrill(spanner)->ornament();
    1198          113 :                     if (ornament) {
    1199          113 :                         ornament->setOwnershipParent(this);
    1200          113 :                         ornament->computeNotesAboveAndBelow(as);
    1201              :                     }
    1202              :                 }
    1203              :             }
    1204        51149 :             sortNotes();
    1205        51149 :         }
    1206        64360 :         const std::vector<Chord*> gna(graceNotesAfter());
    1207        64610 :         for (Chord* ch : gna) {
    1208          250 :             if (ch->vStaffIdx() != staffIdx) {
    1209           23 :                 continue;
    1210              :             }
    1211          227 :             std::vector<Note*> notes(ch->notes());        // we need a copy!
    1212          476 :             for (Note* note : notes) {
    1213          249 :                 note->updateAccidental(as);
    1214              :             }
    1215          227 :             ch->sortNotes();
    1216          227 :         }
    1217        65476 :     } else if (staffGroup == StaffGroup::PERCUSSION) {
    1218         1116 :         const Instrument* instrument = originPart()->instrument(this->tick());
    1219         1116 :         const Drumset* drumset = instrument->drumset();
    1220         1116 :         if (!drumset) {
    1221            0 :             LOGW("no drumset");
    1222              :         }
    1223         1116 :         updatePercussionNotes(this, drumset);
    1224         1116 :         sortNotes();
    1225              :     }
    1226              : }
    1227              : 
    1228              : //---------------------------------------------------------
    1229              : //   pagePos
    1230              : //---------------------------------------------------------
    1231              : 
    1232       284571 : PointF Chord::pagePos() const
    1233              : {
    1234       284571 :     if (!isGrace()) {
    1235       278134 :         return EngravingItem::pagePos();
    1236              :     }
    1237              : 
    1238         6437 :     PointF p(pos());
    1239         6437 :     if (!ownershipParent()) {
    1240            0 :         return p;
    1241              :     }
    1242         6437 :     p.rx() = pageX();
    1243              : 
    1244         6437 :     const Chord* pc = toChord(ownershipParent());
    1245         6437 :     System* system = pc->segment()->system();
    1246         6437 :     if (!system) {
    1247            3 :         return p;
    1248              :     }
    1249         6434 :     p.ry() += system->staffYpage(vStaffIdx()) + staffOffsetY();
    1250         6434 :     return p;
    1251              : }
    1252              : 
    1253              : //---------------------------------------------------------
    1254              : //   scanElements
    1255              : //---------------------------------------------------------
    1256              : 
    1257        14071 : void Chord::scanElements(std::function<void(EngravingItem*)> func)
    1258              : {
    1259        14375 :     for (Articulation* a : m_articulations) {
    1260          304 :         a->scanElements(func);
    1261              :     }
    1262        14071 :     if (m_hook) {
    1263          814 :         func(m_hook);
    1264              :     }
    1265        14071 :     if (m_stem) {
    1266        13350 :         func(m_stem);
    1267              :     }
    1268        14071 :     if (m_stemSlash) {
    1269           32 :         func(m_stemSlash);
    1270              :     }
    1271        14071 :     if (m_arpeggio) {
    1272           96 :         func(m_arpeggio);
    1273              :     }
    1274        14071 :     if (m_tremoloTwoChord && (tremoloChordType() != TremoloChordType::TremoloSecondChord)) {
    1275            4 :         func(m_tremoloTwoChord);
    1276              :     }
    1277        14071 :     if (m_tremoloSingleChord) {
    1278           27 :         func(m_tremoloSingleChord);
    1279              :     }
    1280        14071 :     const Staff* st = staff();
    1281        14071 :     if ((st && st->showLedgerLines(tick())) || !st) {       // also for palette
    1282        17791 :         for (LedgerLine* ll : m_ledgerLines) {
    1283         3720 :             func(ll);
    1284              :         }
    1285              :     }
    1286        30537 :     for (Note* note : m_notes) {
    1287        16466 :         note->scanElements(func);
    1288              :     }
    1289        14169 :     for (Chord* chord : m_graceNotes) {
    1290           98 :         chord->scanElements(func);
    1291              :     }
    1292        14123 :     for (EngravingItem* e : el()) {
    1293           52 :         e->scanElements(func);
    1294              :     }
    1295              : 
    1296        14082 :     for (auto& p : m_noteParens) {
    1297           11 :         p->leftParen()->scanElements(func);
    1298           11 :         p->rightParen()->scanElements(func);
    1299              :     }
    1300        14071 :     ChordRest::scanElements(func);
    1301        14071 : }
    1302              : 
    1303          463 : NoteParenthesisInfo* Chord::findNoteParenthesisInfo(const Parenthesis* paren)
    1304              : {
    1305          485 :     for (NoteParenthesisInfo* infoPtr : m_noteParens) {
    1306          485 :         if (paren == infoPtr->leftParen() || paren == infoPtr->rightParen()) {
    1307          463 :             return infoPtr;
    1308              :         }
    1309              :     }
    1310              : 
    1311            0 :     return nullptr;
    1312              : }
    1313              : 
    1314        75970 : const NoteParenthesisInfo* Chord::findNoteParenthesisInfo(const Note* note) const
    1315              : {
    1316        76029 :     for (const NoteParenthesisInfo* infoPtr : m_noteParens) {
    1317          892 :         for (const Note* parenNote : infoPtr->notes()) {
    1318          833 :             if (parenNote == note) {
    1319          482 :                 return infoPtr;
    1320              :             }
    1321              :         }
    1322              :     }
    1323              : 
    1324        75488 :     DO_ASSERT(u"Parentheses are not in chord");
    1325        75488 :     return nullptr;
    1326              : }
    1327              : 
    1328          206 : void Chord::addNoteParenthesisInfo(NoteParenthesisInfo* noteParenInfo)
    1329              : {
    1330          206 :     m_noteParens.push_back(noteParenInfo);
    1331          206 : }
    1332              : 
    1333           13 : void Chord::removeNoteParenthesisInfo(const NoteParenthesisInfo* noteParenInfo)
    1334              : {
    1335           13 :     if (m_noteParens.empty()) {
    1336            0 :         return;
    1337              :     }
    1338              : 
    1339           13 :     if (!noteParenInfo) {
    1340            0 :         return;
    1341              :     }
    1342              : 
    1343           13 :     auto it = std::find_if(m_noteParens.begin(), m_noteParens.end(), [noteParenInfo](const NoteParenthesisInfo* ptr) {
    1344           16 :         return ptr == noteParenInfo;
    1345              :     });
    1346              : 
    1347           13 :     if (it != m_noteParens.end()) {
    1348           13 :         m_noteParens.erase(it);
    1349              :     }
    1350              : }
    1351              : 
    1352            9 : void Chord::addNoteToParenthesisInfo(Note* note, const Parenthesis* paren)
    1353              : {
    1354            9 :     NoteParenthesisInfo* noteParenInfo = findNoteParenthesisInfo(paren);
    1355              : 
    1356            9 :     if (!noteParenInfo) {
    1357            0 :         return;
    1358              :     }
    1359              : 
    1360            9 :     noteParenInfo->insertNote(note);
    1361              : }
    1362              : 
    1363           12 : void Chord::removeNoteFromParenthesisInfo(Note* note, const Parenthesis* paren)
    1364              : {
    1365           12 :     NoteParenthesisInfo* noteParenInfo = findNoteParenthesisInfo(paren);
    1366              : 
    1367           12 :     if (!noteParenInfo) {
    1368            0 :         return;
    1369              :     }
    1370              : 
    1371           12 :     noteParenInfo->removeNote(note);
    1372              : }
    1373              : 
    1374              : //---------------------------------------------------------
    1375              : //   isChordPlayable
    1376              : //   @note Now every related to chord element has it's own "PLAY" property,
    1377              : //         However, there is no way to control these properties outside the scope of the chord since the new Properties panel.
    1378              : //         So we'll use a chord as a proxy entity for "PLAY" property handling
    1379              : //---------------------------------------------------------
    1380              : 
    1381           79 : bool Chord::isChordPlayable() const
    1382              : {
    1383           79 :     if (!m_notes.empty()) {
    1384           79 :         if (m_notes.front()->isPreBendOrDiveStart()) {
    1385           10 :             return false;
    1386              :         }
    1387              : 
    1388           69 :         return m_notes.front()->getProperty(Pid::PLAY).toBool();
    1389            0 :     } else if (tremoloTwoChord()) {
    1390            0 :         return tremoloTwoChord()->getProperty(Pid::PLAY).toBool();
    1391            0 :     } else if (tremoloSingleChord()) {
    1392            0 :         return tremoloSingleChord()->getProperty(Pid::PLAY).toBool();
    1393            0 :     } else if (m_arpeggio) {
    1394            0 :         return m_arpeggio->getProperty(Pid::PLAY).toBool();
    1395              :     }
    1396              : 
    1397            0 :     return false;
    1398              : }
    1399              : 
    1400              : //---------------------------------------------------------
    1401              : //   setIsChordPlayable
    1402              : //---------------------------------------------------------
    1403              : 
    1404            0 : void Chord::setIsChordPlayable(const bool isPlayable)
    1405              : {
    1406            0 :     for (Note* note : m_notes) {
    1407            0 :         note->undoChangeProperty(Pid::PLAY, isPlayable);
    1408              :     }
    1409              : 
    1410            0 :     if (m_arpeggio) {
    1411            0 :         m_arpeggio->undoChangeProperty(Pid::PLAY, isPlayable);
    1412              :     }
    1413              : 
    1414            0 :     if (tremoloTwoChord()) {
    1415            0 :         tremoloTwoChord()->undoChangeProperty(Pid::PLAY, isPlayable);
    1416              :     }
    1417              : 
    1418            0 :     if (tremoloSingleChord()) {
    1419            0 :         tremoloSingleChord()->undoChangeProperty(Pid::PLAY, isPlayable);
    1420              :     }
    1421              : 
    1422            0 :     triggerLayout();
    1423            0 : }
    1424              : 
    1425              : //---------------------------------------------------------
    1426              : //   findNote
    1427              : //---------------------------------------------------------
    1428              : 
    1429          849 : Note* Chord::findNote(int pitch, int skip) const
    1430              : {
    1431          849 :     size_t ns = m_notes.size();
    1432          964 :     for (size_t i = 0; i < ns; ++i) {
    1433          926 :         Note* n = m_notes.at(i);
    1434          926 :         if (n->pitch() == pitch) {
    1435          811 :             if (skip == 0) {
    1436          811 :                 return n;
    1437              :             } else {
    1438            0 :                 --skip;
    1439              :             }
    1440              :         }
    1441              :     }
    1442           38 :     return 0;
    1443              : }
    1444              : 
    1445          311 : void Chord::undoChangeSpanArpeggio(Arpeggio* a)
    1446              : {
    1447          311 :     if (m_spanArpeggio == a) {
    1448          230 :         return;
    1449              :     }
    1450              : 
    1451              :     // TODO: change arpeggio for links
    1452           81 :     score()->undo(new ChangeSpanArpeggio(this, a));
    1453              : }
    1454              : 
    1455        14290 : ChordLine* Chord::chordLine() const
    1456              : {
    1457        14290 :     for (EngravingItem* item : el()) {
    1458            4 :         if (item && item->isChordLine()) {
    1459            4 :             return toChordLine(item);
    1460              :         }
    1461              :     }
    1462              : 
    1463        14286 :     return nullptr;
    1464              : }
    1465              : 
    1466              : //---------------------------------------------------------
    1467              : //   drop
    1468              : //---------------------------------------------------------
    1469              : 
    1470           24 : EngravingItem* Chord::drop(Transaction& tx, EditData& data)
    1471              : {
    1472           24 :     EngravingItem* e = data.dropElement;
    1473           24 :     switch (e->type()) {
    1474            0 :     case ElementType::ARTICULATION:
    1475              :     case ElementType::ORNAMENT:
    1476              :     case ElementType::TAPPING:
    1477              :     {
    1478            0 :         Articulation* atr = toArticulation(e);
    1479            0 :         Articulation* oa = hasArticulation(atr);
    1480            0 :         if (oa) {
    1481            0 :             delete atr;
    1482            0 :             atr = 0;
    1483              :             // if attribute is already there, remove
    1484              :             // score()->cmdRemove(oa); // unexpected behaviour?
    1485            0 :             score()->select(oa, SelectType::SINGLE, 0);
    1486              :         } else {
    1487              :             // intuit anchor for this accidental based on other accidentals in the chord
    1488            0 :             int aboveBelow = 0;
    1489            0 :             bool mixed = false;
    1490            0 :             for (Articulation* a : m_articulations) {
    1491            0 :                 PropertyFlags pf = a->propertyFlags(Pid::ARTICULATION_ANCHOR);
    1492            0 :                 if (pf == PropertyFlags::STYLED) {
    1493            0 :                     continue;
    1494              :                 }
    1495            0 :                 if (a->anchor() == ArticulationAnchor::TOP) {
    1496            0 :                     if (aboveBelow < 0) {
    1497            0 :                         mixed = true;
    1498            0 :                         break;
    1499              :                     }
    1500            0 :                     ++aboveBelow;
    1501            0 :                 } else if (a->anchor() == ArticulationAnchor::BOTTOM) {
    1502            0 :                     if (aboveBelow > 0) {
    1503            0 :                         mixed = true;
    1504            0 :                         break;
    1505              :                     }
    1506            0 :                     --aboveBelow;
    1507              :                 }
    1508              :             }
    1509            0 :             if (!mixed && aboveBelow != 0) {
    1510            0 :                 if (aboveBelow > 0) {
    1511            0 :                     atr->setAnchor(ArticulationAnchor::TOP);
    1512              :                 } else {
    1513            0 :                     atr->setAnchor(ArticulationAnchor::BOTTOM);
    1514              :                 }
    1515            0 :                 atr->setPropertyFlags(Pid::ARTICULATION_ANCHOR, PropertyFlags::UNSTYLED);
    1516              :             }
    1517            0 :             atr->setOwnershipParent(this);
    1518            0 :             atr->setTrack(track());
    1519              : 
    1520              :             // Immediately add if the chord has no existing articulations, toggle otherwise...
    1521            0 :             if (m_articulations.empty()) {
    1522            0 :                 score()->undoAddElement(atr);
    1523              :             } else {
    1524            0 :                 EditChord::toggleArticulation(score(), this, atr);
    1525              :             }
    1526              :         }
    1527            0 :         return atr;
    1528              :     }
    1529              : 
    1530            3 :     case ElementType::CHORDLINE:
    1531            3 :         e->setOwnershipParent(this);
    1532            3 :         e->setTrack(track());
    1533            3 :         score()->undoAddElement(e);
    1534            3 :         break;
    1535              : 
    1536            0 :     case ElementType::TREMOLO_SINGLECHORD:
    1537            0 :         if (tremoloSingleChord()) {
    1538            0 :             bool sameType = (e->subtype() == tremoloSingleChord()->subtype());
    1539            0 :             score()->undoRemoveElement(tremoloSingleChord());
    1540            0 :             if (sameType) {
    1541            0 :                 delete e;
    1542            0 :                 return nullptr;
    1543              :             }
    1544              :         }
    1545            0 :         e->setOwnershipParent(this);
    1546            0 :         e->setTrack(track());
    1547            0 :         score()->undoAddElement(e);
    1548            0 :         break;
    1549              : 
    1550            0 :     case ElementType::TREMOLO_TWOCHORD:
    1551              :     {
    1552            0 :         TremoloTwoChord* t = item_cast<TremoloTwoChord*>(e);
    1553            0 :         Segment* s = segment()->next();
    1554            0 :         while (s) {
    1555            0 :             if (s->element(track()) && s->element(track())->isChord()) {
    1556            0 :                 break;
    1557              :             }
    1558            0 :             s = s->next();
    1559              :         }
    1560            0 :         if (s == 0) {
    1561            0 :             LOGD("no segment for second note of tremolo found");
    1562            0 :             delete e;
    1563            0 :             return 0;
    1564              :         }
    1565            0 :         Chord* ch2 = toChord(s->element(track()));
    1566            0 :         if (ch2->ticks() != ticks()) {
    1567            0 :             LOGD("no matching chord for second note of tremolo found");
    1568            0 :             delete e;
    1569            0 :             return 0;
    1570              :         }
    1571            0 :         t->setChords(this, ch2);
    1572              :     }
    1573            0 :         if (tremoloTwoChord()) {
    1574            0 :             bool sameType = (e->subtype() == tremoloTwoChord()->subtype());
    1575            0 :             score()->undoRemoveElement(tremoloTwoChord());
    1576            0 :             if (sameType) {
    1577            0 :                 delete e;
    1578            0 :                 return 0;
    1579              :             }
    1580              :         }
    1581            0 :         e->setOwnershipParent(this);
    1582            0 :         e->setTrack(track());
    1583            0 :         score()->undoAddElement(e);
    1584            0 :         break;
    1585              : 
    1586            0 :     case ElementType::ARPEGGIO:
    1587              :     case ElementType::CHORD_BRACKET:
    1588              :     {
    1589            0 :         Arpeggio* a = toArpeggio(e);
    1590            0 :         if (arpeggio() && a->isArpeggio()) {
    1591            0 :             score()->undoRemoveElement(arpeggio());
    1592              :         }
    1593            0 :         a->setTrack(track());
    1594            0 :         a->setOwnershipParent(this);
    1595            0 :         score()->undoAddElement(a);
    1596              :     }
    1597            0 :         return e;
    1598              : 
    1599           21 :     default:
    1600           21 :         return ChordRest::drop(tx, data);
    1601              :     }
    1602            3 :     return 0;
    1603              : }
    1604              : 
    1605            0 : void Chord::setColor(const Color& color)
    1606              : {
    1607            0 :     ChordRest::setColor(color);
    1608              : 
    1609            0 :     for (Note* note : m_notes) {
    1610            0 :         note->undoChangeProperty(Pid::COLOR, PropertyValue::fromValue(color));
    1611              :     }
    1612            0 : }
    1613              : 
    1614              : //---------------------------------------------------------
    1615              : //   setStemDirection
    1616              : //---------------------------------------------------------
    1617              : 
    1618         6367 : void Chord::setStemDirection(DirectionV d)
    1619              : {
    1620         6367 :     m_stemDirection = d;
    1621         6367 : }
    1622              : 
    1623              : //---------------------------------------------------------
    1624              : //   localSpatiumChanged
    1625              : //---------------------------------------------------------
    1626              : 
    1627            0 : void Chord::localSpatiumChanged(double oldValue, double newValue)
    1628              : {
    1629            0 :     ChordRest::localSpatiumChanged(oldValue, newValue);
    1630            0 :     for (EngravingItem* e : graceNotes()) {
    1631            0 :         e->localSpatiumChanged(oldValue, newValue);
    1632              :     }
    1633            0 :     if (m_hook) {
    1634            0 :         m_hook->localSpatiumChanged(oldValue, newValue);
    1635              :     }
    1636            0 :     if (m_stem) {
    1637            0 :         m_stem->localSpatiumChanged(oldValue, newValue);
    1638              :     }
    1639            0 :     if (m_stemSlash) {
    1640            0 :         m_stemSlash->localSpatiumChanged(oldValue, newValue);
    1641              :     }
    1642            0 :     if (m_arpeggio) {
    1643            0 :         m_arpeggio->localSpatiumChanged(oldValue, newValue);
    1644              :     }
    1645              : 
    1646            0 :     if (m_tremoloSingleChord) {
    1647            0 :         m_tremoloSingleChord->localSpatiumChanged(oldValue, newValue);
    1648            0 :     } else if (m_tremoloTwoChord && (tremoloChordType() != TremoloChordType::TremoloSecondChord)) {
    1649            0 :         m_tremoloTwoChord->localSpatiumChanged(oldValue, newValue);
    1650              :     }
    1651              : 
    1652            0 :     for (EngravingItem* e : articulations()) {
    1653            0 :         e->localSpatiumChanged(oldValue, newValue);
    1654              :     }
    1655            0 :     for (Note* note : notes()) {
    1656            0 :         note->localSpatiumChanged(oldValue, newValue);
    1657              :     }
    1658            0 : }
    1659              : 
    1660              : //---------------------------------------------------------
    1661              : //   getProperty
    1662              : //---------------------------------------------------------
    1663              : 
    1664       305694 : PropertyValue Chord::getProperty(Pid propertyId) const
    1665              : {
    1666       305694 :     switch (propertyId) {
    1667          136 :     case Pid::NO_STEM:         return noStem();
    1668            3 :     case Pid::SHOW_STEM_SLASH: return showStemSlash();
    1669        19257 :     case Pid::STEM_DIRECTION:  return PropertyValue::fromValue<DirectionV>(stemDirection());
    1670            0 :     case Pid::PLAY: return isChordPlayable();
    1671        18926 :     case Pid::COMBINE_VOICE: return PropertyValue::fromValue<AutoOnOff>(combineVoice());
    1672        18929 :     case Pid::VISIBLE: return true; // Chord cannot be set invisible, only its elements can
    1673       248443 :     default:
    1674       248443 :         return ChordRest::getProperty(propertyId);
    1675              :     }
    1676              : }
    1677              : 
    1678              : //---------------------------------------------------------
    1679              : //   propertyDefault
    1680              : //---------------------------------------------------------
    1681              : 
    1682       266042 : PropertyValue Chord::propertyDefault(Pid propertyId) const
    1683              : {
    1684       266042 :     switch (propertyId) {
    1685            0 :     case Pid::NO_STEM:         return false;
    1686        19592 :     case Pid::SHOW_STEM_SLASH: return noteType() == NoteType::ACCIACCATURA;
    1687        37852 :     case Pid::STEM_DIRECTION:  return PropertyValue::fromValue<DirectionV>(DirectionV::AUTO);
    1688            0 :     case Pid::PLAY: return true;
    1689        37852 :     case Pid::COMBINE_VOICE: return AutoOnOff::AUTO;
    1690       208598 :     default:
    1691       208598 :         return ChordRest::propertyDefault(propertyId);
    1692              :     }
    1693              : }
    1694              : 
    1695            0 : bool Chord::isUserModified() const
    1696              : {
    1697            0 :     if (showStemSlash() != propertyDefault(Pid::SHOW_STEM_SLASH).toBool()) {
    1698            0 :         return true;
    1699              :     }
    1700              : 
    1701            0 :     return EngravingItem::isUserModified();
    1702              : }
    1703              : 
    1704              : //---------------------------------------------------------
    1705              : //   setProperty
    1706              : //---------------------------------------------------------
    1707              : 
    1708         1597 : bool Chord::setProperty(Pid propertyId, const PropertyValue& v)
    1709              : {
    1710         1597 :     switch (propertyId) {
    1711           64 :     case Pid::NO_STEM:
    1712           64 :         setNoStem(v.toBool());
    1713           64 :         break;
    1714          667 :     case Pid::SHOW_STEM_SLASH:
    1715          667 :         requestShowStemSlash(v.toBool());
    1716          667 :         break;
    1717          675 :     case Pid::STEM_DIRECTION:
    1718          675 :         setStemDirection(v.value<DirectionV>());
    1719          675 :         break;
    1720            0 :     case Pid::PLAY:
    1721            0 :         setIsChordPlayable(v.toBool());
    1722            0 :         break;
    1723            0 :     case Pid::COMBINE_VOICE:
    1724            0 :         setCombineVoice(v.value<AutoOnOff>());
    1725            0 :         break;
    1726          191 :     default:
    1727          191 :         return ChordRest::setProperty(propertyId, v);
    1728              :     }
    1729         1406 :     triggerLayout();
    1730         1406 :     return true;
    1731              : }
    1732              : 
    1733              : //---------------------------------------------------------
    1734              : //   hasArticulation
    1735              : //---------------------------------------------------------
    1736              : 
    1737          313 : Articulation* Chord::hasArticulation(const Articulation* aa)
    1738              : {
    1739          462 :     for (Articulation* a : m_articulations) {
    1740          160 :         if (a->subtype() == aa->subtype()) {
    1741           11 :             return a;
    1742              :         }
    1743              :     }
    1744          302 :     return 0;
    1745              : }
    1746              : 
    1747         1163 : Tapping* Chord::tapping() const
    1748              : {
    1749         1163 :     std::vector<Tapping*> tappings;
    1750         1163 :     tappings.reserve(1);
    1751         1177 :     for (Articulation* a : m_articulations) {
    1752           14 :         if (a->isTapping()) {
    1753            0 :             tappings.push_back(toTapping(a));
    1754              :         }
    1755              :     }
    1756         1163 :     DO_ASSERT(tappings.size() <= 1);
    1757              : 
    1758         2326 :     return tappings.size() > 0 ? tappings.front() : nullptr;
    1759         1163 : }
    1760              : 
    1761           18 : void Chord::updateArticulations(const std::set<SymId>& newArticulationIds, ArticulationsUpdateMode updateMode)
    1762              : {
    1763           18 :     Articulation* staccato = nullptr;
    1764           18 :     Articulation* accent = nullptr;
    1765           18 :     Articulation* marcato = nullptr;
    1766           18 :     Articulation* tenuto = nullptr;
    1767           18 :     ArticulationAnchor overallAnchor = ArticulationAnchor::AUTO;
    1768           18 :     bool mixedDirections = false;
    1769           18 :     if (!m_articulations.empty()) {
    1770            0 :         std::vector<Articulation*> articsToRemove;
    1771              :         // split all articulations
    1772            0 :         for (Articulation* artic : m_articulations) {
    1773            0 :             if (!artic->isDouble()) {
    1774            0 :                 continue;
    1775              :             }
    1776            0 :             auto splitSyms = splitArticulations({ artic->symId() });
    1777            0 :             for (const SymId& id : splitSyms) {
    1778            0 :                 Articulation* newArticulation = Factory::createArticulation(this);
    1779            0 :                 newArticulation->setSymId(id);
    1780            0 :                 newArticulation->setAnchor(artic->anchor());
    1781            0 :                 newArticulation->setPropertyFlags(Pid::ARTICULATION_ANCHOR, artic->propertyFlags(Pid::ARTICULATION_ANCHOR));
    1782            0 :                 if (!hasArticulation(newArticulation)) {
    1783            0 :                     EditChord::toggleArticulation(score(), this, newArticulation);
    1784              :                 } else {
    1785            0 :                     delete newArticulation;
    1786              :                 }
    1787              :             }
    1788            0 :             articsToRemove.push_back(artic);
    1789            0 :         }
    1790            0 :         for (Articulation* artic : articsToRemove) {
    1791            0 :             score()->undoRemoveElement(artic);
    1792              :         }
    1793              :         // now we have guaranteed no more combined artics
    1794              :         // take an inventory of which articulations are already present
    1795            0 :         for (Articulation* artic : m_articulations) {
    1796            0 :             PropertyFlags pf = artic->propertyFlags(Pid::ARTICULATION_ANCHOR);
    1797            0 :             if (!mixedDirections && pf == PropertyFlags::UNSTYLED && artic->anchor() != ArticulationAnchor::AUTO) {
    1798            0 :                 if (overallAnchor != ArticulationAnchor::AUTO && artic->anchor() != overallAnchor) {
    1799            0 :                     mixedDirections = true;
    1800            0 :                     overallAnchor = ArticulationAnchor::AUTO;
    1801              :                 } else {
    1802            0 :                     overallAnchor = artic->anchor();
    1803              :                 }
    1804              :             }
    1805            0 :             if (artic->isStaccato()) {
    1806            0 :                 staccato = artic;
    1807            0 :             } else if (artic->isAccent()) {
    1808            0 :                 accent = artic;
    1809            0 :             } else if (artic->isMarcato()) {
    1810            0 :                 marcato = artic;
    1811            0 :             } else if (artic->isTenuto()) {
    1812            0 :                 tenuto = artic;
    1813              :             }
    1814              :         }
    1815            0 :     }
    1816           18 :     std::set<SymId> newArtics;
    1817              :     // get symbol id's for correct direction
    1818           18 :     for (const SymId& id : newArticulationIds) {
    1819            0 :         if (id == SymId::articAccentAbove || id == SymId::articAccentBelow) {
    1820            0 :             if (marcato && updateMode == ArticulationsUpdateMode::Insert) {
    1821              :                 // adding an accent, which replaces marcato
    1822            0 :                 score()->undoRemoveElement(marcato);
    1823              :             }
    1824            0 :         } else if (id == SymId::articMarcatoAbove || id == SymId::articMarcatoBelow) {
    1825            0 :             if (accent && updateMode == ArticulationsUpdateMode::Insert) {
    1826              :                 // adding a marcato, which replaces accent
    1827            0 :                 score()->undoRemoveElement(accent);
    1828              :             }
    1829              :         }
    1830            0 :         newArtics.insert(id);
    1831              :     }
    1832              : 
    1833              :     // newArtics now contains the articulations in the correct direction
    1834           18 :     if (updateMode == ArticulationsUpdateMode::Remove) {
    1835              :         // remove articulations from _articulations that are found in newArtics
    1836            0 :         for (const SymId& id : newArtics) {
    1837            0 :             switch (id) {
    1838            0 :             case SymId::articAccentAbove:
    1839              :             case SymId::articAccentBelow:
    1840            0 :                 score()->undoRemoveElement(accent);
    1841            0 :                 accent = nullptr;
    1842            0 :                 break;
    1843            0 :             case SymId::articMarcatoAbove:
    1844              :             case SymId::articMarcatoBelow:
    1845            0 :                 score()->undoRemoveElement(marcato);
    1846            0 :                 marcato = nullptr;
    1847            0 :                 break;
    1848            0 :             case SymId::articTenutoAbove:
    1849              :             case SymId::articTenutoBelow:
    1850            0 :                 score()->undoRemoveElement(tenuto);
    1851            0 :                 tenuto = nullptr;
    1852            0 :                 break;
    1853            0 :             case SymId::articStaccatoAbove:
    1854              :             case SymId::articStaccatoBelow:
    1855            0 :                 score()->undoRemoveElement(staccato);
    1856            0 :                 staccato = nullptr;
    1857            0 :                 break;
    1858            0 :             default:
    1859            0 :                 break;
    1860              :             }
    1861              :         }
    1862              :     } else {
    1863              :         // add articulations from newArtics that are not found in m_articulations
    1864           18 :         for (const SymId& id : newArtics) {
    1865            0 :             Articulation* newArticulation = Factory::createArticulation(this);
    1866            0 :             newArticulation->setSymId(id);
    1867            0 :             if (overallAnchor != ArticulationAnchor::AUTO) {
    1868            0 :                 newArticulation->setAnchor(overallAnchor);
    1869            0 :                 newArticulation->setPropertyFlags(Pid::ARTICULATION_ANCHOR, PropertyFlags::UNSTYLED);
    1870              :             }
    1871            0 :             if (!hasArticulation(newArticulation)) {
    1872            0 :                 EditChord::toggleArticulation(score(), this, newArticulation);
    1873              :             } else {
    1874            0 :                 delete newArticulation;
    1875              :             }
    1876              :         }
    1877              :     }
    1878           18 : }
    1879              : 
    1880              : //---------------------------------------------------------
    1881              : //   reset
    1882              : //---------------------------------------------------------
    1883              : 
    1884            0 : void Chord::reset()
    1885              : {
    1886            0 :     undoChangeProperty(Pid::STEM_DIRECTION, DirectionV::AUTO);
    1887            0 :     ChordRest::reset();
    1888            0 : }
    1889              : 
    1890              : //---------------------------------------------------------
    1891              : //   slash
    1892              : //---------------------------------------------------------
    1893              : 
    1894         2329 : bool Chord::slash()
    1895              : {
    1896         2329 :     Note* n = upNote();
    1897         2329 :     return n->fixed();
    1898              : }
    1899              : 
    1900              : //---------------------------------------------------------
    1901              : //   setSlash
    1902              : //---------------------------------------------------------
    1903              : 
    1904           90 : void Chord::setSlash(bool flag, bool stemless)
    1905              : {
    1906           90 :     int line = 0;
    1907           90 :     NoteHeadGroup head = NoteHeadGroup::HEAD_SLASH;
    1908              : 
    1909           90 :     if (!flag) {
    1910              :         // restore to normal
    1911            0 :         undoChangeProperty(Pid::NO_STEM, false);
    1912            0 :         undoChangeProperty(Pid::SMALL, false);
    1913            0 :         undoChangeProperty(Pid::OFFSET, PointF());
    1914            0 :         for (Note* n : m_notes) {
    1915            0 :             n->undoChangeProperty(Pid::HEAD_GROUP, NoteHeadGroup::HEAD_NORMAL);
    1916            0 :             n->undoChangeProperty(Pid::FIXED, false);
    1917            0 :             n->undoChangeProperty(Pid::FIXED_LINE, 0);
    1918            0 :             n->undoChangeProperty(Pid::PLAY, true);
    1919            0 :             n->undoChangeProperty(Pid::VISIBLE, true);
    1920            0 :             for (NoteDot* dot : n->dots()) {
    1921            0 :                 dot->undoChangeProperty(Pid::VISIBLE, true);
    1922              :             }
    1923            0 :             if (staff()->isDrumStaff(tick())) {
    1924            0 :                 const Drumset* ds = originPart()->instrument(tick())->drumset();
    1925            0 :                 int pitch = n->pitch();
    1926            0 :                 if (ds && ds->isValid(pitch)) {
    1927            0 :                     undoChangeProperty(Pid::STEM_DIRECTION, ds->stemDirection(pitch));
    1928            0 :                     n->undoChangeProperty(Pid::HEAD_GROUP, ds->noteHead(pitch));
    1929              :                 }
    1930              :             }
    1931              :         }
    1932            0 :         return;
    1933              :     }
    1934              : 
    1935              :     // make stemless if asked
    1936           90 :     if (stemless) {
    1937           36 :         undoChangeProperty(Pid::NO_STEM, true);
    1938           36 :         undoChangeProperty(Pid::BEAM_MODE, BeamMode::NONE);
    1939              :     }
    1940              : 
    1941           90 :     const StaffType* staffType = this->staffType();
    1942              : 
    1943              :     // voice-dependent attributes - line, size, offset, head
    1944           90 :     if (track() % VOICES < 2) {
    1945              :         // use middle line
    1946           64 :         line = staffType->middleLine();
    1947           64 :         undoChangeProperty(Pid::STEM_DIRECTION, PropertyValue::fromValue<DirectionV>(DirectionV::DOWN));
    1948              :     } else {
    1949              :         // set small
    1950           26 :         undoChangeProperty(Pid::SMALL, true);
    1951              :         // set outside the staff
    1952           26 :         double y = 0.0;
    1953           26 :         if (track() % 2) {
    1954           12 :             line = staffType->bottomLine() + 1;
    1955           12 :             y    = 0.5 * spatium();
    1956           12 :             undoChangeProperty(Pid::STEM_DIRECTION, PropertyValue::fromValue<DirectionV>(DirectionV::DOWN));
    1957              :         } else {
    1958           14 :             line = -1;
    1959           14 :             if (!staffType->isDrumStaff()) {
    1960            9 :                 y = -0.5 * spatium();
    1961              :             }
    1962           14 :             undoChangeProperty(Pid::STEM_DIRECTION, PropertyValue::fromValue<DirectionV>(DirectionV::UP));
    1963              :         }
    1964              :         // for non-drum staves, add an additional offset
    1965              :         // for drum staves, no offset, but use normal head
    1966           26 :         if (!staffType->isDrumStaff()) {
    1967           15 :             mutldata()->moveY(y);
    1968              :         } else {
    1969           11 :             head = NoteHeadGroup::HEAD_NORMAL;
    1970              :         }
    1971              :     }
    1972              : 
    1973           90 :     size_t ns = m_notes.size();
    1974          180 :     for (size_t i = 0; i < ns; ++i) {
    1975           90 :         Note* n = m_notes[i];
    1976           90 :         n->undoChangeProperty(Pid::HEAD_GROUP, head);
    1977           90 :         n->undoChangeProperty(Pid::FIXED, true);
    1978           90 :         n->undoChangeProperty(Pid::FIXED_LINE, line);
    1979           90 :         n->undoChangeProperty(Pid::PLAY, false);
    1980              :         // hide all but first notehead
    1981           90 :         if (i) {
    1982            0 :             n->undoChangeProperty(Pid::VISIBLE, false);
    1983            0 :             for (NoteDot* dot : n->dots()) {
    1984            0 :                 dot->undoChangeProperty(Pid::VISIBLE, false);
    1985              :             }
    1986              :         }
    1987              :     }
    1988              : }
    1989              : 
    1990        58613 : bool Chord::endsNoteAnchoredLine() const
    1991              : {
    1992       124407 :     for (const Note* note : m_notes) {
    1993        66455 :         for (const Spanner* sp : note->spannerBack()) {
    1994          661 :             if (sp->isGlissando() || sp->isGuitarBend() || (sp->isNoteLine() && toNoteLine(sp)->enforceMinLength())) {
    1995          661 :                 return true;
    1996              :             }
    1997              :         }
    1998              :     }
    1999        57952 :     return false;
    2000              : }
    2001              : 
    2002              : //---------------------------------------------------------
    2003              : //   removeMarkings
    2004              : //    - this is normally called after cloning a chord to tie a note over the barline
    2005              : //    - there is no special undo handling; the assumption is that undo will simply remove the cloned chord
    2006              : //    - two note tremolos are converted into simple notes
    2007              : //    - single note tremolos are optionally retained
    2008              : //---------------------------------------------------------
    2009              : 
    2010          122 : void Chord::removeMarkings(bool keepTremolo)
    2011              : {
    2012          122 :     if (m_tremoloSingleChord && !keepTremolo) {
    2013            0 :         remove(m_tremoloSingleChord);
    2014              :     }
    2015              : 
    2016          122 :     if (m_tremoloTwoChord && !keepTremolo) {
    2017            0 :         remove(m_tremoloTwoChord);
    2018              :     }
    2019              : 
    2020          122 :     if (arpeggio()) {
    2021            0 :         remove(arpeggio());
    2022              :     }
    2023              : 
    2024          122 :     if (m_spanArpeggio) {
    2025            0 :         m_spanArpeggio = nullptr;
    2026              :     }
    2027              : 
    2028          122 :     muse::DeleteAll(graceNotes());
    2029          122 :     graceNotes().clear();
    2030          122 :     muse::DeleteAll(articulations());
    2031          122 :     articulations().clear();
    2032          246 :     for (Note* n : notes()) {
    2033          124 :         for (EngravingItem* e : n->el()) {
    2034            0 :             n->remove(e);
    2035              :         }
    2036              :     }
    2037          122 :     ChordRest::removeMarkings(keepTremolo);
    2038          122 : }
    2039              : 
    2040              : //---------------------------------------------------------
    2041              : //   intrinsicMag
    2042              : //   returns the INTRINSIC mag of the chord (i.e. NOT scaled
    2043              : //   by staff size)
    2044              : //---------------------------------------------------------
    2045              : 
    2046      5669437 : double Chord::intrinsicMag() const
    2047              : {
    2048      5669437 :     double m = 1.0;
    2049      5669437 :     if (isSmall()) {
    2050        42043 :         m *= style().styleD(Sid::smallNoteMag);
    2051              :     }
    2052      5669437 :     if (m_noteType != NoteType::NORMAL) {
    2053       160402 :         bool tabGraceSizeException = staffType()->isTabStaff() && isPreBendOrGraceBendStart()
    2054       160402 :                                      && !style().styleB(Sid::useCueSizeFretForGraceBends);
    2055       159405 :         if (!tabGraceSizeException) {
    2056       159114 :             m *= style().styleD(Sid::graceNoteMag);
    2057              :         }
    2058              :     }
    2059      5669437 :     return m;
    2060              : }
    2061              : 
    2062              : //---------------------------------------------------------
    2063              : //   mag
    2064              : //---------------------------------------------------------
    2065              : 
    2066      4702081 : double Chord::mag() const
    2067              : {
    2068      4702081 :     const Staff* st = staff();
    2069      4702081 :     return (st ? st->staffMag(this) : 1.0) * intrinsicMag();
    2070              : }
    2071              : 
    2072              : //---------------------------------------------------------
    2073              : //   segment
    2074              : //---------------------------------------------------------
    2075              : 
    2076       873111 : Segment* Chord::segment() const
    2077              : {
    2078       873111 :     EngravingObject* e = ownershipParent();
    2079       882448 :     for (; e && !e->isSegment(); e = e->ownershipParent()) {
    2080              :     }
    2081       873111 :     return toSegment(e);
    2082              : }
    2083              : 
    2084              : //---------------------------------------------------------
    2085              : //   measure
    2086              : //---------------------------------------------------------
    2087              : 
    2088      6159484 : Measure* Chord::measure() const
    2089              : {
    2090      6159484 :     EngravingObject* e = ownershipParent();
    2091     12420978 :     for (; e && !e->isMeasure(); e = e->ownershipParent()) {
    2092              :     }
    2093      6159484 :     return toMeasure(e);
    2094              : }
    2095              : 
    2096              : //---------------------------------------------------------
    2097              : //   graceNotesBefore
    2098              : //---------------------------------------------------------
    2099              : 
    2100       342016 : GraceNotesGroup& Chord::graceNotesBefore(bool filterUnplayable) const
    2101              : {
    2102       342016 :     m_graceNotesBefore.clear();
    2103       350296 :     for (Chord* c : m_graceNotes) {
    2104         8280 :         assert(c->noteType() != NoteType::NORMAL && c->noteType() != NoteType::INVALID);
    2105         8280 :         if (c->noteType() & (
    2106              :                 NoteType::ACCIACCATURA
    2107              :                 | NoteType::APPOGGIATURA
    2108              :                 | NoteType::GRACE4
    2109              :                 | NoteType::GRACE16
    2110              :                 | NoteType::GRACE32)) {
    2111         6183 :             if (filterUnplayable && !c->isChordPlayable()) {
    2112            9 :                 continue;
    2113              :             }
    2114              : 
    2115         6174 :             m_graceNotesBefore.push_back(c);
    2116              :         }
    2117              :     }
    2118       342016 :     return m_graceNotesBefore;
    2119              : }
    2120              : 
    2121              : //---------------------------------------------------------
    2122              : //   graceNotesAfter
    2123              : //---------------------------------------------------------
    2124              : 
    2125       341098 : GraceNotesGroup& Chord::graceNotesAfter(bool filterUnplayable) const
    2126              : {
    2127       341098 :     m_graceNotesAfter.clear();
    2128       348694 :     for (int i = static_cast<int>(m_graceNotes.size()) - 1; i >= 0; i--) {
    2129         7596 :         Chord* c = m_graceNotes[i];
    2130         7596 :         assert(c->noteType() != NoteType::NORMAL && c->noteType() != NoteType::INVALID);
    2131         7596 :         if (c->noteType() & (NoteType::GRACE8_AFTER | NoteType::GRACE16_AFTER | NoteType::GRACE32_AFTER)) {
    2132         1968 :             if (filterUnplayable && !c->isChordPlayable()) {
    2133            0 :                 continue;
    2134              :             }
    2135              : 
    2136         1968 :             m_graceNotesAfter.push_back(c);
    2137              :         }
    2138              :     }
    2139       341098 :     return m_graceNotesAfter;
    2140              : }
    2141              : 
    2142           22 : Chord* Chord::graceNoteAt(size_t idx) const
    2143              : {
    2144           22 :     if (idx > m_graceNotes.size()) {
    2145            0 :         return nullptr;
    2146              :     }
    2147              : 
    2148           22 :     return m_graceNotes.at(idx);
    2149              : }
    2150              : 
    2151              : //---------------------------------------------------------
    2152              : //   allGraceChordsOfMainChord
    2153              : //   returns a list containing all grace notes (chords) attached to the main chord and the main chord itself, in order
    2154              : //---------------------------------------------------------
    2155            0 : std::vector<Chord*> Chord::allGraceChordsOfMainChord()
    2156              : {
    2157            0 :     Chord* mainChord = isGrace() ? toChord(ownershipParent()) : this;
    2158            0 :     std::vector<Chord*> chords = { mainChord };
    2159            0 :     const GraceNotesGroup& gnBefore = mainChord->graceNotesBefore();
    2160            0 :     const GraceNotesGroup& gnAfter = mainChord->graceNotesAfter();
    2161            0 :     chords.insert(chords.begin(), gnBefore.begin(), gnBefore.end());
    2162            0 :     chords.insert(chords.end(), gnAfter.begin(), gnAfter.end());
    2163            0 :     return chords;
    2164            0 : }
    2165              : 
    2166              : //---------------------------------------------------------
    2167              : //   setShowStemSlashInAdvance
    2168              : //---------------------------------------------------------
    2169              : 
    2170           74 : void Chord::setShowStemSlashInAdvance()
    2171              : {
    2172           74 :     if (m_noteType == NoteType::NORMAL) {
    2173            0 :         return;
    2174              :     }
    2175           74 :     if (isGraceBefore()) {
    2176           73 :         GraceNotesGroup& graceBefore = toChord(ownershipParent())->graceNotesBefore();
    2177           73 :         Chord* grace = graceBefore.empty() ? nullptr : graceBefore.front();
    2178           73 :         if (grace && grace->beamMode() != BeamMode::NONE && grace->beamMode() != BeamMode::BEGIN) {
    2179            2 :             grace->requestShowStemSlash(showStemSlash());
    2180              :         }
    2181              :     }
    2182           74 :     if (isGraceAfter()) {
    2183            1 :         GraceNotesGroup& graceAfter = toChord(ownershipParent())->graceNotesAfter();
    2184            1 :         Chord* grace = graceAfter.empty() ? nullptr : graceAfter.back();
    2185            1 :         if (grace && grace->beamMode() != BeamMode::NONE) {
    2186            0 :             grace->requestShowStemSlash(showStemSlash());
    2187              :         }
    2188              :     }
    2189              : }
    2190              : 
    2191              : //---------------------------------------------------------
    2192              : //   requestShowStemSlash
    2193              : //---------------------------------------------------------
    2194              : 
    2195          669 : void Chord::requestShowStemSlash(bool show)
    2196              : {
    2197          669 :     if (m_noteType == NoteType::NORMAL) {
    2198            2 :         return;
    2199              :     }
    2200          667 :     if (beam()) {
    2201            0 :         for (ChordRest* chordRest : beam()->elements()) {
    2202            0 :             if (chordRest->isChord()) {
    2203            0 :                 Chord* chord = toChord(chordRest);
    2204            0 :                 chord->setShowStemSlash(show);
    2205              :             }
    2206              :         }
    2207              :     } else {
    2208          667 :         setShowStemSlash(show);
    2209              :     }
    2210              : }
    2211              : 
    2212              : //---------------------------------------------------------
    2213              : //   sortNotes
    2214              : //---------------------------------------------------------
    2215              : 
    2216        55270 : void Chord::sortNotes()
    2217              : {
    2218        55270 :     std::sort(notes().begin(), notes().end(), noteIsBefore);
    2219        55270 : }
    2220              : 
    2221              : //---------------------------------------------------------
    2222              : //   nextTiedChord
    2223              : //    Return next chord if all notes in this chord are tied to it.
    2224              : //    Set backwards=true to return the previous chord instead.
    2225              : //
    2226              : //    Note: the next chord might have extra notes that are not tied
    2227              : //    back to this one. Set sameSize=true to return 0 in this case.
    2228              : //---------------------------------------------------------
    2229              : 
    2230         1776 : Chord* Chord::nextTiedChord(bool backwards, bool sameSize) const
    2231              : {
    2232         1776 :     Segment* nextSeg = backwards ? segment()->prev1(SegmentType::ChordRest) : segment()->next1(SegmentType::ChordRest);
    2233         1776 :     if (!nextSeg) {
    2234           91 :         return 0;
    2235              :     }
    2236         1685 :     ChordRest* nextCR = nextSeg->nextChordRest(track(), backwards);
    2237         1685 :     if (!nextCR || !nextCR->isChord()) {
    2238          428 :         return 0;
    2239              :     }
    2240         1257 :     Chord* next = toChord(nextCR);
    2241         1257 :     if (sameSize && notes().size() != next->notes().size()) {
    2242            6 :         return 0;     // sizes don't match so some notes can't be tied
    2243              :     }
    2244         1251 :     if (tuplet() != next->tuplet()) {
    2245            2 :         return 0;     // next chord belongs to a different tuplet
    2246              :     }
    2247         2055 :     for (Note* n : m_notes) {
    2248         1260 :         Tie* tie = backwards ? n->tieBack() : n->tieFor();
    2249         1260 :         if (!tie) {
    2250          454 :             return 0;       // not tied
    2251              :         }
    2252          806 :         Note* nn = backwards ? tie->startNote() : tie->endNote();
    2253          806 :         if (!nn || nn->chord() != next) {
    2254            0 :             return 0;       // tied to note in wrong voice, or tied over rest
    2255              :         }
    2256              :     }
    2257          795 :     return next;   // all notes in this chord are tied to notes in next chord
    2258              : }
    2259              : 
    2260              : //---------------------------------------------------------
    2261              : //   setNoteType
    2262              : //---------------------------------------------------------
    2263              : 
    2264          666 : void Chord::setNoteType(NoteType t)
    2265              : {
    2266          666 :     m_noteType = t;
    2267          666 :     setProperty(Pid::SHOW_STEM_SLASH, propertyDefault(Pid::SHOW_STEM_SLASH));
    2268          666 : }
    2269              : 
    2270              : //---------------------------------------------------------
    2271              : //   toGraceAfter
    2272              : //---------------------------------------------------------
    2273              : 
    2274           13 : void Chord::toGraceAfter()
    2275              : {
    2276           13 :     switch (noteType()) {
    2277           13 :     case NoteType::APPOGGIATURA:  setNoteType(NoteType::GRACE8_AFTER);
    2278           13 :         break;
    2279            0 :     case NoteType::GRACE16:       setNoteType(NoteType::GRACE16_AFTER);
    2280            0 :         break;
    2281            0 :     case NoteType::GRACE32:       setNoteType(NoteType::GRACE32_AFTER);
    2282            0 :         break;
    2283            0 :     default: break;
    2284              :     }
    2285           13 : }
    2286              : 
    2287          997 : bool Chord::isPreBendOrGraceBendStart() const
    2288              : {
    2289          997 :     if (!isGrace()) {
    2290            0 :         return false;
    2291              :     }
    2292              : 
    2293         1703 :     for (const Note* note : m_notes) {
    2294          997 :         GuitarBend* gb = note->bendFor();
    2295          997 :         if (gb && (gb->bendType() == GuitarBendType::PRE_BEND || gb->bendType() == GuitarBendType::GRACE_NOTE_BEND
    2296            0 :                    || gb->bendType() == GuitarBendType::PRE_DIVE)) {
    2297          291 :             return true;
    2298              :         }
    2299              :     }
    2300              : 
    2301          706 :     return false;
    2302              : }
    2303              : 
    2304          419 : bool Chord::isGraceBendEnd() const
    2305              : {
    2306          419 :     if (isGrace() || m_graceNotes.empty()) {
    2307          406 :         return false;
    2308              :     }
    2309              : 
    2310           22 :     for (const Note* note : m_notes) {
    2311           13 :         GuitarBend* bendBack = note->bendBack();
    2312           13 :         if (bendBack && bendBack->bendType() == GuitarBendType::GRACE_NOTE_BEND) {
    2313            4 :             return true;
    2314              :         }
    2315              :     }
    2316              : 
    2317            9 :     return false;
    2318              : }
    2319              : 
    2320           18 : void Chord::setIsTrillCueNote(bool v)
    2321              : {
    2322           18 :     m_isTrillCueNote = v;
    2323           18 : }
    2324              : 
    2325              : //---------------------------------------------------------
    2326              : //   tremoloChordType
    2327              : //---------------------------------------------------------
    2328              : 
    2329         7869 : TremoloChordType Chord::tremoloChordType() const
    2330              : {
    2331         7869 :     if (m_tremoloTwoChord) {
    2332          260 :         if (m_tremoloTwoChord->chord1() == this) {
    2333          127 :             return TremoloChordType::TremoloFirstChord;
    2334          133 :         } else if (m_tremoloTwoChord->chord2() == this) {
    2335          133 :             return TremoloChordType::TremoloSecondChord;
    2336              :         } else {
    2337            0 :             ASSERT_X(String(u"Chord::tremoloChordType(): inconsistency"));
    2338              :         }
    2339         7609 :     } else if (m_tremoloSingleChord) {
    2340          134 :         return TremoloChordType::TremoloSingle;
    2341              :     }
    2342              : 
    2343         7475 :     return TremoloChordType::TremoloNone;
    2344              : }
    2345              : 
    2346              : //---------------------------------------------------------
    2347              : //   nextElement
    2348              : //---------------------------------------------------------
    2349              : 
    2350            0 : EngravingItem* Chord::nextElement()
    2351              : {
    2352            0 :     EngravingItem* e = score()->selection().element();
    2353            0 :     if (!e && !score()->selection().elements().empty()) {
    2354            0 :         e = score()->selection().elements().front();
    2355              :     }
    2356              : 
    2357            0 :     switch (e->type()) {
    2358            0 :     case ElementType::SYMBOL:
    2359              :     case ElementType::IMAGE:
    2360              :     case ElementType::FINGERING:
    2361              :     case ElementType::TEXT:
    2362              :     case ElementType::BEND: {
    2363            0 :         Note* n = toNote(e->ownershipParent());
    2364            0 :         if (n == m_notes.front()) {
    2365            0 :             if (m_arpeggio) {
    2366            0 :                 return m_arpeggio;
    2367            0 :             } else if (m_tremoloTwoChord) {
    2368            0 :                 return m_tremoloTwoChord;
    2369            0 :             } else if (m_tremoloSingleChord) {
    2370            0 :                 return m_tremoloSingleChord;
    2371              :             }
    2372            0 :             break;
    2373              :         }
    2374            0 :         for (auto& i : m_notes) {
    2375            0 :             if (i == n) {
    2376            0 :                 return *(&i - 1);
    2377              :             }
    2378              :         }
    2379            0 :         break;
    2380              :     }
    2381              : 
    2382            0 :     case ElementType::GUITAR_BEND_SEGMENT:
    2383              :     case ElementType::GLISSANDO_SEGMENT:
    2384              :     case ElementType::NOTELINE_SEGMENT:
    2385              :     case ElementType::LAISSEZ_VIB_SEGMENT:
    2386              :     case ElementType::PARTIAL_TIE_SEGMENT:
    2387              :     case ElementType::TIE_SEGMENT: {
    2388            0 :         SpannerSegment* s = toSpannerSegment(e);
    2389            0 :         Spanner* sp = s->spanner();
    2390            0 :         EngravingItem* elSt = sp->startElement();
    2391            0 :         assert(elSt->isNote());
    2392            0 :         Note* n = toNote(elSt);
    2393            0 :         assert(n != NULL);
    2394            0 :         if (n == m_notes.front()) {
    2395            0 :             if (m_arpeggio) {
    2396            0 :                 return m_arpeggio;
    2397            0 :             } else if (m_tremoloTwoChord) {
    2398            0 :                 return m_tremoloTwoChord;
    2399            0 :             } else if (m_tremoloSingleChord) {
    2400            0 :                 return m_tremoloSingleChord;
    2401              :             }
    2402            0 :             break;
    2403              :         }
    2404            0 :         for (auto& i : m_notes) {
    2405            0 :             if (i == n) {
    2406            0 :                 return *(&i - 1);
    2407              :             }
    2408              :         }
    2409            0 :         break;
    2410              :     }
    2411            0 :     case ElementType::ARPEGGIO:
    2412              :     case ElementType::CHORD_BRACKET:
    2413            0 :         if (m_tremoloTwoChord) {
    2414            0 :             return m_tremoloTwoChord;
    2415            0 :         } else if (m_tremoloSingleChord) {
    2416            0 :             return m_tremoloSingleChord;
    2417              :         }
    2418            0 :         break;
    2419              : 
    2420            0 :     case ElementType::ACCIDENTAL:
    2421            0 :         e = toAccidental(e)->note();
    2422              :     // fall through
    2423              : 
    2424            0 :     case ElementType::NOTE: {
    2425            0 :         if (e == m_notes.front()) {
    2426            0 :             if (m_arpeggio) {
    2427            0 :                 return m_arpeggio;
    2428            0 :             } else if (m_tremoloTwoChord) {
    2429            0 :                 return m_tremoloTwoChord;
    2430            0 :             } else if (m_tremoloSingleChord) {
    2431            0 :                 return m_tremoloSingleChord;
    2432              :             }
    2433            0 :             break;
    2434              :         }
    2435            0 :         for (auto& i : m_notes) {
    2436            0 :             if (i == e) {
    2437            0 :                 return *(&i - 1);
    2438              :             }
    2439              :         }
    2440              :     }
    2441            0 :     break;
    2442              : 
    2443            0 :     case ElementType::CHORD:
    2444            0 :         return m_notes.back();
    2445              : 
    2446            0 :     default:
    2447            0 :         break;
    2448              :     }
    2449              : 
    2450            0 :     return ChordRest::nextElement();
    2451              : }
    2452              : 
    2453              : //---------------------------------------------------------
    2454              : //   prevElement
    2455              : //---------------------------------------------------------
    2456              : 
    2457            0 : EngravingItem* Chord::prevElement()
    2458              : {
    2459            0 :     EngravingItem* e = score()->selection().element();
    2460            0 :     if (!e && !score()->selection().elements().empty()) {
    2461            0 :         e = score()->selection().elements().back();
    2462              :     }
    2463            0 :     switch (e->type()) {
    2464            0 :     case ElementType::NOTE: {
    2465            0 :         if (isGrace()) {
    2466            0 :             ChordRest* prev = Navigation::prevChordRest(this);
    2467            0 :             if (prev) {
    2468            0 :                 if (prev->isChord()) {
    2469            0 :                     return toChord(prev)->notes().back();
    2470              :                 }
    2471            0 :                 return prev;
    2472              :             }
    2473              :         }
    2474              : 
    2475            0 :         GraceNotesGroup& graceNotesBefore = this->graceNotesBefore();
    2476            0 :         if (!graceNotesBefore.empty()) {
    2477            0 :             if (Chord* graceNotesBeforeLastChord = graceNotesBefore.back()) {
    2478            0 :                 if (e == graceNotesBeforeLastChord->notes().back()) {
    2479            0 :                     break;
    2480              :                 }
    2481              : 
    2482            0 :                 Note* prevNote = graceNotesBeforeLastChord->notes().back();
    2483            0 :                 if (prevNote->isPreBendOrDiveStart() || prevNote->isGraceBendStart()) {
    2484            0 :                     return prevNote->bendFor()->frontSegment();
    2485              :                 }
    2486              : 
    2487            0 :                 ChordRest* prev = Navigation::prevChordRest(this);
    2488            0 :                 if (prev) {
    2489            0 :                     if (prev->isChord()) {
    2490            0 :                         return toChord(prev)->notes().back();
    2491              :                     }
    2492            0 :                     return prev;
    2493              :                 }
    2494              :             }
    2495              :         }
    2496              : 
    2497            0 :         if (e == m_notes.back()) {
    2498            0 :             break;
    2499              :         }
    2500            0 :         Note* prevNote = nullptr;
    2501            0 :         for (auto& i : m_notes) {
    2502            0 :             if (i == e) {
    2503            0 :                 prevNote = *(&i + 1);
    2504              :             }
    2505              :         }
    2506            0 :         EngravingItem* next = prevNote->lastElementBeforeSegment();
    2507            0 :         return next;
    2508              :     }
    2509              : 
    2510            0 :     case ElementType::CHORD:
    2511            0 :         return m_notes.front();
    2512              : 
    2513            0 :     case ElementType::TREMOLO_TWOCHORD:
    2514              :     case ElementType::TREMOLO_SINGLECHORD:
    2515            0 :         if (m_arpeggio) {
    2516            0 :             return m_arpeggio;
    2517              :         }
    2518              :     // fall through
    2519              :     case ElementType::ARPEGGIO:
    2520              :     case ElementType::CHORD_BRACKET:
    2521              :     {
    2522            0 :         Note* n = m_notes.front();
    2523            0 :         EngravingItem* elN = n->lastElementBeforeSegment();
    2524            0 :         assert(elN != NULL);
    2525            0 :         return elN;
    2526              :     }
    2527              : 
    2528            0 :     default:
    2529            0 :         break;
    2530              :     }
    2531            0 :     return ChordRest::prevElement();
    2532              : }
    2533              : 
    2534              : //---------------------------------------------------------
    2535              : //   lastElementBeforeSegment
    2536              : //---------------------------------------------------------
    2537              : 
    2538            0 : EngravingItem* Chord::lastElementBeforeSegment()
    2539              : {
    2540            0 :     if (m_tremoloSingleChord) {
    2541            0 :         return m_tremoloSingleChord;
    2542            0 :     } else if (m_tremoloTwoChord) {
    2543            0 :         return m_tremoloTwoChord;
    2544            0 :     } else if (m_arpeggio) {
    2545            0 :         return m_arpeggio;
    2546              :     } else {
    2547            0 :         Note* n = m_notes.front();
    2548            0 :         EngravingItem* elN = n->lastElementBeforeSegment();
    2549            0 :         assert(elN != NULL);
    2550            0 :         return elN;
    2551              :     }
    2552              : }
    2553              : 
    2554              : //---------------------------------------------------------
    2555              : //   nextSegmentElement
    2556              : //---------------------------------------------------------
    2557              : 
    2558            0 : EngravingItem* Chord::nextSegmentElement()
    2559              : {
    2560            0 :     for (track_idx_t v = track() + 1; staffIdx() == v / VOICES; ++v) {
    2561            0 :         EngravingItem* e = segment()->element(v);
    2562            0 :         if (e) {
    2563            0 :             if (e->isChord()) {
    2564            0 :                 return toChord(e)->notes().back();
    2565              :             }
    2566              : 
    2567            0 :             return e;
    2568              :         }
    2569              :     }
    2570              : 
    2571            0 :     return ChordRest::nextSegmentElement();
    2572              : }
    2573              : 
    2574              : //---------------------------------------------------------
    2575              : //   prevSegmentElement
    2576              : //---------------------------------------------------------
    2577              : 
    2578            0 : EngravingItem* Chord::prevSegmentElement()
    2579              : {
    2580            0 :     EngravingItem* el = score()->selection().element();
    2581            0 :     if (!el && !score()->selection().elements().empty()) {
    2582            0 :         el = score()->selection().elements().front();
    2583              :     }
    2584            0 :     EngravingItem* e = segment()->lastInPrevSegments(el->staffIdx());
    2585            0 :     if (e) {
    2586            0 :         if (e->isChord()) {
    2587            0 :             return toChord(e)->notes().front();
    2588              :         }
    2589            0 :         return e;
    2590              :     }
    2591              : 
    2592            0 :     return ChordRest::prevSegmentElement();
    2593              : }
    2594              : 
    2595              : //---------------------------------------------------------
    2596              : //   accessibleExtraInfo
    2597              : //---------------------------------------------------------
    2598              : 
    2599            0 : String Chord::accessibleExtraInfo() const
    2600              : {
    2601            0 :     String rez;
    2602              : 
    2603            0 :     for (const Chord* c : graceNotes()) {
    2604            0 :         if (!score()->selectionFilter().canSelect(c)) {
    2605            0 :             continue;
    2606              :         }
    2607            0 :         for (const Note* n : c->notes()) {
    2608            0 :             rez = String(u"%1 %2").arg(rez, n->screenReaderInfo());
    2609              :         }
    2610              :     }
    2611              : 
    2612            0 :     for (Articulation* a : articulations()) {
    2613            0 :         if (!score()->selectionFilter().canSelect(a)) {
    2614            0 :             continue;
    2615              :         }
    2616            0 :         rez = String(u"%1 %2").arg(rez, a->screenReaderInfo());
    2617              :     }
    2618              : 
    2619            0 :     if (arpeggio() && score()->selectionFilter().canSelect(arpeggio())) {
    2620            0 :         rez = String(u"%1 %2").arg(rez, arpeggio()->screenReaderInfo());
    2621              :     }
    2622              : 
    2623            0 :     if (tremoloTwoChord() && score()->selectionFilter().canSelect(tremoloTwoChord())) {
    2624            0 :         rez = String(u"%1 %2").arg(rez, tremoloTwoChord()->screenReaderInfo());
    2625              :     }
    2626              : 
    2627            0 :     if (tremoloSingleChord() && score()->selectionFilter().canSelect(tremoloSingleChord())) {
    2628            0 :         rez = String(u"%1 %2").arg(rez, tremoloSingleChord()->screenReaderInfo());
    2629              :     }
    2630              : 
    2631            0 :     for (EngravingItem* e : el()) {
    2632            0 :         if (!score()->selectionFilter().canSelect(e)) {
    2633            0 :             continue;
    2634              :         }
    2635            0 :         rez = String(u"%1 %2").arg(rez, e->screenReaderInfo());
    2636              :     }
    2637              : 
    2638            0 :     return String(u"%1 %2").arg(rez, ChordRest::accessibleExtraInfo());
    2639            0 : }
    2640              : 
    2641          202 : void Chord::undoChangeProperty(Pid id, const PropertyValue& newValue)
    2642              : {
    2643          202 :     undoChangeProperty(id, newValue, propertyFlags(id));
    2644          202 : }
    2645              : 
    2646              : //---------------------------------------------------------
    2647              : //   undoChangeProperty
    2648              : //---------------------------------------------------------
    2649              : 
    2650          320 : void Chord::undoChangeProperty(Pid id, const PropertyValue& newValue, PropertyFlags ps)
    2651              : {
    2652          320 :     if (id == Pid::VISIBLE) {
    2653            1 :         processSiblings([=](EngravingItem* element) {
    2654            2 :             element->undoChangeProperty(id, newValue, ps);
    2655            2 :         }, false);
    2656              :     }
    2657              : 
    2658          320 :     ChordRest::undoChangeProperty(id, newValue, ps);
    2659          320 : }
    2660              : 
    2661           24 : std::set<SymId> Chord::articulationSymbolIds() const
    2662              : {
    2663           24 :     std::set<SymId> result;
    2664           24 :     for (const Articulation* artic: m_articulations) {
    2665            0 :         result.insert(artic->symId());
    2666              :     }
    2667              : 
    2668           24 :     return result;
    2669            0 : }
    2670              : 
    2671              : //---------------------------------------------------------
    2672              : //   getNoteEventLists
    2673              : //    Get contents of all NoteEventLists for all notes in
    2674              : //    the chord.
    2675              : //---------------------------------------------------------
    2676              : 
    2677            0 : std::vector<NoteEventList> Chord::getNoteEventLists()
    2678              : {
    2679            0 :     std::vector<NoteEventList> ell;
    2680            0 :     if (notes().empty()) {
    2681            0 :         return ell;
    2682              :     }
    2683            0 :     ell.reserve(notes().size());
    2684            0 :     for (size_t i = 0; i < notes().size(); ++i) {
    2685            0 :         ell.push_back(NoteEventList(notes()[i]->playEvents()));
    2686              :     }
    2687            0 :     return ell;
    2688            0 : }
    2689              : 
    2690              : //---------------------------------------------------------
    2691              : //   setNoteEventLists
    2692              : //    Set contents of all NoteEventLists for all notes in
    2693              : //    the chord.
    2694              : //---------------------------------------------------------
    2695              : 
    2696          341 : void Chord::setNoteEventLists(std::vector<NoteEventList>& ell)
    2697              : {
    2698          341 :     if (notes().empty()) {
    2699            0 :         return;
    2700              :     }
    2701          341 :     IF_ASSERT_FAILED(ell.size() == notes().size()) {
    2702            0 :         return;
    2703              :     }
    2704          704 :     for (size_t i = 0; i < ell.size(); i++) {
    2705          363 :         notes()[i]->setPlayEvents(ell[i]);
    2706              :     }
    2707              : }
    2708              : 
    2709              : //---------------------------------------------------------
    2710              : //   styleChanged
    2711              : //---------------------------------------------------------
    2712          141 : void Chord::styleChanged()
    2713              : {
    2714          295 :     auto updateElementsStyle = [](EngravingItem* e) {
    2715          295 :         e->styleChanged();
    2716          295 :     };
    2717          141 :     scanElements(updateElementsStyle);
    2718          141 : }
    2719              : 
    2720        56864 : void Chord::computeKerningExceptions()
    2721              : {
    2722        56864 :     m_allowKerningAbove = true;
    2723        56864 :     m_allowKerningBelow = true;
    2724        58617 :     for (Articulation* art : m_articulations) {
    2725         1753 :         if (art->up()) {
    2726         1234 :             m_allowKerningAbove = false;
    2727              :         } else {
    2728          519 :             m_allowKerningBelow = false;
    2729              :         }
    2730              :     }
    2731        56864 :     if ((m_startEndSlurs.startUp || m_startEndSlurs.endUp) && !ldata()->up) {
    2732          565 :         m_allowKerningAbove = false;
    2733              :     }
    2734        56864 :     if ((m_startEndSlurs.startDown || m_startEndSlurs.endDown) && ldata()->up) {
    2735          753 :         m_allowKerningBelow = false;
    2736              :     }
    2737        56864 : }
    2738              : 
    2739       278493 : Ornament* Chord::findOrnament(bool forPlayback) const
    2740              : {
    2741       285560 :     for (Articulation* art : m_articulations) {
    2742         8617 :         if (art->isOrnament()) {
    2743         1550 :             return toOrnament(art);
    2744              :         }
    2745              :     }
    2746       286866 :     for (Spanner* spanner : m_startingSpanners) {
    2747        10334 :         if (spanner->isTrill()) {
    2748          411 :             return toTrill(spanner)->ornament();
    2749              :         }
    2750              :     }
    2751       276532 :     if (forPlayback) {
    2752              :         // TODO: cleanup.
    2753              :         // We shouldn't do this kind of special cases, and the DOM shouldn't know anything about playback.
    2754              :         // This should be in a different function that returns the ornament from the Trill ending on this chord. [MS]
    2755            2 :         for (Spanner* spanner : m_endingSpanners) {
    2756            2 :             if (spanner->isTrill()) {
    2757            2 :                 return toTrill(spanner)->ornament();
    2758              :             }
    2759              :         }
    2760              :     }
    2761       276530 :     return nullptr;
    2762              : }
    2763              : 
    2764              : //---------------------------------
    2765              : // firstGraceOrNote
    2766              : //---------------------------------
    2767          213 : Note* Chord::firstGraceOrNote()
    2768              : {
    2769          213 :     GraceNotesGroup& graceNotesBefore = this->graceNotesBefore();
    2770          213 :     if (!graceNotesBefore.empty()) {
    2771           31 :         if (Chord* graceNotesBeforeFirstChord = graceNotesBefore.front()) {
    2772           31 :             return graceNotesBeforeFirstChord->notes().front();
    2773              :         }
    2774              :     }
    2775              : 
    2776          182 :     return this->notes().back();
    2777              : }
    2778              : 
    2779              : //---------------------------------
    2780              : // GRACE NOTES
    2781              : //---------------------------------
    2782              : 
    2783        73052 : GraceNotesGroup::GraceNotesGroup(DummyParentOr<Chord> c)
    2784        73052 :     : EngravingItem(ElementType::GRACE_NOTES_GROUP, c), _parent(c.as<Chord>()) {}
    2785              : 
    2786         1011 : void GraceNotesGroup::setPos(double x, double y)
    2787              : {
    2788         1011 :     EngravingItem::setPos(x, y);
    2789         2354 :     for (unsigned i = 0; i < this->size(); ++i) {
    2790         1343 :         Chord* chord = this->at(i);
    2791         1343 :         chord->mutldata()->move(PointF(x, y));
    2792              :     }
    2793         1011 : }
    2794              : 
    2795         2182 : void GraceNotesGroup::addToShape()
    2796              : {
    2797         5149 :     for (Chord* grace : *this) {
    2798         2967 :         const PointF yOffset = grace->staffOffset();
    2799         2967 :         staff_idx_t staffIdx = grace->staffIdx();
    2800         2967 :         staff_idx_t vStaffIdx = grace->vStaffIdx();
    2801         2967 :         Shape& s = _appendedSegment->staffShape(staffIdx);
    2802         2967 :         s.add(grace->shape(LD_ACCESS::PASS).translate(grace->pos() + yOffset));
    2803         2967 :         if (vStaffIdx != staffIdx) {
    2804              :             // Cross-staff grace notes add their shape to both the origin and the destination staff
    2805            0 :             Shape& s2 = _appendedSegment->staffShape(vStaffIdx);
    2806            0 :             s2.add(grace->shape().translate(grace->pos() + yOffset));
    2807              :         }
    2808              :     }
    2809         2182 : }
    2810              : }
        

Generated by: LCOV version 2.0-1