LCOV - code coverage report
Current view: top level - src/engraving/dom - measure.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 57.6 % 1864 1073
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 most part of class Measure.
      26              : */
      27              : 
      28              : #include "measure.h"
      29              : 
      30              : #include "../editing/editstaffbrackets.h"
      31              : #include "../editing/editclef.h"
      32              : #include "../editing/editkeysig.h"
      33              : #include "../editing/editmeasures.h"
      34              : #include "../editing/editmeasurerepeat.h"
      35              : #include "../editing/editstaff.h"
      36              : #include "../editing/editstavesharing.h"
      37              : #include "../editing/editsystemlocks.h"
      38              : #include "../editing/edittimesig.h"
      39              : #include "../editing/inserttime.h"
      40              : #include "../editing/navigation.h"
      41              : #include "../editing/transaction/transaction.h"
      42              : 
      43              : #include "accidental.h"
      44              : #include "actionicon.h"
      45              : #include "anchors.h"
      46              : #include "barline.h"
      47              : #include "beam.h"
      48              : #include "bracket.h"
      49              : #include "bracketitem.h"
      50              : #include "chord.h"
      51              : #include "clef.h"
      52              : #include "durationelement.h"
      53              : #include "factory.h"
      54              : #include "hook.h"
      55              : #include "key.h"
      56              : #include "keysig.h"
      57              : #include "linkedobjects.h"
      58              : #include "marker.h"
      59              : #include "masterscore.h"
      60              : #include "measure.h"
      61              : #include "measurenumber.h"
      62              : #include "measurerepeat.h"
      63              : #include "mmrestrange.h"
      64              : #include "note.h"
      65              : #include "page.h"
      66              : #include "part.h"
      67              : #include "pitchspelling.h"
      68              : #include "playcounttext.h"
      69              : #include "rest.h"
      70              : #include "score.h"
      71              : #include "segment.h"
      72              : #include "select.h"
      73              : #include "sig.h"
      74              : #include "spacer.h"
      75              : #include "staff.h"
      76              : #include "stafflines.h"
      77              : #include "stafftype.h"
      78              : #include "stafftypechange.h"
      79              : #include "stem.h"
      80              : #include "system.h"
      81              : #include "tie.h"
      82              : #include "tiemap.h"
      83              : #include "timesig.h"
      84              : #include "tremolosinglechord.h"
      85              : #include "tremolotwochord.h"
      86              : #include "tuplet.h"
      87              : #include "tupletmap.h"
      88              : #include "utils.h"
      89              : 
      90              : #ifndef ENGRAVING_NO_ACCESSIBILITY
      91              : #include "accessibility/accessibleitem.h"
      92              : #endif
      93              : 
      94              : #include "log.h"
      95              : 
      96              : using namespace mu;
      97              : using namespace mu::engraving;
      98              : 
      99              : namespace mu::engraving {
     100              : //---------------------------------------------------------
     101              : //   MStaff
     102              : ///   Per staff values of measure.
     103              : //---------------------------------------------------------
     104              : 
     105        26735 : MStaff::~MStaff()
     106              : {
     107        26735 :     delete m_measureNumber;
     108        26735 :     delete m_mmRangeText;
     109        26735 :     delete m_lines;
     110        26735 :     delete m_vspacerUp;
     111        26735 :     delete m_vspacerDown;
     112        26735 : }
     113              : 
     114            0 : MStaff::MStaff(const MStaff& m)
     115              : {
     116            0 :     m_measureNumber    = 0;
     117            0 :     m_mmRangeText = 0;
     118            0 :     m_lines     = Factory::copyStaffLines(*m.m_lines);
     119            0 :     m_hasVoices = m.m_hasVoices;
     120            0 :     m_vspacerUp = 0;
     121            0 :     m_vspacerDown = 0;
     122            0 :     m_hideIfEmpty = m.m_hideIfEmpty;
     123            0 :     m_visible   = m.m_visible;
     124            0 :     m_stemless  = m.m_stemless;
     125              : #ifndef NDEBUG
     126            0 :     m_corrupted = m.m_corrupted;
     127              : #endif
     128            0 :     m_measureRepeatCount = 0;
     129            0 : }
     130              : 
     131              : //---------------------------------------------------------
     132              : //   MStaff::setScore
     133              : //---------------------------------------------------------
     134              : 
     135            0 : void MStaff::setScore(Score* score)
     136              : {
     137            0 :     if (m_lines) {
     138            0 :         m_lines->setScore(score);
     139              :     }
     140            0 :     if (m_vspacerUp) {
     141            0 :         m_vspacerUp->setScore(score);
     142              :     }
     143            0 :     if (m_vspacerDown) {
     144            0 :         m_vspacerDown->setScore(score);
     145              :     }
     146            0 :     if (m_measureNumber) {
     147            0 :         m_measureNumber->setScore(score);
     148              :     }
     149            0 :     if (m_mmRangeText) {
     150            0 :         m_mmRangeText->setScore(score);
     151              :     }
     152            0 : }
     153              : 
     154              : //---------------------------------------------------------
     155              : //   setTrack
     156              : //---------------------------------------------------------
     157              : 
     158         3465 : void MStaff::setTrack(track_idx_t track)
     159              : {
     160         3465 :     if (m_lines) {
     161         3465 :         m_lines->setTrack(track);
     162              :     }
     163         3465 :     if (m_vspacerUp) {
     164            0 :         m_vspacerUp->setTrack(track);
     165              :     }
     166         3465 :     if (m_vspacerDown) {
     167            0 :         m_vspacerDown->setTrack(track);
     168              :     }
     169         3465 :     if (m_measureNumber) {
     170          114 :         m_measureNumber->setTrack(track);
     171              :     }
     172         3465 :     if (m_mmRangeText) {
     173            0 :         m_mmRangeText->setTrack(track);
     174              :     }
     175         3465 : }
     176              : 
     177              : //---------------------------------------------------------
     178              : //   Measure
     179              : //---------------------------------------------------------
     180              : 
     181        19702 : Measure::Measure(Score* parent)
     182        19702 :     : MeasureBase(ElementType::MEASURE, parent), m_timesig(4, 4)
     183              : {
     184        19702 :     setTicks(Fraction(4, 4));
     185        19702 :     m_repeatCount = 2;
     186              : 
     187        19702 :     size_t n = score()->nstaves();
     188        19702 :     m_mstaves.reserve(n);
     189        47349 :     for (staff_idx_t staffIdx = 0; staffIdx < n; ++staffIdx) {
     190        27647 :         MStaff* ms = new MStaff;
     191        27647 :         Staff* staff = score()->staff(staffIdx);
     192        27647 :         ms->setLines(Factory::createStaffLines(this));
     193        27647 :         ms->lines()->setTrack(staffIdx * VOICES);
     194        27647 :         ms->lines()->setVisible(!staff->isLinesInvisible(tick()));
     195        27647 :         m_mstaves.push_back(ms);
     196              :     }
     197        19702 :     setExcludeFromNumbering(false);
     198        19702 :     m_measureNumberMode     = MeasureNumberMode::AUTO;
     199        19702 :     m_userStretch           = 1.0;
     200        19702 :     m_breakMultiMeasureRest = false;
     201        19702 :     m_mmRest                = nullptr;
     202        19702 :     m_mmRestCount           = 0;
     203        19702 :     setFlag(ElementFlag::MOVABLE, false);
     204        19702 : }
     205              : 
     206              : //---------------------------------------------------------
     207              : //   measure
     208              : //---------------------------------------------------------
     209              : 
     210            0 : Measure::Measure(const Measure& m)
     211            0 :     : MeasureBase(m)
     212              : {
     213            0 :     m_segments    = m.m_segments.clone();
     214            0 :     m_timesig     = m.m_timesig;
     215            0 :     m_len         = m.m_len;
     216            0 :     m_repeatCount = m.m_repeatCount;
     217            0 :     m_measureNumber = m.m_measureNumber;
     218            0 :     m_measureNumberOffset = m.m_measureNumberOffset;
     219            0 :     m_measureNumberMode = m.m_measureNumberMode;
     220            0 :     m_userStretch = m.m_userStretch;
     221              : 
     222            0 :     m_mstaves.reserve(m.m_mstaves.size());
     223            0 :     for (MStaff* ms : m.m_mstaves) {
     224            0 :         m_mstaves.push_back(new MStaff(*ms));
     225              :     }
     226              : 
     227            0 :     m_breakMultiMeasureRest = m.m_breakMultiMeasureRest;
     228            0 :     m_mmRest                = m.m_mmRest;
     229            0 :     m_mmRestCount           = m.m_mmRestCount;
     230            0 :     m_playbackCount         = m.m_playbackCount;
     231            0 : }
     232              : 
     233              : //---------------------------------------------------------
     234              : //   createStaves
     235              : //---------------------------------------------------------
     236              : 
     237          887 : void Measure::createStaves(staff_idx_t staffIdx)
     238              : {
     239          887 :     for (staff_idx_t n = m_mstaves.size(); n <= staffIdx; ++n) {
     240            0 :         Staff* staff = score()->staff(n);
     241            0 :         MStaff* s    = new MStaff;
     242            0 :         s->setLines(Factory::createStaffLines(this));
     243            0 :         s->lines()->setTrack(n * VOICES);
     244            0 :         s->lines()->setVisible(!staff->isLinesInvisible(tick()));
     245            0 :         m_mstaves.push_back(s);
     246              :     }
     247          887 : }
     248              : 
     249              : //---------------------------------------------------------
     250              : //   setScore
     251              : //---------------------------------------------------------
     252              : 
     253            0 : void Measure::setScore(Score* score)
     254              : {
     255            0 :     MeasureBase::setScore(score);
     256            0 :     for (Segment* s = first(); s; s = s->next()) {
     257            0 :         s->setScore(score);
     258              :     }
     259            0 : }
     260              : 
     261      1172708 : MStaff* Measure::mstaff(staff_idx_t staffIndex) const
     262              : {
     263      1172708 :     if (staffIndex < m_mstaves.size()) {
     264      1171087 :         return m_mstaves[staffIndex];
     265              :     }
     266              : 
     267         1621 :     return nullptr;
     268              : }
     269              : 
     270       256899 : bool Measure::hasVoices(staff_idx_t staffIdx) const
     271              : {
     272       256899 :     MStaff* staff = mstaff(staffIdx);
     273       256899 :     return staff ? staff->hasVoices() : false;
     274              : }
     275              : 
     276         5391 : void Measure::setHasVoices(staff_idx_t staffIdx, bool v)
     277              : {
     278         5391 :     MStaff* staff = mstaff(staffIdx);
     279              : 
     280         5391 :     if (staff) {
     281         3770 :         staff->setHasVoices(v);
     282              :     }
     283         5391 : }
     284              : 
     285       560525 : StaffLines* Measure::staffLines(staff_idx_t staffIdx) const
     286              : {
     287       560525 :     MStaff* staff = mstaff(staffIdx);
     288              : 
     289       560525 :     return staff ? staff->lines() : nullptr;
     290              : }
     291              : 
     292       165647 : Spacer* Measure::vspacerDown(staff_idx_t staffIdx) const
     293              : {
     294       165647 :     MStaff* staff = mstaff(staffIdx);
     295              : 
     296       165647 :     return staff ? staff->vspacerDown() : nullptr;
     297              : }
     298              : 
     299       156866 : Spacer* Measure::vspacerUp(staff_idx_t staffIdx) const
     300              : {
     301       156866 :     MStaff* staff = mstaff(staffIdx);
     302              : 
     303       156866 :     return staff ? staff->vspacerUp() : nullptr;
     304              : }
     305              : 
     306           21 : void Measure::setStaffVisible(staff_idx_t staffIdx, bool visible)
     307              : {
     308           21 :     MStaff* staff = mstaff(staffIdx);
     309              : 
     310           21 :     if (staff) {
     311           21 :         staff->setVisible(visible);
     312              :     }
     313           21 : }
     314              : 
     315          945 : void Measure::setStaffStemless(staff_idx_t staffIdx, bool stemless)
     316              : {
     317          945 :     MStaff* staff = mstaff(staffIdx);
     318              : 
     319          945 :     if (staff) {
     320          945 :         staff->setStemless(stemless);
     321              :     }
     322          945 : }
     323              : 
     324        26414 : AutoOnOff Measure::hideStaffIfEmpty(staff_idx_t staffIdx) const
     325              : {
     326        26414 :     MStaff* staff = mstaff(staffIdx);
     327              : 
     328        26414 :     return staff ? staff->hideIfEmpty() : AutoOnOff::AUTO;
     329              : }
     330              : 
     331            0 : void Measure::setHideStaffIfEmpty(staff_idx_t staffIdx, AutoOnOff hideIfEmpty)
     332              : {
     333            0 :     MStaff* staff = mstaff(staffIdx);
     334            0 :     IF_ASSERT_FAILED(staff) {
     335            0 :         return;
     336              :     }
     337              : 
     338            0 :     staff->setHideIfEmpty(hideIfEmpty);
     339              : }
     340              : 
     341            0 : void Measure::setMMRangeText(staff_idx_t staffIdx, MMRestRange* t)
     342              : {
     343            0 :     m_mstaves[staffIdx]->setMMRangeText(t);
     344            0 : }
     345              : 
     346        51556 : MMRestRange* Measure::mmRangeText(staff_idx_t staffIdx) const
     347              : {
     348        51556 :     return m_mstaves[staffIdx]->mmRangeText();
     349              : }
     350              : 
     351              : //---------------------------------------------------------
     352              : //   Measure
     353              : //---------------------------------------------------------
     354              : 
     355        37084 : Measure::~Measure()
     356              : {
     357        87327 :     for (Segment* s = first(); s;) {
     358        68785 :         Segment* ns = s->next();
     359        68785 :         delete s;
     360        68785 :         s = ns;
     361              :     }
     362        18542 :     muse::DeleteAll(m_mstaves);
     363        37084 : }
     364              : 
     365              : #ifndef ENGRAVING_NO_ACCESSIBILITY
     366            0 : AccessibleItemPtr Measure::createAccessible()
     367              : {
     368            0 :     return std::make_shared<AccessibleItem>(this, AccessibleItem::Group);
     369              : }
     370              : 
     371              : #endif
     372              : 
     373              : //---------------------------------------------------------
     374              : //   findAccidental
     375              : ///   return current accidental value at note position
     376              : //---------------------------------------------------------
     377              : 
     378          265 : AccidentalVal Measure::findAccidental(const Note* note) const
     379              : {
     380          265 :     const Chord* chord = note->chord();
     381          265 :     const Staff* vStaff = chord->score()->staff(chord->vStaffIdx());
     382          265 :     AccidentalState tversatz;    // state of already set accidentals for this measure
     383          265 :     tversatz.init(vStaff->keySigEvent(tick()));
     384              : 
     385          265 :     const TrackRange trackRange = vStaff->part()->trackRange();
     386          265 :     track_idx_t mainTrack = chord->vStaffIdx() * VOICES;
     387              : 
     388          958 :     for (const Segment* segment = first(); segment; segment = segment->next()) {
     389          956 :         if (segment->isKeySigType()) {
     390          177 :             const KeySig* ks = toKeySig(segment->element(mainTrack));
     391          177 :             if (!ks) {
     392            0 :                 continue;
     393              :             }
     394          177 :             tversatz.init(vStaff->keySigEvent(segment->tick()));
     395          779 :         } else if (segment->segmentType() == SegmentType::ChordRest) {
     396         1988 :             for (track_idx_t track = trackRange.startTrack; track < trackRange.endTrack; ++track) {
     397         1674 :                 const EngravingItem* e = segment->element(track);
     398         1674 :                 if (!e || !e->isChord()) {
     399         1137 :                     continue;
     400              :                 }
     401          537 :                 const Chord* crd = toChord(e);
     402          757 :                 for (const Chord* chord1 : crd->graceNotes()) {
     403          374 :                     if (chord1->vStaffIdx() != chord->vStaffIdx()) {
     404            0 :                         continue;
     405              :                     }
     406          604 :                     for (const Note* note1 : chord1->notes()) {
     407          384 :                         if (note1->tieBack() && !note1->accidental()) {
     408            0 :                             continue;
     409              :                         }
     410              :                         //
     411              :                         // compute accidental
     412              :                         //
     413          384 :                         int tpc  = note1->tpc();
     414          384 :                         int line = absStep(tpc, note1->epitch());
     415              : 
     416          384 :                         if (note == note1) {
     417          154 :                             return tversatz.accidentalVal(line);
     418              :                         }
     419          230 :                         tversatz.setAccidentalVal(line, tpc2alter(tpc));
     420              :                     }
     421              :                 }
     422          383 :                 if (crd->vStaffIdx() != chord->vStaffIdx()) {
     423            6 :                     continue;
     424              :                 }
     425          707 :                 for (const Note* note1 : crd->notes()) {
     426          439 :                     if (note1->tieBack() && !note1->accidental()) {
     427           27 :                         continue;
     428              :                     }
     429              :                     //
     430              :                     // compute accidental
     431              :                     //
     432          412 :                     int tpc  = note1->tpc();
     433          412 :                     int line = absStep(tpc, note1->epitch());
     434              : 
     435          412 :                     if (note == note1) {
     436          109 :                         return tversatz.accidentalVal(line);
     437              :                     }
     438          303 :                     tversatz.setAccidentalVal(line, tpc2alter(tpc));
     439              :                 }
     440              :             }
     441              :         }
     442              :     }
     443            2 :     LOGD() << "Measure::findAccidental: note not found";
     444            2 :     return AccidentalVal::NATURAL;
     445              : }
     446              : 
     447              : //---------------------------------------------------------
     448              : //   findAccidental
     449              : ///   Compute accidental state at segment/staffIdx for
     450              : ///   relative staff line.
     451              : //---------------------------------------------------------
     452              : 
     453           84 : AccidentalVal Measure::findAccidental(const Segment* s, staff_idx_t staffIdx, int line, bool& error) const
     454              : {
     455           84 :     AccidentalState tversatz;    // state of already set accidentals for this measure
     456           84 :     const Staff* staff = score()->staff(staffIdx);
     457           84 :     tversatz.init(staff->keySigEvent(tick()));
     458              : 
     459           84 :     SegmentType st = SegmentType::ChordRest;
     460           84 :     const TrackRange trackRange = staff->part()->trackRange();
     461          125 :     for (const Segment* segment = first(st); segment; segment = segment->next(st)) {
     462          125 :         if (segment == s && staff->isPitchedStaff(tick())) {
     463           84 :             ClefType clef = staff->clef(s->tick());
     464           84 :             int l = relStep(line, clef);
     465           84 :             return tversatz.accidentalVal(l, error);
     466              :         }
     467          205 :         for (track_idx_t track = trackRange.startTrack; track < trackRange.endTrack; ++track) {
     468          164 :             const EngravingItem* e = segment->element(track);
     469          164 :             if (!e || !e->isChord()) {
     470          115 :                 continue;
     471              :             }
     472           49 :             const Chord* chord = toChord(e);
     473           49 :             for (const Chord* chord1 : chord->graceNotes()) {
     474            0 :                 if (chord1->vStaffIdx() != staffIdx) {
     475            0 :                     continue;
     476              :                 }
     477            0 :                 for (const Note* note : chord1->notes()) {
     478            0 :                     if (note->tieBack() && !note->accidental()) {
     479            0 :                         continue;
     480              :                     }
     481            0 :                     int tpc  = note->tpc();
     482            0 :                     int l    = absStep(tpc, note->epitch());
     483            0 :                     tversatz.setAccidentalVal(l, tpc2alter(tpc));
     484              :                 }
     485              :             }
     486           49 :             if (chord->vStaffIdx() != staffIdx) {
     487            0 :                 continue;
     488              :             }
     489          112 :             for (const Note* note : chord->notes()) {
     490           63 :                 if (note->tieBack() && !note->accidental()) {
     491            0 :                     continue;
     492              :                 }
     493           63 :                 int tpc    = note->tpc();
     494           63 :                 int l      = absStep(tpc, note->epitch());
     495           63 :                 tversatz.setAccidentalVal(l, tpc2alter(tpc));
     496              :             }
     497              :         }
     498              :     }
     499            0 :     LOGD() << "segment not found";
     500            0 :     return AccidentalVal::NATURAL;
     501              : }
     502              : 
     503              : //---------------------------------------------------------
     504              : //   tick2pos
     505              : ///    return x position for tick relative to System
     506              : //---------------------------------------------------------
     507              : 
     508            0 : double Measure::tick2pos(Fraction tck) const
     509              : {
     510            0 :     tck -= ticks();
     511            0 :     if (isMMRest()) {
     512            0 :         Segment* s = first(SegmentType::ChordRest);
     513            0 :         double x1   = s->x();
     514            0 :         double w    = width() - x1;
     515            0 :         return x1 + (tck.ticks() * w) / (ticks().ticks() * mmRestCount());
     516              :     }
     517              : 
     518              :     Segment* s;
     519            0 :     double x1  = 0;
     520            0 :     double x2  = 0;
     521            0 :     Fraction tick1 = Fraction(0, 1);
     522            0 :     Fraction tick2 = Fraction(0, 1);
     523            0 :     for (s = first(SegmentType::ChordRest); s; s = s->next(SegmentType::ChordRest)) {
     524            0 :         x2    = s->x();
     525            0 :         tick2 = s->rtick();
     526            0 :         if (tck == tick2) {
     527            0 :             return x2 + pos().x();
     528              :         }
     529            0 :         if (tck <= tick2) {
     530            0 :             break;
     531              :         }
     532            0 :         x1    = x2;
     533            0 :         tick1 = tick2;
     534              :     }
     535            0 :     if (s == 0) {
     536            0 :         x2    = width();
     537            0 :         tick2 = ticks();
     538              :     }
     539            0 :     double dx = x2 - x1;
     540            0 :     Fraction dt   = tick2 - tick1;
     541            0 :     x1      += dt.isZero() ? 0.0 : (dx * (tck.ticks() - tick1.ticks()) / dt.ticks());
     542            0 :     return x1 + pos().x();
     543              : }
     544              : 
     545              : //---------------------------------------------------------
     546              : //   showsMeasureNumberInAutoMode
     547              : ///    Whether the measure will show measure number(s) when MeasureNumberMode is set to AUTO
     548              : //---------------------------------------------------------
     549              : 
     550       163724 : bool Measure::showMeasureNumberInAutoMode() const
     551              : {
     552              :     // Check whether any measure number should be shown
     553       163724 :     if (!style().styleB(Sid::showMeasureNumber)) {
     554          912 :         return false;
     555              :     }
     556              : 
     557              :     // Measure numbers should not be shown on measures excluded from numbering.
     558       162812 :     if (excludeFromNumbering()) {
     559          156 :         return false;
     560              :     }
     561              : 
     562       162656 :     int interval = style().styleI(Sid::measureNumberInterval);
     563       162656 :     Measure* prevMeasure = this->prevMeasure();
     564              : 
     565              :     // Measure numbers should not show on first measure unless specified with Sid::showMeasureNumberOne
     566              :     // except, when showing numbers on each measure, and first measure is after anacrusis - then show always
     567       162656 :     if (isFirstInSection() || (prevMeasure->excludeFromNumbering() && prevMeasure->isFirstInSection() && interval != 1)) {
     568        21339 :         return style().styleB(Sid::showMeasureNumberOne);
     569              :     }
     570              : 
     571       141317 :     if (style().styleB(Sid::measureNumberSystem)) {
     572              :         // Show either if
     573              :         //   1) This is the first measure of the system OR
     574              :         //   2) The previous measure in the system is the first, and is excluded from numbering.
     575       278819 :         return (system() && isFirstInSystem())
     576       278819 :                || (prevMeasure && prevMeasure->excludeFromNumbering() && prevMeasure->system() && prevMeasure->isFirstInSystem());
     577              :     } else {
     578              :         // In the case of an interval, we should show the measure number either if:
     579              :         //   1) We should show them every measure
     580         1906 :         if (interval == 1) {
     581         1651 :             return true;
     582              :         }
     583              : 
     584              :         //   2) (measureNumber + 1) % interval == 0 (or 1 if measure number one is numbered.)
     585              :         // If measure number 1 is numbered, and the interval is let's say 5, then we should number #1, 6, 11, 16, etc.
     586              :         // If measure number 1 is not numbered, with the same interval (5), then we should number #5, 10, 15, 20, etc.
     587          255 :         return ((measureNumber() + 1) % interval) == (style().styleB(Sid::showMeasureNumberOne) ? 1 : 0);
     588              :     }
     589              : }
     590              : 
     591       163724 : bool Measure::showMeasureNumberOnStaff(staff_idx_t staffIdx) const
     592              : {
     593       163724 :     IF_ASSERT_FAILED(staffIdx < score()->nstaves()) {
     594            0 :         return false;
     595              :     }
     596              : 
     597       163724 :     return showMeasureNumber() && score()->staff(staffIdx)->shouldShowMeasureNumbers() && !score()->allStavesInvisible();
     598              : }
     599              : 
     600              : //---------------------------------------------------------
     601              : //   showsMeasureNumber
     602              : ///     Whether the Measure shows a MeasureNumber
     603              : //---------------------------------------------------------
     604              : 
     605       163724 : bool Measure::showMeasureNumber() const
     606              : {
     607       163724 :     switch (m_measureNumberMode) {
     608       163724 :     case MeasureNumberMode::AUTO:
     609       163724 :         return showMeasureNumberInAutoMode();
     610            0 :     case MeasureNumberMode::SHOW:
     611            0 :         return true;
     612            0 :     case MeasureNumberMode::HIDE:
     613            0 :         break;
     614              :     }
     615            0 :     return false;
     616              : }
     617              : 
     618              : //---------------------------------------------------------
     619              : //   findChord
     620              : ///   Search for chord at position \a tick in \a track
     621              : //---------------------------------------------------------
     622              : 
     623        12632 : Chord* Measure::findChord(Fraction t, track_idx_t track) const
     624              : {
     625        12632 :     t -= tick();
     626        35477 :     for (Segment* seg = last(); seg; seg = seg->prev()) {
     627        32888 :         if (seg->rtick() < t) {
     628         3967 :             return 0;
     629              :         }
     630        28921 :         if (seg->rtick() == t) {
     631        10802 :             EngravingItem* el = seg->element(track);
     632        10802 :             if (el && el->isChord()) {
     633         6076 :                 return toChord(el);
     634              :             }
     635              :         }
     636              :     }
     637         2589 :     return 0;
     638              : }
     639              : 
     640              : //---------------------------------------------------------
     641              : //   findChordRest
     642              : ///   Search for chord or rest at position \a tick at \a staff in \a voice.
     643              : //---------------------------------------------------------
     644              : 
     645          298 : ChordRest* Measure::findChordRest(Fraction t, track_idx_t track) const
     646              : {
     647          298 :     t -= tick();
     648          462 :     for (const Segment& seg : m_segments) {
     649          460 :         if (seg.rtick() > t) {
     650          296 :             return 0;
     651              :         }
     652          455 :         if (seg.rtick() == t) {
     653          402 :             EngravingItem* el = seg.element(track);
     654          402 :             if (el && el->isChordRest()) {
     655          291 :                 return toChordRest(el);
     656              :             }
     657              :         }
     658              :     }
     659            2 :     return 0;
     660              : }
     661              : 
     662              : //---------------------------------------------------------
     663              : //   tick2segment
     664              : //---------------------------------------------------------
     665              : 
     666          277 : Segment* Measure::tick2segment(const Fraction& _t, SegmentType st)
     667              : {
     668          277 :     Fraction t = _t - tick();
     669         1317 :     for (Segment& s : m_segments) {
     670         1317 :         if (s.rtick() == t) {
     671          316 :             if (s.segmentType() & st) {
     672          277 :                 return &s;
     673              :             }
     674              :         }
     675         1040 :         if (s.rtick() > t) {
     676            0 :             break;
     677              :         }
     678              :     }
     679            0 :     return 0;
     680              : }
     681              : 
     682              : //---------------------------------------------------------
     683              : //   findSegmentR
     684              : //    Search for a segment of type st at measure relative
     685              : //    position t.
     686              : //---------------------------------------------------------
     687              : 
     688      1203429 : Segment* Measure::findSegmentR(SegmentType st, const Fraction& t) const
     689              : {
     690              :     Segment* s;
     691      1203429 :     if (t > (ticks() * Fraction(1, 2))) {
     692              :         // search backwards
     693       640759 :         for (s = last(); s && s->rtick() > t; s = s->prev()) {
     694              :         }
     695       654005 :         while (s && s->prev() && s->prev()->rtick() == t) {
     696        19953 :             s = s->prev();
     697              :         }
     698              :     } else {
     699              :         // search forwards
     700       642507 :         for (s = first(); s && s->rtick() < t; s = s->next()) {
     701              :         }
     702              :     }
     703      2506489 :     for (; s && s->rtick() == t; s = s->next()) {
     704      1493475 :         if (s->segmentType() & st) {
     705       190415 :             return s;
     706              :         }
     707              :     }
     708      1013014 :     return 0;
     709              : }
     710              : 
     711              : //---------------------------------------------------------
     712              : //   undoGetSegmentR
     713              : //---------------------------------------------------------
     714              : 
     715         9394 : Segment* Measure::undoGetSegmentR(SegmentType type, const Fraction& t)
     716              : {
     717         9394 :     Segment* s = findSegmentR(type, t);
     718         9394 :     if (s == 0) {
     719         4013 :         s = Factory::createSegment(this, type, t);
     720         4013 :         score()->undoAddElement(s);
     721              :     }
     722         9394 :     return s;
     723              : }
     724              : 
     725              : //---------------------------------------------------------
     726              : //   findFirstR
     727              : //    return first segment of type st at relative
     728              : //    position t.
     729              : //---------------------------------------------------------
     730              : 
     731       197958 : Segment* Measure::findFirstR(SegmentType st, const Fraction& t) const
     732              : {
     733              :     Segment* s;
     734              :     // search forwards
     735       347627 :     for (s = first(); s && s->rtick() <= t; s = s->next()) {
     736       337487 :         if (s->segmentType() & st) {
     737       187818 :             return s;
     738              :         }
     739              :     }
     740        10140 :     return 0;
     741              : }
     742              : 
     743          615 : Segment* Measure::getChordRestOrTimeTickSegment(const Fraction& f)
     744              : {
     745          615 :     Segment* seg = findSegment(SegmentType::ChordRest, f);
     746          615 :     if (!seg) {
     747          416 :         seg = findSegment(SegmentType::TimeTick, f);
     748              :     }
     749          615 :     if (!seg) {
     750          416 :         if (f - tick() == ticks()) { // end of measure
     751            3 :             seg = getSegment(SegmentType::TimeTick, f);
     752              :         } else {
     753          413 :             seg = getSegment(SegmentType::ChordRest, f);
     754              :         }
     755              :     }
     756              : 
     757          615 :     return seg;
     758              : }
     759              : 
     760           51 : Segment* Measure::undoGetChordRestOrTimeTickSegment(const Fraction& f)
     761              : {
     762           51 :     Segment* seg = findSegment(SegmentType::ChordRest, f);
     763           51 :     if (!seg) {
     764           19 :         seg = findSegment(SegmentType::TimeTick, f);
     765              :     }
     766           51 :     if (!seg) {
     767           19 :         if (f - tick() == ticks()) { // end of measure
     768            0 :             seg = undoGetSegment(SegmentType::TimeTick, f);
     769              :         } else {
     770           19 :             seg = undoGetSegment(SegmentType::ChordRest, f);
     771              :         }
     772              :     }
     773              : 
     774           51 :     return seg;
     775              : }
     776              : 
     777              : //---------------------------------------------------------
     778              : //   getSegmentR
     779              : ///   Get a segment of type st at relative tick position t.
     780              : ///   If the segment does not exist, it is created.
     781              : //---------------------------------------------------------
     782              : 
     783        85951 : Segment* Measure::getSegmentR(SegmentType st, const Fraction& t)
     784              : {
     785        85951 :     Segment* s = findSegmentR(st, t);
     786        85951 :     if (!s) {
     787        64166 :         s = Factory::createSegment(this, st, t);
     788        64166 :         add(s);
     789              :     }
     790        85951 :     return s;
     791              : }
     792              : 
     793              : //---------------------------------------------------------
     794              : //   add
     795              : ///   Add new EngravingItem \a el to Measure.
     796              : //---------------------------------------------------------
     797              : 
     798        95116 : void Measure::add(EngravingItem* e)
     799              : {
     800        95116 :     if (e->ownershipParent() != this) {
     801           62 :         e->setOwnershipParent(this);
     802              :     }
     803              : 
     804        95116 :     ElementType type = e->type();
     805              : 
     806        95116 :     switch (type) {
     807        91032 :     case ElementType::SEGMENT:
     808              :     {
     809        91032 :         Segment* seg   = toSegment(e);
     810        91032 :         Fraction t     = seg->rtick();
     811              :         Segment* s;
     812              : 
     813       239427 :         for (s = first(); s && s->rtick() < t; s = s->next()) {
     814              :         }
     815       117615 :         while (s && s->rtick() == t) {
     816        50648 :             if (!seg->isChordRestType() && (seg->segmentType() == s->segmentType())) {
     817            1 :                 if (seg->isType(SegmentType::BarLineTypes)) {
     818              :                     // Barline segments are regenerated every layout
     819              :                     // We need to remove the regenerated segment when undoing to ensure the original element is added back to the score
     820            0 :                     m_segments.remove(s);
     821            0 :                     s = s->next();
     822            0 :                     continue;
     823              :                 }
     824              :                 /// HACK: REMOVED to prevent crash in 4.4.3.
     825              :                 /// Adding multiple identical segments may cause problems, so we should resolve this properly
     826              :                 // return;
     827              :             }
     828        50648 :             if (seg->goesBefore(s)) {
     829        24065 :                 break;
     830              :             }
     831        26583 :             s = s->next();
     832              :         }
     833        91032 :         seg->setOwnershipParent(this);
     834        91032 :         m_segments.insert(seg, s);
     835              :         //
     836              :         // update measure flags
     837              :         //
     838        91032 :         if (seg->header()) {
     839        10263 :             seg->measure()->setHeader(true);
     840              :         }
     841        91032 :         if (seg->trailer()) {
     842            0 :             seg->measure()->setTrailer(true);
     843              :         }
     844              :     }
     845        91032 :     break;
     846              : 
     847         2622 :     case ElementType::MEASURE_NUMBER:
     848         2622 :         if (e->staffIdx() < m_mstaves.size()) {
     849         2622 :             m_mstaves[e->staffIdx()]->setMeasureNumber(toMeasureNumber(e));
     850              :         }
     851         2622 :         break;
     852              : 
     853            0 :     case ElementType::MMREST_RANGE:
     854            0 :         if (e->staffIdx() < m_mstaves.size()) {
     855            0 :             m_mstaves[e->staffIdx()]->setMMRangeText(toMMRestRange(e));
     856              :         }
     857            0 :         break;
     858              : 
     859            1 :     case ElementType::SPACER:
     860              :     {
     861            1 :         Spacer* sp = toSpacer(e);
     862            1 :         switch (sp->spacerType()) {
     863            0 :         case SpacerType::UP:
     864            0 :             m_mstaves[e->staffIdx()]->setVspacerUp(sp);
     865            0 :             break;
     866            1 :         case SpacerType::DOWN:
     867              :         case SpacerType::FIXED:
     868            1 :             m_mstaves[e->staffIdx()]->setVspacerDown(sp);
     869            1 :             break;
     870              :         }
     871            1 :         sp->setGap(sp->gap());            // trigger relayout
     872              :     }
     873            1 :     break;
     874          393 :     case ElementType::JUMP:
     875              :     case ElementType::MARKER:
     876          393 :         if (e && (e->isJump() || (e->isMarker() && toMarker(e)->isRightMarker()))) {
     877              :             // "To coda" markings act like jumps
     878          242 :             setProperty(Pid::REPEAT_JUMP, true);
     879              :         }
     880          393 :         el().push_back(e);
     881          393 :         break;
     882              : 
     883            0 :     case ElementType::HBOX:
     884            0 :         if (e->staff()) {
     885            0 :             e->mutldata()->setMag(e->staff()->staffMag(tick()));                 // ?!
     886              :         }
     887            0 :         el().push_back(e);
     888            0 :         break;
     889              : 
     890            0 :     case ElementType::MEASURE:
     891            0 :         m_mmRest = toMeasure(e);
     892            0 :         break;
     893              : 
     894           18 :     case ElementType::STAFFTYPE_CHANGE:
     895              :     {
     896           18 :         StaffTypeChange* staffTypeChange = toStaffTypeChange(e);
     897           18 :         const StaffType* templateStaffType = staffTypeChange->staffType();
     898              : 
     899           18 :         Staff* staff = staffTypeChange->staff();
     900              : 
     901              :         // This will need to point to the stafftype element within the stafftypelist for the staff
     902           18 :         StaffType* newStaffType = nullptr;
     903              : 
     904           18 :         if (templateStaffType) {
     905              :             // executed on read, undo/redo, clone
     906              :             // setStaffType adds a copy to stafftypelist and returns a pointer to that element within stafftypelist
     907           18 :             newStaffType = staff->setStaffType(tick(), *templateStaffType);
     908              :         } else {
     909              :             // executed on add from palette
     910              :             // staffType returns a pointer to the current stafftype element in the list
     911              :             // setStaffType will make a copy and return a pointer to that element within list
     912            0 :             templateStaffType = staff->staffType(tick());
     913            0 :             newStaffType = staff->setStaffType(tick(), *templateStaffType);
     914              :         }
     915              : 
     916           18 :         staff->staffTypeListChanged(tick());
     917           18 :         staffTypeChange->setStaffType(newStaffType, false);
     918              : 
     919           18 :         MeasureBase::add(e);
     920              :     }
     921           18 :         return;
     922              : 
     923         1050 :     default:
     924         1050 :         MeasureBase::add(e);
     925         1050 :         return;
     926              :     }
     927              : 
     928        94048 :     e->added();
     929              : }
     930              : 
     931              : //---------------------------------------------------------
     932              : //   remove
     933              : ///   Remove EngravingItem \a el from Measure.
     934              : //---------------------------------------------------------
     935              : 
     936         2694 : void Measure::remove(EngravingItem* e)
     937              : {
     938         2694 :     assert(e->ownershipParent() == this);
     939         2694 :     assert(e->score() == score());
     940              : 
     941         2694 :     switch (e->type()) {
     942         2655 :     case ElementType::SEGMENT:
     943              :     {
     944         2655 :         Segment* s = toSegment(e);
     945         2655 :         m_segments.remove(s);
     946              :         //
     947              :         // update measure flags
     948              :         //
     949         2655 :         if (s->header()) {
     950           16 :             s->measure()->checkHeader();
     951              :         }
     952         2655 :         if (s->trailer()) {
     953            4 :             s->measure()->checkTrailer();
     954              :         }
     955              :     }
     956         2655 :     break;
     957              : 
     958            0 :     case ElementType::MEASURE_NUMBER:
     959            0 :         m_mstaves[e->staffIdx()]->setMeasureNumber(nullptr);
     960            0 :         break;
     961              : 
     962            0 :     case ElementType::MMREST_RANGE:
     963            0 :         m_mstaves[e->staffIdx()]->setMMRangeText(nullptr);
     964            0 :         break;
     965              : 
     966            0 :     case ElementType::SPACER:
     967            0 :         switch (toSpacer(e)->spacerType()) {
     968            0 :         case SpacerType::DOWN:
     969              :         case SpacerType::FIXED:
     970            0 :             m_mstaves[e->staffIdx()]->setVspacerDown(0);
     971            0 :             break;
     972            0 :         case SpacerType::UP:
     973            0 :             m_mstaves[e->staffIdx()]->setVspacerUp(0);
     974            0 :             break;
     975              :         }
     976            0 :         break;
     977              : 
     978            4 :     case ElementType::JUMP:
     979              :     case ElementType::MARKER:
     980            4 :         if (e->isJump() || (e->isMarker() && toMarker(e)->isRightMarker())) {
     981            4 :             setProperty(Pid::REPEAT_JUMP, false);
     982              :         }
     983              :         [[fallthrough]];
     984              :     case ElementType::HBOX:
     985            4 :         if (!el().remove(e)) {
     986            0 :             LOGD("Measure(%p)::remove(%s,%p) not found", this, e->typeName(), e);
     987              :         }
     988            4 :         break;
     989              : 
     990            0 :     case ElementType::CLEF:
     991              :     case ElementType::CHORD:
     992              :     case ElementType::REST:
     993              :     case ElementType::MMREST:
     994              :     case ElementType::TIMESIG:
     995            0 :         for (Segment* segment = first(); segment; segment = segment->next()) {
     996            0 :             size_t staves = score()->nstaves();
     997            0 :             size_t tracks = staves * VOICES;
     998            0 :             for (track_idx_t track = 0; track < tracks; ++track) {
     999            0 :                 EngravingItem* ee = segment->element(track);
    1000            0 :                 if (ee == e) {
    1001            0 :                     segment->setElement(track, nullptr);
    1002            0 :                     e->removed();
    1003            0 :                     return;
    1004              :                 }
    1005              :             }
    1006              :         }
    1007            0 :         LOGD("Measure::remove: %s %p not found", e->typeName(), e);
    1008            0 :         break;
    1009              : 
    1010            0 :     case ElementType::MEASURE:
    1011            0 :         m_mmRest = 0;
    1012            0 :         break;
    1013              : 
    1014            0 :     case ElementType::STAFFTYPE_CHANGE:
    1015              :     {
    1016            0 :         StaffTypeChange* stc = toStaffTypeChange(e);
    1017            0 :         Staff* staff = stc->staff();
    1018            0 :         if (staff) {
    1019              :             // st currently points to an list element that is about to be removed
    1020              :             // make a copy now to use on undo/redo
    1021            0 :             StaffType* st = new StaffType(*stc->staffType());
    1022            0 :             if (!tick().isZero()) {
    1023            0 :                 staff->removeStaffType(tick());
    1024              :             }
    1025            0 :             stc->setStaffType(st, true);
    1026              :         }
    1027            0 :         MeasureBase::remove(e);
    1028              :     }
    1029            0 :         return;
    1030              : 
    1031           35 :     default:
    1032           35 :         MeasureBase::remove(e);
    1033           35 :         return;
    1034              :     }
    1035              : 
    1036         2659 :     e->removed();
    1037              : }
    1038              : 
    1039              : //---------------------------------------------------------
    1040              : //   change
    1041              : //---------------------------------------------------------
    1042              : 
    1043            0 : void Measure::change(EngravingItem* o, EngravingItem* n)
    1044              : {
    1045            0 :     if (o->isTuplet()) {
    1046            0 :         Tuplet* t = toTuplet(n);
    1047            0 :         for (DurationElement* e : t->elements()) {
    1048            0 :             e->setTuplet(t);
    1049              :         }
    1050              :     } else {
    1051            0 :         remove(o);
    1052            0 :         add(n);
    1053              :     }
    1054            0 : }
    1055              : 
    1056              : //---------------------------------------------------------
    1057              : //   spatiumChanged
    1058              : //---------------------------------------------------------
    1059              : 
    1060            0 : void Measure::spatiumChanged(double /*oldValue*/, double /*newValue*/)
    1061              : {
    1062            0 : }
    1063              : 
    1064              : //-------------------------------------------------------------------
    1065              : //   moveTicks
    1066              : //    Also adjust endBarLine if measure len has changed. For this
    1067              : //    diff == 0 cannot be optimized away
    1068              : //-------------------------------------------------------------------
    1069              : 
    1070        54894 : void Measure::moveTicks(const Fraction& diff)
    1071              : {
    1072              :     // This part must be run also for diff = 0
    1073       215393 :     for (Segment* segment = last(); segment; segment = segment->prev()) {
    1074       160499 :         if (segment->segmentType() & (SegmentType::EndBarLine | SegmentType::TimeSigAnnounce)) {
    1075        22622 :             segment->setRtick(ticks());
    1076              :         }
    1077              :     }
    1078        54894 :     if (diff.numerator() == 0) {
    1079        53088 :         return;
    1080              :     }
    1081         1806 :     std::set<Tuplet*> tuplets;
    1082         1806 :     setTick(tick() + diff);
    1083         6361 :     for (Segment* segment = last(); segment; segment = segment->prev()) {
    1084         4555 :         if (segment->isChordRestType()) {
    1085              :             // Tuplet ticks are stored as absolute ticks, so they must be adjusted,
    1086              :             // outer tuplets of nested ones too. But each tuplet must only be adjusted once.
    1087        16238 :             for (EngravingItem* e : segment->elist()) {
    1088        13860 :                 if (e) {
    1089         3155 :                     ChordRest* cr = toChordRest(e);
    1090         3163 :                     for (Tuplet* tuplet = cr->tuplet(); tuplet && tuplets.insert(tuplet).second; tuplet = tuplet->tuplet()) {
    1091            8 :                         tuplet->setTick(tuplet->tick() + diff);
    1092              :                     }
    1093              :                 }
    1094              :             }
    1095              :         }
    1096              :     }
    1097         1806 :     tuplets.clear();
    1098         1806 : }
    1099              : 
    1100              : //---------------------------------------------------------
    1101              : //   removeStaves
    1102              : //---------------------------------------------------------
    1103              : 
    1104          389 : void Measure::removeStaves(staff_idx_t sStaff, staff_idx_t eStaff)
    1105              : {
    1106         1508 :     for (Segment* s = first(); s; s = s->next()) {
    1107         2238 :         for (int staff = static_cast<int>(eStaff) - 1; staff >= static_cast<int>(sStaff); --staff) {
    1108         1119 :             s->removeStaff(staff);
    1109              :         }
    1110              :     }
    1111          389 :     for (EngravingItem* e : el()) {
    1112            0 :         if (e->track() == muse::nidx) {
    1113            0 :             continue;
    1114              :         }
    1115            0 :         voice_idx_t voice = e->voice();
    1116            0 :         staff_idx_t staffIdx = e->staffIdx();
    1117            0 :         if (staffIdx >= eStaff) {
    1118            0 :             staffIdx -= eStaff - sStaff;
    1119            0 :             e->setTrack(staffIdx * VOICES + voice);
    1120              :         }
    1121              :     }
    1122          389 : }
    1123              : 
    1124              : //---------------------------------------------------------
    1125              : //   insertStaves
    1126              : //---------------------------------------------------------
    1127              : 
    1128          784 : void Measure::insertStaves(staff_idx_t sStaff, staff_idx_t eStaff)
    1129              : {
    1130          829 :     for (EngravingItem* e : el()) {
    1131           45 :         if (e->track() == muse::nidx) {
    1132            0 :             continue;
    1133              :         }
    1134           45 :         staff_idx_t staffIdx = e->staffIdx();
    1135           45 :         if (moveDownWhenAddingStaves(e, sStaff, eStaff)) {
    1136            0 :             voice_idx_t voice = e->voice();
    1137            0 :             staffIdx += eStaff - sStaff;
    1138            0 :             e->setTrack(staffIdx * VOICES + voice);
    1139              :         }
    1140              :     }
    1141         3446 :     for (Segment* s = first(); s; s = s->next()) {
    1142         5324 :         for (staff_idx_t staff = sStaff; staff < eStaff; ++staff) {
    1143         2662 :             s->insertStaff(staff);
    1144              :         }
    1145              :     }
    1146          784 : }
    1147              : 
    1148              : //---------------------------------------------------------
    1149              : //   cmdRemoveStaves
    1150              : //---------------------------------------------------------
    1151              : 
    1152          304 : void Measure::cmdRemoveStaves(staff_idx_t sStaff, staff_idx_t eStaff)
    1153              : {
    1154          304 :     track_idx_t sTrack = sStaff * VOICES;
    1155          304 :     track_idx_t eTrack = eStaff * VOICES;
    1156              : 
    1157         1177 :     for (Segment* s = first(); s; s = s->next()) {
    1158         4365 :         for (int track = static_cast<int>(eTrack) - 1; track >= static_cast<int>(sTrack); --track) {
    1159         3492 :             EngravingItem* el = s->element(track);
    1160         3492 :             if (el) {
    1161          924 :                 el->undoUnlink();
    1162          924 :                 score()->doUndoRemoveElement(el);
    1163              :             }
    1164              :         }
    1165              : 
    1166              :         // Create copy, because s->annotations() will be modified during the loop
    1167          873 :         std::vector<EngravingItem*> annotations = s->annotations();
    1168          884 :         for (EngravingItem* e : annotations) {
    1169           11 :             if (allowRemoveWhenRemovingStaves(e, sStaff, eStaff)) {
    1170            6 :                 e->undoUnlink();
    1171            6 :                 score()->doUndoRemoveElement(e);
    1172              :             }
    1173              :         }
    1174          873 :     }
    1175              : 
    1176          304 :     for (EngravingItem* e : el()) {
    1177            0 :         if (e->track() == muse::nidx) {
    1178            0 :             continue;
    1179              :         }
    1180              : 
    1181            0 :         if (allowRemoveWhenRemovingStaves(e, sStaff, eStaff)) {
    1182            0 :             e->undoUnlink();
    1183            0 :             score()->doUndoRemoveElement(e);
    1184              :         }
    1185              :     }
    1186              : 
    1187          304 :     score()->undo(new RemoveStaves(this, sStaff, eStaff));
    1188              : 
    1189          608 :     for (int i = static_cast<int>(eStaff) - 1; i >= static_cast<int>(sStaff); --i) {
    1190          304 :         MStaff* ms = *(m_mstaves.begin() + i);
    1191          304 :         score()->undo(new RemoveMStaff(this, ms, i));
    1192              :     }
    1193          304 :     score()->undoUpdatePlayCountText(this);
    1194          304 : }
    1195              : 
    1196              : //---------------------------------------------------------
    1197              : //   cmdAddStaves
    1198              : //---------------------------------------------------------
    1199              : 
    1200          636 : void Measure::cmdAddStaves(staff_idx_t sStaff, staff_idx_t eStaff, bool createRest)
    1201              : {
    1202          636 :     score()->undo(new InsertStaves(this, sStaff, eStaff));
    1203              : 
    1204          636 :     Segment* ts = findSegment(SegmentType::TimeSig, tick());
    1205          636 :     Segment* bs = findSegmentR(SegmentType::EndBarLine, ticks());
    1206              : 
    1207         1272 :     for (staff_idx_t i = sStaff; i < eStaff; ++i) {
    1208          636 :         Staff* staff = score()->staff(i);
    1209          636 :         MStaff* ms   = new MStaff;
    1210          636 :         ms->setLines(Factory::createStaffLines(this));
    1211          636 :         ms->lines()->setTrack(i * VOICES);
    1212          636 :         ms->lines()->setVisible(!staff->isLinesInvisible(tick()));
    1213          636 :         score()->undo(new InsertMStaff(this, ms, i));
    1214              :     }
    1215              : 
    1216          636 :     if (!createRest && !ts) {
    1217           71 :         return;
    1218              :     }
    1219              : 
    1220              :     // collect unique staves (only one instance for linked staves):
    1221              : 
    1222          565 :     std::vector<staff_idx_t> sl;
    1223         1130 :     for (staff_idx_t staffIdx = sStaff; staffIdx < eStaff; ++staffIdx) {
    1224          565 :         Staff* s = score()->staff(staffIdx);
    1225          565 :         if (s->links()) {
    1226            0 :             bool alreadyInList = false;
    1227            0 :             for (staff_idx_t idx : sl) {
    1228            0 :                 if (s->links()->contains(score()->staff(idx))) {
    1229            0 :                     alreadyInList = true;
    1230            0 :                     break;
    1231              :                 }
    1232              :             }
    1233            0 :             if (alreadyInList) {
    1234            0 :                 continue;
    1235              :             }
    1236              :         }
    1237          565 :         sl.push_back(staffIdx);
    1238              :     }
    1239              : 
    1240         1130 :     for (staff_idx_t staffIdx : sl) {
    1241          565 :         if (createRest) {
    1242          537 :             score()->setRest(tick(), staffIdx * VOICES, ticks(), false, 0, m_timesig == ticks());
    1243              :         }
    1244              : 
    1245              :         // replicate time signature
    1246          565 :         if (ts) {
    1247          107 :             TimeSig* ots = 0;
    1248          107 :             bool constructed = false;
    1249          351 :             for (track_idx_t track = 0; track < m_mstaves.size() * VOICES; ++track) {
    1250          320 :                 if (ts->element(track)) {
    1251           76 :                     ots = toTimeSig(ts->element(track));
    1252           76 :                     break;
    1253              :                 }
    1254              :             }
    1255          107 :             if (!ots) {
    1256              :                 // no time signature found; use measure timesig to construct one
    1257           31 :                 ots = Factory::createTimeSig(score()->dummy());
    1258           31 :                 ots->setSig(timesig());
    1259           31 :                 constructed = true;
    1260              :             }
    1261              :             // do no replicate local time signatures
    1262          107 :             if (ots && !ots->isLocal()) {
    1263          105 :                 TimeSig* timesig = Factory::copyTimeSig(*ots);
    1264          105 :                 timesig->setTrack(staffIdx * VOICES);
    1265          105 :                 timesig->setOwnershipParent(ts);
    1266          105 :                 timesig->setSig(ots->sig(), ots->timeSigType());
    1267          105 :                 score()->undoAddElement(timesig);
    1268          105 :                 if (constructed) {
    1269           31 :                     delete ots;
    1270              :                 }
    1271              :             }
    1272              :         }
    1273              : 
    1274              :         // replicate barline
    1275          565 :         if (bs) {
    1276          492 :             BarLine* obl = nullptr;
    1277         7036 :             for (track_idx_t track = 0; track < m_mstaves.size() * VOICES; ++track) {
    1278         6552 :                 EngravingItem* e = bs->element(track);
    1279         6552 :                 if (e && !e->generated()) {
    1280            8 :                     obl = toBarLine(e);
    1281            8 :                     break;
    1282              :                 }
    1283              :             }
    1284          492 :             if (obl) {
    1285            8 :                 BarLine* barline = Factory::copyBarLine(*obl);
    1286            8 :                 barline->setSpanStaff(score()->staff(staffIdx)->barLineSpan());
    1287            8 :                 barline->setTrack(staffIdx * VOICES);
    1288            8 :                 barline->setOwnershipParent(bs);
    1289            8 :                 barline->setGenerated(false);
    1290            8 :                 score()->undoAddElement(barline);
    1291              :             }
    1292              :         }
    1293              :     }
    1294          565 : }
    1295              : 
    1296              : //---------------------------------------------------------
    1297              : //   insertMStaff
    1298              : //---------------------------------------------------------
    1299              : 
    1300          948 : void Measure::insertMStaff(MStaff* staff, staff_idx_t idx)
    1301              : {
    1302          948 :     m_mstaves.insert(m_mstaves.begin() + idx, staff);
    1303         3787 :     for (staff_idx_t staffIdx = 0; staffIdx < m_mstaves.size(); ++staffIdx) {
    1304         2839 :         m_mstaves[staffIdx]->setTrack(staffIdx * VOICES);
    1305              :     }
    1306          948 : }
    1307              : 
    1308              : //---------------------------------------------------------
    1309              : //   removeMStaff
    1310              : //---------------------------------------------------------
    1311              : 
    1312          389 : void Measure::removeMStaff(MStaff* /*staff*/, staff_idx_t idx)
    1313              : {
    1314          389 :     m_mstaves.erase(m_mstaves.begin() + idx);
    1315         1015 :     for (staff_idx_t staffIdx = 0; staffIdx < m_mstaves.size(); ++staffIdx) {
    1316          626 :         m_mstaves[staffIdx]->setTrack(staffIdx * VOICES);
    1317              :     }
    1318          389 : }
    1319              : 
    1320              : //---------------------------------------------------------
    1321              : //   insertStaff
    1322              : //---------------------------------------------------------
    1323              : 
    1324          164 : void Measure::insertStaff(Staff* staff, staff_idx_t staffIdx)
    1325              : {
    1326          836 :     for (Segment* s = first(); s; s = s->next()) {
    1327          672 :         s->insertStaff(staffIdx);
    1328              :     }
    1329              : 
    1330          164 :     MStaff* ms = new MStaff;
    1331          164 :     ms->setLines(Factory::createStaffLines(this));
    1332          164 :     ms->lines()->setTrack(staffIdx * VOICES);
    1333          164 :     ms->lines()->setVisible(!staff->isLinesInvisible(tick()));
    1334          164 :     insertMStaff(ms, staffIdx);
    1335          164 : }
    1336              : 
    1337              : //---------------------------------------------------------
    1338              : //   staffPageBoundingRect
    1339              : //---------------------------------------------------------
    1340              : 
    1341         3760 : RectF Measure::staffPageBoundingRect(staff_idx_t staffIdx) const
    1342              : {
    1343         3760 :     System* s = system();
    1344         3760 :     IF_ASSERT_FAILED(s) {
    1345            0 :         return RectF();
    1346              :     }
    1347         3760 :     RectF sysStaffPageBbox = s->staff(staffIdx)->bbox().translated(s->pagePos());
    1348         3760 :     RectF measurePageBbox = pageBoundingRect();
    1349         3760 :     return RectF(measurePageBbox.x(), sysStaffPageBbox.y(), measurePageBbox.width(), sysStaffPageBbox.height());
    1350              : }
    1351              : 
    1352              : //---------------------------------------------------------
    1353              : //   acceptDrop
    1354              : //---------------------------------------------------------
    1355              : 
    1356              : /**
    1357              :  Return true if an EngravingItem of type \a type can be dropped on a Measure
    1358              : */
    1359              : 
    1360            0 : bool Measure::acceptDrop(EditData& data) const
    1361              : {
    1362            0 :     const EngravingItem* e = data.dropElement;
    1363              : 
    1364            0 :     if (data.track == muse::nidx || !system()) {
    1365            0 :         return false;
    1366              :     }
    1367              : 
    1368            0 :     const staff_idx_t staffIdx = track2staff(data.track);
    1369              : 
    1370              :     //! NOTE: Should match NotationInteraction::dragMeasureAnchorElement
    1371            0 :     switch (e->type()) {
    1372            0 :     case ElementType::VBOX:
    1373              :     case ElementType::TBOX:
    1374              :     case ElementType::FBOX:
    1375              :     case ElementType::HBOX: {
    1376            0 :         const Measure* m = isMMRest() ? mmRestFirst() : this;
    1377            0 :         for (staff_idx_t staffIdxLoop = 0; staffIdxLoop < score()->nstaves(); ++staffIdxLoop) {
    1378            0 :             if (m->isMeasureRepeatGroupWithPrevM(staffIdxLoop)) {
    1379            0 :                 return false;
    1380              :             }
    1381              :         }
    1382              :         [[fallthrough]];
    1383              :     }
    1384              : 
    1385              :     case ElementType::MEASURE_NUMBER:
    1386              :     case ElementType::JUMP:
    1387              :     case ElementType::MARKER:
    1388              :     case ElementType::VOLTA:
    1389              :     case ElementType::GRADUAL_TEMPO_CHANGE:
    1390              :     case ElementType::KEYSIG:
    1391              :     case ElementType::TIMESIG:
    1392              :         // Drop to all staves or single staff depending on modifier
    1393            0 :         return true;
    1394              : 
    1395            0 :     case ElementType::BRACKET:
    1396              :     case ElementType::MEASURE_REPEAT:
    1397              :     case ElementType::MEASURE:
    1398              :     case ElementType::SPACER:
    1399              :     case ElementType::IMAGE:
    1400              :     case ElementType::BAR_LINE:
    1401              :     case ElementType::SYMBOL:
    1402              :     case ElementType::CLEF:
    1403              :     case ElementType::STAFFTYPE_CHANGE:
    1404              :         // Always drop to single staff
    1405            0 :         return true;
    1406              : 
    1407            0 :     case ElementType::STRING_TUNINGS:
    1408            0 :         return canAddStringTunings(staffIdx);
    1409              : 
    1410            0 :     case ElementType::ACTION_ICON:
    1411            0 :         switch (toActionIcon(e)->actionType()) {
    1412            0 :         case ActionIconType::VFRAME:
    1413              :         case ActionIconType::HFRAME:
    1414              :         case ActionIconType::TFRAME:
    1415              :         case ActionIconType::FFRAME:
    1416              :         case ActionIconType::MEASURE: {
    1417            0 :             const Measure* m = isMMRest() ? mmRestFirst() : this;
    1418            0 :             for (staff_idx_t staffIdxLoop = 0; staffIdxLoop < score()->nstaves(); ++staffIdxLoop) {
    1419            0 :                 if (m->isMeasureRepeatGroupWithPrevM(staffIdxLoop)) {
    1420            0 :                     return false;
    1421              :                 }
    1422              :             }
    1423            0 :             return true;
    1424              :         }
    1425            0 :         case ActionIconType::STAFF_TYPE_CHANGE:
    1426            0 :             return canAddStaffTypeChange(staffIdx);
    1427            0 :         default:
    1428            0 :             break;
    1429              :         }
    1430            0 :         break;
    1431              : 
    1432            0 :     default:
    1433            0 :         break;
    1434              :     }
    1435            0 :     return MeasureBase::acceptDrop(data);
    1436              : }
    1437              : 
    1438              : //---------------------------------------------------------
    1439              : //   drop
    1440              : ///   Drop element.
    1441              : ///   Handle a dropped element at position \a pos of given
    1442              : ///   element \a type and \a subtype.
    1443              : //---------------------------------------------------------
    1444              : 
    1445           17 : EngravingItem* Measure::drop(Transaction& tx, EditData& data)
    1446              : {
    1447           17 :     EngravingItem* e = data.dropElement;
    1448           17 :     staff_idx_t staffIdx = track2staff(data.track);
    1449           17 :     if (staffIdx == muse::nidx) {
    1450            0 :         delete e;
    1451            0 :         return nullptr;
    1452              :     }
    1453           17 :     Staff* staff = score()->staff(staffIdx);
    1454              : 
    1455           17 :     switch (e->type()) {
    1456            0 :     case ElementType::MARKER:
    1457              :     case ElementType::JUMP:
    1458            0 :         e->setOwnershipParent(this);
    1459            0 :         e->setTrack(0);
    1460            0 :         score()->undoAddElement(e);
    1461            0 :         return e;
    1462              : 
    1463            0 :     case ElementType::STRING_TUNINGS:
    1464              :     {
    1465            0 :         staff = staff->isPrimaryStaff() ? staff : staff->primaryStaff();
    1466            0 :         if (!staff) {
    1467            0 :             delete e;
    1468            0 :             return nullptr;
    1469              :         }
    1470            0 :         staffIdx = staff->idx();
    1471            0 :         Segment* parentSeg = first(SegmentType::Duration);
    1472            0 :         if (!parentSeg) {
    1473            0 :             delete e;
    1474            0 :             return nullptr;
    1475              :         }
    1476            0 :         e->setOwnershipParent(parentSeg);
    1477            0 :         e->setTrack(staff2track(staffIdx));
    1478            0 :         score()->undoAddElement(e);
    1479            0 :         return e;
    1480              :     }
    1481              : 
    1482            0 :     case ElementType::MEASURE_NUMBER:
    1483            0 :         undoChangeProperty(Pid::MEASURE_NUMBER_MODE, static_cast<int>(MeasureNumberMode::SHOW));
    1484            0 :         delete e;
    1485            0 :         break;
    1486              : 
    1487            0 :     case ElementType::BRACKET:
    1488              :     {
    1489            0 :         Selection sel = score()->selection();
    1490            0 :         staff_idx_t firstStaff = sel.staffStart();
    1491            0 :         staff_idx_t lastStaff = sel.staffEnd() - 1;
    1492            0 :         size_t level = 0;
    1493            0 :         for (staff_idx_t idx = 0; idx < score()->nstaves(); ++idx) {
    1494            0 :             for (const BracketItem* bi : score()->brackets(idx)) {
    1495            0 :                 if (bi->intersects(firstStaff, lastStaff)) {
    1496            0 :                     level = std::max(level, bi->column() + 1);
    1497              :                 }
    1498              :             }
    1499              :         }
    1500              : 
    1501            0 :         Bracket* b = toBracket(e);
    1502            0 :         if (sel.isRange()) {
    1503            0 :             EditStaffBrackets::undoAddBracket(score(), staff, level, b->bracketType(), sel.staffEnd() - sel.staffStart());
    1504              :         } else {
    1505            0 :             EditStaffBrackets::undoAddBracket(score(), staff, level, b->bracketType(), 1);
    1506              :         }
    1507            0 :         delete b;
    1508            0 :     }
    1509            0 :     break;
    1510              : 
    1511           12 :     case ElementType::CLEF:
    1512           12 :         EditClef::undoChangeClef(tx, score(), staff, this, toClef(e)->clefType());
    1513           12 :         delete e;
    1514           12 :         break;
    1515              : 
    1516            0 :     case ElementType::KEYSIG:
    1517              :     {
    1518            0 :         KeySigEvent k = toKeySig(e)->keySigEvent();
    1519            0 :         delete e;
    1520              : 
    1521            0 :         if (data.modifiers & ControlModifier) {
    1522              :             // apply only to this stave
    1523            0 :             EditKeySig::undoChangeKeySig(tx, score(), staff, tick(), k);
    1524              :         } else {
    1525              :             // apply to all staves:
    1526            0 :             for (Staff* s : score()->staves()) {
    1527            0 :                 EditKeySig::undoChangeKeySig(tx, score(), s, tick(), k);
    1528              :             }
    1529              :         }
    1530              : 
    1531            0 :         break;
    1532            0 :     }
    1533              : 
    1534            4 :     case ElementType::TIMESIG: {
    1535            4 :         EditTimeSig::addTimeSig(tx, score(), this, staffIdx, toTimeSig(e), data.modifiers & ControlModifier);
    1536            4 :         break;
    1537              :     }
    1538              : 
    1539            0 :     case ElementType::SPACER:
    1540              :     {
    1541            0 :         Spacer* spacer = toSpacer(e);
    1542            0 :         spacer->setTrack(trackZeroVoice(data.track));
    1543            0 :         spacer->setOwnershipParent(this);
    1544            0 :         if (spacer->spacerType() == SpacerType::FIXED) {
    1545            0 :             double gap = spatium() * 10;
    1546            0 :             System* s = system();
    1547            0 :             const staff_idx_t nextVisStaffIdx = s->nextVisibleStaff(staffIdx);
    1548            0 :             const bool systemEnd = (nextVisStaffIdx == muse::nidx);
    1549            0 :             if (systemEnd) {
    1550            0 :                 System* ns = 0;
    1551            0 :                 for (System* ts : score()->systems()) {
    1552            0 :                     if (ns) {
    1553            0 :                         ns = ts;
    1554            0 :                         break;
    1555              :                     }
    1556            0 :                     if (ts == s) {
    1557            0 :                         ns = ts;
    1558              :                     }
    1559              :                 }
    1560            0 :                 if (ns == s) {
    1561            0 :                     double y1 = s->staffYpage(staffIdx);
    1562            0 :                     double y2 = s->page()->height() - s->page()->bm();
    1563            0 :                     gap = y2 - y1 - score()->staff(staffIdx)->staffHeight();
    1564            0 :                 } else if (ns && ns->page() == s->page()) {
    1565            0 :                     double y1 = s->staffYpage(staffIdx);
    1566            0 :                     double y2 = ns->staffYpage(0);
    1567            0 :                     gap = y2 - y1 - score()->staff(staffIdx)->staffHeight();
    1568              :                 }
    1569              :             } else {
    1570            0 :                 double y1 = s->staffYpage(staffIdx);
    1571            0 :                 double y2 = s->staffYpage(nextVisStaffIdx);
    1572            0 :                 gap = y2 - y1 - score()->staff(staffIdx)->staffHeight();
    1573              :             }
    1574            0 :             spacer->setGap(Spatium::fromAbsolute(gap, spatium()));
    1575              :         }
    1576            0 :         score()->undoAddElement(spacer);
    1577            0 :         triggerLayout();
    1578            0 :         return spacer;
    1579              :     }
    1580              : 
    1581            1 :     case ElementType::BAR_LINE:
    1582              :     {
    1583            1 :         BarLine* bl = toBarLine(e);
    1584              : 
    1585            1 :         if (bl->playCount() != -1) {
    1586            0 :             undoChangeProperty(Pid::REPEAT_COUNT, bl->playCount());
    1587              :         }
    1588              : 
    1589              :         // if dropped bar line refers to span rather than to subtype
    1590              :         // or if Ctrl key used
    1591            1 :         if ((bl->spanFrom() && bl->spanTo()) || data.control()) {
    1592              :             // get existing bar line for this staff, and drop the change to it
    1593            0 :             Segment* seg = undoGetSegmentR(SegmentType::EndBarLine, ticks());
    1594            0 :             BarLine* cbl = toBarLine(seg->element(trackZeroVoice(data.track)));
    1595            0 :             if (cbl) {
    1596            0 :                 return cbl->drop(tx, data);
    1597              :             }
    1598            1 :         } else if (bl->barLineType() == BarLineType::START_REPEAT) {
    1599            0 :             Measure* m2 = isMMRest() ? mmRestFirst() : this;
    1600            0 :             for (size_t stIdx = 0; stIdx < score()->nstaves(); ++stIdx) {
    1601            0 :                 if (m2->isMeasureRepeatGroupWithPrevM(stIdx)) {
    1602            0 :                     MScore::setError(MsError::CANNOT_SPLIT_MEASURE_REPEAT);
    1603            0 :                     return nullptr;
    1604              :                 }
    1605              :             }
    1606            0 :             for (Score* lscore : score()->scoreList()) {
    1607            0 :                 Measure* lmeasure = lscore->tick2measure(m2->tick());
    1608            0 :                 if (lmeasure) {
    1609            0 :                     lmeasure->undoChangeProperty(Pid::REPEAT_START, true);
    1610              :                 }
    1611            0 :             }
    1612            1 :         } else if (bl->barLineType() == BarLineType::END_REPEAT) {
    1613            0 :             Measure* m2 = isMMRest() ? mmRestLast() : this;
    1614            0 :             for (size_t stIdx = 0; stIdx < score()->nstaves(); ++stIdx) {
    1615            0 :                 if (m2->isMeasureRepeatGroupWithNextM(stIdx)) {
    1616            0 :                     MScore::setError(MsError::CANNOT_SPLIT_MEASURE_REPEAT);
    1617            0 :                     return nullptr;
    1618              :                 }
    1619              :             }
    1620            0 :             for (Score* lscore : score()->scoreList()) {
    1621            0 :                 Measure* lmeasure = lscore->tick2measure(m2->tick());
    1622            0 :                 if (lmeasure) {
    1623            0 :                     lmeasure->undoChangeProperty(Pid::REPEAT_END, true);
    1624              :                 }
    1625            0 :             }
    1626            0 :             score()->undoUpdatePlayCountText(this);
    1627            1 :         } else if (bl->barLineType() == BarLineType::END_START_REPEAT) {
    1628            0 :             Measure* m2 = isMMRest() ? mmRestLast() : this;
    1629            0 :             for (size_t stIdx = 0; stIdx < score()->nstaves(); ++stIdx) {
    1630            0 :                 if (m2->isMeasureRepeatGroupWithNextM(stIdx)) {
    1631            0 :                     MScore::setError(MsError::CANNOT_SPLIT_MEASURE_REPEAT);
    1632            0 :                     return nullptr;
    1633              :                 }
    1634              :             }
    1635            0 :             for (Score* lscore : score()->scoreList()) {
    1636            0 :                 Measure* lmeasure = lscore->tick2measure(m2->tick());
    1637            0 :                 if (lmeasure) {
    1638            0 :                     lmeasure->undoChangeProperty(Pid::REPEAT_END, true);
    1639            0 :                     lmeasure = lmeasure->nextMeasure();
    1640            0 :                     if (lmeasure) {
    1641            0 :                         lmeasure->undoChangeProperty(Pid::REPEAT_START, true);
    1642              :                     }
    1643              :                 }
    1644            0 :             }
    1645              :         } else {
    1646            1 :             Segment* seg = undoGetSegmentR(SegmentType::EndBarLine, ticks());
    1647              :             // if any staff lacks a barline, create one
    1648            2 :             for (size_t stIdx = 0; stIdx < score()->nstaves(); ++stIdx) {
    1649            1 :                 BarLine* staffBarLine = toBarLine(seg->element(stIdx * VOICES));
    1650            1 :                 if (!staffBarLine) {
    1651            0 :                     staffBarLine = Factory::createBarLine(seg);
    1652            0 :                     staffBarLine->setTrack(stIdx * VOICES);
    1653            0 :                     undoAddElement(staffBarLine);
    1654              :                 }
    1655              :             }
    1656              :             // drop to barline
    1657            1 :             return seg->element(trackZeroVoice(data.track))->drop(tx, data);
    1658              :         }
    1659            0 :         break;
    1660              :     }
    1661              : 
    1662            0 :     case ElementType::MEASURE_REPEAT:
    1663              :     {
    1664            0 :         int numMeasures = toMeasureRepeat(e)->numMeasures();
    1665            0 :         delete e;
    1666            0 :         EditMeasureRepeat::addMeasureRepeat(tx, score(), this, numMeasures, staffIdx);
    1667            0 :         break;
    1668              :     }
    1669            0 :     case ElementType::ACTION_ICON:
    1670            0 :         switch (toActionIcon(e)->actionType()) {
    1671            0 :         case ActionIconType::VFRAME:
    1672            0 :             return score()->insertBox(ElementType::VBOX, this);
    1673            0 :         case ActionIconType::HFRAME:
    1674            0 :             return score()->insertBox(ElementType::HBOX, this);
    1675            0 :         case ActionIconType::TFRAME:
    1676            0 :             return score()->insertBox(ElementType::TBOX, this);
    1677            0 :         case ActionIconType::FFRAME:
    1678              :         {
    1679            0 :             Score::InsertMeasureOptions options;
    1680            0 :             options.cloneBoxToAllParts = false;
    1681            0 :             return score()->insertBox(ElementType::FBOX, this, options);
    1682              :         }
    1683            0 :         case ActionIconType::MEASURE:
    1684            0 :             score()->insertMeasure(ElementType::MEASURE, this);
    1685            0 :             break;
    1686            0 :         case ActionIconType::STAFF_TYPE_CHANGE: {
    1687            0 :             if (!canAddStaffTypeChange(staffIdx)) {
    1688            0 :                 return nullptr;
    1689              :             }
    1690            0 :             EngravingItem* stc = Factory::createStaffTypeChange(this);
    1691            0 :             stc->setTrack(trackZeroVoice(data.track));
    1692            0 :             score()->undoAddElement(stc);
    1693            0 :             break;
    1694              :         }
    1695            0 :         default:
    1696            0 :             return MeasureBase::drop(tx, data);
    1697              :         }
    1698            0 :         break;
    1699              : 
    1700            0 :     case ElementType::STAFFTYPE_CHANGE:
    1701              :     {
    1702            0 :         e->setOwnershipParent(this);
    1703            0 :         e->setTrack(trackZeroVoice(data.track));
    1704            0 :         score()->undoAddElement(e);
    1705              :     }
    1706            0 :     break;
    1707              : 
    1708            0 :     case ElementType::VBOX:
    1709              :     case ElementType::TBOX:
    1710              :     case ElementType::FBOX:
    1711              :     case ElementType::HBOX:
    1712            0 :         return score()->insertBox(toMeasureBase(e), this);
    1713              : 
    1714            0 :     default:
    1715            0 :         return MeasureBase::drop(tx, data);
    1716              :     }
    1717              : 
    1718           16 :     return nullptr;
    1719              : }
    1720              : 
    1721              : //---------------------------------------------------------
    1722              : //   adjustToLen
    1723              : //    change actual measure len, adjust elements to
    1724              : //    new len
    1725              : //---------------------------------------------------------
    1726              : 
    1727           60 : void Measure::adjustToLen(Fraction nf, bool appendRestsIfNecessary)
    1728              : {
    1729           60 :     Fraction ol   = ticks();
    1730           60 :     Fraction nl   = nf;
    1731           60 :     Fraction diff = nl - ol;
    1732              : 
    1733           60 :     Fraction startTick = endTick();
    1734           60 :     if (diff < Fraction(0, 1)) {
    1735           38 :         startTick += diff;
    1736              :     }
    1737              : 
    1738           60 :     score()->undoInsertTime(startTick, diff);
    1739              : 
    1740          124 :     for (Score* s : score()->scoreList()) {
    1741           64 :         if (diff < Fraction(0, 1)) {
    1742           42 :             EditStaveSharing::removeStaveSharingStatesOnRemoveTime(s->transactionManager()->currentOrDummyTransaction(), s, startTick,
    1743           84 :                                                                    startTick - diff);
    1744              :         }
    1745           64 :         s->undo(new InsertTime(s, startTick, diff));
    1746           64 :         Measure* m = s->tick2measure(tick());
    1747           64 :         s->undo(new ChangeMeasureLen(m, nf));
    1748           64 :         if (nl > ol) {
    1749              :             // move EndBarLine, TimeSigAnnounce, KeySigAnnounce
    1750           19 :             for (Segment* seg = m->first(); seg; seg = seg->next()) {
    1751            6 :                 if (seg->segmentType() & (SegmentType::EndBarLine | SegmentType::CourtesyTimeSigTypes | SegmentType::CourtesyKeySigTypes)) {
    1752            0 :                     seg->setRtick(nl);
    1753              :                 }
    1754              :             }
    1755              :         }
    1756           60 :     }
    1757           60 :     Score* s      = score()->masterScore();
    1758           60 :     Measure* m    = s->tick2measure(tick());
    1759           60 :     std::vector<staff_idx_t> sl = s->uniqueStaves();
    1760              : 
    1761          133 :     for (staff_idx_t staffIdx : sl) {
    1762           73 :         int rests  = 0;
    1763           73 :         int chords = 0;
    1764           73 :         Rest* rest = 0;
    1765           95 :         for (Segment* segment = m->first(); segment; segment = segment->next()) {
    1766           22 :             track_idx_t strack = staffIdx * VOICES;
    1767           22 :             track_idx_t etrack = strack + VOICES;
    1768          110 :             for (track_idx_t track = strack; track < etrack; ++track) {
    1769           88 :                 EngravingItem* e = segment->element(track);
    1770           88 :                 if (e) {
    1771           22 :                     if (e->isRest()) {
    1772            0 :                         ++rests;
    1773            0 :                         rest = toRest(e);
    1774           22 :                     } else if (e->isChord()) {
    1775            6 :                         ++chords;
    1776              :                     }
    1777              :                 }
    1778              :             }
    1779              :         }
    1780           73 :         Fraction stretch = s->staff(staffIdx)->timeStretch(tick());
    1781              :         // if just a single rest
    1782           73 :         if (rests == 1 && chords == 0) {
    1783              :             // if measure value didn't change, stick to whole measure rest
    1784            0 :             if (m_timesig == nf) {
    1785            0 :                 rest->undoChangeProperty(Pid::DURATION, nf * stretch);
    1786            0 :                 rest->undoChangeProperty(Pid::DURATION_TYPE_WITH_DOTS, DurationTypeWithDots(DurationType::V_MEASURE));
    1787              :             } else {          // if measure value did change, represent with rests actual measure value
    1788              :                 // convert the measure duration in a list of values (no dots for rests)
    1789            0 :                 std::vector<TDuration> durList = toRhythmicDurationList(nf * stretch,
    1790              :                                                                         /*isRest=*/ true,
    1791            0 :                                                                         /*rtickStart=*/ Fraction(0, 1),
    1792            0 :                                                                         /*nominal=*/ score()->sigmap()->timesig(tick().ticks()).nominal(),
    1793              :                                                                         /*measure=*/ this,
    1794              :                                                                         /*maxDots=*/ 0,
    1795            0 :                                                                         stretch);
    1796            0 :                 if (durList.empty()) {
    1797            0 :                     LOGD("Could not make durations for: %d/%d", (nf * stretch).numerator(), (nf * stretch).denominator());
    1798            0 :                     continue;
    1799              :                 }
    1800              :                 // set the existing rest to the first value of the duration list
    1801            0 :                 TDuration firstDur = durList[0];
    1802            0 :                 rest->undoChangeProperty(Pid::DURATION, firstDur.isMeasure() ? ticks() : firstDur.fraction());
    1803            0 :                 rest->undoChangeProperty(Pid::DURATION_TYPE_WITH_DOTS, durList[0].typeWithDots());
    1804              : 
    1805              :                 // add rests for any other duration list value
    1806            0 :                 Fraction tickOffset = tick() + rest->actualTicks();
    1807            0 :                 for (unsigned i = 1; i < durList.size(); i++) {
    1808            0 :                     Rest* newRest = Factory::createRest(s->dummy());
    1809            0 :                     TDuration dur = durList.at(i);
    1810            0 :                     newRest->setDurationType(dur);
    1811            0 :                     newRest->setTicks(dur.isMeasure() ? ticks() : dur.fraction());
    1812            0 :                     newRest->setTrack(rest->track());
    1813            0 :                     score()->undoAddCR(newRest, this, tickOffset);
    1814            0 :                     tickOffset += newRest->actualTicks();
    1815              :                 }
    1816            0 :             }
    1817            0 :             continue;
    1818            0 :         }
    1819              : 
    1820           73 :         track_idx_t strack = staffIdx * VOICES;
    1821           73 :         track_idx_t etrack = strack + VOICES;
    1822              : 
    1823          365 :         for (track_idx_t trk = strack; trk < etrack; ++trk) {
    1824          292 :             Fraction n = diff;
    1825          292 :             bool rFlag = false;
    1826          292 :             if (n < Fraction(0, 1)) {
    1827          199 :                 for (Segment* segment = m->last(); segment;) {
    1828            8 :                     Segment* pseg = segment->prev();
    1829            8 :                     if (segment->segmentType() == SegmentType::ChordRest) {
    1830            8 :                         const auto annotations = segment->annotations(); // make a copy since we alter the list
    1831            8 :                         for (EngravingItem* a : annotations) {
    1832            0 :                             if (a->track() == trk) {
    1833            0 :                                 s->undoRemoveElement(a);
    1834              :                             }
    1835              :                         }
    1836            8 :                         EngravingItem* e = segment->element(trk);
    1837            8 :                         if (e && e->isChordRest()) {
    1838            2 :                             ChordRest* cr = toChordRest(e);
    1839            2 :                             if (cr->durationType() == DurationType::V_MEASURE) {
    1840            0 :                                 Fraction actualTicks = cr->actualTicks();
    1841            0 :                                 n += actualTicks;
    1842            0 :                                 cr->setDurationType(TDuration(actualTicks));
    1843              :                             } else {
    1844            2 :                                 n += cr->actualTicks();
    1845              :                             }
    1846            2 :                             s->undoRemoveElement(e);
    1847            2 :                             if (n >= Fraction(0, 1)) {
    1848            1 :                                 break;
    1849              :                             }
    1850              :                         }
    1851            8 :                     } else if (segment->segmentType() == SegmentType::Breath) {
    1852            0 :                         EngravingItem* e = segment->element(trk);
    1853            0 :                         if (e) {
    1854            0 :                             s->undoRemoveElement(e);
    1855              :                         }
    1856              :                     }
    1857            7 :                     segment = pseg;
    1858              :                 }
    1859          192 :                 rFlag = true;
    1860              :             }
    1861          292 :             voice_idx_t voice = trk % VOICES;
    1862          292 :             if (appendRestsIfNecessary && (n > Fraction(0, 1)) && (rFlag || voice == 0)) {
    1863              :                 // add rest to measure
    1864            1 :                 Fraction rtick = tick() + nl - n;
    1865            1 :                 track_idx_t track = staffIdx * VOICES + voice;
    1866            1 :                 s->setRest(rtick, track, n * stretch, false, 0, false);
    1867              :             }
    1868              :         }
    1869              :     }
    1870           60 :     if (diff < Fraction(0, 1)) {
    1871              :         //
    1872              :         //  CHECK: do not remove all slurs
    1873              :         //
    1874           38 :         for (EngravingItem* e : m->el()) {
    1875            0 :             if (e->isSlur()) {
    1876            0 :                 s->undoRemoveElement(e);
    1877              :             }
    1878              :         }
    1879              :     }
    1880              : 
    1881           60 :     Selection& sel = score()->selection();
    1882           60 :     if (!sel.isRange()) {
    1883           60 :         return;
    1884              :     }
    1885              :     // If the selection started and/or ended inside this measure, we should pass it
    1886              :     // updated start/end segments...
    1887            0 :     const Segment* oldStart = sel.startSegment();
    1888            0 :     if (oldStart && sel.startMeasureBase() == this) {
    1889            0 :         Segment* newStart = this->tick2segment(oldStart->tick());
    1890            0 :         sel.setStartSegment(newStart ? newStart : first());
    1891              :     }
    1892            0 :     const Segment* oldEnd = sel.endSegment();
    1893            0 :     if (oldEnd && sel.endMeasureBase() == this) {
    1894            0 :         Segment* newEnd = this->tick2segment(oldEnd->tick());
    1895            0 :         sel.setEndSegment(newEnd ? newEnd : last()->next1());
    1896              :     }
    1897           60 : }
    1898              : 
    1899              : //---------------------------------------------------------
    1900              : //   visible
    1901              : //---------------------------------------------------------
    1902              : 
    1903        84190 : bool Measure::visible(staff_idx_t staffIdx) const
    1904              : {
    1905        84190 :     if (staffIdx >= score()->staves().size()) {
    1906            0 :         LOGD("Measure::visible: bad staffIdx: %zu", staffIdx);
    1907            0 :         return false;
    1908              :     }
    1909        84190 :     if (system() && (system()->staves().empty() || !system()->staff(staffIdx)->show())) {
    1910         2030 :         return false;
    1911              :     }
    1912        82160 :     if (score()->staff(staffIdx)->cutaway() && isEmpty(staffIdx)) {
    1913            0 :         return false;
    1914              :     }
    1915        82160 :     return score()->staff(staffIdx)->show() && m_mstaves[staffIdx]->visible();
    1916              : }
    1917              : 
    1918              : //---------------------------------------------------------
    1919              : //   stemless
    1920              : //---------------------------------------------------------
    1921              : 
    1922      3002135 : bool Measure::stemless(staff_idx_t staffIdx) const
    1923              : {
    1924      3002135 :     const Staff* staff = score()->staff(staffIdx);
    1925      3002135 :     if (!staff) {
    1926           15 :         return false;
    1927              :     }
    1928              : 
    1929      3002120 :     return staff->stemless(tick()) || m_mstaves[staffIdx]->stemless() || staff->staffType(tick())->stemless();
    1930              : }
    1931              : 
    1932              : //---------------------------------------------------------
    1933              : //   isFinalMeasureOfSection
    1934              : //    returns true if this measure is final actual measure of a section
    1935              : //    takes into consideration fact that subsequent measures base objects
    1936              : //    may have section break before encountering next actual measure
    1937              : //---------------------------------------------------------
    1938              : 
    1939            0 : bool Measure::isFinalMeasureOfSection() const
    1940              : {
    1941            0 :     const MeasureBase* mb = static_cast<const MeasureBase*>(this);
    1942              : 
    1943              :     do {
    1944            0 :         if (mb->sectionBreak()) {
    1945            0 :             return true;
    1946              :         }
    1947              : 
    1948            0 :         mb = mb->next();
    1949            0 :     } while (mb && !mb->isMeasure());           // loop until reach next actual measure or end of score
    1950              : 
    1951            0 :     return false;
    1952              : }
    1953              : 
    1954       212294 : LayoutBreak* Measure::sectionBreakElement(bool includeNextFrames) const
    1955              : {
    1956       212294 :     const MeasureBase* mb = static_cast<const MeasureBase*>(this);
    1957              : 
    1958              :     do {
    1959       213460 :         if (LayoutBreak* sectionBreak = mb->sectionBreakElement()) {
    1960          373 :             return sectionBreak;
    1961              :         }
    1962              : 
    1963       213087 :         mb = mb->next();
    1964       213087 :     } while (includeNextFrames && mb && !mb->isMeasure());           // loop until reach next actual measure or end of score
    1965              : 
    1966       211921 :     return nullptr;
    1967              : }
    1968              : 
    1969              : //---------------------------------------------------------
    1970              : //   isAnacrusis
    1971              : //---------------------------------------------------------
    1972              : 
    1973        93488 : bool Measure::isAnacrusis() const
    1974              : {
    1975        93488 :     const MeasureBase* pm = prev();
    1976              : 
    1977        93488 :     if (excludeFromNumbering() || !pm || pm->isBox() || pm->lineBreak() || pm->pageBreak() || pm->sectionBreak()) {
    1978        32406 :         if (timesig() - ticks() > Fraction(0, 1)) {
    1979          117 :             return true;
    1980              :         }
    1981              :     }
    1982        93371 :     return false;
    1983              : }
    1984              : 
    1985              : //---------------------------------------------------------
    1986              : //   isFirstInSystem
    1987              : //---------------------------------------------------------
    1988              : 
    1989       644091 : bool Measure::isFirstInSystem() const
    1990              : {
    1991       644091 :     IF_ASSERT_FAILED(system()) {
    1992            0 :         return false;
    1993              :     }
    1994       644091 :     return system()->firstMeasure() == this;
    1995              : }
    1996              : 
    1997       187454 : bool Measure::isLastInSystem() const
    1998              : {
    1999       187454 :     IF_ASSERT_FAILED(system()) {
    2000            0 :         return false;
    2001              :     }
    2002       187454 :     return system()->lastMeasure() == this;
    2003              : }
    2004              : 
    2005              : //---------------------------------------------------------
    2006              : //   isFirstInSection
    2007              : //---------------------------------------------------------
    2008              : 
    2009       162809 : bool Measure::isFirstInSection() const
    2010              : {
    2011       174803 :     for (MeasureBase* m = prev(); m; m = m->prev()) {
    2012       153667 :         if (m->sectionBreak()) {
    2013          207 :             return true;
    2014       153460 :         } else if (m->isMeasure()) {
    2015       141466 :             return false;
    2016              :         }
    2017              :     }
    2018        21136 :     return true;
    2019              : }
    2020              : 
    2021              : //---------------------------------------------------------
    2022              : //   scanElements
    2023              : //---------------------------------------------------------
    2024              : 
    2025         3368 : void Measure::scanElements(std::function<void(EngravingItem*)> func)
    2026              : {
    2027         3368 :     size_t nstaves = score()->nstaves();
    2028         3368 :     if (nstaves == 0) {
    2029           64 :         return;
    2030              :     }
    2031              : 
    2032         3304 :     MeasureBase::scanElements(func);
    2033              : 
    2034         7397 :     for (staff_idx_t staffIdx = 0; staffIdx < nstaves; ++staffIdx) {
    2035         4093 :         MStaff* ms = m_mstaves[staffIdx];
    2036         4093 :         MeasureNumber* measureNumber = ms->measureNumber();
    2037         4093 :         if (measureNumber && measureNumber->systemFlag() && showMeasureNumberOnStaff(staffIdx)) {
    2038          361 :             func(measureNumber);
    2039              :         }
    2040              : 
    2041         4093 :         if (!(visible(staffIdx) && score()->staff(staffIdx)->show()) && !isCutawayClef(staffIdx)) {
    2042          117 :             continue;
    2043              :         }
    2044              : 
    2045         3976 :         if (measureNumber && !measureNumber->systemFlag()) {
    2046            0 :             func(measureNumber);
    2047              :         }
    2048              : 
    2049         3976 :         func(ms->lines());
    2050         3976 :         if (ms->vspacerUp()) {
    2051            0 :             func(ms->vspacerUp());
    2052              :         }
    2053         3976 :         if (ms->vspacerDown()) {
    2054            0 :             func(ms->vspacerDown());
    2055              :         }
    2056              : 
    2057         3976 :         if (ms->mmRangeText()) {
    2058            0 :             func(ms->mmRangeText());
    2059              :         }
    2060              :     }
    2061              : 
    2062        13485 :     for (Segment* s = first(); s; s = s->next()) {
    2063        10181 :         if (!s->enabled()) {
    2064           53 :             continue;
    2065              :         }
    2066        10128 :         s->scanElements(func);
    2067              :     }
    2068              : }
    2069              : 
    2070              : //---------------------------------------------------------
    2071              : //   connectTremolo
    2072              : ///   Connect two-notes tremolo and update duration types
    2073              : ///   for the involved chords.
    2074              : //---------------------------------------------------------
    2075              : 
    2076        52624 : void Measure::connectTremolo()
    2077              : {
    2078        52624 :     const size_t ntracks = score()->ntracks();
    2079        52624 :     constexpr SegmentType st = SegmentType::ChordRest;
    2080       159449 :     for (Segment* s = first(st); s; s = s->next(st)) {
    2081       881685 :         for (track_idx_t i = 0; i < ntracks; ++i) {
    2082       774860 :             EngravingItem* e = s->element(i);
    2083       774860 :             if (!e || !e->isChord()) {
    2084       691174 :                 continue;
    2085              :             }
    2086              : 
    2087        83686 :             Chord* c = toChord(e);
    2088        83686 :             TremoloTwoChord* tremolo = c->tremoloTwoChord();
    2089        83686 :             if (tremolo) {
    2090              :                 // Ensure correct duration type for chord
    2091          264 :                 c->setDurationType(tremolo->durationType());
    2092              : 
    2093              :                 // If it is the first tremolo's chord, find the second
    2094              :                 // chord for tremolo, if needed.
    2095          264 :                 if (!tremolo->chord1()) {
    2096           32 :                     tremolo->setChords(c, tremolo->chord2());
    2097          232 :                 } else if (tremolo->chord1() != c || tremolo->chord2()) {
    2098          229 :                     continue;
    2099              :                 }
    2100              : 
    2101           35 :                 for (Segment* ls = s->next(st); ls; ls = ls->next(st)) {
    2102           35 :                     if (EngravingItem* element = ls->element(i)) {
    2103           35 :                         if (!element->isChord()) {
    2104            0 :                             LOGD("cannot connect tremolo");
    2105            0 :                             continue;
    2106              :                         }
    2107           35 :                         Chord* nc = toChord(element);
    2108           35 :                         tremolo->setChords(c, nc);
    2109           35 :                         nc->setTremoloTwoChord(tremolo);
    2110           35 :                         break;
    2111              :                     }
    2112              :                 }
    2113              : 
    2114           35 :                 if (!tremolo->chord2()) {
    2115              :                     // this is an invalid tremolo! a continued tremolo was started on one note without a valid next note in that measure
    2116              :                     // remove the tremolo entirely
    2117            0 :                     c->setTremoloTwoChord(nullptr);
    2118            0 :                     score()->removeElement(tremolo);
    2119              :                 }
    2120              :             }
    2121              :         }
    2122              :     }
    2123        52624 : }
    2124              : 
    2125              : //---------------------------------------------------------
    2126              : //   createVoice
    2127              : //    Create a voice on demand by filling the measure
    2128              : //    with a whole measure rest.
    2129              : //    Check if there are any chord/rests in track; if
    2130              : //    not create a whole measure rest
    2131              : //---------------------------------------------------------
    2132              : 
    2133            0 : void Measure::createVoice(int track)
    2134              : {
    2135            0 :     for (Segment* s = first(); s; s = s->next()) {
    2136            0 :         if (s->segmentType() != SegmentType::ChordRest) {
    2137            0 :             continue;
    2138              :         }
    2139            0 :         if (s->element(track) == 0) {
    2140            0 :             score()->setRest(s->tick(), track, ticks(), true, 0);
    2141              :         }
    2142            0 :         break;
    2143              :     }
    2144            0 : }
    2145              : 
    2146              : //---------------------------------------------------------
    2147              : //   sortStaves
    2148              : //---------------------------------------------------------
    2149              : 
    2150           36 : void Measure::sortStaves(std::vector<staff_idx_t>& dst)
    2151              : {
    2152           36 :     std::vector<MStaff*> ms;
    2153           36 :     ms.reserve(dst.size());
    2154          140 :     for (staff_idx_t idx : dst) {
    2155          104 :         ms.push_back(m_mstaves[idx]);
    2156              :     }
    2157           36 :     m_mstaves = ms;
    2158              : 
    2159          140 :     for (staff_idx_t staffIdx = 0; staffIdx < m_mstaves.size(); ++staffIdx) {
    2160          104 :         m_mstaves[staffIdx]->lines()->setTrack(staffIdx * VOICES);
    2161              :     }
    2162          102 :     for (Segment& s : m_segments) {
    2163           66 :         s.sortStaves(dst);
    2164              :     }
    2165              : 
    2166           36 :     for (EngravingItem* e : el()) {
    2167            0 :         if (e->track() == muse::nidx || e->isTopSystemObject()) {
    2168            0 :             continue;
    2169              :         }
    2170            0 :         voice_idx_t voice    = e->voice();
    2171            0 :         staff_idx_t staffIdx = e->staffIdx();
    2172            0 :         staff_idx_t idx = muse::indexOf(dst, staffIdx);
    2173            0 :         e->setTrack(idx * VOICES + voice);
    2174              :     }
    2175              : 
    2176           36 :     score()->undoUpdatePlayCountText(this);
    2177           36 : }
    2178              : 
    2179              : //---------------------------------------------------------
    2180              : //   checkMultiVoices
    2181              : ///   Check for more than on voice in this measure and staff and
    2182              : ///   set MStaff->hasVoices
    2183              : //---------------------------------------------------------
    2184              : 
    2185         6084 : void Measure::checkMultiVoices(staff_idx_t staffIdx)
    2186              : {
    2187         6084 :     if (hasVoices(staffIdx, tick(), ticks())) {
    2188          949 :         m_mstaves[staffIdx]->setHasVoices(true);
    2189              :     } else {
    2190         5135 :         m_mstaves[staffIdx]->setHasVoices(false);
    2191              :     }
    2192         6084 : }
    2193              : 
    2194              : //---------------------------------------------------------
    2195              : //   hasVoices
    2196              : //---------------------------------------------------------
    2197              : 
    2198       141527 : bool Measure::hasVoices(staff_idx_t staffIdx, Fraction stick, Fraction len, bool considerInvisible) const
    2199              : {
    2200       141527 :     Staff* st = score()->staff(staffIdx);
    2201       141527 :     if (st->isTabStaff(stick)) {
    2202              :         // TODO: tab staves use different rules for stem direction etc
    2203              :         // see for example https://musescore.org/en/node/308371
    2204              :         // we should consider coming up with a more comprehensive solution
    2205              :         // but for now, we are forcing measures on tab staves to be consider as a whole -
    2206              :         // either they have voices or not
    2207              :         // (rather than checking tick ranges)
    2208         1998 :         stick = tick();
    2209         1998 :         len = stretchedLen(st);
    2210              :     }
    2211              : 
    2212       141527 :     track_idx_t strack = staffIdx * VOICES + 1;
    2213       141527 :     track_idx_t etrack = staffIdx * VOICES + VOICES;
    2214       141527 :     Fraction etick = stick + len;
    2215              : 
    2216       477573 :     for (Segment* s = first(SegmentType::ChordRest); s; s = s->next(SegmentType::ChordRest)) {
    2217       387928 :         if (s->tick() >= etick) {
    2218        42128 :             break;
    2219              :         }
    2220      1355079 :         for (track_idx_t track = strack; track < etrack; ++track) {
    2221      1019033 :             ChordRest* cr = toChordRest(s->element(track));
    2222      1019033 :             if (cr) {
    2223        23382 :                 if (cr->endTick() <= stick) {
    2224        13082 :                     continue;
    2225              :                 }
    2226        10300 :                 if (considerInvisible) {
    2227         1116 :                     return true;
    2228              :                 }
    2229         9184 :                 bool v = false;
    2230         9184 :                 if (cr->isChord()) {
    2231         7547 :                     Chord* c = toChord(cr);
    2232              : 
    2233              :                     // consider a chord visible if stem, hook(s) or beam(s) are visible
    2234        14747 :                     if ((c->stem() && c->stem()->visible())
    2235          363 :                         || (c->hook() && c->hook()->visible())
    2236        15094 :                         || (c->beam() && c->beam()->visible())) {
    2237         7184 :                         v = true;
    2238              :                     } else {
    2239              :                         // or any of its notes
    2240          379 :                         for (Note* n : c->notes()) {
    2241          363 :                             if (n->visible()) {
    2242          347 :                                 v = true;
    2243          347 :                                 break;
    2244              :                             }
    2245              :                         }
    2246              :                     }
    2247         1637 :                 } else if (cr->isRest()) {
    2248         1637 :                     v = cr->visible() && !toRest(cr)->isGap();
    2249              :                 }
    2250         9184 :                 if (v) {
    2251         8638 :                     return true;
    2252              :                 }
    2253              :             }
    2254              :         }
    2255              :     }
    2256       131773 :     return false;
    2257              : }
    2258              : 
    2259              : //---------------------------------------------------------
    2260              : //   hasVoice
    2261              : //---------------------------------------------------------
    2262              : 
    2263         3762 : bool Measure::hasVoice(track_idx_t track) const
    2264              : {
    2265         3762 :     if (track >= score()->ntracks()) {
    2266            0 :         return false;
    2267              :     }
    2268        21027 :     for (Segment* s = first(); s; s = s->next()) {
    2269        18107 :         if (s->segmentType() != SegmentType::ChordRest) {
    2270         7102 :             continue;
    2271              :         }
    2272        11005 :         if (s->element(track)) {
    2273          842 :             return true;
    2274              :         }
    2275              :     }
    2276         2920 :     return false;
    2277              : }
    2278              : 
    2279              : //-------------------------------------------------------------------
    2280              : //   isEmpty
    2281              : ///   Check if the measure is filled by a full-measure rest, or is
    2282              : ///   full of rests on this staff, that may have fermatas on them.
    2283              : ///   If staff is -1, then check for all staves.
    2284              : //-------------------------------------------------------------------
    2285              : 
    2286         1791 : bool Measure::isEmpty(staff_idx_t staffIdx) const
    2287              : {
    2288         1791 :     if (isMMRest()) {
    2289            0 :         return true;
    2290              :     }
    2291         1791 :     track_idx_t strack = 0;
    2292         1791 :     track_idx_t etrack = 0;
    2293         1791 :     if (staffIdx == muse::nidx) {
    2294            0 :         strack = 0;
    2295            0 :         etrack = score()->nstaves() * VOICES;
    2296              :     } else {
    2297         1791 :         strack = staffIdx * VOICES;
    2298         1791 :         etrack = strack + VOICES;
    2299              :     }
    2300         1791 :     if (isMeasureRepeatGroup(staffIdx)) {
    2301            0 :         return false;
    2302              :     }
    2303         5081 :     for (Segment* s = first(SegmentType::ChordRest); s; s = s->next(SegmentType::ChordRest)) {
    2304        16824 :         for (track_idx_t track = strack; track < etrack; ++track) {
    2305        13532 :             EngravingItem* e = s->element(track);
    2306        13532 :             if (e && !e->isRest()) {
    2307          337 :                 return false;
    2308              :             }
    2309              :             // Check for cross-staff chords
    2310        13195 :             bool hasStaves = score()->staff(track / VOICES)->part()->staves().size() > 1;
    2311        13195 :             if (hasStaves) {
    2312        13123 :                 if (strack >= VOICES) {
    2313        11992 :                     e = s->element(track - VOICES);
    2314        11992 :                     if (e && !e->isRest() && e->vStaffIdx() == staffIdx) {
    2315            0 :                         return false;
    2316              :                     }
    2317              :                 }
    2318        13123 :                 if (etrack < score()->nstaves() * VOICES) {
    2319        13087 :                     e = s->element(track + VOICES);
    2320        13087 :                     if (e && !e->isRest() && e->vStaffIdx() == staffIdx) {
    2321            0 :                         return false;
    2322              :                     }
    2323              :                 }
    2324              :             }
    2325              :         }
    2326         3602 :         for (EngravingItem* a : s->annotations()) {
    2327          312 :             if (a && staffIdx == muse::nidx) {
    2328            2 :                 return false;
    2329              :             }
    2330          312 :             if (!a || a->systemFlag() || !a->visible() || a->isFermata()) {
    2331            0 :                 continue;
    2332              :             }
    2333          312 :             size_t atrack = a->track();
    2334          312 :             if (atrack >= strack && atrack < etrack) {
    2335            2 :                 return false;
    2336              :             }
    2337              :         }
    2338              :     }
    2339         1452 :     return true;
    2340              : }
    2341              : 
    2342              : //---------------------------------------------------------
    2343              : //   isCutawayClef
    2344              : ///    Check for empty measure with only
    2345              : ///    a Courtesy Clef before End Bar Line
    2346              : //---------------------------------------------------------
    2347              : 
    2348        51039 : bool Measure::isCutawayClef(staff_idx_t staffIdx) const
    2349              : {
    2350        51039 :     if (!score()->staff(staffIdx) || !m_mstaves[staffIdx]) {
    2351            0 :         return false;
    2352              :     }
    2353        51039 :     bool empty = (score()->staff(staffIdx)->cutaway() && isEmpty(staffIdx)) || !m_mstaves[staffIdx]->visible();
    2354        51039 :     if (!empty) {
    2355        51035 :         return false;
    2356              :     }
    2357              :     track_idx_t strack;
    2358              :     track_idx_t etrack;
    2359            4 :     if (staffIdx == muse::nidx) {
    2360            0 :         strack = 0;
    2361            0 :         etrack = score()->nstaves() * VOICES;
    2362              :     } else {
    2363            4 :         strack = staffIdx * VOICES;
    2364            4 :         etrack = strack + VOICES;
    2365              :     }
    2366              :     // find segment before EndBarLine
    2367            4 :     Segment* s = nullptr;
    2368            4 :     for (Segment* ls = last(); ls; ls = ls->prev()) {
    2369            4 :         if (ls->segmentType() == SegmentType::EndBarLine) {
    2370            4 :             s = ls->prev();
    2371            4 :             break;
    2372              :         }
    2373              :     }
    2374            4 :     while (s && (s->isTimeTickType() || s->isBreathType())) {
    2375            0 :         s = s->prev();
    2376              :     }
    2377            4 :     if (!s) {
    2378            0 :         return false;
    2379              :     }
    2380           20 :     for (track_idx_t track = strack; track < etrack; ++track) {
    2381           16 :         EngravingItem* e = s->element(track);
    2382           16 :         if (!e || !e->isClef()) {
    2383           16 :             continue;
    2384              :         }
    2385            0 :         if ((nextMeasure() && (nextMeasure()->system() == system())) || toClef(e)->showCourtesy()) {
    2386            0 :             return true;
    2387              :         }
    2388              :     }
    2389            4 :     return false;
    2390              : }
    2391              : 
    2392              : //---------------------------------------------------------
    2393              : //   isFullMeasureRest
    2394              : //    Check for an empty measure, filled with full measure
    2395              : //    rests.
    2396              : //---------------------------------------------------------
    2397              : 
    2398           91 : bool Measure::isFullMeasureRest() const
    2399              : {
    2400           91 :     track_idx_t strack = 0;
    2401           91 :     track_idx_t etrack = score()->nstaves() * VOICES;
    2402              : 
    2403           91 :     Segment* s = first(SegmentType::ChordRest);
    2404          536 :     for (track_idx_t track = strack; track < etrack; ++track) {
    2405          469 :         EngravingItem* e = s->element(track);
    2406          469 :         if (e) {
    2407          136 :             if (!e->isRest()) {
    2408           23 :                 return false;
    2409              :             }
    2410          113 :             Rest* rest = toRest(e);
    2411          113 :             if (rest->durationType().type() != DurationType::V_MEASURE) {
    2412            1 :                 return false;
    2413              :             }
    2414              :         }
    2415              :     }
    2416           67 :     return true;
    2417              : }
    2418              : 
    2419              : //---------------------------------------------------------
    2420              : //   isEmpty
    2421              : //---------------------------------------------------------
    2422              : 
    2423            0 : bool Measure::empty() const
    2424              : {
    2425            0 :     if (excludeFromNumbering()) {
    2426            0 :         return false;
    2427              :     }
    2428            0 :     int n = 0;
    2429            0 :     track_idx_t tracks = m_mstaves.size() * VOICES;
    2430              :     static const SegmentType st = SegmentType::ChordRest;
    2431            0 :     for (const Segment* s = first(st); s; s = s->next(st)) {
    2432            0 :         bool restFound = false;
    2433            0 :         for (track_idx_t track = 0; track < tracks; ++track) {
    2434            0 :             if ((track % VOICES) == 0 && !score()->staff(track / VOICES)->show()) {
    2435            0 :                 track += VOICES - 1;
    2436            0 :                 continue;
    2437              :             }
    2438            0 :             if (s->element(track)) {
    2439            0 :                 if (!s->element(track)->isRest()) {
    2440            0 :                     return false;
    2441              :                 }
    2442            0 :                 restFound = true;
    2443              :             }
    2444              :         }
    2445            0 :         if (restFound) {
    2446            0 :             ++n;
    2447              :         }
    2448              :         // measure is not empty if there is more than one rest
    2449            0 :         if (n > 1) {
    2450            0 :             return false;
    2451              :         }
    2452              :     }
    2453            0 :     return true;
    2454              : }
    2455              : 
    2456              : //---------------------------------------------------------
    2457              : //   isOnlyRests
    2458              : //---------------------------------------------------------
    2459              : 
    2460           38 : bool Measure::isOnlyRests(track_idx_t track) const
    2461              : {
    2462              :     static const SegmentType st = SegmentType::ChordRest;
    2463           95 :     for (const Segment* s = first(st); s; s = s->next(st)) {
    2464           67 :         if (s->segmentType() != st || !s->element(track)) {
    2465           41 :             continue;
    2466              :         }
    2467           26 :         if (!s->element(track)->isRest()) {
    2468           10 :             return false;
    2469              :         }
    2470              :     }
    2471           28 :     return true;
    2472              : }
    2473              : 
    2474              : //---------------------------------------------------------
    2475              : //   isOnlyGapRests
    2476              : //---------------------------------------------------------
    2477              : 
    2478            7 : bool Measure::isOnlyGapRests(track_idx_t track) const
    2479              : {
    2480              :     static const SegmentType st { SegmentType::ChordRest };
    2481            9 :     for (const Segment* s = first(st); s; s = s->next(st)) {
    2482            9 :         if (s->segmentType() != st || !s->element(track)) {
    2483            0 :             continue;
    2484              :         }
    2485            9 :         if (!s->element(track)->isRest() || !toRest(s->element(track))->isGap()) {
    2486            7 :             return false;
    2487              :         }
    2488              :     }
    2489            0 :     return true;
    2490              : }
    2491              : 
    2492              : //---------------------------------------------------------
    2493              : //   stretchedLen
    2494              : //---------------------------------------------------------
    2495              : 
    2496         4480 : Fraction Measure::stretchedLen(const Staff* staff) const
    2497              : {
    2498         4480 :     return ticks() * staff->timeStretch(tick());
    2499              : }
    2500              : 
    2501              : //---------------------------------------------------------
    2502              : //   cloneMeasure
    2503              : //---------------------------------------------------------
    2504              : 
    2505            0 : Measure* Measure::cloneMeasure(Score* sc, const Fraction& tick, TieMap* tieMap)
    2506              : {
    2507            0 :     Measure* m      = new Measure(sc);
    2508            0 :     m->m_timesig    = m_timesig;
    2509            0 :     m->m_len         = m_len;
    2510            0 :     m->m_repeatCount = m_repeatCount;
    2511              : 
    2512            0 :     assert(sc->staves().size() >= m_mstaves.size());   // destination score we're cloning into must have at least as many staves as measure being cloned
    2513              : 
    2514            0 :     m->setMeasureNumber(measureNumber());
    2515            0 :     m->setMeasureNumberOffset(measureNumberOffset());
    2516            0 :     m->setExcludeFromNumbering(excludeFromNumbering());
    2517            0 :     m->m_userStretch           = m_userStretch;
    2518            0 :     m->m_breakMultiMeasureRest = m_breakMultiMeasureRest;
    2519            0 :     m->m_playbackCount         = m_playbackCount;
    2520              : 
    2521            0 :     m->setTick(tick);
    2522            0 :     m->setLineBreak(lineBreak());
    2523            0 :     m->setPageBreak(pageBreak());
    2524            0 :     m->setSectionBreak(sectionBreak());
    2525              : 
    2526            0 :     m->setHeader(header());
    2527            0 :     m->setTrailer(trailer());
    2528              : 
    2529            0 :     size_t tracks = sc->nstaves() * VOICES;
    2530            0 :     TupletMap tupletMap;
    2531              : 
    2532            0 :     for (Segment* oseg = first(); oseg; oseg = oseg->next()) {
    2533            0 :         Segment* s = Factory::createSegment(m, oseg->segmentType(), oseg->rtick());
    2534            0 :         s->setEnabled(oseg->enabled());
    2535            0 :         s->setVisible(oseg->visible());
    2536            0 :         s->setHeader(oseg->header());
    2537            0 :         s->setTrailer(oseg->trailer());
    2538              : 
    2539            0 :         m->m_segments.push_back(s);
    2540            0 :         for (track_idx_t track = 0; track < tracks; ++track) {
    2541            0 :             EngravingItem* oe = oseg->element(track);
    2542            0 :             for (EngravingItem* e : oseg->annotations()) {
    2543            0 :                 if (e->generated() || e->track() != track) {
    2544            0 :                     continue;
    2545              :                 }
    2546            0 :                 EngravingItem* ne = e->clone();
    2547            0 :                 ne->setTrack(track);
    2548            0 :                 ne->setOffset(e->offset());
    2549            0 :                 ne->setScore(sc);
    2550            0 :                 s->add(ne);
    2551              :             }
    2552            0 :             if (!oe) {
    2553            0 :                 continue;
    2554              :             }
    2555            0 :             EngravingItem* ne = oe->clone();
    2556            0 :             if (oe->isChordRest()) {
    2557            0 :                 ChordRest* ocr = toChordRest(oe);
    2558            0 :                 ChordRest* ncr = toChordRest(ne);
    2559            0 :                 Tuplet* ot     = ocr->tuplet();
    2560            0 :                 if (ot) {
    2561            0 :                     Tuplet* nt = tupletMap.findNew(ot);
    2562            0 :                     if (nt == 0) {
    2563            0 :                         nt = Factory::copyTuplet(*ot);
    2564            0 :                         nt->clear();
    2565            0 :                         nt->setTrack(track);
    2566            0 :                         nt->setScore(sc);
    2567            0 :                         nt->setOwnershipParent(m);
    2568            0 :                         nt->setTick(m->tick() + ot->rtick());
    2569            0 :                         tupletMap.add(ot, nt);
    2570              :                     }
    2571            0 :                     ncr->setTuplet(nt);
    2572            0 :                     nt->add(ncr);
    2573              :                 }
    2574            0 :                 if (oe->isChord()) {
    2575            0 :                     Chord* och = toChord(ocr);
    2576            0 :                     Chord* nch = toChord(ncr);
    2577            0 :                     size_t n = och->notes().size();
    2578            0 :                     for (size_t i = 0; i < n; ++i) {
    2579            0 :                         Note* on = och->notes().at(i);
    2580            0 :                         Note* nn = nch->notes().at(i);
    2581            0 :                         if (on->tieFor()) {
    2582            0 :                             Tie* tie = on->tieFor()->clone();
    2583            0 :                             tie->setScore(sc);
    2584            0 :                             nn->setTieFor(tie);
    2585            0 :                             tie->setStartNote(nn);
    2586            0 :                             tieMap->add(on->tieFor(), tie);
    2587              :                         }
    2588            0 :                         if (on->tieBack()) {
    2589            0 :                             Tie* tie = tieMap->findNew(on->tieBack());
    2590            0 :                             if (tie) {
    2591            0 :                                 nn->setTieBack(tie);
    2592            0 :                                 tie->setEndNote(nn);
    2593              :                             } else {
    2594            0 :                                 LOGD("cloneMeasure: cannot find tie, track %zu", track);
    2595              :                             }
    2596              :                         }
    2597              :                     }
    2598              :                 }
    2599              :             }
    2600            0 :             ne->setOffset(oe->offset());
    2601            0 :             ne->setScore(sc);
    2602            0 :             s->add(ne);
    2603              :         }
    2604              :     }
    2605            0 :     for (EngravingItem* e : el()) {
    2606            0 :         EngravingItem* ne = e->clone();
    2607            0 :         ne->setScore(sc);
    2608            0 :         ne->setOffset(e->offset());
    2609            0 :         m->add(ne);
    2610              :     }
    2611            0 :     return m;
    2612            0 : }
    2613              : 
    2614              : //---------------------------------------------------------
    2615              : //   snap
    2616              : //---------------------------------------------------------
    2617              : 
    2618            0 : Fraction Measure::snap(const Fraction& tick, const PointF p) const
    2619              : {
    2620            0 :     Segment* s = first();
    2621            0 :     for (; s->next(); s = s->next()) {
    2622            0 :         double x  = s->x();
    2623            0 :         double dx = s->next()->x() - x;
    2624            0 :         if (s->tick() == tick) {
    2625            0 :             x += dx / 3.0 * 2.0;
    2626            0 :         } else if (s->next()->tick() == tick) {
    2627            0 :             x += dx / 3.0;
    2628              :         } else {
    2629            0 :             x += dx * .5;
    2630              :         }
    2631            0 :         if (p.x() < x) {
    2632            0 :             break;
    2633              :         }
    2634              :     }
    2635            0 :     return s->tick();
    2636              : }
    2637              : 
    2638              : //---------------------------------------------------------
    2639              : //   snapNote
    2640              : //---------------------------------------------------------
    2641              : 
    2642            0 : Fraction Measure::snapNote(const Fraction& /*tick*/, const PointF p, int staff) const
    2643              : {
    2644            0 :     Segment* s = first();
    2645              :     for (;;) {
    2646            0 :         Segment* ns = s->next();
    2647            0 :         while (ns && ns->element(staff) == 0) {
    2648            0 :             ns = ns->next();
    2649              :         }
    2650            0 :         if (ns == 0) {
    2651            0 :             break;
    2652              :         }
    2653            0 :         double x  = s->x();
    2654            0 :         double nx = x + (ns->x() - x) * .5;
    2655            0 :         if (p.x() < nx) {
    2656            0 :             break;
    2657              :         }
    2658            0 :         s = ns;
    2659            0 :     }
    2660            0 :     return s->tick();
    2661              : }
    2662              : 
    2663              : //---------------------------------------------------------
    2664              : //   searchSegment
    2665              : ///   Finds a segment which x position is most close to the
    2666              : ///   given \p x.
    2667              : ///   \param x The x coordinate in measure coordinates.
    2668              : ///   \param st Type of segments to search.
    2669              : ///   \param strack start of track range (strack included)
    2670              : ///   in which the found segment should contain elements.
    2671              : ///   \param etrack end of track range (etrack excluded)
    2672              : ///   in which the found segment should contain elements.
    2673              : ///   \param preferredSegment If not nullptr, will give
    2674              : ///   more space to the given segment when searching it by
    2675              : ///   coordinate.
    2676              : ///   \returns The segment that was found.
    2677              : //---------------------------------------------------------
    2678              : 
    2679            0 : Segment* Measure::searchSegment(double x, SegmentType st, track_idx_t strack, track_idx_t etrack, const Segment* preferredSegment,
    2680              :                                 double spacingFactor) const
    2681              : {
    2682            0 :     const track_idx_t lastTrack = etrack - 1;
    2683            0 :     for (Segment* segment = first(st); segment; segment = segment->next(st)) {
    2684            0 :         if (!segment->hasElements(strack, lastTrack)) {
    2685            0 :             continue;
    2686              :         }
    2687            0 :         if (segment->isTimeTickType() && segment->rtick() == ticks()) {
    2688            0 :             continue;
    2689              :         }
    2690            0 :         Segment* ns = segment->next(st);
    2691            0 :         for (; ns; ns = ns->next(st)) {
    2692            0 :             if (ns->hasElements(strack, lastTrack)) {
    2693            0 :                 break;
    2694              :             }
    2695              :         }
    2696            0 :         if (!ns) {
    2697            0 :             return segment;
    2698              :         }
    2699            0 :         if (preferredSegment == segment) {
    2700            0 :             if (x < (segment->x() + (ns->x() - segment->x()))) {
    2701            0 :                 return segment;
    2702              :             }
    2703            0 :         } else if (preferredSegment == ns) {
    2704            0 :             if (x <= segment->x()) {
    2705            0 :                 return segment;
    2706              :             }
    2707              :         } else {
    2708            0 :             if (x < (segment->x() + (ns->x() - segment->x()) * spacingFactor)) {
    2709            0 :                 return segment;
    2710              :             }
    2711              :         }
    2712              :     }
    2713            0 :     return nullptr;
    2714              : }
    2715              : 
    2716              : //---------------------------------------------------------
    2717              : //   getProperty
    2718              : //---------------------------------------------------------
    2719              : 
    2720        61421 : PropertyValue Measure::getProperty(Pid propertyId) const
    2721              : {
    2722        61421 :     switch (propertyId) {
    2723           26 :     case Pid::TIMESIG_NOMINAL:
    2724           26 :         return PropertyValue::fromValue(m_timesig);
    2725           14 :     case Pid::TIMESIG_ACTUAL:
    2726           14 :         return PropertyValue::fromValue(m_len);
    2727        12041 :     case Pid::EXCLUDE_FROM_NUMBERING:
    2728        12041 :         return excludeFromNumbering();
    2729        12041 :     case Pid::MEASURE_NUMBER_OFFSET:
    2730        12041 :         return measureNumberOffset();
    2731        12041 :     case Pid::MEASURE_NUMBER_MODE:
    2732        12041 :         return int(measureNumberMode());
    2733        12049 :     case Pid::BREAK_MMR:
    2734        12049 :         return breakMultiMeasureRest();
    2735            2 :     case Pid::REPEAT_COUNT:
    2736            2 :         return repeatCount();
    2737        12039 :     case Pid::USER_STRETCH:
    2738        12039 :         return userStretch();
    2739           40 :     case Pid::REPEAT_END:
    2740           40 :         return repeatEnd();
    2741           79 :     case Pid::REPEAT_START:
    2742           79 :         return repeatStart();
    2743            2 :     case Pid::REPEAT_JUMP:
    2744            2 :         return repeatJump();
    2745         1047 :     default:
    2746         1047 :         return MeasureBase::getProperty(propertyId);
    2747              :     }
    2748              : }
    2749              : 
    2750              : //---------------------------------------------------------
    2751              : //   setProperty
    2752              : //---------------------------------------------------------
    2753              : 
    2754          268 : bool Measure::setProperty(Pid propertyId, const PropertyValue& value)
    2755              : {
    2756          268 :     switch (propertyId) {
    2757            3 :     case Pid::TIMESIG_NOMINAL:
    2758            3 :         m_timesig = value.value<Fraction>();
    2759            3 :         break;
    2760            3 :     case Pid::TIMESIG_ACTUAL:
    2761            3 :         m_len = value.value<Fraction>();
    2762            3 :         break;
    2763            0 :     case Pid::EXCLUDE_FROM_NUMBERING:
    2764            0 :         setExcludeFromNumbering(value.toBool());
    2765            0 :         triggerLayoutAll();
    2766            0 :         return true;
    2767            0 :     case Pid::MEASURE_NUMBER_OFFSET:
    2768            0 :         setMeasureNumberOffset(value.toInt());
    2769            0 :         triggerLayoutAll();
    2770            0 :         return true;
    2771            0 :     case Pid::MEASURE_NUMBER_MODE:
    2772            0 :         setMeasureNumberMode(MeasureNumberMode(value.toInt()));
    2773            0 :         break;
    2774            2 :     case Pid::BREAK_MMR:
    2775            2 :         setBreakMultiMeasureRest(value.toBool());
    2776            2 :         break;
    2777            0 :     case Pid::REPEAT_COUNT:
    2778            0 :         setRepeatCount(value.toInt());
    2779            0 :         score()->undoUpdatePlayCountText(this);
    2780            0 :         break;
    2781            0 :     case Pid::USER_STRETCH:
    2782            0 :         setUserStretch(value.toDouble());
    2783            0 :         break;
    2784            9 :     case Pid::REPEAT_END:
    2785            9 :         setRepeatEnd(value.toBool());
    2786            9 :         score()->invalidateRepeatList();
    2787            9 :         break;
    2788            5 :     case Pid::REPEAT_START:
    2789            5 :         setRepeatStart(value.toBool());
    2790            5 :         score()->invalidateRepeatList();
    2791            5 :         break;
    2792          246 :     case Pid::REPEAT_JUMP:
    2793          246 :         setRepeatJump(value.toBool());
    2794          246 :         score()->invalidateRepeatList();
    2795          246 :         break;
    2796            0 :     default:
    2797            0 :         return MeasureBase::setProperty(propertyId, value);
    2798              :     }
    2799          268 :     triggerLayout();
    2800          268 :     return true;
    2801              : }
    2802              : 
    2803              : //---------------------------------------------------------
    2804              : //   propertyDefault
    2805              : //---------------------------------------------------------
    2806              : 
    2807        61380 : PropertyValue Measure::propertyDefault(Pid propertyId) const
    2808              : {
    2809        61380 :     switch (propertyId) {
    2810            0 :     case Pid::TIMESIG_NOMINAL:
    2811              :     case Pid::TIMESIG_ACTUAL:
    2812            0 :         return PropertyValue();
    2813        12039 :     case Pid::EXCLUDE_FROM_NUMBERING:
    2814        12039 :         return false;
    2815        12039 :     case Pid::MEASURE_NUMBER_OFFSET:
    2816        12039 :         return 0;
    2817        12039 :     case Pid::MEASURE_NUMBER_MODE:
    2818        12039 :         return int(MeasureNumberMode::AUTO);
    2819        12039 :     case Pid::BREAK_MMR:
    2820        12039 :         return false;
    2821            0 :     case Pid::REPEAT_COUNT:
    2822            0 :         return 2;
    2823        12039 :     case Pid::USER_STRETCH:
    2824        12039 :         return 1.0;
    2825            0 :     case Pid::REPEAT_END:
    2826              :     case Pid::REPEAT_START:
    2827              :     case Pid::REPEAT_JUMP:
    2828            0 :         return false;
    2829         1185 :     default:
    2830         1185 :         break;
    2831              :     }
    2832         1185 :     return MeasureBase::propertyDefault(propertyId);
    2833              : }
    2834              : 
    2835              : //---------------------------------------------------------
    2836              : //   undoChangeProperty
    2837              : //---------------------------------------------------------
    2838              : 
    2839           58 : void Measure::undoChangeProperty(Pid id, const PropertyValue& v)
    2840              : {
    2841           58 :     undoChangeProperty(id, v, propertyFlags(id));
    2842           58 : }
    2843              : 
    2844           58 : void Measure::undoChangeProperty(Pid id, const PropertyValue& v, PropertyFlags ps)
    2845              : {
    2846           58 :     if ((getProperty(id) == v) && (propertyFlags(id) == ps)) {
    2847           38 :         return;
    2848              :     }
    2849           20 :     if (id == Pid::REPEAT_START || id == Pid::REPEAT_END) {
    2850              :         // change also coresponding mmRestMeasure
    2851           12 :         Measure* m = const_cast<Measure*>(toMeasure(this)->coveringMMRestOrThis());
    2852           12 :         if (m != this) {
    2853            0 :             m->undoChangeProperty(id, v);
    2854              :         }
    2855              :     }
    2856           20 :     EngravingObject::undoChangeProperty(id, v, ps);
    2857              : }
    2858              : 
    2859              : //-------------------------------------------------------------------
    2860              : //   mmRestFirst
    2861              : //    this is a multi measure rest
    2862              : //    returns first measure of replaced sequence of empty measures
    2863              : //-------------------------------------------------------------------
    2864              : 
    2865          760 : Measure* Measure::mmRestFirst() const
    2866              : {
    2867          760 :     assert(isMMRest());
    2868          760 :     if (prev()) {
    2869          702 :         return toMeasure(prev()->next());
    2870              :     }
    2871           58 :     return score()->firstMeasure();
    2872              : }
    2873              : 
    2874              : //-------------------------------------------------------------------
    2875              : //   mmRestLast
    2876              : //    this is a multi measure rest
    2877              : //    returns last measure of replaced sequence of empty measures
    2878              : //-------------------------------------------------------------------
    2879              : 
    2880         4457 : Measure* Measure::mmRestLast() const
    2881              : {
    2882         4457 :     assert(isMMRest());
    2883         4457 :     if (next()) {
    2884         4288 :         return toMeasure(next()->prev());
    2885              :     }
    2886          169 :     return score()->lastMeasure();
    2887              : }
    2888              : 
    2889              : //---------------------------------------------------------
    2890              : //   coveringMMRestOrThis
    2891              : //    if multi-measure rests are enabled,
    2892              : //        and this measure is covered by an MMRest,
    2893              : //            return that MMRest.
    2894              : //    otherwise, return the measure itself.
    2895              : //---------------------------------------------------------
    2896              : 
    2897       520182 : Measure* Measure::coveringMMRestOrThis()
    2898              : {
    2899       520182 :     if (!style().styleB(Sid::createMultiMeasureRests)) {
    2900       512020 :         return this;
    2901              :     }
    2902              : 
    2903         8162 :     if (m_mmRest) {
    2904          735 :         return m_mmRest;
    2905              :     }
    2906              : 
    2907         7427 :     if (isMMRest()) {
    2908          265 :         return this;
    2909              :     }
    2910              : 
    2911         7162 :     const Measure* m = this;
    2912        60897 :     while (m && !m->m_mmRest) {
    2913        53735 :         m = m->prevMeasure();
    2914              :     }
    2915              : 
    2916         7162 :     if (m && !m->m_mmRest->mmRestLast()->isBefore(this)) {
    2917         1255 :         return m->m_mmRest;
    2918              :     }
    2919              : 
    2920         5907 :     return this;
    2921              : }
    2922              : 
    2923        25008 : const Measure* Measure::coveringMMRestOrThis() const
    2924              : {
    2925        25008 :     return const_cast<Measure*>(this)->coveringMMRestOrThis();
    2926              : }
    2927              : 
    2928       429435 : int Measure::measureRepeatCount(staff_idx_t staffIdx) const
    2929              : {
    2930       429435 :     if (staffIdx >= m_mstaves.size()) {
    2931            0 :         return 0;
    2932              :     }
    2933              : 
    2934       429435 :     return m_mstaves[staffIdx]->measureRepeatCount();
    2935              : }
    2936              : 
    2937           11 : bool Measure::containsMeasureRepeat(const staff_idx_t staffIdxFrom, const staff_idx_t staffIdxTo) const
    2938              : {
    2939           22 :     for (staff_idx_t idx = staffIdxFrom; idx <= staffIdxTo; ++idx) {
    2940           11 :         if (measureRepeatCount(idx) > 0) {
    2941            0 :             return true;
    2942              :         }
    2943              :     }
    2944              : 
    2945           11 :     return false;
    2946              : }
    2947              : 
    2948         4767 : void Measure::setMeasureRepeatCount(int n, staff_idx_t staffIdx)
    2949              : {
    2950         4767 :     if (staffIdx >= m_mstaves.size()) {
    2951            0 :         return;
    2952              :     }
    2953              : 
    2954         4767 :     m_mstaves[staffIdx]->setMeasureRepeatCount(n);
    2955              : }
    2956              : 
    2957        20499 : bool Measure::isMeasureRepeatGroup(staff_idx_t staffIdx) const
    2958              : {
    2959        20499 :     return measureRepeatCount(staffIdx) > 0;
    2960              : }
    2961              : 
    2962              : //---------------------------------------------------------
    2963              : //   isMeasureRepeatGroupWithNextM
    2964              : //    true if this and next measure are part of same MeasureRepeat group
    2965              : //---------------------------------------------------------
    2966              : 
    2967          133 : bool Measure::isMeasureRepeatGroupWithNextM(staff_idx_t staffIdx) const
    2968              : {
    2969          133 :     if (!isMeasureRepeatGroup(staffIdx) || !nextMeasure() || !nextMeasure()->isMeasureRepeatGroup(staffIdx)) {
    2970          127 :         return false;
    2971              :     }
    2972            6 :     if (measureRepeatCount(staffIdx) == nextMeasure()->measureRepeatCount(staffIdx) - 1) {
    2973            3 :         return true;
    2974              :     }
    2975            3 :     return false;
    2976              : }
    2977              : 
    2978              : //---------------------------------------------------------
    2979              : //   isMeasureRepeatGroupWithPrevM
    2980              : //    true if this and prev measure are part of same MeasureRepeat group
    2981              : //---------------------------------------------------------
    2982              : 
    2983          318 : bool Measure::isMeasureRepeatGroupWithPrevM(staff_idx_t staffIdx) const
    2984              : {
    2985          318 :     return measureRepeatCount(staffIdx) > 1;
    2986              : }
    2987              : 
    2988              : //---------------------------------------------------------
    2989              : //   firstMeasureOfGroup
    2990              : //    for measures within group containing MeasureRepeat,
    2991              : //    return the measure (possibly this) at start of group
    2992              : //---------------------------------------------------------
    2993              : 
    2994         3742 : Measure* Measure::firstOfMeasureRepeatGroup(staff_idx_t staffIdx) const
    2995              : {
    2996         3742 :     if (!isMeasureRepeatGroup(staffIdx)) {
    2997         3314 :         return nullptr;
    2998              :     }
    2999          428 :     Measure* m = const_cast<Measure*>(this);
    3000          593 :     for (int i = 1; i < measureRepeatCount(staffIdx); ++i) {
    3001          165 :         m = m->prevMeasure();
    3002              :     }
    3003          428 :     return m;
    3004              : }
    3005              : 
    3006              : //---------------------------------------------------------
    3007              : //   measureRepeatElement
    3008              : //    access MeasureRepeat element from anywhere in related group
    3009              : //---------------------------------------------------------
    3010              : 
    3011         3535 : MeasureRepeat* Measure::measureRepeatElement(staff_idx_t staffIdx) const
    3012              : {
    3013         3535 :     Measure* m = firstOfMeasureRepeatGroup(staffIdx);
    3014         3535 :     if (!m) {
    3015         3314 :         return nullptr;
    3016              :     }
    3017          308 :     while (m && m->isMeasureRepeatGroup(staffIdx)) {
    3018          308 :         track_idx_t strack = staff2track(staffIdx);
    3019          308 :         track_idx_t etrack = staff2track(staffIdx + 1);
    3020          656 :         for (track_idx_t track = strack; track < etrack; ++track) {
    3021              :             // should only be in first track, but just in case
    3022         1141 :             for (Segment* s = m->first(SegmentType::ChordRest); s; s = s->next(SegmentType::ChordRest)) {
    3023              :                 // should only be in first segment, but just in case
    3024          793 :                 EngravingItem* e = s->element(track);
    3025          793 :                 if (e && e->isMeasureRepeat()) {
    3026          221 :                     return toMeasureRepeat(e);
    3027              :                 }
    3028              :             }
    3029              :         }
    3030           87 :         Measure* nm = m->nextMeasure();
    3031           87 :         if (nm && nm->measureRepeatCount(staffIdx) == m->measureRepeatCount(staffIdx) + 1) {
    3032           87 :             m = nm;
    3033              :         } else {
    3034            0 :             break;
    3035              :         }
    3036              :     }
    3037            0 :     return nullptr;
    3038              : }
    3039              : 
    3040              : //---------------------------------------------------------
    3041              : //   measureRepeatNumMeasures
    3042              : //---------------------------------------------------------
    3043              : 
    3044          776 : int Measure::measureRepeatNumMeasures(staff_idx_t staffIdx) const
    3045              : {
    3046          776 :     MeasureRepeat* el = measureRepeatElement(staffIdx);
    3047          776 :     if (!el) {
    3048          674 :         return 0;
    3049              :     }
    3050          102 :     return el->numMeasures();
    3051              : }
    3052              : 
    3053              : //---------------------------------------------------------
    3054              : //   isOneMeasureRepeat
    3055              : //---------------------------------------------------------
    3056              : 
    3057            0 : bool Measure::isOneMeasureRepeat(staff_idx_t staffIdx) const
    3058              : {
    3059            0 :     return measureRepeatNumMeasures(staffIdx) == 1;
    3060              : }
    3061              : 
    3062              : //---------------------------------------------------------
    3063              : //   nextIsOneMeasureRepeat
    3064              : //---------------------------------------------------------
    3065              : 
    3066            0 : bool Measure::nextIsOneMeasureRepeat(staff_idx_t staffIdx) const
    3067              : {
    3068            0 :     if (!nextMeasure()) {
    3069            0 :         return false;
    3070              :     }
    3071            0 :     return nextMeasure()->isOneMeasureRepeat(staffIdx);
    3072              : }
    3073              : 
    3074              : //---------------------------------------------------------
    3075              : //   prevIsOneMeasureRepeat
    3076              : //---------------------------------------------------------
    3077              : 
    3078            0 : bool Measure::prevIsOneMeasureRepeat(staff_idx_t staffIdx) const
    3079              : {
    3080            0 :     if (!prevMeasure()) {
    3081            0 :         return false;
    3082              :     }
    3083            0 :     return prevMeasure()->isOneMeasureRepeat(staffIdx);
    3084              : }
    3085              : 
    3086       764530 : ChordRest* Measure::lastChordRest(track_idx_t track) const
    3087              : {
    3088      3451437 :     for (const Segment* seg = last(); seg; seg = seg->prev()) {
    3089      2896439 :         if (!seg->isChordRestType()) {
    3090      1249269 :             continue;
    3091              :         }
    3092      1647170 :         ChordRest* cr = seg->cr(track);
    3093      1647170 :         if (!cr) {
    3094      1437638 :             continue;
    3095              :         }
    3096              : 
    3097       209532 :         return cr;
    3098              :     }
    3099       554998 :     return nullptr;
    3100              : }
    3101              : 
    3102      1045728 : ChordRest* Measure::firstChordRest(track_idx_t track) const
    3103              : {
    3104      4732001 :     for (const Segment* seg = first(); seg; seg = seg->next()) {
    3105      3957312 :         if (!seg->isChordRestType()) {
    3106      1837108 :             continue;
    3107              :         }
    3108      2120204 :         ChordRest* cr = seg->cr(track);
    3109      2120204 :         if (!cr) {
    3110      1849165 :             continue;
    3111              :         }
    3112              : 
    3113       271039 :         return cr;
    3114              :     }
    3115       774689 :     return nullptr;
    3116              : }
    3117              : 
    3118              : //-------------------------------------------------------------------
    3119              : //   userStretch
    3120              : //-------------------------------------------------------------------
    3121              : 
    3122       203104 : double Measure::userStretch() const
    3123              : {
    3124       203104 :     return score()->layoutOptions().isMode(LayoutMode::FLOAT) ? 1.0 : m_userStretch;
    3125              : }
    3126              : 
    3127              : //---------------------------------------------------------
    3128              : //   nextElementStaff
    3129              : //---------------------------------------------------------
    3130              : 
    3131            0 : EngravingItem* Measure::nextElementStaff(staff_idx_t staff, EngravingItem* fromItem)
    3132              : {
    3133            0 :     EngravingItem* e = fromItem ? fromItem : score()->selection().element();
    3134            0 :     if (!e && !score()->selection().elements().empty()) {
    3135            0 :         e = score()->selection().elements().front();
    3136              :     }
    3137              : 
    3138              :     // handle measure elements
    3139            0 :     if (e->ownershipParent() == this) {
    3140            0 :         auto i = std::find(el().begin(), el().end(), e);
    3141            0 :         if (i != el().end()) {
    3142            0 :             if (++i != el().end()) {
    3143            0 :                 EngravingItem* resElement = *i;
    3144            0 :                 if (resElement) {
    3145            0 :                     return resElement;
    3146              :                 }
    3147              :             }
    3148              :         }
    3149              :     }
    3150              : 
    3151            0 :     for (; e && !e->isSegment(); e = e->layoutParent()) {
    3152              :     }
    3153            0 :     Segment* seg = toSegment(e);
    3154            0 :     Segment* nextSegment = seg ? seg->next() : first();
    3155            0 :     EngravingItem* next = nextSegment->firstElementOfSegment(staff);
    3156            0 :     if (next) {
    3157            0 :         return next;
    3158              :     }
    3159              : 
    3160            0 :     return Navigation::lastElement(score());
    3161              : }
    3162              : 
    3163              : //---------------------------------------------------------
    3164              : //   prevElementStaff
    3165              : //---------------------------------------------------------
    3166              : 
    3167            0 : EngravingItem* Measure::prevElementStaff(staff_idx_t staff, EngravingItem* fromItem)
    3168              : {
    3169            0 :     EngravingItem* e = fromItem ? fromItem : score()->selection().element();
    3170            0 :     if (!e && !score()->selection().elements().empty()) {
    3171            0 :         e = score()->selection().elements().front();
    3172              :     }
    3173              : 
    3174              :     // handle measure elements
    3175            0 :     if (e->ownershipParent() == this) {
    3176            0 :         auto i = std::find(el().rbegin(), el().rend(), e);
    3177            0 :         if (i != el().rend()) {
    3178            0 :             if (++i != el().rend()) {
    3179            0 :                 EngravingItem* resElement = *i;
    3180            0 :                 if (resElement) {
    3181            0 :                     return resElement;
    3182              :                 }
    3183              :             }
    3184              :         }
    3185              :     }
    3186              : 
    3187            0 :     Measure* prevM = prevMeasureMM();
    3188            0 :     if (prevM) {
    3189            0 :         Segment* seg = prevM->last();
    3190            0 :         if (seg) {
    3191            0 :             return seg->lastElementForNavigation(staff);
    3192              :         }
    3193              :     }
    3194            0 :     return Navigation::firstElement(score());
    3195              : }
    3196              : 
    3197          784 : double Measure::firstNoteRestSegmentX(bool leading) const
    3198              : {
    3199          784 :     const System* sys = system();
    3200          784 :     double margin = style().styleAbsolute(Sid::headerToLineStartDistance);
    3201         3061 :     for (const Segment* seg = first(); seg; seg = seg->next()) {
    3202         3061 :         if (seg->isChordRestType()) {
    3203          784 :             double noteRestPos = seg->measure()->pos().x() + seg->pos().x();
    3204          784 :             if (!leading) {
    3205            0 :                 return noteRestPos;
    3206              :             }
    3207              : 
    3208              :             // first CR found; back up to previous segment
    3209          784 :             seg = seg->prevActive();
    3210          988 :             while (seg && (seg->allElementsInvisible() || seg->hasTimeSigAboveStaves())) {
    3211          204 :                 seg = seg->prevActive();
    3212              :             }
    3213          784 :             if (seg) {
    3214              :                 // find maximum width
    3215          740 :                 double width = 0.0;
    3216          740 :                 size_t n = score()->nstaves();
    3217         2077 :                 for (staff_idx_t i = 0; i < n; ++i) {
    3218         1337 :                     if (!sys->staff(i)->show()) {
    3219           10 :                         continue;
    3220              :                     }
    3221         1327 :                     EngravingItem* e = seg->element(i * VOICES);
    3222         1327 :                     if (e && e->addToSkyline()) {
    3223         1315 :                         width = std::max(width, e->pos().x() + e->ldata()->bbox().right());
    3224              :                     }
    3225              :                 }
    3226          740 :                 if (seg->isStartRepeatBarLineType()) {
    3227           59 :                     margin = style().styleAbsolute(Sid::lineEndToBarlineDistance);
    3228              :                 }
    3229          740 :                 return std::min(seg->measure()->pos().x() + seg->pos().x() + width + margin, noteRestPos);
    3230           44 :             } else if (!isFirstInSystem() && !isFirstInSection() && prevMeasure()) {
    3231            6 :                 const BarLine* endBl = prevMeasure()->endBarLine();
    3232            6 :                 const Segment* endBlSeg = endBl ? endBl->segment() : nullptr;
    3233              : 
    3234            6 :                 if (!endBlSeg) {
    3235            0 :                     return noteRestPos;
    3236              :                 }
    3237              : 
    3238            6 :                 double width = 0.0;
    3239            6 :                 if (endBl && endBl->addToSkyline()) {
    3240            6 :                     width = std::max(width, endBl->pos().x() + endBl->ldata()->bbox().right());
    3241              :                 }
    3242              : 
    3243            6 :                 const double startBlMargin = style().styleAbsolute(Sid::barlineToLineStartDistance);
    3244              : 
    3245            6 :                 return std::min(endBlSeg->measure()->pos().x() + endBlSeg->pos().x() + width + startBlMargin, noteRestPos);
    3246              :             } else {
    3247           38 :                 return noteRestPos;
    3248              :             }
    3249              :         }
    3250              :     }
    3251              : 
    3252            0 :     return margin;
    3253              : }
    3254              : 
    3255          670 : double Measure::endingXForOpenEndedLines() const
    3256              : {
    3257          670 :     const System* sys = system();
    3258          670 :     double margin = style().styleAbsolute(Sid::lineEndToBarlineDistance);
    3259          670 :     double systemEndX = sys->ldata()->bbox().width();
    3260              : 
    3261          670 :     Segment* lastSeg = last();
    3262         1686 :     while (lastSeg && !lastSeg->isType(SegmentType::BarLineTypes)) {
    3263         1016 :         lastSeg = lastSeg->prevEnabled();
    3264              :     }
    3265          670 :     if (!lastSeg) {
    3266          249 :         return systemEndX - margin;
    3267              :     }
    3268              : 
    3269          421 :     return lastSeg->x() + x() - margin;
    3270              : }
    3271              : 
    3272              : //---------------------------------------------------------
    3273              : //   accessibleInfo
    3274              : //---------------------------------------------------------
    3275              : 
    3276            0 : String Measure::accessibleInfo() const
    3277              : {
    3278            0 :     return String(u"%1: %2").arg(EngravingItem::accessibleInfo(), String::number(measureNumber() + 1));
    3279              : }
    3280              : 
    3281            0 : void Measure::styleChanged()
    3282              : {
    3283            0 :     if (endBarLineType() == BarLineType::END_REPEAT) {
    3284            0 :         score()->undoUpdatePlayCountText(this);
    3285              :     }
    3286            0 :     MeasureBase::styleChanged();
    3287            0 : }
    3288              : 
    3289              : //---------------------------------------------------
    3290              : //    computeTicks
    3291              : //    set ticks for all segments
    3292              : //       return minTick
    3293              : //---------------------------------------------------
    3294              : 
    3295        33994 : void Measure::computeTicks()
    3296              : {
    3297       138170 :     for (Segment* segment = firstActive(); segment; segment = segment->nextActive()) {
    3298       104176 :         Segment* nextSegment = segment->nextActive();
    3299       104176 :         Fraction nextTick = nextSegment ? nextSegment->rtick() : ticks();
    3300       104176 :         segment->setTicks(nextTick - segment->rtick());
    3301              :     }
    3302              : 
    3303        34493 :     for (Segment* segment = first(SegmentType::TimeTick); segment; segment = segment->next(SegmentType::TimeTick)) {
    3304          499 :         segment->setTicks(Fraction(0, 1));
    3305          499 :         Segment* nextSegment = segment->next();
    3306          706 :         while (nextSegment) {
    3307          335 :             Fraction tickDiff = nextSegment->rtick() - segment->rtick();
    3308          335 :             if (!tickDiff.isZero()) {
    3309          128 :                 segment->setTicks(tickDiff);
    3310          128 :                 break;
    3311              :             }
    3312          207 :             nextSegment = nextSegment->next();
    3313              :         }
    3314          499 :         if (segment->ticks().isZero()) {
    3315          371 :             segment->setTicks(ticks() - segment->rtick());
    3316              :         }
    3317              :     }
    3318        33994 : }
    3319              : 
    3320              : //---------------------------------------------------------
    3321              : //   anacrusisOffset
    3322              : //      determine if measure is anacrusis
    3323              : //      and return tick offset relative to measure end
    3324              : //---------------------------------------------------------
    3325              : 
    3326        89941 : Fraction Measure::anacrusisOffset() const
    3327              : {
    3328        89941 :     return isAnacrusis() ? (timesig() - ticks()) : Fraction(0, 1);
    3329              : }
    3330              : 
    3331              : //---------------------------------------------------------
    3332              : //   endBarLine
    3333              : //      return the first one
    3334              : //---------------------------------------------------------
    3335              : 
    3336        14761 : const BarLine* Measure::endBarLine() const
    3337              : {
    3338        14761 :     return endBarLine(0, true);
    3339              : }
    3340              : 
    3341        14762 : const BarLine* Measure::endBarLine(staff_idx_t staffIdx, bool first) const
    3342              : {
    3343              :     // search barline segment:
    3344        14762 :     Segment* s = last();
    3345        16863 :     while (s && !s->isEndBarLineType()) {
    3346         2101 :         s = s->prev();
    3347              :     }
    3348              : 
    3349        14762 :     if (s) {
    3350        14217 :         for (const EngravingItem* e : s->elist()) {
    3351              :             // Return first barline or barline with matching staffIdx
    3352        14217 :             if (e && (e->staffIdx() == staffIdx || first)) {
    3353        14217 :                 return toBarLine(e);
    3354              :             }
    3355              :         }
    3356              :     }
    3357          545 :     return nullptr;
    3358              : }
    3359              : 
    3360              : //---------------------------------------------------------
    3361              : //   endBarLineType
    3362              : //    Assume all barlines have same type if there is more
    3363              : //    than one.
    3364              : //---------------------------------------------------------
    3365              : 
    3366         4255 : BarLineType Measure::endBarLineType() const
    3367              : {
    3368         4255 :     const BarLine* bl = endBarLine();
    3369         4255 :     return bl ? bl->barLineType() : BarLineType::NORMAL;
    3370              : }
    3371              : 
    3372              : //---------------------------------------------------------
    3373              : //   endBarLineVisible
    3374              : //    Assume all barlines have same visibility if there is more
    3375              : //    than one.
    3376              : //---------------------------------------------------------
    3377              : 
    3378         3207 : bool Measure::endBarLineVisible() const
    3379              : {
    3380         3207 :     const BarLine* bl = endBarLine();
    3381         3207 :     return bl ? bl->visible() : true;
    3382              : }
    3383              : 
    3384          111 : const BarLine* Measure::startBarLine() const
    3385              : {
    3386          111 :     return startBarLine(0, true);
    3387              : }
    3388              : 
    3389          111 : const BarLine* Measure::startBarLine(staff_idx_t staffIdx, bool firstStaff) const
    3390              : {
    3391              :     // search barline segment:
    3392          111 :     Segment* s = first();
    3393          654 :     while (s && !(s->isStartRepeatBarLineType() || s->isBeginBarLineType())) {
    3394          543 :         s = s->next();
    3395              :     }
    3396              :     // search first element
    3397          111 :     if (s) {
    3398           23 :         for (const EngravingItem* e : s->elist()) {
    3399           23 :             if (e && (e->staffIdx() == staffIdx || firstStaff)) {
    3400           23 :                 return toBarLine(e);
    3401              :             }
    3402              :         }
    3403              :     }
    3404           88 :     return nullptr;
    3405              : }
    3406              : 
    3407              : //---------------------------------------------------------
    3408              : //   triggerLayout
    3409              : //---------------------------------------------------------
    3410              : 
    3411         2840 : void Measure::triggerLayout() const
    3412              : {
    3413         2840 :     if (prev() || next()) { // avoid triggering layout before getting added to a score
    3414         1767 :         score()->setLayout(tick(), endTick(), 0, score()->nstaves() - 1, this);
    3415              :     }
    3416         2840 : }
    3417              : 
    3418            0 : void Measure::triggerLayout(staff_idx_t staffIdx) const
    3419              : {
    3420            0 :     score()->setLayout(tick(), endTick(), staffIdx, staffIdx, this);
    3421            0 : }
    3422              : 
    3423              : //---------------------------------------------------------
    3424              : //   setEndBarLineType
    3425              : //     Create a *generated* barline with the given type and
    3426              : //     properties if none exists. Modify if it exists.
    3427              : //     Useful for import filters.
    3428              : //---------------------------------------------------------
    3429              : 
    3430          692 : void Measure::setEndBarLineType(BarLineType val, track_idx_t track, bool visible, Color color)
    3431              : {
    3432          692 :     Segment* seg = undoGetSegment(SegmentType::EndBarLine, endTick());
    3433              :     // get existing bar line for this staff, if any
    3434          692 :     BarLine* bl = toBarLine(seg->element(track));
    3435          692 :     if (!bl) {
    3436              :         // no suitable bar line: create a new one
    3437          678 :         bl = Factory::createBarLine(seg);
    3438          678 :         bl->setTrack(track);
    3439          678 :         Part* part = score()->staff(track / VOICES)->part();
    3440              :         // by default, barlines for multi-staff parts should span across staves
    3441          678 :         if (part && part->nstaves() > 1) {
    3442           16 :             bl->setSpanStaff(true);
    3443              :         }
    3444          678 :         score()->addElement(bl);
    3445              :     }
    3446          692 :     bl->setGenerated(false);
    3447          692 :     bl->setBarLineType(val);
    3448          692 :     bl->setVisible(visible);
    3449          692 :     if (color.isValid()) {
    3450            0 :         bl->setColor(color);
    3451              :     }
    3452          692 : }
    3453              : 
    3454              : //---------------------------------------------------------
    3455              : //   checkHeader
    3456              : //---------------------------------------------------------
    3457              : 
    3458        12960 : void Measure::checkHeader()
    3459              : {
    3460        13352 :     for (Segment* seg = first(); seg; seg = seg->next()) {
    3461        13202 :         if (seg->enabled() && seg->header()) {
    3462        12810 :             setHeader(seg->header());
    3463        12810 :             break;
    3464              :         }
    3465              :     }
    3466        12960 : }
    3467              : 
    3468              : //---------------------------------------------------------
    3469              : //   checkTrailer
    3470              : //---------------------------------------------------------
    3471              : 
    3472        39071 : void Measure::checkTrailer()
    3473              : {
    3474       147450 :     for (Segment* seg = last(); seg != first(); seg = seg->prev()) {
    3475       109059 :         if (seg->enabled() && seg->trailer()) {
    3476          680 :             setTrailer(seg->trailer());
    3477          680 :             break;
    3478              :         }
    3479              :     }
    3480        39071 : }
    3481              : 
    3482        29718 : void Measure::checkEndOfMeasureChange()
    3483              : {
    3484        29718 :     bool found = false;
    3485        87116 :     for (Segment* seg = last(); seg != first(); seg = seg->prev()) {
    3486        57421 :         if (seg->enabled() && seg->endOfMeasureChange()) {
    3487           23 :             setEndOfMeasureChange(seg->endOfMeasureChange());
    3488           23 :             found = true;
    3489           23 :             break;
    3490              :         }
    3491              :     }
    3492              : 
    3493        29718 :     if (!found) {
    3494        29695 :         setEndOfMeasureChange(false);
    3495              :     }
    3496        29718 : }
    3497              : 
    3498            0 : bool Measure::canAddStringTunings(staff_idx_t staffIdx) const
    3499              : {
    3500            0 :     Staff* staff = score()->staff(staffIdx);
    3501            0 :     if (!staff) {
    3502            0 :         return false;
    3503              :     }
    3504              : 
    3505            0 :     if (!staff->isPrimaryStaff()) {
    3506            0 :         staff = staff->primaryStaff();
    3507            0 :         if (!staff) {
    3508            0 :             return false;
    3509              :         }
    3510              : 
    3511            0 :         staffIdx = staff->idx();
    3512              :     }
    3513              : 
    3514            0 :     const StringData* stringData = staff->originPart(tick())->instrument(tick())->stringData();
    3515            0 :     if (!stringData || stringData->frettedStrings() == 0) {
    3516            0 :         return false;
    3517              :     }
    3518              : 
    3519              :     // already a string tunings element in this measure
    3520            0 :     bool alreadyHasStringTunings = false;
    3521            0 :     for (const Segment& segment : m_segments) {
    3522            0 :         for (EngravingItem* element : segment.annotations()) {
    3523            0 :             if (element && element->isStringTunings() && element->staffIdx() == staffIdx) {
    3524            0 :                 alreadyHasStringTunings = true;
    3525            0 :                 break;
    3526              :             }
    3527              :         }
    3528              :     }
    3529              : 
    3530            0 :     return !alreadyHasStringTunings;
    3531              : }
    3532              : 
    3533            0 : bool Measure::canAddStaffTypeChange(staff_idx_t staffIdx) const
    3534              : {
    3535            0 :     if (isMMRest()) {
    3536            0 :         return false;
    3537              :     }
    3538              : 
    3539            0 :     for (const EngravingObject* child : el()) {
    3540            0 :         if (!child || !child->isStaffTypeChange()) {
    3541            0 :             continue;
    3542              :         }
    3543              : 
    3544            0 :         const StaffTypeChange* stc = toStaffTypeChange(child);
    3545            0 :         if (stc->staffIdx() == staffIdx) {
    3546              :             // Staff already has a StaffTypeChange at this measure...
    3547            0 :             return false;
    3548              :         }
    3549              :     }
    3550              : 
    3551            0 :     return true;
    3552              : }
    3553              : 
    3554            0 : Fraction Measure::maxTicks() const
    3555              : {
    3556            0 :     Segment* s = first();
    3557            0 :     Fraction maxticks = Fraction(0, 1);
    3558            0 :     if (isMMRest()) {
    3559            0 :         return timesig();
    3560              :     }
    3561            0 :     while (s) {
    3562            0 :         if (s->enabled() && s->isChordRestType()) {
    3563            0 :             maxticks = std::max(maxticks, s->ticks());
    3564              :         }
    3565            0 :         s = s->next();
    3566              :     }
    3567            0 :     return maxticks;
    3568              : }
    3569              : 
    3570        36155 : void Measure::respaceSegments()
    3571              : {
    3572        36155 :     double x = 0.0;
    3573              :     // Find starting x position (i.e. position of first relevant segment)
    3574        37055 :     for (Segment& s : m_segments) {
    3575        37055 :         if (s.enabled() && s.visible() && !s.allElementsInvisible()) {
    3576        36155 :             x = s.x();
    3577        36155 :             break;
    3578              :         }
    3579              :     }
    3580              :     // Start respacing segments
    3581       166598 :     for (Segment& s : m_segments) {
    3582       130443 :         if (s.isTimeTickType()) {
    3583          292 :             continue;
    3584              :         }
    3585       130151 :         s.mutldata()->setPosX(x);
    3586       130151 :         if (s.enabled() && s.visible() && !s.allElementsInvisible()) {
    3587       122330 :             x += s.width(LD_ACCESS::BAD);
    3588              :         }
    3589              :     }
    3590              :     // Update measure width
    3591        36155 :     setWidth(x);
    3592        36155 : }
    3593              : }
        

Generated by: LCOV version 2.0-1