LCOV - code coverage report
Current view: top level - src/engraving/editing - editmeasurerepeat.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 95.1 % 82 78
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) 2026 MuseScore Limited
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program. If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : 
      23              : #include "editmeasurerepeat.h"
      24              : 
      25              : #include <vector>
      26              : 
      27              : #include "../dom/barline.h"
      28              : #include "../dom/factory.h"
      29              : #include "../dom/measure.h"
      30              : #include "../dom/measurerepeat.h"
      31              : #include "../dom/rest.h"
      32              : #include "../dom/score.h"
      33              : #include "../dom/segment.h"
      34              : #include "../dom/select.h"
      35              : #include "../dom/staff.h"
      36              : #include "../dom/utils.h"
      37              : 
      38              : using namespace mu;
      39              : using namespace mu::engraving;
      40              : 
      41              : //---------------------------------------------------------
      42              : //   addMeasureRepeat
      43              : //---------------------------------------------------------
      44              : 
      45           11 : void EditMeasureRepeat::addMeasureRepeat(Transaction& tx, Score* score, Measure* firstMeasure, int numMeasures, staff_idx_t staffIdx)
      46              : {
      47              :     //
      48              :     // make measures into group
      49              :     //
      50           11 :     if (!makeMeasureRepeatGroup(tx, score, firstMeasure, numMeasures, staffIdx)) {
      51            0 :         return;
      52              :     }
      53              : 
      54              :     //
      55              :     // add MeasureRepeat element
      56              :     //
      57              :     int measureWithElementNo;
      58           11 :     if (numMeasures % 2) {
      59              :         // odd number, element anchored to center measure of group
      60            1 :         measureWithElementNo = numMeasures / 2 + 1;
      61              :     } else {
      62              :         // even number, element anchored to last measure in first half of group
      63           10 :         measureWithElementNo = numMeasures / 2;
      64              :     }
      65           11 :     Measure* measureWithElement = firstMeasure;
      66           14 :     for (int i = 1; i < measureWithElementNo; ++i) {
      67            3 :         measureWithElement = measureWithElement->nextMeasure();
      68              :     }
      69              :     // MeasureRepeat element will be positioned appropriately (in center of measure / on following barline)
      70              :     // when stretchMeasure() is called on measureWithElement
      71           11 :     MeasureRepeat* mr = addMeasureRepeat(tx, score, measureWithElement->tick(), staff2track(staffIdx), numMeasures);
      72           11 :     score->select(mr, SelectType::SINGLE, 0);
      73              : }
      74              : 
      75              : //---------------------------------------------------------
      76              : //   the measures of a measure repeat group
      77              : //    empty if there are not enough of them, with equal durations
      78              : //---------------------------------------------------------
      79              : 
      80           18 : static std::vector<Measure*> groupMeasures(Measure* firstMeasure, int numMeasures)
      81              : {
      82           18 :     std::vector<Measure*> measures;
      83           18 :     Measure* measure = firstMeasure;
      84           55 :     for (int i = 1; i <= numMeasures; ++i) {
      85           38 :         if (!measure || measure->ticks() != firstMeasure->ticks()) {
      86            1 :             return {};
      87              :         }
      88           37 :         measures.push_back(measure);
      89           37 :         measure = measure->nextMeasure();
      90              :     }
      91              : 
      92           17 :     return measures;
      93           18 : }
      94              : 
      95              : //---------------------------------------------------------
      96              : //   whether there is something in the measures, deleted to make room for a measure repeat
      97              : //---------------------------------------------------------
      98              : 
      99            6 : static bool hasContentsToReplace(const std::vector<Measure*>& measures, staff_idx_t staffIdx)
     100              : {
     101           11 :     for (const Measure* m : measures) {
     102            8 :         if (m != measures.back()) {
     103            3 :             if (m->endBarLineType() != BarLineType::NORMAL) {
     104            3 :                 return true;
     105              :             }
     106              :         }
     107           30 :         for (const Segment* seg = m->first(); seg; seg = seg->next()) {
     108           25 :             if (seg->segmentType() & SegmentType::ChordRest) {
     109            8 :                 track_idx_t strack = staffIdx * VOICES;
     110            8 :                 track_idx_t etrack = strack + VOICES;
     111           28 :                 for (track_idx_t track = strack; track < etrack; ++track) {
     112           23 :                     const EngravingItem* e = seg->element(track);
     113           23 :                     if (e && !e->generated() && !e->isRest()) {
     114            3 :                         return true;
     115              :                     }
     116              :                 }
     117              :             }
     118              :         }
     119              :     }
     120              : 
     121            3 :     return false;
     122              : }
     123              : 
     124            7 : bool EditMeasureRepeat::addingReplacesContents(Measure* firstMeasure, int numMeasures, staff_idx_t staffIdx)
     125              : {
     126            7 :     const std::vector<Measure*> measures = groupMeasures(firstMeasure, numMeasures);
     127           14 :     return !measures.empty() && hasContentsToReplace(measures, staffIdx);
     128            7 : }
     129              : 
     130            7 : bool EditMeasureRepeat::droppingReplacesContents(EngravingItem* target, track_idx_t track, int numMeasures)
     131              : {
     132            7 :     if (!target) {
     133            1 :         return false;
     134              :     }
     135              : 
     136            6 :     if (target->isMeasure()) {
     137            3 :         if (track == muse::nidx) {
     138            1 :             return false;
     139              :         }
     140            2 :         return addingReplacesContents(toMeasure(target), numMeasures, track2staff(track));
     141              :     }
     142              : 
     143            3 :     if (target->isRest()) {
     144            2 :         Rest* rest = toRest(target);
     145            2 :         if (rest->durationType().type() != DurationType::V_MEASURE) {
     146            1 :             return false;
     147              :         }
     148            1 :         return addingReplacesContents(rest->measure(), numMeasures, rest->staffIdx());
     149              :     }
     150              : 
     151            1 :     return false;
     152              : }
     153              : 
     154              : //---------------------------------------------------------
     155              : //   makeMeasureRepeatGroup
     156              : ///   clear measures, apply noBreak, set measureRepeatCount
     157              : ///   returns false if these measures won't work
     158              : //---------------------------------------------------------
     159              : 
     160           11 : bool EditMeasureRepeat::makeMeasureRepeatGroup(Transaction&, Score* score, Measure* firstMeasure, int numMeasures,
     161              :                                                staff_idx_t staffIdx)
     162              : {
     163              :     //
     164              :     // check that sufficient measures exist, with equal durations
     165              :     //
     166           11 :     std::vector<Measure*> measures = groupMeasures(firstMeasure, numMeasures);
     167           11 :     if (measures.empty()) {
     168            0 :         MScore::setError(MsError::INSUFFICIENT_MEASURES);
     169            0 :         return false;
     170              :     }
     171              : 
     172              :     // the contents of the measures are replaced, the user is asked beforehand
     173              : 
     174              :     //
     175              :     // group measures and clear current contents
     176              :     //
     177              : 
     178           11 :     score->deselectAll();
     179           11 :     int i = 1;
     180           38 :     for (auto m : measures) {
     181           27 :         score->select(m, SelectType::RANGE, staffIdx);
     182           27 :         if (m->isMeasureRepeatGroup(staffIdx)) {
     183            0 :             score->deleteItem(m->measureRepeatElement(staffIdx)); // reset measures related to an earlier MeasureRepeat
     184              :         }
     185           27 :         score->undoChangeMeasureRepeatCount(m, i++, staffIdx);
     186           27 :         if (m != measures.front()) {
     187           16 :             m->undoChangeProperty(Pid::REPEAT_START, false);
     188              :         }
     189           27 :         if (m != measures.back()) {
     190           16 :             m->undoSetNoBreak(true);
     191           16 :             Segment* seg = m->findSegmentR(SegmentType::EndBarLine, m->ticks());
     192           16 :             BarLine* endBarLine = toBarLine(seg->element(staff2track(staffIdx)));
     193           16 :             score->deleteItem(endBarLine); // also takes care of Pid::REPEAT_END
     194              :         }
     195              :     }
     196           11 :     score->cmdDeleteSelection();
     197           11 :     return true;
     198           11 : }
     199              : 
     200              : //---------------------------------------------------------
     201              : //   addMeasureRepeat
     202              : //    create one MeasureRepeat at tick of subtype numMeasures
     203              : //    create segment if necessary
     204              : //    does NOT set measureRepeatCount or do anything else with measure(s)!
     205              : //---------------------------------------------------------
     206              : 
     207           36 : MeasureRepeat* EditMeasureRepeat::addMeasureRepeat(Transaction&, Score* score, const Fraction& tick, track_idx_t track,
     208              :                                                    int numMeasures)
     209              : {
     210           36 :     Measure* measure = score->tick2measure(tick);
     211           36 :     MeasureRepeat* mr = Factory::createMeasureRepeat(score->dummy());
     212           36 :     mr->setNumMeasures(numMeasures);
     213           36 :     mr->setTicks(measure->stretchedLen(score->staff(track2staff(track))));
     214           36 :     mr->setTrack(track);
     215           36 :     score->undoAddCR(mr, measure, tick);
     216           36 :     return mr;
     217              : }
        

Generated by: LCOV version 2.0-1