LCOV - code coverage report
Current view: top level - src/engraving/dom - engravingitem.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 67.1 % 1573 1056
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              : /**
      24              :  \file
      25              :  Implementation of EngravingItem, ElementList
      26              : */
      27              : 
      28              : #include "engravingitem.h"
      29              : 
      30              : #include <cmath>
      31              : 
      32              : #include "containers.h"
      33              : #include "io/buffer.h"
      34              : #include "translation.h"
      35              : 
      36              : #include "iengravingconfiguration.h"
      37              : #include "iengravingcontextconfiguration.h"
      38              : #include "iengravingfont.h"
      39              : 
      40              : #include "rw/rwregister.h"
      41              : 
      42              : #include "types/typesconv.h"
      43              : 
      44              : #include "rendering/iscorerenderer.h"
      45              : #include "rendering/score/autoplace.h"
      46              : #include "rendering/score/chordlayout.h"
      47              : #include "rendering/score/tlayout.h"
      48              : 
      49              : #ifndef ENGRAVING_NO_ACCESSIBILITY
      50              : #include "accessibility/accessibleitem.h"
      51              : #include "accessibility/accessibleroot.h"
      52              : #endif
      53              : 
      54              : #include "../editing/addremoveelement.h"
      55              : #include "../editing/editdata.h"
      56              : #include "../editing/editproperty.h"
      57              : #include "../editing/elementeditdata.h"
      58              : #include "../editing/navigation.h"
      59              : 
      60              : #include "chord.h"
      61              : #include "dummyparent.h"
      62              : #include "factory.h"
      63              : #include "linkedobjects.h"
      64              : #include "masterscore.h"
      65              : #include "measure.h"
      66              : #include "mscore.h"
      67              : #include "note.h"
      68              : #include "page.h"
      69              : #include "parenthesis.h"
      70              : #include "part.h"
      71              : #include "rootitem.h"
      72              : #include "score.h"
      73              : #include "segment.h"
      74              : #include "shape.h"
      75              : #include "sig.h"
      76              : #include "staff.h"
      77              : #include "stafflines.h"
      78              : #include "stafftype.h"
      79              : #include "system.h"
      80              : 
      81              : #include "log.h"
      82              : #define LOG_PROP() if (0) LOGD()
      83              : 
      84              : using namespace mu;
      85              : using namespace muse::io;
      86              : using namespace muse::draw;
      87              : using namespace mu::engraving;
      88              : using namespace mu::engraving::rendering::score;
      89              : 
      90              : namespace mu::engraving {
      91       664374 : EngravingItem::EngravingItem(const ElementType& type, EngravingObject* parent, ElementFlags f)
      92       664374 :     : EngravingObject(type, parent)
      93              : {
      94       664374 :     m_flags         = f;
      95       664374 :     m_color         = configuration()->defaultColor();
      96       664374 :     m_z             = -1;
      97       664374 :     m_minDistance   = 0.0_sp;
      98       664374 : }
      99              : 
     100        20715 : EngravingItem::EngravingItem(const EngravingItem& e, bool link)
     101        20715 :     : EngravingObject(e)
     102              : {
     103        20715 :     m_offset     = e.m_offset;
     104        20715 :     m_track      = e.m_track;
     105        20715 :     m_flags      = e.m_flags;
     106        20715 :     setFlag(ElementFlag::SELECTED, false);
     107        20715 :     m_z          = e.m_z;
     108        20715 :     m_color      = e.m_color;
     109        20715 :     m_minDistance = e.m_minDistance;
     110        20715 :     m_excludeVerticalAlign = e.m_excludeVerticalAlign;
     111        20715 :     itemDiscovered = false;
     112              : 
     113        20715 :     m_accessibleEnabled = e.m_accessibleEnabled;
     114              : 
     115        20715 :     if (e.m_leftParenthesis) {
     116            0 :         m_leftParenthesis = e.m_leftParenthesis->clone();
     117            0 :         m_leftParenthesis->setOwnershipParent(this);
     118            0 :         m_leftParenthesis->setTrack(track());
     119            0 :         if (link) {
     120            0 :             score()->undo(new Link(m_leftParenthesis, e.m_leftParenthesis));
     121              :         }
     122              :     }
     123        20715 :     if (e.m_rightParenthesis) {
     124            0 :         m_rightParenthesis = e.m_rightParenthesis->clone();
     125            0 :         m_rightParenthesis->setOwnershipParent(this);
     126            0 :         m_rightParenthesis->setTrack(track());
     127            0 :         if (link) {
     128            0 :             score()->undo(new Link(m_rightParenthesis, e.m_rightParenthesis));
     129              :         }
     130              :     }
     131        20715 : }
     132              : 
     133       641920 : EngravingItem::~EngravingItem()
     134              : {
     135       641920 :     Score::onElementDestruction(this);
     136              : 
     137       641920 :     delete m_layoutData;
     138       641920 : }
     139              : 
     140              : #ifndef ENGRAVING_NO_ACCESSIBILITY
     141         5530 : void EngravingItem::setupAccessible()
     142              : {
     143         5530 :     if (m_accessible) {
     144            0 :         return;
     145              :     }
     146              : 
     147              :     static const std::set<ElementType> accessibleDisabled = {
     148              :         ElementType::LEDGER_LINE
     149         5564 :     };
     150              : 
     151         5530 :     if (score() && !score()->isPaletteScore()) {
     152         5530 :         if (!muse::contains(accessibleDisabled, type())) {
     153         5530 :             m_accessible = createAccessible();
     154         5530 :             m_accessible->setup();
     155              :         }
     156              :     }
     157              : }
     158              : 
     159              : #endif
     160              : 
     161         6891 : bool EngravingItem::accessibleEnabled() const
     162              : {
     163         6891 :     return m_accessibleEnabled;
     164              : }
     165              : 
     166       366820 : void EngravingItem::setAccessibleEnabled(bool enabled)
     167              : {
     168       366820 :     m_accessibleEnabled = enabled;
     169       366820 : }
     170              : 
     171          634 : EngravingItem* EngravingItem::accessibleParentItem() const
     172              : {
     173          634 :     if (EngravingItem* p = layoutParent()) {
     174          623 :         return p;
     175              :     }
     176              : 
     177           11 :     if (ownershipParent()) {
     178              :         // attached to something, but not placed anywhere: in neither tree
     179            4 :         return nullptr;
     180              :     }
     181              : 
     182              :     // not attached to anything: head of the tree those objects form
     183            7 :     return score() && score()->dummy() ? score()->dummy()->rootItem() : nullptr;
     184              : }
     185              : 
     186          625 : EngravingItemList EngravingItem::accessibleChildren() const
     187              : {
     188          625 :     return childrenItems();
     189              : }
     190              : 
     191      8551914 : EngravingItem* EngravingItem::ownershipParentItem() const
     192              : {
     193      8551914 :     EngravingObject* p = ownershipParent();
     194      8551914 :     if (p && p->isEngravingItem()) {
     195      8469345 :         return toEngravingItem(p);
     196              :     }
     197              : 
     198        82569 :     return nullptr;
     199              : }
     200              : 
     201      7976665 : EngravingItem* EngravingItem::layoutParent() const
     202              : {
     203      7976665 :     return ownershipParentItem();
     204              : }
     205              : 
     206       779653 : const std::shared_ptr<IEngravingConfiguration>& EngravingItem::configuration() const
     207              : {
     208       779653 :     return score()->configuration.get();
     209              : }
     210              : 
     211         5530 : const muse::modularity::ContextPtr& EngravingItem::iocContext() const
     212              : {
     213         5530 :     return score()->iocContext();
     214              : }
     215              : 
     216         1990 : const std::shared_ptr<IEngravingContextConfiguration>& EngravingItem::contextConfiguration() const
     217              : {
     218         1990 :     return score()->contextConfiguration.get();
     219              : }
     220              : 
     221        76154 : const std::shared_ptr<rendering::IScoreRenderer>& EngravingItem::renderer() const
     222              : {
     223        76154 :     return score()->renderer.get();
     224              : }
     225              : 
     226              : #ifndef ENGRAVING_NO_ACCESSIBILITY
     227            0 : AccessibleItemPtr EngravingItem::createAccessible()
     228              : {
     229            0 :     return std::make_shared<AccessibleItem>(this);
     230              : }
     231              : 
     232        19922 : void EngravingItem::notifyAboutNameChanged()
     233              : {
     234        19922 :     if (!selected()) {
     235        19735 :         return;
     236              :     }
     237              : 
     238          187 :     if (m_accessible) {
     239            0 :         doInitAccessible();
     240            0 :         m_accessible->accessibleRoot()->notifyAboutFocusedElementNameChanged();
     241              :     }
     242              : }
     243              : 
     244              : #endif
     245              : 
     246              : //---------------------------------------------------------
     247              : //   spatiumChanged
     248              : //---------------------------------------------------------
     249              : 
     250         3705 : void EngravingItem::spatiumChanged(double oldValue, double newValue)
     251              : {
     252         3705 :     if (offsetIsSpatiumDependent()) {
     253         3636 :         m_offset *= (newValue / oldValue);
     254              :     }
     255         3705 : }
     256              : 
     257              : //---------------------------------------------------------
     258              : //   localSpatiumChanged
     259              : //    the scale of a staff changed
     260              : //---------------------------------------------------------
     261              : 
     262            0 : void EngravingItem::localSpatiumChanged(double oldValue, double newValue)
     263              : {
     264            0 :     if (offsetIsSpatiumDependent()) {
     265            0 :         m_offset *= (newValue / oldValue);
     266              :     }
     267            0 : }
     268              : 
     269              : //---------------------------------------------------------
     270              : //   spatium
     271              : //---------------------------------------------------------
     272              : 
     273      5309567 : double EngravingItem::spatium() const
     274              : {
     275      5309567 :     EngravingItem* p = layoutParent();
     276      5309567 :     if (systemFlag() || (p && p->systemFlag())) {
     277        47068 :         return style().spatium();
     278              :     }
     279      5262499 :     Staff* s = staff();
     280      5262499 :     return s ? s->spatium(this) : style().spatium();
     281              : }
     282              : 
     283      2430379 : double EngravingItem::defaultSpatium() const
     284              : {
     285      2430379 :     return style().defaultSpatium();
     286              : }
     287              : 
     288          444 : bool EngravingItem::isInteractionAvailable() const
     289              : {
     290          444 :     if (!getProperty(Pid::VISIBLE).toBool() && (score()->printing() || !score()->isShowInvisible())) {
     291            0 :         return false;
     292              :     }
     293              : 
     294          444 :     return true;
     295              : }
     296              : 
     297              : //---------------------------------------------------------
     298              : //   offsetIsSpatiumDependent
     299              : //---------------------------------------------------------
     300              : 
     301        91948 : bool EngravingItem::offsetIsSpatiumDependent() const
     302              : {
     303        91948 :     return sizeIsSpatiumDependent() || (m_flags & ElementFlag::ON_STAFF);
     304              : }
     305              : 
     306            1 : Sid EngravingItem::defaultPosSid() const
     307              : {
     308            1 :     return Sid::NOSTYLE;
     309              : }
     310              : 
     311        86819 : PointF EngravingItem::defaultPos() const
     312              : {
     313        86819 :     Sid styleId = defaultPosSid();
     314              : 
     315        86819 :     if (styleId == Sid::NOSTYLE) {
     316           13 :         return PointF();
     317              :     }
     318              : 
     319        86806 :     PointF offsetPos = style().value(styleId).value<PointF>();
     320        86806 :     if (offsetIsSpatiumDependent()) {
     321        74272 :         offsetPos *= spatium();
     322              :     } else {
     323        12534 :         offsetPos *= DPMM;
     324              :     }
     325              : 
     326        86806 :     return offsetPos;
     327              : }
     328              : 
     329       251697 : PlacementV EngravingItem::placement() const
     330              : {
     331       251697 :     return flag(ElementFlag::PLACE_ABOVE) && !isSystemObjectBelowBottomStaff() ? PlacementV::ABOVE : PlacementV::BELOW;
     332              : }
     333              : 
     334              : //---------------------------------------------------------
     335              : //   magS
     336              : //---------------------------------------------------------
     337              : 
     338      2213024 : double EngravingItem::magS() const
     339              : {
     340      2213024 :     return mag() * (style().spatium() / defaultSpatium());
     341              : }
     342              : 
     343              : //---------------------------------------------------------
     344              : //   name
     345              : //---------------------------------------------------------
     346              : 
     347            0 : TranslatableString EngravingItem::subtypeUserName() const
     348              : {
     349            0 :     return {};
     350              : }
     351              : 
     352           16 : String EngravingItem::translatedSubtypeUserName() const
     353              : {
     354           16 :     return subtypeUserName().translated();
     355              : }
     356              : 
     357              : //---------------------------------------------------------
     358              : //   linkedClone
     359              : //---------------------------------------------------------
     360              : 
     361          811 : EngravingItem* EngravingItem::linkedClone()
     362              : {
     363          811 :     EngravingItem* e = clone();
     364          811 :     score()->undo(new Link(e, this));
     365          811 :     return e;
     366              : }
     367              : 
     368              : //---------------------------------------------------------
     369              : //   deleteLater
     370              : //---------------------------------------------------------
     371              : 
     372          156 : void EngravingItem::deleteLater()
     373              : {
     374          156 :     if (selected()) {
     375            0 :         score()->deselect(this);
     376              :     }
     377          156 :     masterScore()->cmdState().deleteLater(this);
     378          156 : }
     379              : 
     380              : //---------------------------------------------------------
     381              : //   scanElements
     382              : /// If leaf node, apply `func` on this element (after checking if it is visible).
     383              : /// Otherwise, recurse over all children (see ScoreElement::scanElements).
     384              : /// Note: This function is overridden in some classes to skip certain children,
     385              : /// or to apply `func` even to non-leaf nodes.
     386              : //---------------------------------------------------------
     387              : 
     388        11070 : void EngravingItem::scanElements(std::function<void(EngravingItem*)> func)
     389              : {
     390        11070 :     if (m_leftParenthesis) {
     391            2 :         m_leftParenthesis->scanElements(func);
     392              :     }
     393              : 
     394        11070 :     if (m_rightParenthesis) {
     395            2 :         m_rightParenthesis->scanElements(func);
     396              :     }
     397              : 
     398        11070 :     func(this);
     399        11070 : }
     400              : 
     401          780 : bool EngravingItem::collectForDrawing() const
     402              : {
     403          780 :     if (!visible() && !score()->isShowInvisible()) {
     404            0 :         return false;
     405              :     }
     406              : 
     407          780 :     return true;
     408              : }
     409              : 
     410              : //---------------------------------------------------------
     411              : //   reset
     412              : //---------------------------------------------------------
     413              : 
     414           23 : void EngravingItem::reset()
     415              : {
     416           23 :     undoResetProperty(Pid::AUTOPLACE);
     417           23 :     undoResetProperty(Pid::PLACEMENT);
     418           23 :     undoResetProperty(Pid::MIN_DISTANCE);
     419           23 :     undoResetProperty(Pid::OFFSET);
     420           23 :     undoResetProperty(Pid::LEADING_SPACE);
     421           23 :     EngravingObject::reset();
     422           23 : }
     423              : 
     424              : //---------------------------------------------------------
     425              : //   change
     426              : //---------------------------------------------------------
     427              : 
     428          115 : void EngravingItem::change(EngravingItem* o, EngravingItem* n)
     429              : {
     430          115 :     remove(o);
     431          115 :     add(n);
     432          115 : }
     433              : 
     434              : //---------------------------------------------------------
     435              : //   staff
     436              : //---------------------------------------------------------
     437              : 
     438     30316096 : Staff* EngravingItem::staff() const
     439              : {
     440     30316096 :     if (!hasStaff() || score()->staves().empty()) {
     441      1079685 :         return nullptr;
     442              :     }
     443              : 
     444     29236411 :     return score()->staff(staffIdx());
     445              : }
     446              : 
     447     30316096 : bool EngravingItem::hasStaff() const
     448              : {
     449     30316096 :     return m_track != muse::nidx;
     450              : }
     451              : 
     452              : //---------------------------------------------------------
     453              : //   staffType
     454              : //---------------------------------------------------------
     455              : 
     456      2858057 : const StaffType* EngravingItem::staffType() const
     457              : {
     458      2858057 :     Staff* s = staff();
     459      2858057 :     return s ? s->staffTypeForElement(this) : nullptr;
     460              : }
     461              : 
     462              : //---------------------------------------------------------
     463              : //   onTabStaff
     464              : //---------------------------------------------------------
     465              : 
     466       913239 : bool EngravingItem::onTabStaff() const
     467              : {
     468       913239 :     const StaffType* stt = staffType();
     469       913239 :     return stt ? stt->isTabStaff() : false;
     470              : }
     471              : 
     472       381156 : bool EngravingItem::onJianpuStaff() const
     473              : {
     474       381156 :     const Staff* st = staff();
     475       381156 :     return st ? st->isJianpuStaff(tick()) : false;
     476              : }
     477              : 
     478            0 : bool EngravingItem::hasGrips() const
     479              : {
     480            0 :     return gripsCount() > 0;
     481              : }
     482              : 
     483      5097730 : track_idx_t EngravingItem::track() const
     484              : {
     485      5097730 :     return m_track;
     486              : }
     487              : 
     488       577228 : void EngravingItem::setTrack(track_idx_t val)
     489              : {
     490       577228 :     IF_ASSERT_FAILED(val < m_score->ntracks() || val == 0 || val == muse::nidx || m_score->isPaletteScore()) {
     491              :         // Zero and muse::nidx have special use cases.
     492              :         // In all other cases the track can't be larger than the total track count.
     493            0 :         return;
     494              :     }
     495              : 
     496       577228 :     m_track = val;
     497              : 
     498       577228 :     if (m_leftParenthesis) {
     499            2 :         m_leftParenthesis->setTrack(val);
     500              :     }
     501       577228 :     if (m_rightParenthesis) {
     502            2 :         m_rightParenthesis->setTrack(val);
     503              :     }
     504              : }
     505              : 
     506              : //---------------------------------------------------------
     507              : //   z
     508              : //---------------------------------------------------------
     509              : 
     510       107757 : int EngravingItem::z() const
     511              : {
     512       107757 :     if (m_z == -1) {
     513        89551 :         m_z = int(type()) * 100;
     514              :     }
     515       107757 :     return m_z;
     516              : }
     517              : 
     518         6630 : void EngravingItem::setZ(int val)
     519              : {
     520         6630 :     m_z = val;
     521         6630 : }
     522              : 
     523     40104272 : staff_idx_t EngravingItem::staffIdx() const
     524              : {
     525     40104272 :     return track2staff(m_track);
     526              : }
     527              : 
     528         5175 : void EngravingItem::setStaffIdx(staff_idx_t val)
     529              : {
     530         5175 :     voice_idx_t voiceIdx = voice();
     531         5175 :     setTrack(staff2track(val, voiceIdx == muse::nidx ? 0 : voiceIdx));
     532         5175 : }
     533              : 
     534       810440 : staff_idx_t EngravingItem::effectiveStaffIdx() const
     535              : {
     536       810440 :     if (!systemFlag()) {
     537       788697 :         return vStaffIdx();
     538              :     }
     539              : 
     540        21743 :     const System* system = toSystem(findAncestor(ElementType::SYSTEM));
     541        21743 :     if (!system || system->vbox()) {
     542            0 :         return vStaffIdx();
     543              :     }
     544              : 
     545        21743 :     if (isSystemObjectBelowBottomStaff()) {
     546            0 :         return system->lastVisibleStaff();
     547              :     }
     548              : 
     549        21743 :     staff_idx_t originalStaffIdx = staffIdx();
     550        21743 :     if (originalStaffIdx == muse::nidx) {
     551           44 :         return muse::nidx;
     552              :     }
     553              : 
     554        21699 :     const Measure* m = findMeasure();
     555              : 
     556              :     // TODO - make more system markings ignore empty cutaway measures
     557        22281 :     auto cutawayMeasureNo =[&](staff_idx_t staffIdx) {
     558        22281 :         if (!m) {
     559         1135 :             return false;
     560              :         }
     561        21146 :         bool measureNo = isMeasureNumber();
     562        21146 :         bool staffVisible = m->mstaves()[staffIdx]->visible();
     563        21146 :         bool cutaway = score()->staff(staffIdx)->cutaway() && m->isEmpty(staffIdx);
     564        21146 :         return (cutaway || !staffVisible) && measureNo;
     565        21699 :     };
     566              : 
     567        21699 :     const std::vector<Staff*>& systemObjectStaves = m_score->systemObjectStaves(); // CAUTION: may not be ordered
     568        21699 :     if (originalStaffIdx > 0) {
     569          658 :         staff_idx_t prevSysObjStaffIdx = 0;
     570         2272 :         for (Staff* sysObjStaff : systemObjectStaves) {
     571         1614 :             staff_idx_t sysObjStaffIdx = sysObjStaff->idx();
     572         1614 :             if (sysObjStaffIdx > prevSysObjStaffIdx && sysObjStaffIdx < originalStaffIdx) {
     573          430 :                 prevSysObjStaffIdx = sysObjStaffIdx;
     574              :             }
     575              :         }
     576              : 
     577          658 :         bool omitObject = true;
     578          658 :         for (staff_idx_t stfIdx = prevSysObjStaffIdx; stfIdx < originalStaffIdx; ++stfIdx) {
     579          658 :             if (m_score->staff(stfIdx)->show() && system->staff(stfIdx)->show() && !cutawayMeasureNo(stfIdx)) {
     580          658 :                 omitObject = false;
     581          658 :                 break;
     582              :             }
     583              :         }
     584              : 
     585          658 :         if (omitObject) {
     586              :             // If all staves between this and the previous system object are hidden
     587              :             // we omit this object because it will be replaced by the upper one
     588            0 :             return muse::nidx;
     589              :         }
     590              :     }
     591              : 
     592        21699 :     if (m_score->staff(originalStaffIdx)->show() && system->staff(originalStaffIdx)->show() && !cutawayMeasureNo(originalStaffIdx)) {
     593        21623 :         return originalStaffIdx;
     594              :     }
     595              : 
     596           76 :     return system->nextVisibleStaff(originalStaffIdx);
     597              : }
     598              : 
     599         2694 : bool EngravingItem::isTopSystemObject() const
     600              : {
     601         2694 :     return systemFlag() && track() == 0;
     602              : }
     603              : 
     604      1646453 : staff_idx_t EngravingItem::vStaffIdx() const
     605              : {
     606      1646453 :     return staffIdx();
     607              : }
     608              : 
     609      2735752 : voice_idx_t EngravingItem::voice() const
     610              : {
     611      2735752 :     return track2voice(m_track);
     612              : }
     613              : 
     614           56 : void EngravingItem::setVoice(voice_idx_t v)
     615              : {
     616           56 :     setTrack((m_track / VOICES) * VOICES + v);
     617           56 : }
     618              : 
     619              : //---------------------------------------------------------
     620              : //   tick
     621              : //---------------------------------------------------------
     622              : 
     623      8196606 : Fraction EngravingItem::tick() const
     624              : {
     625              :     // walks EngravingObject, because the chain leaves EngravingItem at the score
     626     10346272 :     for (const EngravingObject* e = ownershipParent(); e; e = e->ownershipParent()) {
     627      9767416 :         if (e->isSegment()) {
     628      7357910 :             return toSegment(e)->tick();
     629      2409506 :         } else if (e->isMeasureBase()) {
     630       259840 :             return toMeasureBase(e)->tick();
     631              :         }
     632              :     }
     633       578856 :     return Fraction(0, 1);
     634              : }
     635              : 
     636              : //---------------------------------------------------------
     637              : //   rtick
     638              : //---------------------------------------------------------
     639              : 
     640      1520489 : Fraction EngravingItem::rtick() const
     641              : {
     642      2687197 :     for (const EngravingObject* e = ownershipParent(); e; e = e->ownershipParent()) {
     643      2687107 :         if (e->isSegment()) {
     644      1520399 :             return toSegment(e)->rtick();
     645              :         }
     646              :     }
     647           90 :     return Fraction(0, 1);
     648              : }
     649              : 
     650              : //---------------------------------------------------------
     651              : //   beat
     652              : //---------------------------------------------------------
     653              : 
     654            0 : Fraction EngravingItem::beat() const
     655              : {
     656              :     // Returns an appropriate fraction of ticks for use as a "Beat" reference
     657              :     // in the Select All Similar filter.
     658              :     int bar, beat, ticks;
     659            0 :     TimeSigMap* tsm = score()->sigmap();
     660            0 :     tsm->tickValues(tick().ticks(), &bar, &beat, &ticks);
     661            0 :     int ticksB = ticks_beat(tsm->timesig(tick().ticks()).timesig().denominator());
     662              : 
     663            0 :     Fraction complexFraction((++beat * ticksB) + ticks, ticksB);
     664            0 :     return complexFraction.reduced();
     665              : }
     666              : 
     667              : //---------------------------------------------------------
     668              : //   part
     669              : //---------------------------------------------------------
     670              : 
     671       753282 : Part* EngravingItem::part() const
     672              : {
     673       753282 :     Staff* s = staff();
     674       753282 :     return s ? s->part() : 0;
     675              : }
     676              : 
     677       312828 : Part* EngravingItem::originPart() const
     678              : {
     679       312828 :     Part* p = part();
     680       312828 :     return p ? p->originPart(track(), tick()) : nullptr;
     681              : }
     682              : 
     683       309384 : void EngravingItem::setColor(const Color& c)
     684              : {
     685       309384 :     m_color = c;
     686       309384 : }
     687              : 
     688       158557 : Color EngravingItem::color() const
     689              : {
     690       158557 :     return m_color;
     691              : }
     692              : 
     693              : //---------------------------------------------------------
     694              : //   curColor
     695              : //---------------------------------------------------------
     696              : 
     697          385 : Color EngravingItem::curColor(const rendering::PaintOptions& opt) const
     698              : {
     699          385 :     return curColor(getProperty(Pid::VISIBLE).toBool(), opt);
     700              : }
     701              : 
     702              : //---------------------------------------------------------
     703              : //   curColor
     704              : //---------------------------------------------------------
     705              : 
     706          385 : Color EngravingItem::curColor(bool isVisible, const rendering::PaintOptions& opt) const
     707              : {
     708          385 :     return curColor(isVisible, color(), opt);
     709              : }
     710              : 
     711          385 : Color EngravingItem::curColor(bool isVisible, Color normalColor, const rendering::PaintOptions& opt) const
     712              : {
     713          385 :     if (opt.isPrinting) {
     714              :         // For printing, always use the actual color of the item (default black unless overridden by the user)
     715          385 :         return normalColor;
     716              :     }
     717              : 
     718            0 :     if (flag(ElementFlag::DROP_TARGET)) {
     719            0 :         return configuration()->highlightSelectionColor(track() == muse::nidx ? 0 : voice());
     720              :     }
     721              : 
     722            0 :     bool marked = false;
     723            0 :     if (isNote()) {
     724            0 :         marked = toNote(this)->mark();
     725              :     }
     726              : 
     727            0 :     auto maybeOverrideColor = [&](Color c) -> Color {
     728            0 :         if (opt.overrideItemColor) {
     729            0 :             return opt.overrideItemColor(this, c);
     730              :         }
     731            0 :         return c;
     732            0 :     };
     733              : 
     734            0 :     if (selected() || marked) {
     735            0 :         voice_idx_t voiceForColorChoice = track() == muse::nidx ? 0 : voice();
     736            0 :         if (hasVoiceAssignmentProperties()) {
     737            0 :             VoiceAssignment voiceAssignment = getProperty(Pid::VOICE_ASSIGNMENT).value<VoiceAssignment>();
     738            0 :             if (voiceAssignment != VoiceAssignment::CURRENT_VOICE_ONLY) {
     739            0 :                 voiceForColorChoice = VOICES;
     740              :             }
     741              :         }
     742              :         return maybeOverrideColor(
     743            0 :             configuration()->selectionColor(voiceForColorChoice, isVisible, isUnlinkedFromMaster()));
     744              :     }
     745              : 
     746            0 :     if (!isVisible) {
     747            0 :         return configuration()->invisibleColor();
     748              :     }
     749              : 
     750            0 :     if (!opt.ignoreDisplayedDefaultColor && normalColor == configuration()->defaultColor()) {
     751            0 :         return maybeOverrideColor(configuration()->displayedDefaultColor(opt.invertColors));
     752              :     }
     753              : 
     754            0 :     if (opt.invertColors) {
     755            0 :         return maybeOverrideColor(normalColor.inverted());
     756              :     }
     757            0 :     return maybeOverrideColor(normalColor);
     758              : }
     759              : 
     760        18006 : PointF EngravingItem::systemPos() const
     761              : {
     762              :     // Returns position in system coordinates. Only applicable to items
     763              :     // that have a System ancestor.
     764              : 
     765        18006 :     IF_ASSERT_FAILED(findAncestor(ElementType::SYSTEM)) {
     766            0 :         return PointF();
     767              :     }
     768              : 
     769        18006 :     PointF result = pos();
     770        18006 :     EngravingItem* ancestor = layoutParent();
     771        51130 :     while (ancestor && !ancestor->isSystem()) {
     772        33124 :         result += ancestor->pos();
     773        33124 :         ancestor = ancestor->layoutParent();
     774              :     }
     775              : 
     776        18006 :     return result;
     777              : }
     778              : 
     779              : //---------------------------------------------------------
     780              : //   pagePos
     781              : //    return position in canvas coordinates
     782              : //---------------------------------------------------------
     783              : 
     784       711274 : PointF EngravingItem::pagePos() const
     785              : {
     786       711274 :     PointF p(pos());
     787       711274 :     EngravingItem* parent = layoutParent();
     788       711274 :     if (parent == nullptr) {
     789         4200 :         return p;
     790              :     }
     791              : 
     792       707074 :     staff_idx_t idx = effectiveStaffIdx();
     793              : 
     794       707074 :     if (idx == muse::nidx) {
     795        14065 :         idx = vStaffIdx();
     796              :     }
     797              : 
     798       296100 :     auto measureStaffY = [this](const Measure* measure, staff_idx_t idx) -> double {
     799       296100 :         if (!measure) {
     800            0 :             return 0.0;
     801              :         }
     802       296100 :         if (measure->mstaves().size() <= idx) {
     803            0 :             LOGD("staffIdx out of bounds: %s", typeName());
     804            0 :             return 0.0;
     805              :         }
     806       296100 :         return measure->staffLines(idx)->y();
     807       707074 :     };
     808              : 
     809       557131 :     auto systemStaffY = [this](const System* system, staff_idx_t idx) -> double {
     810       557131 :         if (!system) {
     811          367 :             return 0.0;
     812              :         }
     813       556764 :         if (system->staves().size() <= idx) {
     814            0 :             LOGD("staffIdx out of bounds: %s", typeName());
     815            0 :             return 0.0;
     816              :         }
     817       556764 :         return system->staffYpage(idx);
     818       707074 :     };
     819              : 
     820       707074 :     if (m_flags & ElementFlag::ON_STAFF) {
     821              :         // Segments, measures and systems all contain multiple staves
     822              :         // Find the position of the stave that this item is on
     823       561464 :         if (parent->isSegment()) {
     824       290659 :             Segment* segment = toSegment(parent);
     825       290659 :             Measure* measure = segment ? segment->measure() : nullptr;
     826       290659 :             System* system = measure ? measure->system() : nullptr;
     827       290659 :             p.ry() += measureStaffY(measure, idx) + systemStaffY(system, idx);
     828       270805 :         } else if (parent->isMeasure()) {
     829         5441 :             Measure* measure = toMeasure(parent);
     830         5441 :             System* system = measure->system();
     831         5441 :             p.ry() += measureStaffY(measure, idx) + systemStaffY(system, idx);
     832       265364 :         } else if (parent->isSystem()) {
     833       261031 :             System* system = toSystem(parent);
     834       261031 :             p.ry() += systemStaffY(system, idx);
     835              :         } else {
     836         4333 :             return p + parent->pagePos();
     837              :         }
     838       557131 :         p.rx() = pageX();
     839              :     } else {
     840       145610 :         if (parent->layoutParent()) {
     841       135671 :             p += parent->pagePos();
     842              :         }
     843              :     }
     844       702741 :     return p;
     845              : }
     846              : 
     847              : //---------------------------------------------------------
     848              : //   canvasPos
     849              : //---------------------------------------------------------
     850              : 
     851        76963 : PointF EngravingItem::canvasPos() const
     852              : {
     853        76963 :     PointF p(pos());
     854        76963 :     EngravingItem* parent = layoutParent();
     855        76963 :     if (parent == nullptr) {
     856         1253 :         return p;
     857              :     }
     858              : 
     859        75710 :     staff_idx_t idx = effectiveStaffIdx();
     860              : 
     861        75710 :     if (idx == muse::nidx) {
     862         1669 :         idx = vStaffIdx();
     863              :     }
     864              : 
     865        39073 :     auto measureStaffY = [this](const Measure* measure, staff_idx_t idx) -> double {
     866        39073 :         if (!measure) {
     867            0 :             return 0.0;
     868              :         }
     869        39073 :         if (measure->mstaves().size() <= idx) {
     870         1174 :             LOGD("staffIdx out of bounds: %s", typeName());
     871         1174 :             return 0.0;
     872              :         }
     873        37899 :         return measure->staffLines(idx)->y();
     874        75710 :     };
     875              : 
     876        42837 :     auto systemStaffY = [this](const System* system, staff_idx_t idx) -> double {
     877        42837 :         if (!system) {
     878         1118 :             return 0.0;
     879              :         }
     880        41719 :         if (system->staves().size() <= idx) {
     881           74 :             LOGD("staffIdx out of bounds: %s", typeName());
     882           74 :             return 0.0;
     883              :         }
     884        41645 :         return system->staffYpage(idx);
     885        75710 :     };
     886              : 
     887        39073 :     auto pageY = [](const Page* page) -> double {
     888        39073 :         if (!page) {
     889        15660 :             return 0.0;
     890              :         }
     891        23413 :         return page->y();
     892              :     };
     893              : 
     894        75710 :     if (m_flags & ElementFlag::ON_STAFF) {
     895              :         // Segments, measures and systems all contain multiple staves
     896              :         // Find the position of the stave that this item is on
     897        47183 :         if (parent->isSegment()) {
     898        38849 :             Segment* segment = toSegment(parent);
     899        38849 :             Measure* measure = segment ? segment->measure() : nullptr;
     900        38849 :             System* system = measure ? measure->system() : nullptr;
     901        38849 :             Page* page = system ? system->page() : nullptr;
     902        38849 :             p.ry() += measureStaffY(measure, idx) + systemStaffY(system, idx) + pageY(page);
     903         8334 :         } else if (parent->isMeasure()) {
     904          224 :             Measure* measure = toMeasure(parent)->coveringMMRestOrThis();
     905          224 :             System* system = measure->system();
     906          224 :             Page* page = system->page();
     907          224 :             p.ry() += measureStaffY(measure, idx) + systemStaffY(system, idx) + pageY(page);
     908         8110 :         } else if (parent->isSystem()) {
     909         3764 :             System* system = toSystem(parent);
     910         3764 :             p.ry() += systemStaffY(system, idx);
     911              :         } else {
     912         4346 :             return p + parent->canvasPos();
     913              :         }
     914        42837 :         p.rx() = canvasX();
     915              :     } else {
     916        28527 :         p += parent->canvasPos();
     917              :     }
     918        71364 :     return p;
     919              : }
     920              : 
     921              : //---------------------------------------------------------
     922              : //   pageX
     923              : //---------------------------------------------------------
     924              : 
     925       599583 : double EngravingItem::pageX() const
     926              : {
     927       599583 :     double xp = x();
     928      1387447 :     for (EngravingItem* e = layoutParent(); e && e->layoutParent(); e = e->layoutParent()) {
     929       787864 :         xp += e->x();
     930              :     }
     931       599583 :     return xp;
     932              : }
     933              : 
     934              : //---------------------------------------------------------
     935              : //    canvasX
     936              : //---------------------------------------------------------
     937              : 
     938        43044 : double EngravingItem::canvasX() const
     939              : {
     940        43044 :     double xp = x();
     941       189727 :     for (EngravingItem* e = layoutParent(); e; e = e->layoutParent()) {
     942       146683 :         xp += e->x();
     943              :     }
     944        43044 :     return xp;
     945              : }
     946              : 
     947              : //---------------------------------------------------------
     948              : //   contains
     949              : //    Return true if p is inside the shape of the object.
     950              : //    Note: p is in page coordinates
     951              : //---------------------------------------------------------
     952              : 
     953            0 : bool EngravingItem::contains(const PointF& p) const
     954              : {
     955            0 :     return shape().contains(p - pagePos());
     956              : }
     957              : 
     958              : //---------------------------------------------------------
     959              : //  intersects
     960              : //    Return true if \a rr intersects bounding box of object.
     961              : //    Note: \a rr is in page coordinates
     962              : //---------------------------------------------------------
     963              : 
     964            0 : bool EngravingItem::intersects(const RectF& rr) const
     965              : {
     966            0 :     return shape().intersects(rr.translated(-pagePos()));
     967              : }
     968              : 
     969            0 : bool EngravingItem::hitShapeContains(const PointF& p) const
     970              : {
     971            0 :     return hitShape().contains(p - pagePos());
     972              : }
     973              : 
     974            0 : bool EngravingItem::hitShapeIntersects(const RectF& rr) const
     975              : {
     976            0 :     return hitShape().intersects(rr.translated(-pagePos()));
     977              : }
     978              : 
     979              : //---------------------------------------------------------
     980              : //   remove
     981              : ///   Remove \a el from the list. Return true on success.
     982              : //---------------------------------------------------------
     983              : 
     984           91 : bool ElementList::remove(EngravingItem* el)
     985              : {
     986           91 :     auto i = find(begin(), end(), el);
     987           91 :     if (i == end()) {
     988            0 :         return false;
     989              :     }
     990           91 :     erase(i);
     991           91 :     return true;
     992              : }
     993              : 
     994              : //---------------------------------------------------------
     995              : //   replace
     996              : //---------------------------------------------------------
     997              : 
     998            0 : void ElementList::replace(EngravingItem* o, EngravingItem* n)
     999              : {
    1000            0 :     auto i = find(begin(), end(), o);
    1001            0 :     if (i == end()) {
    1002            0 :         LOGD("ElementList::replace: element not found");
    1003            0 :         return;
    1004              :     }
    1005            0 :     *i = n;
    1006              : }
    1007              : 
    1008              : //---------------------------------------------------------
    1009              : //   dump
    1010              : //---------------------------------------------------------
    1011              : 
    1012            0 : void EngravingItem::dump() const
    1013              : {
    1014            0 :     const LayoutData* ldata = this->ldata();
    1015            0 :     LOGD("---EngravingItem: %s, pos(%4.2f,%4.2f)"
    1016              :          "\n   bbox(%g,%g,%g,%g)"
    1017              :          "\n   abox(%g,%g,%g,%g)"
    1018              :          "\n  parent: %p",
    1019            0 :          typeName(), ldata->pos().x(), ldata->pos().y(),
    1020            0 :          ldata->bbox().x(), ldata->bbox().y(), ldata->bbox().width(), ldata->bbox().height(),
    1021            0 :          pageBoundingRect().x(), pageBoundingRect().y(), pageBoundingRect().width(), pageBoundingRect().height(),
    1022              :          ownershipParent());
    1023            0 : }
    1024              : 
    1025              : //---------------------------------------------------------
    1026              : //   mimeData
    1027              : //---------------------------------------------------------
    1028              : 
    1029           16 : muse::ByteArray EngravingItem::mimeData(const PointF& dragOffset) const
    1030              : {
    1031           16 :     auto buffer = Buffer::opened(IODevice::WriteOnly);
    1032           16 :     XmlWriter xml(&buffer);
    1033              : 
    1034           16 :     xml.startElement("EngravingItem");
    1035           16 :     if (isNote()) {
    1036           15 :         xml.tagFraction("duration", toNote(this)->chord()->ticks());
    1037              :     }
    1038           16 :     if (!dragOffset.isNull()) {
    1039            0 :         xml.tagPoint("dragOffset", dragOffset);
    1040              :     }
    1041              : 
    1042           16 :     rw::RWRegister::writer()->writeItem(this, xml);
    1043              : 
    1044           16 :     xml.endElement();
    1045           16 :     xml.flush();
    1046           16 :     buffer.close();
    1047           32 :     return buffer.data();
    1048           16 : }
    1049              : 
    1050              : //---------------------------------------------------------
    1051              : //   readType
    1052              : //    return new position of QDomElement in e
    1053              : //---------------------------------------------------------
    1054              : 
    1055           16 : ElementType EngravingItem::readType(XmlReader& e, PointF* dragOffset, Fraction* duration)
    1056              : {
    1057           16 :     while (e.readNextStartElement()) {
    1058           16 :         if (e.name() == "EngravingItem") {
    1059           31 :             while (e.readNextStartElement()) {
    1060           31 :                 const AsciiStringView tag = e.name();
    1061           31 :                 if (tag == "dragOffset") {
    1062            0 :                     *dragOffset = e.readPoint();
    1063           31 :                 } else if (tag == "duration") {
    1064           15 :                     *duration = e.readFraction();
    1065              :                 } else {
    1066           16 :                     ElementType type = TConv::fromXml(tag, ElementType::INVALID);
    1067           16 :                     if (type == ElementType::INVALID) {
    1068            0 :                         break;
    1069              :                     }
    1070           16 :                     return type;
    1071              :                 }
    1072              :             }
    1073              :         } else {
    1074            0 :             e.unknown();
    1075              :         }
    1076              :     }
    1077            0 :     return ElementType::INVALID;
    1078              : }
    1079              : 
    1080              : //---------------------------------------------------------
    1081              : //   readMimeData
    1082              : //---------------------------------------------------------
    1083              : 
    1084           16 : EngravingItem* EngravingItem::readMimeData(Score* score, const muse::ByteArray& data, PointF* dragOffset, Fraction* duration)
    1085              : {
    1086           16 :     XmlReader e(data);
    1087              : 
    1088           16 :     const ElementType type = EngravingItem::readType(e, dragOffset, duration);
    1089           16 :     if (type == ElementType::INVALID) {
    1090            0 :         LOGD("cannot read type");
    1091            0 :         return nullptr;
    1092              :     }
    1093              : 
    1094           16 :     EngravingItem* el = Factory::createItem(type, score->dummy(), false);
    1095           16 :     if (el) {
    1096           16 :         rw::RWRegister::reader()->readItem(el, e);
    1097              :     }
    1098              : 
    1099           16 :     return el;
    1100           16 : }
    1101              : 
    1102              : //---------------------------------------------------------
    1103              : //   add
    1104              : //---------------------------------------------------------
    1105              : 
    1106           40 : void EngravingItem::add(EngravingItem* e)
    1107              : {
    1108           40 :     switch (e->type()) {
    1109           40 :     case ElementType::PARENTHESIS: {
    1110           40 :         Parenthesis* p = toParenthesis(e);
    1111           40 :         p->setVisible(visible());
    1112           40 :         if (p->direction() == DirectionH::LEFT) {
    1113           20 :             m_leftParenthesis = p;
    1114           20 :         } else if (p->direction() == DirectionH::RIGHT) {
    1115           20 :             m_rightParenthesis = p;
    1116              :         }
    1117           40 :         break;
    1118              :     }
    1119            0 :     default:
    1120            0 :         LOGD("EngravingItem: cannot add %s to %s", e->typeName(), typeName());
    1121              :     }
    1122           40 : }
    1123              : 
    1124              : //---------------------------------------------------------
    1125              : //   remove
    1126              : //---------------------------------------------------------
    1127              : 
    1128            0 : void EngravingItem::remove(EngravingItem* e)
    1129              : {
    1130            0 :     switch (e->type()) {
    1131            0 :     case ElementType::PARENTHESIS: {
    1132            0 :         if (e == m_leftParenthesis) {
    1133            0 :             m_leftParenthesis = nullptr;
    1134              :         }
    1135            0 :         if (e == m_rightParenthesis) {
    1136            0 :             m_rightParenthesis = nullptr;
    1137              :         }
    1138            0 :         break;
    1139              :     }
    1140            0 :     default:
    1141            0 :         ASSERT_X(String(u"EngravingItem: cannot remove %1 from %2").arg(String::fromAscii(e->typeName()), String::fromAscii(typeName())));
    1142              :     }
    1143            0 : }
    1144              : 
    1145              : //---------------------------------------------------------
    1146              : //   elementLessThan
    1147              : //---------------------------------------------------------
    1148              : 
    1149         2366 : bool elementLessThan(const EngravingItem* const e1, const EngravingItem* const e2)
    1150              : {
    1151         2366 :     if (e1->z() == e2->z()) {
    1152         1133 :         if (e1->selected() && !e2->selected()) {
    1153            0 :             return false;
    1154              :         }
    1155         1133 :         if (!e1->selected() && e2->selected()) {
    1156            0 :             return true;
    1157              :         }
    1158         1133 :         if (e1->visible() && !e2->visible()) {
    1159            0 :             return false;
    1160              :         }
    1161         1133 :         if (!e1->visible() && e2->visible()) {
    1162            0 :             return true;
    1163              :         }
    1164              : 
    1165         1133 :         return e1->track() < e2->track();
    1166              :     }
    1167              : 
    1168         1233 :     return e1->z() < e2->z();
    1169              : }
    1170              : 
    1171              : //---------------------------------------------------------
    1172              : //   autoplace
    1173              : //---------------------------------------------------------
    1174              : 
    1175       156267 : bool EngravingItem::autoplace() const
    1176              : {
    1177       156267 :     if (!score() || !style().styleB(Sid::autoplaceEnabled)) {
    1178            0 :         return false;
    1179              :     }
    1180       156267 :     return !flag(ElementFlag::NO_AUTOPLACE);
    1181              : }
    1182              : 
    1183              : //---------------------------------------------------------
    1184              : //   getProperty
    1185              : //---------------------------------------------------------
    1186              : 
    1187       809204 : PropertyValue EngravingItem::getProperty(Pid propertyId) const
    1188              : {
    1189       809204 :     switch (propertyId) {
    1190           47 :     case Pid::TICK:
    1191           47 :         return tick();
    1192            6 :     case Pid::TRACK:
    1193            6 :         return track();
    1194            0 :     case Pid::VOICE:
    1195            0 :         return voice();
    1196         3029 :     case Pid::GENERATED:
    1197         3029 :         return generated();
    1198        90631 :     case Pid::COLOR:
    1199        90631 :         return PropertyValue::fromValue(color());
    1200        80391 :     case Pid::VISIBLE:
    1201        80391 :         return visible();
    1202            0 :     case Pid::SELECTED:
    1203            0 :         return selected();
    1204        18400 :     case Pid::OFFSET:
    1205        18400 :         return PropertyValue::fromValue(m_offset);
    1206         1731 :     case Pid::MIN_DISTANCE:
    1207         1731 :         return m_minDistance;
    1208        76653 :     case Pid::PLACEMENT:
    1209        76653 :         return placement();
    1210        95776 :     case Pid::AUTOPLACE:
    1211        95776 :         return autoplace();
    1212        95508 :     case Pid::Z:
    1213        95508 :         return z();
    1214            8 :     case Pid::SYSTEM_FLAG:
    1215            8 :         return systemFlag();
    1216         1239 :     case Pid::SIZE_SPATIUM_DEPENDENT:
    1217         1239 :         return sizeIsSpatiumDependent();
    1218       105121 :     case Pid::POSITION_LINKED_TO_MASTER:
    1219       105121 :         return _isPositionLinkedToMaster;
    1220       105166 :     case Pid::APPEARANCE_LINKED_TO_MASTER:
    1221       105166 :         return _isAppearanceLinkedToMaster;
    1222        77613 :     case Pid::EXCLUDE_FROM_OTHER_PARTS:
    1223        77613 :         return _excludeFromOtherParts;
    1224          819 :     case Pid::EXCLUDE_VERTICAL_ALIGN:
    1225          819 :         return m_excludeVerticalAlign;
    1226        53798 :     case Pid::HAS_PARENTHESES:
    1227        53798 :         return parenthesesMode();
    1228         3268 :     default:
    1229         3268 :         if (ownershipParent()) {
    1230         3258 :             return ownershipParent()->getProperty(propertyId);
    1231              :         }
    1232              : 
    1233           10 :         return PropertyValue();
    1234              :     }
    1235              : }
    1236              : 
    1237              : //---------------------------------------------------------
    1238              : //   setProperty
    1239              : //---------------------------------------------------------
    1240              : 
    1241       134045 : bool EngravingItem::setProperty(Pid propertyId, const PropertyValue& v)
    1242              : {
    1243       134045 :     switch (propertyId) {
    1244            1 :     case Pid::TRACK:
    1245            1 :         setTrack(v.value<track_idx_t>());
    1246            1 :         break;
    1247            0 :     case Pid::VOICE:
    1248            0 :         setVoice(v.value<voice_idx_t>());
    1249            0 :         break;
    1250           77 :     case Pid::GENERATED:
    1251           77 :         setGenerated(v.toBool());
    1252           77 :         break;
    1253        46815 :     case Pid::COLOR:
    1254        46815 :         setColor(v.value<Color>());
    1255        46815 :         break;
    1256          183 :     case Pid::VISIBLE:
    1257          183 :         setVisible(v.toBool());
    1258          183 :         break;
    1259            0 :     case Pid::SELECTED:
    1260            0 :         setSelected(v.toBool());
    1261            0 :         break;
    1262         1497 :     case Pid::OFFSET:
    1263         1497 :         m_offset = v.value<PointF>();
    1264         1497 :         break;
    1265        11649 :     case Pid::MIN_DISTANCE:
    1266        11649 :         setMinDistance(v.value<Spatium>());
    1267        11649 :         break;
    1268         8145 :     case Pid::PLACEMENT:
    1269         8145 :         setPlacement(v.value<PlacementV>());
    1270         8145 :         break;
    1271           21 :     case Pid::AUTOPLACE:
    1272           21 :         setAutoplace(v.toBool());
    1273           21 :         break;
    1274            0 :     case Pid::Z:
    1275            0 :         setZ(v.toInt());
    1276            0 :         break;
    1277        17783 :     case Pid::SYSTEM_FLAG:
    1278        17783 :         setSystemFlag(v.toBool());
    1279        17783 :         break;
    1280        47807 :     case Pid::SIZE_SPATIUM_DEPENDENT:
    1281        47807 :         setSizeIsSpatiumDependent(v.toBool());
    1282        47807 :         break;
    1283            7 :     case Pid::POSITION_LINKED_TO_MASTER:
    1284            7 :         if (_isPositionLinkedToMaster == v.toBool()) {
    1285            0 :             break;
    1286              :         }
    1287            7 :         if (!_isPositionLinkedToMaster) {
    1288            1 :             relinkPropertiesToMaster(PropertyGroup::POSITION);
    1289              :         }
    1290            7 :         setPositionLinkedToMaster(v.toBool());
    1291            7 :         break;
    1292           39 :     case Pid::APPEARANCE_LINKED_TO_MASTER:
    1293           39 :         if (_isAppearanceLinkedToMaster == v.toBool()) {
    1294           16 :             break;
    1295              :         }
    1296           23 :         if (!_isAppearanceLinkedToMaster) {
    1297            1 :             relinkPropertiesToMaster(PropertyGroup::APPEARANCE);
    1298              :         }
    1299           23 :         setAppearanceLinkedToMaster(v.toBool());
    1300           23 :         break;
    1301           20 :     case Pid::EXCLUDE_FROM_OTHER_PARTS:
    1302           20 :         setExcludeFromOtherParts(v.toBool());
    1303           20 :         break;
    1304            0 :     case Pid::EXCLUDE_VERTICAL_ALIGN:
    1305            0 :         setExcludeVerticalAlign(v.toBool());
    1306            0 :         break;
    1307            1 :     case Pid::HAS_PARENTHESES:
    1308            1 :         setParenthesesMode(v.value<ParenthesesMode>());
    1309            1 :         break;
    1310            0 :     default:
    1311            0 :         if (ownershipParent()) {
    1312            0 :             return ownershipParent()->setProperty(propertyId, v);
    1313              :         }
    1314              : 
    1315              :         LOG_PROP() << typeName() << " unknown <" << propertyName(propertyId) << ">(" << int(propertyId) << "), data: " << v.value<String>();
    1316            0 :         return false;
    1317              :     }
    1318       134045 :     triggerLayout();
    1319       134045 :     return true;
    1320              : }
    1321              : 
    1322            7 : void EngravingItem::manageExclusionFromParts(bool exclude)
    1323              : {
    1324            7 :     if (exclude) {
    1325            4 :         const std::list<EngravingObject*> links = linkList();
    1326           12 :         for (EngravingObject* linkedObject : links) {
    1327            8 :             if (linkedObject->score() == score()) {
    1328            4 :                 continue;
    1329              :             }
    1330            4 :             EngravingItem* linkedItem = toEngravingItem(linkedObject);
    1331            4 :             if (linkedItem->selected()) {
    1332            0 :                 linkedItem->score()->deselect(linkedItem);
    1333              :             }
    1334            4 :             linkedItem->score()->undoRemoveElement(linkedItem, false);
    1335            4 :             linkedItem->undoUnlink();
    1336              :         }
    1337            4 :     } else {
    1338            3 :         score()->undoAddElement(this, /*addToLinkedStaves*/ true, /*ctrlModifier*/ false, this);
    1339              :     }
    1340            7 : }
    1341              : 
    1342        22693 : EngravingItem* EngravingItem::sharedItem() const
    1343              : {
    1344        22693 :     return ldata()->m_sharedItem;
    1345              : }
    1346              : 
    1347        31537 : const std::vector<EngravingItem*>& EngravingItem::originItems() const
    1348              : {
    1349        31537 :     return ldata()->m_originItems;
    1350              : }
    1351              : 
    1352         3530 : void EngravingItem::connectSharedItem(EngravingItem* sharedItem, EngravingItem* originItem)
    1353              : {
    1354         3530 :     if (originItem->ldata()->m_sharedItem == sharedItem) {
    1355           76 :         return;
    1356              :     }
    1357              : 
    1358         3454 :     IF_ASSERT_FAILED(sharedItem->type() == originItem->type()) {
    1359            0 :         return;
    1360              :     }
    1361              : 
    1362         3454 :     IF_ASSERT_FAILED(!sharedItem->isChord() && !originItem->isChord()) {
    1363              :         // Notes are connected, not chords
    1364            0 :         return;
    1365              :     }
    1366              : 
    1367         3454 :     IF_ASSERT_FAILED(sharedItem->ldata()->m_sharedItem == nullptr && originItem->ldata()->m_originItems.empty()) {
    1368            0 :         return;
    1369              :     }
    1370              : 
    1371         3454 :     originItem->mutldata()->m_sharedItem = sharedItem;
    1372              : 
    1373         3454 :     std::vector<EngravingItem*>& curOriginItems = sharedItem->mutldata()->m_originItems;
    1374         3454 :     auto it = std::lower_bound(curOriginItems.begin(), curOriginItems.end(), originItem, [](EngravingItem* item1, EngravingItem* item2) {
    1375         1212 :         return item1->track() < item2->track();
    1376              :     });
    1377              : 
    1378         3454 :     curOriginItems.insert(it, originItem);
    1379              : }
    1380              : 
    1381           36 : void EngravingItem::disconnectSharedItem(EngravingItem* sharedItem, EngravingItem* originItem)
    1382              : {
    1383           36 :     IF_ASSERT_FAILED(originItem->m_layoutData->m_sharedItem == sharedItem) {
    1384            0 :         return;
    1385              :     }
    1386              : 
    1387           36 :     originItem->m_layoutData->m_sharedItem = nullptr;
    1388              : 
    1389           36 :     DO_ASSERT(muse::remove(sharedItem->m_layoutData->m_originItems, originItem));
    1390              : }
    1391              : 
    1392        19454 : void EngravingItem::disconnectAllOriginItems(EngravingItem* sharedItem)
    1393              : {
    1394        22177 :     for (EngravingItem* originItem : sharedItem->originItems()) {
    1395         2723 :         originItem->m_layoutData->m_sharedItem = nullptr;
    1396              :     }
    1397              : 
    1398        19454 :     sharedItem->m_layoutData->m_originItems.clear();
    1399        19454 : }
    1400              : 
    1401        12500 : void EngravingItem::disconnectSharedTree(EngravingItem* item)
    1402              : {
    1403        16529 :     auto disconnect = [](EngravingItem* item) {
    1404        16529 :         if (EngravingItem* sharedItem = item->sharedItem()) {
    1405           36 :             disconnectSharedItem(sharedItem, item);
    1406              :         }
    1407              : 
    1408        16529 :         disconnectAllOriginItems(item);
    1409        16529 :     };
    1410              : 
    1411        12500 :     if (item->isSpanner()) {
    1412         1042 :         disconnect(item);
    1413         1983 :         for (SpannerSegment* ss : toSpanner(item)->spannerSegments()) {
    1414          941 :             ss->scanElements(disconnect);
    1415              :         }
    1416         1042 :         return;
    1417              :     }
    1418              : 
    1419        11458 :     item->scanElements(disconnect);
    1420              : }
    1421              : 
    1422         5183 : bool EngravingItem::isBefore(const EngravingItem* item) const
    1423              : {
    1424         5183 :     if (!item) {
    1425            0 :         return false;
    1426              :     }
    1427         5183 :     if (tick() != item->tick()) {
    1428         3956 :         return tick() < item->tick();
    1429              :     }
    1430              : 
    1431         1227 :     const Measure* thisMeasure = findMeasure();
    1432         1227 :     const Measure* otherMeasure = item->findMeasure();
    1433         1227 :     if (thisMeasure != otherMeasure) {
    1434          227 :         return thisMeasure->isBefore(otherMeasure);
    1435              :     }
    1436              : 
    1437         1000 :     const EngravingItem* thisSeg = findAncestor(ElementType::SEGMENT);
    1438         1000 :     const EngravingItem* otherSeg = item->findAncestor(ElementType::SEGMENT);
    1439         1000 :     if (!thisSeg || !otherSeg || !thisSeg->isSegment() || !otherSeg->isSegment()) {
    1440            0 :         return false;
    1441              :     }
    1442              : 
    1443         1000 :     return toSegment(thisSeg)->goesBefore(toSegment(otherSeg));
    1444              : }
    1445              : 
    1446         2651 : const Staff* EngravingItem::staffToCenterAgainst(bool above, const System* system) const
    1447              : {
    1448         2651 :     const Part* thisPart = part();
    1449         2651 :     const Staff* thisStaff = staff();
    1450         2651 :     if (!thisPart || !thisStaff) {
    1451            0 :         return nullptr;
    1452              :     }
    1453              : 
    1454         2651 :     const Fraction itemTick = tick();
    1455         2651 :     const Instrument* thisPartInstrument = thisPart->instrument(itemTick);
    1456         2651 :     const bool thisInstrumentIsVocal = thisPartInstrument && thisPartInstrument->isVocalInstrument();
    1457         2651 :     const bool participatesInStaffCentering = thisPart->nstaves() > 1 || thisInstrumentIsVocal;
    1458         2651 :     if (!participatesInStaffCentering) {
    1459         1249 :         return nullptr;
    1460              :     }
    1461              : 
    1462         1402 :     if (!system) {
    1463          140 :         if (isSpanner()) {
    1464            6 :             const Segment* startSeg = toSpanner(this)->startSegment();
    1465            6 :             system = startSeg ? startSeg->measure()->system() : nullptr;
    1466              :         } else {
    1467          134 :             system = toSystem(findAncestor(ElementType::SYSTEM));
    1468              :         }
    1469              :     }
    1470              : 
    1471         1402 :     if (!system) {
    1472            0 :         return nullptr;
    1473              :     }
    1474              : 
    1475         1402 :     const staff_idx_t thisIdx = thisStaff->idx();
    1476         1402 :     const staff_idx_t otherIdx = above ? system->prevVisibleStaff(thisIdx) : system->nextVisibleStaff(thisIdx);
    1477         1402 :     if (otherIdx == muse::nidx) {
    1478          515 :         return nullptr;
    1479              :     }
    1480              : 
    1481          887 :     const Staff* otherStaff = score()->staff(otherIdx);
    1482          887 :     if (!otherStaff) {
    1483            0 :         return nullptr;
    1484              :     }
    1485              : 
    1486          887 :     const Part* otherPart = otherStaff->part();
    1487          887 :     if (otherPart == thisPart) {
    1488          477 :         return otherStaff;
    1489              :     }
    1490              : 
    1491          410 :     const Instrument* otherPartInstrument = otherPart->instrument(itemTick);
    1492          410 :     const bool otherInstrumentIsVocal = otherPartInstrument && otherPartInstrument->isVocalInstrument();
    1493              : 
    1494              :     // If the staves are not of the same part, only allow centering if they are both vocal parts:
    1495          410 :     return thisInstrumentIsVocal && otherInstrumentIsVocal ? otherStaff : nullptr;
    1496              : }
    1497              : 
    1498          322 : bool EngravingItem::appliesToAllVoicesInInstrument() const
    1499              : {
    1500          644 :     return hasVoiceAssignmentProperties()
    1501          322 :            && getProperty(Pid::VOICE_ASSIGNMENT).value<VoiceAssignment>() == VoiceAssignment::ALL_VOICE_IN_INSTRUMENT;
    1502              : }
    1503              : 
    1504            0 : void EngravingItem::setInitialTrackAndVoiceAssignment(track_idx_t track, bool curVoiceOnlyOverride)
    1505              : {
    1506            0 :     IF_ASSERT_FAILED(track != muse::nidx) {
    1507            0 :         return;
    1508              :     }
    1509              : 
    1510            0 :     if (configuration()->dynamicsApplyToAllVoices() && !curVoiceOnlyOverride) {
    1511            0 :         setTrack(trackZeroVoice(track));
    1512            0 :         setProperty(Pid::VOICE_ASSIGNMENT, VoiceAssignment::ALL_VOICE_IN_INSTRUMENT);
    1513              :     } else {
    1514            0 :         setTrack(track);
    1515            0 :         setProperty(Pid::VOICE_ASSIGNMENT, VoiceAssignment::CURRENT_VOICE_ONLY);
    1516              :     }
    1517              : }
    1518              : 
    1519            5 : void EngravingItem::checkVoiceAssignmentCompatibleWithTrack()
    1520              : {
    1521            5 :     voice_idx_t currentVoice = voice();
    1522            5 :     VoiceAssignment voiceAssignment = getProperty(Pid::VOICE_ASSIGNMENT).value<VoiceAssignment>();
    1523              : 
    1524            5 :     if (voiceAssignment != VoiceAssignment::CURRENT_VOICE_ONLY && currentVoice != 0) {
    1525            0 :         setProperty(Pid::TRACK, trackZeroVoice(track()));
    1526              :     }
    1527            5 : }
    1528              : 
    1529           84 : bool EngravingItem::elementAppliesToTrack(const track_idx_t refTrack) const
    1530              : {
    1531           84 :     if (!hasVoiceAssignmentProperties()) {
    1532           34 :         return refTrack == track();
    1533              :     }
    1534           50 :     const VoiceAssignment voiceAssignment = getProperty(Pid::VOICE_ASSIGNMENT).value<VoiceAssignment>();
    1535              : 
    1536           50 :     return elementAppliesToTrack(track(), refTrack, voiceAssignment, part());
    1537              : }
    1538              : 
    1539         1728 : void EngravingItem::setPlacementBasedOnVoiceAssignment(DirectionV styledDirection)
    1540              : {
    1541         1728 :     PlacementV oldPlacement = placement();
    1542         1728 :     PlacementV newPlacement = PlacementV::BELOW;
    1543              : 
    1544         1728 :     DirectionV internalDirectionProperty = getProperty(Pid::DIRECTION).value<DirectionV>();
    1545         1728 :     const bool directionIsAuto = internalDirectionProperty == DirectionV::AUTO && styledDirection == DirectionV::AUTO;
    1546         1728 :     const bool centerBetweenStaves = getProperty(Pid::CENTER_BETWEEN_STAVES).value<AutoOnOff>() == AutoOnOff::ON;
    1547         1728 :     const bool gapAbove = directionIsAuto && centerBetweenStaves && staffToCenterAgainst(true);
    1548         1728 :     const bool gapBelow = directionIsAuto && centerBetweenStaves && staffToCenterAgainst(false);
    1549              : 
    1550         1728 :     if (internalDirectionProperty != DirectionV::AUTO) {
    1551          374 :         newPlacement = internalDirectionProperty == DirectionV::UP ? PlacementV::ABOVE : PlacementV::BELOW;
    1552         1354 :     } else if (styledDirection != DirectionV::AUTO) {
    1553            0 :         newPlacement = styledDirection == DirectionV::UP ? PlacementV::ABOVE : PlacementV::BELOW;
    1554         1354 :     } else if (centerBetweenStaves && part()->nstaves() > 1 && (gapAbove || gapBelow)) {
    1555           48 :         bool isOnLastStaffOfInstrument = staffIdx() == part()->staves().back()->idx();
    1556           48 :         newPlacement = isOnLastStaffOfInstrument ? PlacementV::ABOVE : PlacementV::BELOW;
    1557              :     } else {
    1558         1306 :         VoiceAssignment voiceAssignment = getProperty(Pid::VOICE_ASSIGNMENT).value<VoiceAssignment>();
    1559         1306 :         if (voiceAssignment == VoiceAssignment::ALL_VOICE_IN_INSTRUMENT || voiceAssignment == VoiceAssignment::ALL_VOICE_IN_STAFF) {
    1560         2558 :             if (style().styleB(Sid::dynamicsHairpinsAboveForVocalStaves) && originPart()->instrument(tick())->isVocalInstrument()) {
    1561          191 :                 newPlacement = PlacementV::ABOVE;
    1562              :             } else {
    1563         1088 :                 newPlacement = PlacementV::BELOW;
    1564              :             }
    1565           27 :         } else if (voice() == 0) {
    1566              :             // Put above the staff only in case of multiple voices at this tick (similar to stem directions)
    1567           13 :             const Measure* measure = score()->tick2measure(tick());
    1568           13 :             Fraction startTick = Fraction(-1, 1);
    1569           13 :             Fraction length = Fraction(-1, 1);
    1570           13 :             if (isSpanner()) {
    1571            2 :                 startTick = tick();
    1572            2 :                 length = toSpanner(this)->ticks();
    1573           11 :             } else if (const Segment* segment = toSegment(findAncestor(ElementType::SEGMENT))) {
    1574           11 :                 if (segment && segment->isTimeTickType() && segment->measure() != measure) {
    1575              :                     // Edge case: this is a TimeTick segment at the end of previous measure. Happens only
    1576              :                     // when dynamic is anchorToEndOfPrevious. In this case look for preceding segment.
    1577            0 :                     segment = segment->prev1(SegmentType::Duration);
    1578            0 :                     assert(segment);
    1579            0 :                     measure = segment->measure();
    1580              :                 }
    1581           11 :                 startTick = segment->tick();
    1582           11 :                 length = segment->ticks();
    1583            0 :             } else if (measure) {
    1584            0 :                 startTick = measure->tick();
    1585            0 :                 length = measure->ticks();
    1586              :             }
    1587           13 :             if (measure && measure->hasVoices(staffIdx(), startTick, length)) {
    1588            7 :                 newPlacement = PlacementV::ABOVE;
    1589              :             } else {
    1590            6 :                 newPlacement = PlacementV::BELOW;
    1591              :             }
    1592              :         } else {
    1593           14 :             newPlacement = voice() % 2 ? PlacementV::BELOW : PlacementV::ABOVE;
    1594              :         }
    1595              :     }
    1596              : 
    1597         1728 :     if (directionIsAuto && centerBetweenStaves) {
    1598              :         /* If the preferred side has no staff to center against but the other side has one, place
    1599              :          * the item on that side instead. If neither side has one, leave the placement alone: */
    1600           56 :         const bool preferAbove = newPlacement == PlacementV::ABOVE;
    1601           56 :         const bool preferredGap = preferAbove ? gapAbove : gapBelow;
    1602           56 :         const bool otherGap = preferAbove ? gapBelow : gapAbove;
    1603           56 :         if (!preferredGap && otherGap) {
    1604            5 :             newPlacement = preferAbove ? PlacementV::BELOW : PlacementV::ABOVE;
    1605              :         }
    1606              :     }
    1607              : 
    1608         1728 :     if (newPlacement != oldPlacement) {
    1609          172 :         setPlacement(newPlacement);
    1610              :     }
    1611         1728 : }
    1612              : 
    1613            2 : void EngravingItem::relinkPropertiesToMaster(PropertyGroup propGroup)
    1614              : {
    1615            2 :     assert(!score()->isMaster());
    1616              : 
    1617            2 :     const std::list<EngravingObject*> linkedElements = linkListForPropertyPropagation();
    1618            2 :     EngravingObject* masterElement = nullptr;
    1619            2 :     for (EngravingObject* element : linkedElements) {
    1620            2 :         if (element->score()->isMaster()) {
    1621            2 :             masterElement = element;
    1622            2 :             break;
    1623              :         }
    1624              :     }
    1625              : 
    1626            2 :     if (!masterElement) {
    1627            0 :         return;
    1628              :     }
    1629              : 
    1630          804 :     for (int i = 0; i < static_cast<int>(Pid::END); ++i) {
    1631          802 :         const Pid propertyId = static_cast<Pid>(i);
    1632          802 :         if (propertyGroup(propertyId) != propGroup) {
    1633          448 :             continue;
    1634              :         }
    1635          354 :         const PropertyValue masterValue = masterElement->getProperty(propertyId);
    1636          354 :         const PropertyFlags masterFlags = masterElement->propertyFlags(propertyId);
    1637          354 :         if (getProperty(propertyId) != masterValue) {
    1638            2 :             setProperty(propertyId, masterValue);
    1639              :         }
    1640          354 :         if (propertyFlags(propertyId) != masterFlags) {
    1641            1 :             setPropertyFlags(propertyId, masterFlags);
    1642              :         }
    1643          354 :     }
    1644            2 : }
    1645              : 
    1646            0 : void EngravingItem::relinkPropertyToMaster(Pid propertyId)
    1647              : {
    1648            0 :     assert(!score()->isMaster());
    1649              : 
    1650            0 :     const std::list<EngravingObject*> linkedElements = linkListForPropertyPropagation();
    1651            0 :     EngravingObject* masterElement = nullptr;
    1652            0 :     for (EngravingObject* element : linkedElements) {
    1653            0 :         if (element->score()->isMaster()) {
    1654            0 :             masterElement = element;
    1655            0 :             break;
    1656              :         }
    1657              :     }
    1658              : 
    1659            0 :     if (!masterElement) {
    1660            0 :         return;
    1661              :     }
    1662              : 
    1663            0 :     setProperty(propertyId, masterElement->getProperty(propertyId));
    1664            0 :     setPropertyFlags(propertyId, masterElement->propertyFlags(propertyId));
    1665            0 : }
    1666              : 
    1667          500 : PropertyPropagation EngravingItem::propertyPropagation(const EngravingItem* destinationItem, Pid propertyId) const
    1668              : {
    1669          500 :     assert(destinationItem != this);
    1670              : 
    1671          500 :     if (propertyLink(propertyId)) {
    1672          159 :         return PropertyPropagation::PROPAGATE; // These properties are always linked, no matter what
    1673              :     }
    1674              : 
    1675          341 :     if (propertyGroup(propertyId) == PropertyGroup::NONE) {
    1676          285 :         return PropertyPropagation::NONE;
    1677              :     }
    1678              : 
    1679           56 :     const Score* sourceScore = score();
    1680           56 :     const Score* destinationScore = destinationItem->score();
    1681           56 :     const Staff* sourceStaff = staff();
    1682           56 :     const Staff* destinationStaff = destinationItem->staff();
    1683              : 
    1684           56 :     if (sourceScore == destinationScore) {
    1685            3 :         const bool diffStaff = sourceStaff != destinationStaff;
    1686            3 :         const bool visiblePositionOrColor = propertyId == Pid::VISIBLE || propertyId == Pid::COLOR
    1687            6 :                                             || propertyGroup(propertyId) == PropertyGroup::POSITION;
    1688            0 :         const bool hasParens = propertyId == Pid::HAS_PARENTHESES && isNote() && toNote(this)->ghost()
    1689            3 :                                && !toNote(this)->hideGeneratedParens();
    1690            3 :         const bool linkSameScore = propertyLinkSameScore(propertyId);
    1691            3 :         if ((diffStaff && (visiblePositionOrColor || hasParens)) || !linkSameScore) {
    1692              :             // Allow visibility and position to stay independent
    1693            3 :             return PropertyPropagation::NONE;
    1694              :         }
    1695              :         // Maintain every other property synced
    1696            0 :         return PropertyPropagation::PROPAGATE;
    1697              :     }
    1698              : 
    1699           53 :     const bool isTextProperty = propertyGroup(propertyId) == PropertyGroup::TEXT;
    1700           53 :     if (isTextProperty) {
    1701            3 :         if (sourceScore->isMaster() && destinationItem->isPropertyLinkedToMaster(propertyId)) {
    1702              :             // From master score - check if destination part follows master
    1703            3 :             return PropertyPropagation::PROPAGATE;
    1704              :         }
    1705              : 
    1706            0 :         if (!sourceScore->isMaster() && isPropertyLinkedToMaster(propertyId)) {
    1707              :             // From part - check if source part follows master
    1708            0 :             return PropertyPropagation::PROPAGATE;
    1709              :         }
    1710              :     }
    1711              : 
    1712           50 :     if (!sourceScore->isMaster()) {
    1713              :         // Properties are only propagated when being edited from master. If this is being edited
    1714              :         // from a part score, we mark it as unlinked so it becomes independent in the part.
    1715           34 :         return PropertyPropagation::UNLINK;
    1716              :     }
    1717              : 
    1718           16 :     if (systemFlag() && !isTopSystemObject()) {
    1719              :         // Let only the top system object propagate
    1720            0 :         return PropertyPropagation::NONE;
    1721              :     }
    1722              : 
    1723           16 :     if (destinationItem->isPropertyLinkedToMaster(propertyId)) {
    1724           16 :         return PropertyPropagation::PROPAGATE;
    1725              :     }
    1726              : 
    1727            0 :     return PropertyPropagation::NONE;
    1728              : }
    1729              : 
    1730              : //---------------------------------------------------------
    1731              : //   undoChangeProperty
    1732              : //---------------------------------------------------------
    1733              : 
    1734         6984 : void EngravingItem::undoChangeProperty(Pid pid, const PropertyValue& val, PropertyFlags ps)
    1735              : {
    1736         6984 :     if (pid == Pid::AUTOPLACE && (val.toBool() == true && !autoplace())) {
    1737              :         // Switching autoplacement on. Save user-defined
    1738              :         // placement properties to undo stack.
    1739            3 :         undoPushProperty(Pid::PLACEMENT);
    1740            3 :         undoPushProperty(Pid::OFFSET);
    1741              :     }
    1742         6984 :     EngravingObject::undoChangeProperty(pid, val, ps);
    1743         6984 : }
    1744              : 
    1745              : //---------------------------------------------------------
    1746              : //   propertyDefault
    1747              : //---------------------------------------------------------
    1748              : 
    1749       824684 : PropertyValue EngravingItem::propertyDefault(Pid pid) const
    1750              : {
    1751       824684 :     switch (pid) {
    1752            0 :     case Pid::GENERATED:
    1753            0 :         return false;
    1754        95718 :     case Pid::VISIBLE:
    1755        95718 :         return true;
    1756        88724 :     case Pid::COLOR:
    1757        88724 :         return PropertyValue::fromValue(configuration()->defaultColor());
    1758        71258 :     case Pid::PLACEMENT: {
    1759        71258 :         PropertyValue v = EngravingObject::propertyDefault(pid);
    1760        71258 :         if (v.isValid()) {        // if it's a styled property
    1761           26 :             return v;
    1762              :         }
    1763        71232 :         return PlacementV::BELOW;
    1764        71258 :     }
    1765            0 :     case Pid::SELECTED:
    1766            0 :         return false;
    1767        19142 :     case Pid::OFFSET:
    1768        19142 :         return PropertyValue::fromValue(PointF());
    1769         1685 :     case Pid::MIN_DISTANCE: {
    1770         1685 :         PropertyValue v = EngravingObject::propertyDefault(pid);
    1771         1685 :         if (v.isValid()) {
    1772          372 :             return v;
    1773              :         }
    1774         1313 :         return 0.0_sp;
    1775         1685 :     }
    1776        95762 :     case Pid::AUTOPLACE:
    1777        95762 :         return true;
    1778        95506 :     case Pid::Z:
    1779        95506 :         return int(type()) * 100;
    1780        77551 :     case Pid::POSITION_LINKED_TO_MASTER:
    1781        77551 :         return true;
    1782        77551 :     case Pid::APPEARANCE_LINKED_TO_MASTER:
    1783        77551 :         return true;
    1784        77551 :     case Pid::EXCLUDE_FROM_OTHER_PARTS:
    1785        77551 :         return false;
    1786          817 :     case Pid::EXCLUDE_VERTICAL_ALIGN:
    1787          817 :         return false;
    1788        77551 :     case Pid::HAS_PARENTHESES:
    1789       155102 :         return ParenthesesMode::NONE;
    1790        45868 :     default: {
    1791        45868 :         PropertyValue v = EngravingObject::propertyDefault(pid);
    1792              : 
    1793        45868 :         if (v.isValid()) {
    1794        41451 :             return v;
    1795              :         }
    1796              : 
    1797         4417 :         if (ownershipParent()) {
    1798         4063 :             return ownershipParent()->propertyDefault(pid);
    1799              :         }
    1800              : 
    1801          354 :         return PropertyValue();
    1802        45868 :     }
    1803              :     }
    1804              : }
    1805              : 
    1806              : //---------------------------------------------------------
    1807              : //   custom
    1808              : //    check if property is != default
    1809              : //---------------------------------------------------------
    1810              : 
    1811            0 : bool EngravingItem::custom(Pid id) const
    1812              : {
    1813            0 :     return propertyDefault(id) != getProperty(id);
    1814              : }
    1815              : 
    1816              : //---------------------------------------------------------
    1817              : //   isPrintable
    1818              : //---------------------------------------------------------
    1819              : 
    1820            0 : bool EngravingItem::isPrintable() const
    1821              : {
    1822            0 :     switch (type()) {
    1823            0 :     case ElementType::PAGE:
    1824              :     case ElementType::SYSTEM:
    1825              :     case ElementType::MEASURE:
    1826              :     case ElementType::SEGMENT:
    1827              :     case ElementType::VBOX:
    1828              :     case ElementType::HBOX:
    1829              :     case ElementType::TBOX:
    1830              :     case ElementType::FBOX:
    1831              :     case ElementType::SPACER:
    1832              :     case ElementType::SHADOW_NOTE:
    1833              :     case ElementType::LASSO:
    1834            0 :         return false;
    1835            0 :     default:
    1836            0 :         return true;
    1837              :     }
    1838              : }
    1839              : 
    1840            6 : bool EngravingItem::isPlayable() const
    1841              : {
    1842            6 :     switch (type()) {
    1843            3 :     case ElementType::NOTE:
    1844              :     case ElementType::CHORD:
    1845            3 :         return true;
    1846            0 :     case ElementType::HARMONY:
    1847            0 :         return ownershipParent() && ownershipParent()->isSegment();
    1848            3 :     default:
    1849            3 :         return false;
    1850              :     }
    1851              : }
    1852              : 
    1853        85922 : bool EngravingItem::isSystemObjectBelowBottomStaff() const
    1854              : {
    1855        85922 :     return systemFlag() && staff() && staff()->hasSystemObjectsBelowBottomStaff();
    1856              : }
    1857              : 
    1858              : //---------------------------------------------------------
    1859              : //   findAncestor
    1860              : //---------------------------------------------------------
    1861              : 
    1862         1315 : EngravingItem* EngravingItem::findAncestor(ElementType t)
    1863              : {
    1864         1315 :     EngravingItem* e = this;
    1865         4262 :     while (e && e->type() != t) {
    1866         2947 :         e = e->layoutParent();
    1867              :     }
    1868         1315 :     return e;
    1869              : }
    1870              : 
    1871       200843 : const EngravingItem* EngravingItem::findAncestor(ElementType t) const
    1872              : {
    1873       200843 :     const EngravingItem* e = this;
    1874       625345 :     while (e && e->type() != t) {
    1875       424502 :         e = e->layoutParent();
    1876              :     }
    1877       200843 :     return e;
    1878              : }
    1879              : 
    1880              : //---------------------------------------------------------
    1881              : //   findMeasure
    1882              : //---------------------------------------------------------
    1883              : 
    1884       555735 : Measure* EngravingItem::findMeasure()
    1885              : {
    1886       555735 :     if (isMeasure()) {
    1887       176614 :         return toMeasure(this);
    1888       379121 :     } else if (EngravingItem* p = layoutParent()) {
    1889       374927 :         return p->findMeasure();
    1890              :     } else {
    1891         4194 :         return 0;
    1892              :     }
    1893              : }
    1894              : 
    1895              : //---------------------------------------------------------
    1896              : //   findMeasure
    1897              : //---------------------------------------------------------
    1898              : 
    1899       169520 : const Measure* EngravingItem::findMeasure() const
    1900              : {
    1901       169520 :     EngravingItem* e = const_cast<EngravingItem*>(this);
    1902       169520 :     return e->findMeasure();
    1903              : }
    1904              : 
    1905              : //---------------------------------------------------------
    1906              : //   findMeasureBase
    1907              : //---------------------------------------------------------
    1908              : 
    1909        45461 : MeasureBase* EngravingItem::findMeasureBase()
    1910              : {
    1911        45461 :     if (isMeasureBase()) {
    1912        15592 :         return toMeasureBase(this);
    1913        29869 :     } else if (EngravingItem* p = layoutParent()) {
    1914        29174 :         return p->findMeasureBase();
    1915              :     } else {
    1916          695 :         return 0;
    1917              :     }
    1918              : }
    1919              : 
    1920              : //---------------------------------------------------------
    1921              : //   findMeasureBase
    1922              : //---------------------------------------------------------
    1923              : 
    1924        16287 : const MeasureBase* EngravingItem::findMeasureBase() const
    1925              : {
    1926        16287 :     EngravingItem* e = const_cast<EngravingItem*>(this);
    1927        16287 :     return e->findMeasureBase();
    1928              : }
    1929              : 
    1930              : //---------------------------------------------------------
    1931              : //   undoSetVisible
    1932              : //---------------------------------------------------------
    1933              : 
    1934           12 : void EngravingItem::undoSetVisible(bool v)
    1935              : {
    1936           12 :     undoChangeProperty(Pid::VISIBLE, v);
    1937           12 : }
    1938              : 
    1939            0 : void EngravingItem::undoAddElement(EngravingItem* element, bool addToLinkedStaves)
    1940              : {
    1941            0 :     score()->undoAddElement(element, addToLinkedStaves);
    1942            0 : }
    1943              : 
    1944              : //---------------------------------------------------------
    1945              : //   drawSymbol
    1946              : //---------------------------------------------------------
    1947              : 
    1948          231 : void EngravingItem::drawSymbol(SymId id, Painter* p, const PointF& o, double scale) const
    1949              : {
    1950          231 :     score()->engravingFont()->draw(id, p, magS() * scale, o);
    1951          231 : }
    1952              : 
    1953            0 : void EngravingItem::drawSymbols(const SymIdList& symbols, Painter* p, const PointF& o, double scale) const
    1954              : {
    1955            0 :     score()->engravingFont()->draw(symbols, p, magS() * scale, o);
    1956            0 : }
    1957              : 
    1958           20 : void EngravingItem::drawSymbols(const SymIdList& symbols, Painter* p, const PointF& o, const SizeF& scale) const
    1959              : {
    1960           20 :     score()->engravingFont()->draw(symbols, p, SizeF(magS() * scale), o);
    1961           20 : }
    1962              : 
    1963              : //---------------------------------------------------------
    1964              : //   symHeight
    1965              : //---------------------------------------------------------
    1966              : 
    1967         8218 : double EngravingItem::symHeight(SymId id) const
    1968              : {
    1969         8218 :     return score()->engravingFont()->height(id, magS());
    1970              : }
    1971              : 
    1972              : //---------------------------------------------------------
    1973              : //   symWidth
    1974              : //---------------------------------------------------------
    1975              : 
    1976       536791 : double EngravingItem::symWidth(SymId id) const
    1977              : {
    1978       536791 :     return score()->engravingFont()->width(id, magS());
    1979              : }
    1980              : 
    1981            0 : double EngravingItem::symWidth(const SymIdList& symbols) const
    1982              : {
    1983            0 :     return score()->engravingFont()->width(symbols, magS());
    1984              : }
    1985              : 
    1986              : //---------------------------------------------------------
    1987              : //   symAdvance
    1988              : //---------------------------------------------------------
    1989              : 
    1990          114 : double EngravingItem::symAdvance(SymId id) const
    1991              : {
    1992          114 :     return score()->engravingFont()->advance(id, magS());
    1993              : }
    1994              : 
    1995              : //---------------------------------------------------------
    1996              : //   symBbox
    1997              : //---------------------------------------------------------
    1998              : 
    1999       443428 : RectF EngravingItem::symBbox(SymId id) const
    2000              : {
    2001       443428 :     return score()->engravingFont()->bbox(id, magS());
    2002              : }
    2003              : 
    2004         2626 : RectF EngravingItem::symBbox(const SymIdList& symbols) const
    2005              : {
    2006         2626 :     return score()->engravingFont()->bbox(symbols, magS());
    2007              : }
    2008              : 
    2009        45956 : Shape EngravingItem::symShapeWithCutouts(SymId id) const
    2010              : {
    2011        45956 :     Shape shape = score()->engravingFont()->shapeWithCutouts(id, magS());
    2012       191936 :     for (ShapeElement& element : shape.elements()) {
    2013       145980 :         element.setItem(this);
    2014              :     }
    2015              : 
    2016        45956 :     return shape;
    2017              : }
    2018              : 
    2019              : //---------------------------------------------------------
    2020              : //   symSmuflAnchor
    2021              : //---------------------------------------------------------
    2022              : 
    2023       783101 : PointF EngravingItem::symSmuflAnchor(SymId symId, SmuflAnchorId anchorId) const
    2024              : {
    2025       783101 :     return score()->engravingFont()->smuflAnchor(symId, anchorId, magS());
    2026              : }
    2027              : 
    2028              : //---------------------------------------------------------
    2029              : //   symIsValid
    2030              : //---------------------------------------------------------
    2031              : 
    2032            0 : bool EngravingItem::symIsValid(SymId id) const
    2033              : {
    2034            0 :     return score()->engravingFont()->isValid(id);
    2035              : }
    2036              : 
    2037              : //---------------------------------------------------------
    2038              : //   concertPitch
    2039              : //---------------------------------------------------------
    2040              : 
    2041      2314690 : bool EngravingItem::concertPitch() const
    2042              : {
    2043      2314690 :     return style().styleB(Sid::concertPitch);
    2044              : }
    2045              : 
    2046              : //---------------------------------------------------------
    2047              : //   nextElement
    2048              : //   selects the next score element
    2049              : //---------------------------------------------------------
    2050              : 
    2051            1 : EngravingItem* EngravingItem::nextElement()
    2052              : {
    2053            1 :     EngravingItem* e = score()->selection().element();
    2054            1 :     if (!e && !score()->selection().elements().empty()) {
    2055            0 :         e = score()->selection().elements().front();
    2056              :     }
    2057            1 :     if (e) {
    2058            1 :         switch (e->type()) {
    2059            0 :         case ElementType::SEGMENT: {
    2060            0 :             Segment* s = toSegment(e);
    2061            0 :             return s->nextElement(staffIdx());
    2062              :         }
    2063            0 :         case ElementType::MEASURE: {
    2064            0 :             Measure* m = toMeasure(e);
    2065            0 :             return m->nextElementStaff(staffIdx());
    2066              :         }
    2067            1 :         case ElementType::CLEF:
    2068              :         case ElementType::KEYSIG:
    2069              :         case ElementType::TIMESIG:
    2070              :         case ElementType::BAR_LINE:
    2071            1 :             return nextSegmentElement();
    2072            0 :         default: {
    2073            0 :             return e->layoutParent()->nextElement();
    2074              :         }
    2075              :         }
    2076              :     }
    2077            0 :     return nullptr;
    2078              : }
    2079              : 
    2080              : //---------------------------------------------------------
    2081              : //   prevElement
    2082              : //   selects the previous score element
    2083              : //---------------------------------------------------------
    2084              : 
    2085            0 : EngravingItem* EngravingItem::prevElement()
    2086              : {
    2087            0 :     EngravingItem* e = score()->selection().element();
    2088            0 :     if (!e && !score()->selection().elements().empty()) {
    2089            0 :         e = score()->selection().elements().back();
    2090              :     }
    2091            0 :     if (e) {
    2092            0 :         switch (e->type()) {
    2093            0 :         case ElementType::SEGMENT: {
    2094            0 :             Segment* s = toSegment(e);
    2095            0 :             return s->prevElement(staffIdx());
    2096              :         }
    2097            0 :         case ElementType::MEASURE: {
    2098            0 :             Measure* m = toMeasure(e);
    2099            0 :             return m->prevElementStaff(staffIdx());
    2100              :         }
    2101            0 :         case ElementType::CLEF:
    2102              :         case ElementType::KEYSIG:
    2103              :         case ElementType::TIMESIG:
    2104              :         case ElementType::BAR_LINE:
    2105            0 :             return prevSegmentElement();
    2106            0 :         default: {
    2107            0 :             return e->layoutParent()->prevElement();
    2108              :         }
    2109              :         }
    2110              :     }
    2111            0 :     return nullptr;
    2112              : }
    2113              : 
    2114              : //------------------------------------------------------------------------------------------
    2115              : //   nextSegmentElement
    2116              : //   This function is used in for the next-element command to navigate between main elements
    2117              : //   of segments. (Note, Rest, Clef, Time Signature, Key Signature, Barline, Ambitus, Breath, etc.)
    2118              : //   The default implementation is to look for the first such element. After it is found each
    2119              : //   element knows how to find the next one and overrides this method
    2120              : //------------------------------------------------------------------------------------------
    2121              : 
    2122            0 : EngravingItem* EngravingItem::nextSegmentElement()
    2123              : {
    2124            0 :     EngravingItem* p = this;
    2125            0 :     while (p) {
    2126            0 :         switch (p->type()) {
    2127            0 :         case ElementType::NOTE:
    2128            0 :             if (toNote(p)->chord()->isGrace()) {
    2129            0 :                 break;
    2130              :             }
    2131            0 :             return p;
    2132            0 :         case ElementType::REST:
    2133              :         case ElementType::MMREST:
    2134            0 :             return p;
    2135            0 :         case ElementType::CHORD: {
    2136            0 :             Chord* c = toChord(p);
    2137            0 :             if (!c->isGrace()) {
    2138            0 :                 return c->notes().back();
    2139              :             }
    2140              :         }
    2141            0 :         break;
    2142            0 :         case ElementType::SEGMENT: {
    2143            0 :             Segment* s = toSegment(p);
    2144            0 :             return s->firstElementForNavigation(staffIdx());
    2145              :         }
    2146            0 :         case ElementType::MEASURE: {
    2147            0 :             Measure* m = toMeasure(p);
    2148            0 :             return m->nextElementStaff(staffIdx());
    2149              :         }
    2150            0 :         case ElementType::SYSTEM: {
    2151            0 :             System* sys = toSystem(p);
    2152            0 :             return sys->nextSegmentElement();
    2153              :         }
    2154            0 :         default:
    2155            0 :             break;
    2156              :         }
    2157            0 :         p = p->layoutParent();
    2158              :     }
    2159            0 :     return Navigation::firstElement(score());
    2160              : }
    2161              : 
    2162              : //------------------------------------------------------------------------------------------
    2163              : //   prevSegmentElement
    2164              : //   This function is used in for the prev-element command to navigate between main elements
    2165              : //   of segments. (Note, Rest, Clef, Time Signature, Key Signature, Barline, Ambitus, Breath, etc.)
    2166              : //   The default implementation is to look for the first such element. After it is found each
    2167              : //   element knows how to find the previous one and overrides this method
    2168              : //------------------------------------------------------------------------------------------
    2169              : 
    2170            0 : EngravingItem* EngravingItem::prevSegmentElement()
    2171              : {
    2172            0 :     EngravingItem* p = this;
    2173            0 :     while (p) {
    2174            0 :         switch (p->type()) {
    2175            0 :         case ElementType::NOTE:
    2176            0 :             if (toNote(p)->chord()->isGrace()) {
    2177            0 :                 break;
    2178              :             }
    2179            0 :             return p;
    2180            0 :         case ElementType::REST:
    2181              :         case ElementType::MMREST:
    2182            0 :             return p;
    2183            0 :         case ElementType::CHORD: {
    2184            0 :             Chord* c = toChord(p);
    2185            0 :             if (!c->isGrace()) {
    2186            0 :                 return c->notes().front();
    2187              :             }
    2188              :         }
    2189            0 :         break;
    2190            0 :         case ElementType::SEGMENT: {
    2191            0 :             Segment* s = toSegment(p);
    2192            0 :             return s->lastElementForNavigation(staffIdx());
    2193              :         }
    2194            0 :         case ElementType::MEASURE: {
    2195            0 :             Measure* m = toMeasure(p);
    2196            0 :             return m->prevElementStaff(staffIdx());
    2197              :         }
    2198            0 :         case ElementType::SYSTEM: {
    2199            0 :             System* sys = toSystem(p);
    2200            0 :             return sys->prevSegmentElement();
    2201              :         }
    2202            0 :         default:
    2203            0 :             break;
    2204              :         }
    2205            0 :         p = p->layoutParent();
    2206              :     }
    2207            0 :     return Navigation::firstElement(score());
    2208              : }
    2209              : 
    2210              : #ifndef ENGRAVING_NO_ACCESSIBILITY
    2211         1994 : AccessibleItemPtr EngravingItem::accessible() const
    2212              : {
    2213         1994 :     return m_accessible;
    2214              : }
    2215              : 
    2216              : #endif
    2217              : 
    2218            0 : String EngravingItem::accessibleInfo() const
    2219              : {
    2220            0 :     return EngravingItem::translatedTypeUserName();
    2221              : }
    2222              : 
    2223              : //---------------------------------------------------------
    2224              : //   nextGrip
    2225              : //---------------------------------------------------------
    2226              : 
    2227            0 : bool EngravingItem::nextGrip(EditData& ed) const
    2228              : {
    2229            0 :     int i = int(ed.curGrip) + 1;
    2230            0 :     if (i >= ed.grips) {
    2231            0 :         ed.curGrip = Grip(0);
    2232            0 :         return false;
    2233              :     }
    2234            0 :     ed.curGrip = Grip(i);
    2235            0 :     return true;
    2236              : }
    2237              : 
    2238              : //---------------------------------------------------------
    2239              : //   prevGrip
    2240              : //---------------------------------------------------------
    2241              : 
    2242            0 : bool EngravingItem::prevGrip(EditData& ed) const
    2243              : {
    2244            0 :     int i = int(ed.curGrip) - 1;
    2245            0 :     if (i < 0) {
    2246            0 :         ed.curGrip = Grip(ed.grips - 1);
    2247            0 :         return false;
    2248              :     }
    2249            0 :     ed.curGrip = Grip(i);
    2250            0 :     return true;
    2251              : }
    2252              : 
    2253              : //---------------------------------------------------------
    2254              : //   isUserModified
    2255              : //    Check if this element was modified by user and
    2256              : //    therefore must be saved.
    2257              : //---------------------------------------------------------
    2258              : 
    2259        17958 : bool EngravingItem::isUserModified() const
    2260              : {
    2261        34714 :     for (const StyledProperty& spp : *styledProperties()) {
    2262        16756 :         Pid pid = spp.pid;
    2263        16756 :         PropertyValue val = getProperty(pid);
    2264        16756 :         PropertyValue defaultValue = propertyDefault(pid);
    2265              : 
    2266        16756 :         if (propertyType(pid) == P_TYPE::ABSOLUTE) {
    2267            0 :             if (std::abs(val.value<double>() - defaultValue.value<double>()) > 0.0001) {         // we don’t care spatium diffs that small
    2268            0 :                 return true;
    2269              :             }
    2270              :         } else {
    2271        16756 :             if (getProperty(pid) != propertyDefault(pid)) {
    2272            0 :                 return true;
    2273              :             }
    2274              :         }
    2275        16756 :     }
    2276       107734 :     for (Pid p : { Pid::VISIBLE, Pid::OFFSET, Pid::COLOR, Pid::Z, Pid::AUTOPLACE }) {
    2277        89779 :         if (getProperty(p) != propertyDefault(p)) {
    2278            3 :             return true;
    2279              :         }
    2280              :     }
    2281        17955 :     return false;
    2282              : }
    2283              : 
    2284              : //---------------------------------------------------------
    2285              : //   triggerLayout
    2286              : //---------------------------------------------------------
    2287              : 
    2288      1274684 : void EngravingItem::triggerLayout() const
    2289              : {
    2290      1274684 :     if (isInScoreTree()) {
    2291      1014885 :         score()->setLayout(tick(), staffIdx(), this);
    2292              :     }
    2293      1274684 : }
    2294              : 
    2295              : //----------------------------------------------------------------------
    2296              : //   triggerLayoutAll
    2297              : //
    2298              : //   *************************** CAUTION *******************************
    2299              : //   This causes a layout of the entire score: extremely expensive and
    2300              : //   likely unnecessary! Consider overriding triggerLayout() instead.
    2301              : //----------------------------------------------------------------------
    2302              : 
    2303         4216 : void EngravingItem::triggerLayoutAll() const
    2304              : {
    2305         4216 :     if (isInScoreTree()) {
    2306         2251 :         score()->setLayoutAll(staffIdx(), this);
    2307              :     }
    2308         4216 : }
    2309              : 
    2310         1090 : void EngravingItem::triggerLayoutToEnd() const
    2311              : {
    2312         1090 :     if (isInScoreTree()) {
    2313         1090 :         score()->setLayout(tick(), score()->endTick(), staffIdx(), staffIdx(), this);
    2314              :     }
    2315         1090 : }
    2316              : 
    2317              : //---------------------------------------------------------
    2318              : //   addData
    2319              : //---------------------------------------------------------
    2320              : 
    2321            5 : void EditData::addData(std::shared_ptr<ElementEditData> ed)
    2322              : {
    2323            5 :     m_data.push_back(ed);
    2324            5 : }
    2325              : 
    2326              : //---------------------------------------------------------
    2327              : //   startDrag
    2328              : //---------------------------------------------------------
    2329              : 
    2330            0 : void EngravingItem::startDrag(EditData& ed)
    2331              : {
    2332            0 :     if (!isMovable()) {
    2333            0 :         return;
    2334              :     }
    2335            0 :     std::shared_ptr<ElementEditData> eed = std::make_shared<ElementEditData>();
    2336            0 :     eed->e = this;
    2337            0 :     eed->pushProperty(Pid::OFFSET);
    2338            0 :     eed->pushProperty(Pid::AUTOPLACE);
    2339            0 :     ed.addData(eed);
    2340            0 :     if (ed.modifiers & AltModifier) {
    2341            0 :         setAutoplace(false);
    2342              :     }
    2343            0 : }
    2344              : 
    2345              : //---------------------------------------------------------
    2346              : //   drag
    2347              : ///   Return update Rect relative to canvas.
    2348              : //---------------------------------------------------------
    2349              : 
    2350            0 : RectF EngravingItem::drag(EditData& ed)
    2351              : {
    2352            0 :     if (!isMovable()) {
    2353            0 :         return RectF();
    2354              :     }
    2355              : 
    2356            0 :     const RectF r0(canvasBoundingRect());
    2357              : 
    2358            0 :     const PointF offset0 = ed.evtDelta + offset();
    2359            0 :     double x = offset0.x();
    2360            0 :     double y = offset0.y();
    2361              : 
    2362            0 :     double _spatium = spatium();
    2363            0 :     if (ed.hRaster) {
    2364            0 :         double hRaster = _spatium / MScore::hRaster();
    2365            0 :         int n = lrint(x / hRaster);
    2366            0 :         x = hRaster * n;
    2367              :     }
    2368            0 :     if (ed.vRaster) {
    2369            0 :         double vRaster = _spatium / MScore::vRaster();
    2370            0 :         int n = lrint(y / vRaster);
    2371            0 :         y = vRaster * n;
    2372              :     }
    2373              : 
    2374            0 :     setOffset(PointF(x, y));
    2375              : //      setGenerated(false);
    2376              : 
    2377            0 :     if (isTextBase()) {           // TODO: check for other types
    2378              :         //
    2379              :         // restrict move to page boundaries
    2380              :         //
    2381            0 :         const RectF r(canvasBoundingRect());
    2382            0 :         Page* p = 0;
    2383            0 :         EngravingItem* e = this;
    2384            0 :         while (e) {
    2385            0 :             if (e->isPage()) {
    2386            0 :                 p = toPage(e);
    2387            0 :                 break;
    2388              :             }
    2389            0 :             e = e->layoutParent();
    2390              :         }
    2391            0 :         if (p) {
    2392            0 :             bool move = false;
    2393            0 :             RectF pr(p->canvasBoundingRect());
    2394            0 :             if (r.right() > pr.right()) {
    2395            0 :                 x -= r.right() - pr.right();
    2396            0 :                 move = true;
    2397            0 :             } else if (r.left() < pr.left()) {
    2398            0 :                 x += pr.left() - r.left();
    2399            0 :                 move = true;
    2400              :             }
    2401            0 :             if (r.bottom() > pr.bottom()) {
    2402            0 :                 y -= r.bottom() - pr.bottom();
    2403            0 :                 move = true;
    2404            0 :             } else if (r.top() < pr.top()) {
    2405            0 :                 y += pr.top() - r.top();
    2406            0 :                 move = true;
    2407              :             }
    2408            0 :             if (move) {
    2409            0 :                 setOffset(PointF(x, y));
    2410              :             }
    2411              :         }
    2412              :     }
    2413            0 :     return canvasBoundingRect().united(r0);
    2414              : }
    2415              : 
    2416              : //---------------------------------------------------------
    2417              : //   endDrag
    2418              : //---------------------------------------------------------
    2419              : 
    2420            0 : void EngravingItem::endDrag(EditData& ed)
    2421              : {
    2422            0 :     if (!isMovable()) {
    2423            0 :         return;
    2424              :     }
    2425            0 :     ElementEditDataPtr eed = ed.getData(this);
    2426            0 :     if (!eed) {
    2427            0 :         return;
    2428              :     }
    2429            0 :     for (const auto& [id, v, f] : eed->propertyData) {
    2430            0 :         setPropertyFlags(id, f);     // reset initial property flags state
    2431            0 :         score()->undoPropertyChanged(this, id, v,
    2432            0 :                                      f == PropertyFlags::STYLED ? PropertyFlags::UNSTYLED : f);
    2433            0 :         setGenerated(false);
    2434              :     }
    2435            0 :     score()->hideAnchors();
    2436            0 : }
    2437              : 
    2438              : //---------------------------------------------------------
    2439              : //   genericDragAnchorLines
    2440              : //---------------------------------------------------------
    2441              : 
    2442            0 : std::vector<LineF> EngravingItem::genericDragAnchorLines() const
    2443              : {
    2444            0 :     double xp = 0.0;
    2445            0 :     for (EngravingItem* e = layoutParent(); e; e = e->layoutParent()) {
    2446            0 :         xp += e->x();
    2447              :     }
    2448              :     double yp;
    2449            0 :     if (ownershipParent()->isSegment() || ownershipParent()->isMeasure()) {
    2450            0 :         Measure* meas = ownershipParent()->isSegment() ? toSegment(ownershipParent())->measure() : toMeasure(ownershipParent());
    2451            0 :         System* system = meas->system();
    2452            0 :         const staff_idx_t stIdx = effectiveStaffIdx();
    2453            0 :         if (stIdx == muse::nidx) {
    2454            0 :             return { LineF() };
    2455              :         }
    2456            0 :         yp = system ? system->staffCanvasYpage(stIdx) : 0.0;
    2457            0 :         if (placement() == PlacementV::BELOW) {
    2458            0 :             yp += system ? system->staff(stIdx)->bbox().height() : 0.0;
    2459              :         }
    2460              :         //adjust anchor Y positions to staffType offset
    2461            0 :         if (staff()) {
    2462            0 :             yp += staff()->staffTypeForElement(this)->yoffset().val() * spatium();
    2463              :         }
    2464              :     } else {
    2465            0 :         yp = layoutParent()->canvasPos().y();
    2466              :     }
    2467            0 :     PointF p1(xp, yp);
    2468            0 :     LineF anchorLine(p1, canvasPos());
    2469            0 :     return { anchorLine };
    2470              : }
    2471              : 
    2472              : //---------------------------------------------------------
    2473              : //   updateGrips
    2474              : //---------------------------------------------------------
    2475              : 
    2476            0 : void EngravingItem::updateGrips(EditData& ed) const
    2477              : {
    2478            0 :     const auto positions(gripsPositions(ed));
    2479            0 :     const size_t ngrips = positions.size();
    2480            0 :     for (int i = 0; i < int(ngrips); ++i) {
    2481            0 :         ed.grip[i].translate(positions[i]);
    2482              :     }
    2483            0 : }
    2484              : 
    2485              : //---------------------------------------------------------
    2486              : //   startEdit
    2487              : //---------------------------------------------------------
    2488              : 
    2489            0 : void EngravingItem::startEdit(EditData& ed)
    2490              : {
    2491            0 :     std::shared_ptr<ElementEditData> elementData = std::make_shared<ElementEditData>();
    2492            0 :     elementData->e = this;
    2493            0 :     ed.addData(elementData);
    2494            0 : }
    2495              : 
    2496              : //---------------------------------------------------------
    2497              : //   isEditAllowed
    2498              : //---------------------------------------------------------
    2499              : 
    2500            0 : bool EngravingItem::isEditAllowed(EditData& ed) const
    2501              : {
    2502            0 :     return ed.key == Key_Home;
    2503              : }
    2504              : 
    2505              : //---------------------------------------------------------
    2506              : //   edit
    2507              : //    return true if event is accepted
    2508              : //---------------------------------------------------------
    2509              : 
    2510            0 : bool EngravingItem::edit(EditData& ed)
    2511              : {
    2512            0 :     if (ed.key == Key_Home) {
    2513            0 :         setOffset(PointF());
    2514            0 :         return true;
    2515              :     }
    2516            0 :     return false;
    2517              : }
    2518              : 
    2519              : //---------------------------------------------------------
    2520              : //   startDragGrip
    2521              : //---------------------------------------------------------
    2522              : 
    2523            0 : void EngravingItem::startDragGrip(EditData& ed)
    2524              : {
    2525            0 :     ElementEditDataPtr eed = ed.getData(this);
    2526            0 :     if (!eed) {
    2527            0 :         eed = std::make_shared<ElementEditData>();
    2528            0 :         eed->e = this;
    2529            0 :         ed.addData(eed);
    2530              :     }
    2531            0 :     eed->pushProperty(Pid::OFFSET);
    2532            0 :     eed->pushProperty(Pid::AUTOPLACE);
    2533            0 :     if (ed.modifiers & AltModifier) {
    2534            0 :         setAutoplace(false);
    2535              :     }
    2536            0 : }
    2537              : 
    2538              : //---------------------------------------------------------
    2539              : //   dragGrip
    2540              : //---------------------------------------------------------
    2541              : 
    2542            0 : void EngravingItem::dragGrip(EditData& ed)
    2543              : {
    2544            0 :     IF_ASSERT_FAILED(ed.curGrip != Grip::NO_GRIP) {
    2545            0 :         return;
    2546              :     }
    2547              : 
    2548            0 :     score()->addRefresh(canvasBoundingRect());
    2549            0 :     setOffset(offset() + ed.delta);
    2550            0 :     score()->addRefresh(canvasBoundingRect());
    2551              : }
    2552              : 
    2553              : //---------------------------------------------------------
    2554              : //   endDragGrip
    2555              : //---------------------------------------------------------
    2556              : 
    2557            0 : void EngravingItem::endDragGrip(EditData& ed)
    2558              : {
    2559            0 :     ElementEditDataPtr eed = ed.getData(this);
    2560            0 :     bool changed = false;
    2561            0 :     if (eed) {
    2562            0 :         for (const auto& [id, data, flags] : eed->propertyData) {
    2563            0 :             setPropertyFlags(id, flags);       // reset initial property flags state
    2564            0 :             PropertyFlags f = flags;
    2565            0 :             if (f == PropertyFlags::STYLED) {
    2566            0 :                 f = PropertyFlags::UNSTYLED;
    2567              :             }
    2568            0 :             if (score()->undoPropertyChanged(this, id, data, f)) {
    2569            0 :                 changed = true;
    2570              :             }
    2571              :         }
    2572            0 :         eed->propertyData.clear();
    2573              :     }
    2574            0 :     if (changed) {
    2575            0 :         undoChangeProperty(Pid::GENERATED, false);
    2576              :     }
    2577            0 :     score()->hideAnchors();
    2578            0 : }
    2579              : 
    2580              : //---------------------------------------------------------
    2581              : //   endEdit
    2582              : //---------------------------------------------------------
    2583              : 
    2584            0 : void EngravingItem::endEdit(EditData&)
    2585              : {
    2586            0 :     score()->hideAnchors();
    2587            0 : }
    2588              : 
    2589              : //---------------------------------------------------------
    2590              : //   styleP
    2591              : //---------------------------------------------------------
    2592              : 
    2593         7469 : double EngravingItem::styleP(Sid idx) const
    2594              : {
    2595         7469 :     return style().styleAbsolute(idx);
    2596              : }
    2597              : 
    2598          257 : void EngravingItem::setParenthesesMode(const ParenthesesMode& v, bool addToLinked, bool generated)
    2599              : {
    2600          257 :     setHasLeftParenthesis(v & ParenthesesMode::LEFT, addToLinked, generated);
    2601          257 :     setHasRightParenthesis(v & ParenthesesMode::RIGHT, addToLinked, generated);
    2602          257 : }
    2603              : 
    2604        53885 : ParenthesesMode EngravingItem::parenthesesMode() const
    2605              : {
    2606        53885 :     ParenthesesMode p = ParenthesesMode::NONE;
    2607        53885 :     if (m_leftParenthesis) {
    2608            0 :         p |= ParenthesesMode::LEFT;
    2609              :     }
    2610        53885 :     if (m_rightParenthesis) {
    2611            0 :         p |= ParenthesesMode::RIGHT;
    2612              :     }
    2613              : 
    2614        53885 :     return p;
    2615              : }
    2616              : 
    2617          257 : void EngravingItem::setHasLeftParenthesis(bool v, bool addToLinked, bool generated)
    2618              : {
    2619          257 :     const bool hasGeneratedParen = m_leftParenthesis && m_leftParenthesis->generated();
    2620          257 :     const bool hasUserParen = m_leftParenthesis && !m_leftParenthesis->generated();
    2621              : 
    2622          257 :     if (generated && v == hasGeneratedParen) {
    2623           64 :         return;
    2624              :     }
    2625              : 
    2626          193 :     if (!generated && v == hasUserParen) {
    2627          174 :         return;
    2628              :     }
    2629              : 
    2630           19 :     if (v) {
    2631           19 :         if (!m_leftParenthesis) {
    2632           19 :             Parenthesis* paren = Factory::createParenthesis(this);
    2633           19 :             paren->setTrack(track());
    2634           19 :             paren->setDirection(DirectionH::LEFT);
    2635           19 :             paren->setGenerated(generated);
    2636              : 
    2637           19 :             score()->undoAddElement(paren, addToLinked);
    2638              :         }
    2639              :     } else {
    2640            0 :         score()->undoRemoveElement(m_leftParenthesis, addToLinked);
    2641            0 :         assert(m_leftParenthesis == nullptr);
    2642              :     }
    2643              : }
    2644              : 
    2645          257 : void EngravingItem::setHasRightParenthesis(bool v, bool addToLinked, bool generated)
    2646              : {
    2647          257 :     const bool hasGeneratedParen = m_rightParenthesis && m_rightParenthesis->generated();
    2648          257 :     const bool hasUserParen = m_rightParenthesis && !m_rightParenthesis->generated();
    2649              : 
    2650          257 :     if (generated && v == hasGeneratedParen) {
    2651           64 :         return;
    2652              :     }
    2653              : 
    2654          193 :     if (!generated && v == hasUserParen) {
    2655          174 :         return;
    2656              :     }
    2657              : 
    2658           19 :     if (v) {
    2659           19 :         if (!m_rightParenthesis) {
    2660           19 :             Parenthesis* paren = Factory::createParenthesis(this);
    2661           19 :             paren->setTrack(track());
    2662           19 :             paren->setDirection(DirectionH::RIGHT);
    2663           19 :             paren->setGenerated(generated);
    2664              : 
    2665           19 :             score()->undoAddElement(paren, addToLinked);
    2666              :         }
    2667              :     } else {
    2668            0 :         score()->undoRemoveElement(m_rightParenthesis, addToLinked);
    2669            0 :         assert(m_rightParenthesis == nullptr);
    2670              :     }
    2671              : }
    2672              : 
    2673           35 : EngravingItem::BarBeat EngravingItem::barbeat() const
    2674              : {
    2675           35 :     EngravingItem::BarBeat barBeat = { 0, 0, 0.0F };
    2676           35 :     const EngravingItem* parent = this;
    2677           82 :     while (parent && !parent->isSegment() && !parent->isMeasure()) {
    2678           47 :         parent = parent->layoutParent();
    2679              :     }
    2680              : 
    2681           35 :     if (!parent) {
    2682            0 :         return barBeat;
    2683              :     }
    2684              : 
    2685           35 :     int bar = 0;
    2686           35 :     int displayedBar = 0;
    2687           35 :     int beat = 0;
    2688           35 :     int ticks = 0;
    2689           35 :     const Measure* measure = nullptr;
    2690              : 
    2691           35 :     const TimeSigMap* timeSigMap = score()->sigmap();
    2692           35 :     int ticksB = ticks_beat(timeSigMap->timesig(0).timesig().denominator());
    2693              : 
    2694           35 :     if (parent->isSegment()) {
    2695           35 :         const Segment* segment = toSegment(parent);
    2696           35 :         timeSigMap->tickValues(segment->tick().ticks(), &bar, &beat, &ticks);
    2697           35 :         ticksB = ticks_beat(timeSigMap->timesig(segment->tick().ticks()).timesig().denominator());
    2698           35 :         measure = segment->findMeasure();
    2699           35 :         if (measure) {
    2700           35 :             displayedBar = measure->measureNumber();
    2701              :         }
    2702            0 :     } else if (parent->isMeasure()) {
    2703            0 :         measure = toMeasure(parent);
    2704            0 :         bar = measure->measureNumber();
    2705            0 :         displayedBar = bar;
    2706            0 :         beat = -1;
    2707            0 :         ticks = 0;
    2708              :     }
    2709              : 
    2710           35 :     barBeat = { bar + 1, displayedBar + 1, beat + 1 + ticks / static_cast<float>(ticksB) };
    2711           35 :     return barBeat;
    2712              : }
    2713              : 
    2714           32 : EngravingItem* EngravingItem::findLinkedInScore(const Score* score) const
    2715              : {
    2716           32 :     if (!score || !staff() || !links() || links()->empty()) {
    2717           16 :         return nullptr;
    2718              :     }
    2719              : 
    2720              :     // If this is a system element it may not be on the same stave, so just check if linked elements are in the score
    2721           16 :     if (systemFlag() && track() == 0) {
    2722            0 :         for (EngravingObject* linked : *links()) {
    2723            0 :             if (linked != this && linked->score() == score) {
    2724            0 :                 return toEngravingItem(linked);
    2725              :             }
    2726              :         }
    2727            0 :         return nullptr;
    2728              :     }
    2729              : 
    2730           16 :     Staff* linkedStaffInScore = staff()->findLinkedInScore(score);
    2731              : 
    2732           16 :     if (!linkedStaffInScore) {
    2733           12 :         return nullptr;
    2734              :     }
    2735              : 
    2736            4 :     return findLinkedInStaff(linkedStaffInScore);
    2737              : }
    2738              : 
    2739         1643 : EngravingItem* EngravingItem::findLinkedInStaff(const Staff* staff) const
    2740              : {
    2741         1643 :     if (!links() || links()->empty()) {
    2742          517 :         return nullptr;
    2743              :     }
    2744              : 
    2745         2296 :     for (EngravingObject* linked : *links()) {
    2746         2295 :         if (toEngravingItem(linked)->staff() == staff) {
    2747         1125 :             return toEngravingItem(linked);
    2748              :         }
    2749              :     }
    2750            1 :     return nullptr;
    2751              : }
    2752              : 
    2753        48676 : bool EngravingItem::selected() const
    2754              : {
    2755        48676 :     return flag(ElementFlag::SELECTED);
    2756              : }
    2757              : 
    2758       327650 : void EngravingItem::setSelected(bool f)
    2759              : {
    2760       327650 :     setFlag(ElementFlag::SELECTED, f);
    2761       327650 : }
    2762              : 
    2763      1371506 : void EngravingItem::setVisible(bool f)
    2764              : {
    2765      1371506 :     setFlag(ElementFlag::INVISIBLE, !f);
    2766      1371506 :     if (m_leftParenthesis) {
    2767            0 :         m_leftParenthesis->setVisible(f);
    2768              :     }
    2769      1371506 :     if (m_rightParenthesis) {
    2770            0 :         m_rightParenthesis->setVisible(f);
    2771              :     }
    2772      1371506 : }
    2773              : 
    2774              : #ifndef ENGRAVING_NO_ACCESSIBILITY
    2775         1990 : void EngravingItem::initAccessibleIfNeed()
    2776              : {
    2777         1990 :     if (!contextConfiguration() || !contextConfiguration()->isAccessibleEnabled()) {
    2778         1990 :         return;
    2779              :     }
    2780              : 
    2781            0 :     if (!m_accessibleEnabled) {
    2782            0 :         return;
    2783              :     }
    2784              : 
    2785            0 :     doInitAccessible();
    2786              : }
    2787              : 
    2788            0 : void EngravingItem::doInitAccessible()
    2789              : {
    2790            0 :     EngravingItemList parents;
    2791            0 :     EngravingItem* parent = accessibleParentItem();
    2792            0 :     while (parent) {
    2793            0 :         parents.push_back(parent);
    2794            0 :         parent = parent->accessibleParentItem();
    2795              :     }
    2796              : 
    2797            0 :     for (auto it = parents.rbegin(); it != parents.rend(); ++it) {
    2798            0 :         (*it)->setupAccessible();
    2799              :     }
    2800              : 
    2801            0 :     setupAccessible();
    2802            0 : }
    2803              : 
    2804              : #endif // ENGRAVING_NO_ACCESSIBILITY
    2805              : 
    2806            0 : String EngravingItem::formatBarsAndBeats() const
    2807              : {
    2808            0 :     String result;
    2809            0 :     EngravingItem::BarBeat barbeat = this->barbeat();
    2810              : 
    2811            0 :     if (barbeat.bar != 0) {
    2812            0 :         result = muse::mtrc("engraving", "Measure: %1").arg(barbeat.bar);
    2813              : 
    2814            0 :         if (barbeat.displayedBar != barbeat.bar) {
    2815            0 :             result += u"; " + muse::mtrc("engraving", "Displayed measure: %1").arg(barbeat.displayedBar);
    2816              :         }
    2817              : 
    2818            0 :         if (!muse::RealIsNull(barbeat.beat)) {
    2819            0 :             result += u"; " + muse::mtrc("engraving", "Beat: %1").arg(barbeat.beat);
    2820              :         }
    2821              :     }
    2822              : 
    2823            0 :     return result;
    2824            0 : }
    2825              : 
    2826           19 : bool EngravingItem::isPropertyLinkedToMaster(Pid id) const
    2827              : {
    2828           19 :     if (propertyGroup(id) == PropertyGroup::POSITION) {
    2829            0 :         return isPositionLinkedToMaster();
    2830              :     }
    2831              : 
    2832           19 :     if (propertyGroup(id) == PropertyGroup::APPEARANCE) {
    2833           19 :         return isAppearanceLinkedToMaster();
    2834              :     }
    2835              : 
    2836            0 :     return true;
    2837              : }
    2838              : 
    2839            0 : bool EngravingItem::isUnlinkedFromMaster() const
    2840              : {
    2841            0 :     return !(getProperty(Pid::POSITION_LINKED_TO_MASTER).toBool()
    2842            0 :              && getProperty(Pid::APPEARANCE_LINKED_TO_MASTER).toBool());
    2843              : }
    2844              : 
    2845           34 : void EngravingItem::unlinkPropertyFromMaster(Pid id)
    2846              : {
    2847           34 :     if (propertyGroup(id) == PropertyGroup::POSITION) {
    2848            1 :         score()->undo(new ChangeProperty(this, Pid::POSITION_LINKED_TO_MASTER, false));
    2849           33 :     } else if (propertyGroup(id) == PropertyGroup::APPEARANCE) {
    2850           33 :         score()->undo(new ChangeProperty(this, Pid::APPEARANCE_LINKED_TO_MASTER, false));
    2851              :     }
    2852           34 : }
    2853              : 
    2854       201263 : EngravingItem::LayoutData* EngravingItem::createLayoutData() const
    2855              : {
    2856       201263 :     return new EngravingItem::LayoutData();
    2857              : }
    2858              : 
    2859     35069432 : const EngravingItem::LayoutData* EngravingItem::ldata() const
    2860              : {
    2861     35069432 :     return ldataInternal();
    2862              : }
    2863              : 
    2864      5892085 : EngravingItem::LayoutData* EngravingItem::mutldata()
    2865              : {
    2866      5892085 :     return mutldataInternal();
    2867              : }
    2868              : 
    2869     35069432 : const EngravingItem::LayoutData* EngravingItem::ldataInternal() const
    2870              : {
    2871     35069432 :     if (!m_layoutData) {
    2872       343775 :         m_layoutData = createLayoutData();
    2873       343775 :         m_layoutData->m_item = this;
    2874              :     }
    2875     35069432 :     return m_layoutData;
    2876              : }
    2877              : 
    2878      5892085 : EngravingItem::LayoutData* EngravingItem::mutldataInternal()
    2879              : {
    2880      5892085 :     if (!m_layoutData) {
    2881       326451 :         m_layoutData = createLayoutData();
    2882       326451 :         m_layoutData->m_item = this;
    2883              :     }
    2884      5892085 :     return m_layoutData;
    2885              : }
    2886              : 
    2887           67 : bool EngravingItem::elementAppliesToTrack(const track_idx_t elementTrack, const track_idx_t refTrack,
    2888              :                                           const VoiceAssignment voiceAssignment, const Part* part)
    2889              : {
    2890           67 :     if (voiceAssignment == VoiceAssignment::CURRENT_VOICE_ONLY && elementTrack == refTrack) {
    2891            3 :         return true;
    2892              :     }
    2893              : 
    2894           64 :     if (voiceAssignment == VoiceAssignment::ALL_VOICE_IN_STAFF && track2staff(elementTrack) == track2staff(refTrack)) {
    2895            0 :         return true;
    2896              :     }
    2897              : 
    2898           64 :     if (!part) {
    2899            0 :         return false;
    2900              :     }
    2901              : 
    2902           64 :     if (voiceAssignment == VoiceAssignment::ALL_VOICE_IN_INSTRUMENT) {
    2903           49 :         const TrackRange range = part->trackRange();
    2904           49 :         if (range.startTrack <= refTrack && range.endTrack - 1 >= refTrack) {
    2905           10 :             return true;
    2906              :         }
    2907              :     }
    2908           54 :     return false;
    2909              : }
    2910              : 
    2911      1864695 : void EngravingItem::LayoutData::setBbox(const RectF& r)
    2912              : {
    2913      1864695 :     DO_ASSERT(!std::isnan(r.x()) && !std::isinf(r.x()));
    2914      1864695 :     DO_ASSERT(!std::isnan(r.y()) && !std::isinf(r.y()));
    2915      1864695 :     DO_ASSERT(!std::isnan(r.width()) && !std::isinf(r.width()));
    2916      1864695 :     DO_ASSERT(!std::isnan(r.height()) && !std::isinf(r.height()));
    2917              : 
    2918              :     //DO_ASSERT(!isShapeComposite());
    2919      1864695 :     m_shape.set_value(Shape(r, m_item, Shape::Type::Fixed));
    2920      1864695 : }
    2921              : 
    2922          137 : void EngravingItem::LayoutData::connectItemSnappedBefore(EngravingItem* itemBefore)
    2923              : {
    2924          137 :     IF_ASSERT_FAILED(itemBefore && itemBefore != m_item && itemBefore != m_itemSnappedAfter
    2925              :                      && itemBefore->ldata()->itemSnappedBefore() != m_item) {
    2926            0 :         return;
    2927              :     }
    2928          137 :     m_itemSnappedBefore = itemBefore;
    2929          137 :     itemBefore->mutldata()->m_itemSnappedAfter = const_cast<EngravingItem*>(m_item);
    2930              : }
    2931              : 
    2932         2949 : void EngravingItem::LayoutData::disconnectItemSnappedBefore()
    2933              : {
    2934         2949 :     if (!m_itemSnappedBefore) {
    2935         2932 :         return;
    2936              :     }
    2937           17 :     m_itemSnappedBefore->mutldata()->m_itemSnappedAfter = nullptr;
    2938           17 :     m_itemSnappedBefore = nullptr;
    2939              : }
    2940              : 
    2941           73 : void EngravingItem::LayoutData::connectItemSnappedAfter(EngravingItem* itemAfter)
    2942              : {
    2943           73 :     IF_ASSERT_FAILED(itemAfter && itemAfter != m_item && itemAfter != m_itemSnappedBefore
    2944              :                      && itemAfter->ldata()->itemSnappedAfter() != m_item) {
    2945            0 :         return;
    2946              :     }
    2947           73 :     m_itemSnappedAfter = itemAfter;
    2948           73 :     itemAfter->mutldata()->m_itemSnappedBefore = const_cast<EngravingItem*>(m_item);
    2949              : }
    2950              : 
    2951         2949 : void EngravingItem::LayoutData::disconnectItemSnappedAfter()
    2952              : {
    2953         2949 :     if (!m_itemSnappedAfter) {
    2954         2910 :         return;
    2955              :     }
    2956           39 :     m_itemSnappedAfter->mutldata()->m_itemSnappedBefore = nullptr;
    2957           39 :     m_itemSnappedAfter = nullptr;
    2958              : }
    2959              : 
    2960         6121 : void EngravingItem::LayoutData::scanConnectedItemsInSnappingChain(const System* system,
    2961              :                                                                   const std::function<void(EngravingItem*)>& func) const
    2962              : {
    2963         6121 :     EngravingItem* item = const_cast<EngravingItem*>(m_item);
    2964         6121 :     func(item);
    2965              : 
    2966        12242 :     std::vector<EngravingItem*> visited = { item }; // To break out of circular snap chains
    2967              : 
    2968         6121 :     EngravingItem* snappedBefore = m_itemSnappedBefore;
    2969         6310 :     while (snappedBefore && snappedBefore->findAncestor(ElementType::SYSTEM) == system) {
    2970          189 :         IF_ASSERT_FAILED(!muse::contains(visited, snappedBefore)) {
    2971            0 :             break;
    2972              :         }
    2973          189 :         visited.push_back(snappedBefore);
    2974          189 :         func(snappedBefore);
    2975          189 :         snappedBefore = snappedBefore->ldata()->itemSnappedBefore();
    2976              :     }
    2977              : 
    2978         6121 :     EngravingItem* snappedAfter = m_itemSnappedAfter;
    2979         6524 :     while (snappedAfter && snappedAfter->findAncestor(ElementType::SYSTEM) == system) {
    2980          403 :         IF_ASSERT_FAILED(!muse::contains(visited, snappedAfter)) {
    2981            0 :             break;
    2982              :         }
    2983          403 :         visited.push_back(snappedAfter);
    2984          403 :         func(snappedAfter);
    2985          403 :         snappedAfter = snappedAfter->ldata()->itemSnappedAfter();
    2986              :     }
    2987         6121 : }
    2988              : 
    2989      3210371 : const RectF& EngravingItem::LayoutData::bbox(LD_ACCESS mode) const
    2990              : {
    2991              :     //! NOTE Temporary disabled CHECK - a lot of messages
    2992              :     UNUSED(mode);
    2993      3210371 :     const Shape& sh = m_shape.value(LD_ACCESS::MAYBE_NOTINITED);
    2994              : 
    2995              :     //! NOTE Temporary
    2996              :     {
    2997              :         static const RectF _dummy;
    2998              : 
    2999      3210371 :         switch (m_item->type()) {
    3000       190193 :         case ElementType::NOTE:
    3001              :         case ElementType::ORNAMENT:
    3002       190193 :             return !sh.elements().empty() ? sh.elements().at(0) : _dummy;
    3003      3020178 :         default:
    3004      3020178 :             break;
    3005              :         }
    3006              :     }
    3007              : 
    3008      3020178 :     return sh.bbox();
    3009              : }
    3010              : 
    3011      3087318 : Shape EngravingItem::LayoutData::shape(LD_ACCESS mode) const
    3012              : {
    3013              :     //! NOTE Temporary disabled CHECK - a lot of messages
    3014      3087318 :     Shape sh = m_shape.value(LD_ACCESS::MAYBE_NOTINITED);
    3015              : 
    3016      3087318 :     const Parenthesis* leftParen = m_item->leftParen();
    3017      3087318 :     if (leftParen && leftParen->addToSkyline()) {
    3018          118 :         sh.add(leftParen->ldata()->shape().translated(leftParen->pos()));
    3019              :     }
    3020      3087318 :     const Parenthesis* rightParen = m_item->rightParen();
    3021      3087318 :     if (rightParen && rightParen->addToSkyline()) {
    3022          118 :         sh.add(rightParen->ldata()->shape().translated(rightParen->pos()));
    3023              :     }
    3024              : 
    3025              :     //! NOTE Temporary
    3026              :     //! Reimplementation: done
    3027              :     {
    3028      3087318 :         switch (m_item->type()) {
    3029      1171811 :         case ElementType::BEAM:
    3030              :         case ElementType::GRACE_NOTES_GROUP:
    3031              :         case ElementType::ORNAMENT:
    3032              :         case ElementType::OTTAVA_SEGMENT:
    3033              :         case ElementType::VOLTA_SEGMENT:
    3034              :         case ElementType::PEDAL_SEGMENT:
    3035              :         case ElementType::TEXTLINE_SEGMENT:
    3036              :         case ElementType::HARMONIC_MARK_SEGMENT:
    3037              :         case ElementType::PALM_MUTE_SEGMENT:
    3038              :         case ElementType::LET_RING_SEGMENT:
    3039              :         case ElementType::GRADUAL_TEMPO_CHANGE_SEGMENT:
    3040              :         case ElementType::RASGUEADO_SEGMENT:
    3041              :         case ElementType::WHAMMY_BAR_SEGMENT:
    3042              :         case ElementType::SLUR_SEGMENT:
    3043              :         case ElementType::TIE_SEGMENT:
    3044              :         case ElementType::LAISSEZ_VIB_SEGMENT:
    3045              :         case ElementType::PARTIAL_TIE_SEGMENT:
    3046              :         case ElementType::NOTE:
    3047      1171811 :             return sh;
    3048       493520 :         case ElementType::CHORD:
    3049              :         case ElementType::REST:
    3050              :         case ElementType::MEASURE_REPEAT:
    3051              :         case ElementType::MMREST: {
    3052       493520 :             if (mode == LD_ACCESS::CHECK) {
    3053              :                 //! NOTE Temporary fix
    3054              :                 //! We can remove it the moment we figure out the layout order of the elements
    3055       489640 :                 LayoutContext ctx(m_item->score());
    3056       489640 :                 ChordRest::LayoutData* ldata = static_cast<ChordRest::LayoutData*>(const_cast<LayoutData*>(this));
    3057       489640 :                 ChordLayout::checkAndFillShape(toChordRest(m_item), ldata, ctx.conf());
    3058       489640 :             }
    3059       493520 :             return m_shape.value(LD_ACCESS::CHECK);
    3060              :         } break;
    3061         1323 :         case ElementType::GUITAR_BEND_SEGMENT: {
    3062              :             //! NOTE Temporary fix
    3063              :             //! We can remove it the moment we figure out the layout order of the elements
    3064         1323 :             TLayout::fillGuitarBendSegmentShape(toGuitarBendSegment(m_item),
    3065              :                                                 static_cast<GuitarBendSegment::LayoutData*>(const_cast<LayoutData*>(this)));
    3066         1323 :             return m_shape.value(LD_ACCESS::CHECK);
    3067              :         } break;
    3068         1078 :         case ElementType::HAIRPIN_SEGMENT: {
    3069              :             //! To be removed when we're confident enough...
    3070         1078 :             IF_ASSERT_FAILED(m_shape.has_value()) {
    3071            0 :                 const_cast<LayoutData*>(this)->setShape(TLayout::recalculateTextLineBaseSegmentShape(toHairpinSegment(m_item)));
    3072              :             }
    3073         1078 :             return m_shape.value(LD_ACCESS::CHECK);
    3074              :         } break;
    3075          232 :         case ElementType::TRILL_SEGMENT: {
    3076              :             //! NOTE Temporary fix
    3077              :             //! We can remove it the moment we figure out the layout order of the elements
    3078          232 :             LayoutContext ctx(m_item->score());
    3079          232 :             TLayout::fillTrillSegmentShape(toTrillSegment(m_item),
    3080              :                                            static_cast<HairpinSegment::LayoutData*>(const_cast<LayoutData*>(this)),
    3081              :                                            ctx.conf());
    3082          232 :             return m_shape.value(LD_ACCESS::CHECK);
    3083          232 :         } break;
    3084         1829 :         case ElementType::TUPLET: {
    3085              :             //! NOTE Temporary fix
    3086              :             //! We can remove it the moment we figure out the layout order of the elements
    3087         1829 :             TLayout::fillTupletShape(toTuplet(m_item), static_cast<Tuplet::LayoutData*>(const_cast<LayoutData*>(this)));
    3088         1829 :             return m_shape.value(LD_ACCESS::CHECK);
    3089              :         } break;
    3090      1417525 :         default:
    3091      1417525 :             break;
    3092              :         }
    3093              :     }
    3094              : 
    3095      1417525 :     return sh;
    3096      3087318 : }
    3097              : 
    3098              : #ifndef NDEBUG
    3099      2783147 : void EngravingItem::LayoutData::doSetPosDebugHook(double x, double y)
    3100              : {
    3101              :     UNUSED(x);
    3102              :     UNUSED(y);
    3103      2783147 : }
    3104              : 
    3105       866162 : void EngravingItem::LayoutData::setWidthDebugHook(double w)
    3106              : {
    3107              :     UNUSED(w);
    3108       866162 : }
    3109              : 
    3110              : #endif
    3111              : 
    3112            0 : void EngravingItem::LayoutData::dump(std::stringstream& ss) const
    3113              : {
    3114            0 :     ss << "\n";
    3115            0 :     ss << m_item->typeName() << " id: " << m_item->eid().toStdString() << "\n";
    3116              : 
    3117            0 :     ss << "skip: " << (m_isSkipDraw ? "yes" : "no") << "\n";
    3118            0 :     ss << "mag: " << m_mag << "\n";
    3119              : 
    3120            0 :     ss << "pos: ";
    3121            0 :     mu::engraving::dump(m_pos, ss);
    3122            0 :     ss << "\n";
    3123              : 
    3124            0 :     ss << "shape: ";
    3125            0 :     mu::engraving::dump(m_shape, ss);
    3126            0 :     ss << "\n";
    3127              : 
    3128            0 :     supDump(ss);
    3129            0 : }
    3130              : 
    3131      5153192 : double EngravingItem::mag() const
    3132              : {
    3133      5153192 :     if (!ldata()) {
    3134              :         //LOGD() << "no layout data, will be returned default (1.0)";
    3135            0 :         return 1.0;
    3136              :     }
    3137      5153192 :     return ldata()->mag();
    3138              : }
    3139              : 
    3140       790936 : PointF EngravingItem::staffOffset() const
    3141              : {
    3142       790936 :     const StaffType* st = staffType();
    3143       790936 :     const double yOffset = st ? st->yoffset().val() * spatium() : 0.0;
    3144       790936 :     return PointF(0.0, yOffset);
    3145              : }
    3146              : }
        

Generated by: LCOV version 2.0-1