LCOV - code coverage report
Current view: top level - src/engraving/editing - editslur.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 90.5 % 105 95
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 and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program. If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : 
      23              : #include "editslur.h"
      24              : 
      25              : #include "../dom/barline.h"
      26              : #include "../dom/chord.h"
      27              : #include "../dom/factory.h"
      28              : #include "../dom/measure.h"
      29              : #include "../dom/note.h"
      30              : #include "../dom/score.h"
      31              : #include "../dom/segment.h"
      32              : #include "../dom/select.h"
      33              : #include "../dom/slur.h"
      34              : #include "../dom/utils.h"
      35              : 
      36              : using namespace mu::engraving;
      37              : 
      38           13 : std::vector<EditSlur::AddedSlur> EditSlur::addSlurs(Transaction& tx, Score* score, const Slur* slurTemplate)
      39              : {
      40           13 :     std::vector<AddedSlur> addedSlurs;
      41           14 :     const auto addSlurBetween = [&](EngravingItem* firstItem, EngravingItem* secondItem) {
      42           14 :         const AddedSlur added = addSlur(tx, score, firstItem, secondItem, slurTemplate);
      43           14 :         if (added.slur) {
      44           14 :             addedSlurs.push_back(added);
      45              :         }
      46           14 :     };
      47              : 
      48           13 :     ChordRest* firstChordRest = nullptr;
      49           13 :     ChordRest* secondChordRest = nullptr;
      50           13 :     const auto& sel = score->selection();
      51           13 :     auto el = sel.uniqueElements();
      52              : 
      53           13 :     if (sel.isRange()) {
      54            2 :         track_idx_t startTrack = staff2track(sel.staffStart());
      55            2 :         track_idx_t endTrack = staff2track(sel.staffEnd());
      56           10 :         for (track_idx_t track = startTrack; track < endTrack; ++track) {
      57            8 :             firstChordRest = nullptr;
      58            8 :             secondChordRest = nullptr;
      59          160 :             for (EngravingItem* e : el) {
      60          152 :                 if (e->track() != track) {
      61          114 :                     continue;
      62              :                 }
      63           38 :                 if (e->isNote()) {
      64           16 :                     e = toNote(e)->chord();
      65              :                 }
      66           38 :                 if (!e->isChord()) {
      67           22 :                     continue;
      68              :                 }
      69           16 :                 ChordRest* cr = toChordRest(e);
      70           16 :                 if (!firstChordRest || firstChordRest->tick() > cr->tick()) {
      71            3 :                     firstChordRest = cr;
      72              :                 }
      73           16 :                 if (!secondChordRest || secondChordRest->tick() < cr->tick()) {
      74           16 :                     secondChordRest = cr;
      75              :                 }
      76              :             }
      77              : 
      78            8 :             bool firstCrTrill = firstChordRest && firstChordRest->isChord() && toChord(firstChordRest)->isTrillCueNote();
      79            8 :             bool secondCrTrill = secondChordRest && secondChordRest->isChord() && toChord(secondChordRest)->isTrillCueNote();
      80              : 
      81            8 :             if (firstChordRest && (firstChordRest != secondChordRest)
      82            3 :                 && !(firstCrTrill || secondCrTrill)) {
      83            3 :                 addSlurBetween(firstChordRest, secondChordRest);
      84              :             }
      85              :         }
      86           11 :     } else if (sel.isSingle()) {
      87            6 :         if (sel.element()->isNote() && !toNote(sel.element())->isTrillCueNote()) {
      88            5 :             addSlurBetween(toNote(sel.element())->chord(), nullptr);
      89            1 :         } else if (sel.element()->isSlurSegment() && slurTemplate && slurTemplate->isHammerOnPullOff()) {
      90            1 :             Slur* slur = toSlurSegment(sel.element())->slur();
      91            1 :             addSlurBetween(slur->startElement(), slur->endElement());
      92              :         }
      93              :     } else {
      94            5 :         EngravingItem* firstItem = nullptr;
      95            5 :         EngravingItem* secondItem = nullptr;
      96           15 :         for (EngravingItem* e : el) {
      97           10 :             if (e->isNote()) {
      98            8 :                 e = toNote(e)->chord();
      99              :             }
     100              : 
     101           10 :             Measure* meas = e->findMeasure();
     102           10 :             bool header = meas && meas->header() && e->tick() == meas->tick();
     103              : 
     104           10 :             if (!e->isChord() && !e->isBarLine() && !header) {
     105            0 :                 continue;
     106              :             }
     107              : 
     108           10 :             if (!firstItem || e->isBefore(firstItem)) {
     109            6 :                 firstItem = e;
     110              :             }
     111           10 :             if (!secondItem || secondItem->isBefore(e)) {
     112            7 :                 secondItem = e;
     113              :             }
     114              :         }
     115              : 
     116            5 :         bool firstCrTrill = firstItem && firstItem->isChord() && toChord(firstItem)->isTrillCueNote();
     117            5 :         bool secondCrTrill = secondItem && secondItem->isChord() && toChord(secondItem)->isTrillCueNote();
     118              : 
     119            5 :         if (firstItem && firstItem == secondItem) {
     120              :             // A single chord is slurred as a single note is
     121            4 :             if (firstItem->isChord() && !firstCrTrill) {
     122            2 :                 addSlurBetween(firstItem, nullptr);
     123              :             }
     124            3 :         } else if (firstItem && secondItem && (firstItem->isChordRest() || secondItem->isChordRest())
     125            6 :                    && !(firstCrTrill || secondCrTrill)) {
     126            3 :             addSlurBetween(firstItem, secondItem);
     127              :         }
     128              :     }
     129              : 
     130           13 :     return addedSlurs;
     131           13 : }
     132              : 
     133           14 : EditSlur::AddedSlur EditSlur::addSlur(Transaction&, Score* score, EngravingItem* firstItem, EngravingItem* secondItem,
     134              :                                       const Slur* slurTemplate)
     135              : {
     136           14 :     ChordRest* firstChordRest = nullptr;
     137           14 :     ChordRest* secondChordRest = nullptr;
     138              : 
     139           14 :     Measure* meas = firstItem ? firstItem->findMeasure() : nullptr;
     140           14 :     bool header = meas && meas->header() && firstItem->tick() == meas->tick() && !firstItem->isChordRest();
     141              : 
     142           14 :     if (firstItem && secondItem
     143           28 :         && ((firstItem->isBarLine() != secondItem->isBarLine()) || (header && secondItem->isChordRest()))) {
     144            2 :         const bool outgoing = firstItem->isChordRest();
     145            2 :         const EngravingItem* otherElement = outgoing ? secondItem : firstItem;
     146            2 :         ChordRest* cr = outgoing ? toChordRest(firstItem) : toChordRest(secondItem);
     147              : 
     148              :         // Check that cr and otherElement are part of the same repeat section
     149            2 :         Segment* seg1 = toSegment(firstItem->findAncestor(ElementType::SEGMENT));
     150            2 :         Segment* seg2 = toSegment(secondItem->findAncestor(ElementType::SEGMENT));
     151              : 
     152            2 :         bool differentRepeatSegments = segmentsAreInDifferentRepeatSegments(seg1, seg2);
     153            2 :         bool validBarline = isValidBarLineForRepeatSection(seg1, seg2);
     154              : 
     155            2 :         if (!cr || (!header && (differentRepeatSegments && !validBarline))) {
     156            0 :             return {};
     157              :         }
     158              : 
     159            2 :         Slur* partialSlur = slurTemplate ? slurTemplate->clone() : Factory::createSlur(score->dummy());
     160            2 :         if (outgoing) {
     161            2 :             partialSlur->undoSetOutgoing(true);
     162            2 :             firstChordRest = toChordRest(cr);
     163            2 :             const Measure* endMeas = otherElement->findMeasure();
     164            2 :             ChordRest* endCr = endMeas->lastChordRest(otherElement->track());
     165            2 :             secondChordRest = endCr;
     166              :         } else {
     167            0 :             partialSlur->undoSetIncoming(true);
     168            0 :             secondChordRest = toChordRest(cr);
     169            0 :             const Measure* startMeas = otherElement->findMeasure();
     170            0 :             if (otherElement->isBarLine()) {
     171            0 :                 if (toBarLine(otherElement)->barLineType() == BarLineType::END_REPEAT) {
     172            0 :                     startMeas = startMeas->nextMeasure();
     173              :                 }
     174              :             }
     175            0 :             ChordRest* startCr = startMeas->firstChordRest(otherElement->track());
     176            0 :             firstChordRest = startCr;
     177              :         }
     178            2 :         slurTemplate = partialSlur;
     179              :     } else {
     180           12 :         firstChordRest = toChordRest(firstItem);
     181           12 :         secondChordRest = toChordRest(secondItem);
     182              :     }
     183              : 
     184           14 :     const bool toNextChord = !secondItem;
     185           14 :     Slur* slur = firstChordRest->slur(secondChordRest);
     186           14 :     if (!slur || slur->slurDirection() != DirectionV::AUTO) {
     187           12 :         return { score->addSlur(firstChordRest, secondChordRest, slurTemplate), false, toNextChord };
     188            2 :     } else if (slurTemplate && slurTemplate->isHammerOnPullOff()) {
     189              :         // Replace existing slur with HOPO
     190            1 :         score->undoRemoveElement(slur);
     191            1 :         return { score->addSlur(firstChordRest, secondChordRest, slurTemplate), true, toNextChord };
     192              :     }
     193              : 
     194            1 :     return { slur, false, toNextChord };
     195              : }
        

Generated by: LCOV version 2.0-1