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 "editduration.h"
24 :
25 : #include <algorithm>
26 : #include <unordered_set>
27 :
28 : #include "rw/xmlreader.h"
29 :
30 : #include "../dom/chord.h"
31 : #include "../dom/chordrest.h"
32 : #include "../dom/durationtype.h"
33 : #include "../dom/factory.h"
34 : #include "../dom/input.h"
35 : #include "../dom/laissezvib.h"
36 : #include "../dom/measure.h"
37 : #include "../dom/note.h"
38 : #include "../dom/score.h"
39 : #include "../dom/segment.h"
40 : #include "../dom/select.h"
41 : #include "../dom/tie.h"
42 : #include "../dom/utils.h"
43 :
44 : #include "navigation.h"
45 : #include "noteinput.h"
46 : #include "paste.h"
47 : #include "transaction/transaction.h"
48 :
49 : #include "log.h"
50 :
51 : using namespace mu::engraving;
52 :
53 8 : void EditDuration::doubleDuration(Transaction& tx, Score* score)
54 : {
55 8 : incDecDuration(tx, score, -1, false);
56 8 : }
57 :
58 8 : void EditDuration::halfDuration(Transaction& tx, Score* score)
59 : {
60 8 : incDecDuration(tx, score, 1, false);
61 8 : }
62 :
63 14 : void EditDuration::incDurationDotted(Transaction& tx, Score* score)
64 : {
65 14 : incDecDuration(tx, score, -1, true);
66 14 : }
67 :
68 14 : void EditDuration::decDurationDotted(Transaction& tx, Score* score)
69 : {
70 14 : incDecDuration(tx, score, 1, true);
71 14 : }
72 :
73 : //---------------------------------------------------------
74 : // incDecDuration
75 : // When stepDotted is false and nSteps is 1 or -1, will halve or double the duration
76 : // When stepDotted is true, will step by nearest dotted or undotted note
77 : //---------------------------------------------------------
78 :
79 44 : void EditDuration::incDecDuration(Transaction& tx, Score* score, int nSteps, bool stepDotted)
80 : {
81 44 : Selection& selection = score->selection();
82 44 : InputState& is = score->inputState();
83 :
84 44 : if (selection.isRange()) {
85 0 : if (!selection.canCopy()) {
86 0 : return;
87 : }
88 0 : ChordRest* firstCR = selection.firstChordRest();
89 0 : if (firstCR->isGrace()) {
90 0 : firstCR = toChordRest(firstCR->parent());
91 : }
92 0 : TDuration initialDuration = firstCR->ticks();
93 0 : TDuration d = initialDuration.shiftRetainDots(nSteps, stepDotted);
94 0 : if (!d.isValid()) {
95 0 : return;
96 : }
97 0 : Fraction scale = d.ticks() / initialDuration.ticks();
98 0 : for (ChordRest* cr : score->getSelectedChordRests()) {
99 0 : Fraction newTicks = cr->ticks() * scale;
100 0 : if (newTicks < Fraction(1, 1024)
101 0 : || (stepDotted && cr->durationType().dots() != firstCR->durationType().dots()
102 0 : && !cr->isGrace())) {
103 0 : return;
104 : }
105 0 : }
106 0 : const muse::ByteArray mimeData(selection.mimeData());
107 0 : XmlReader e(mimeData);
108 0 : score->deleteRange(selection.startSegment(), selection.endSegment(), staff2track(selection.staffStart()),
109 0 : staff2track(selection.staffEnd()), score->selectionFilter(), selection.rangeContainsMultiNoteChords());
110 0 : Paste::pasteStaff(tx, score, e, selection.startSegment(), selection.staffStart(), scale);
111 44 : } else if (selection.isList()) {
112 44 : const std::vector<Note*> notes = selection.noteList();
113 44 : const std::set<ChordRest*> crsSet = score->getSelectedChordRests();
114 44 : std::vector<ChordRest*> crs(crsSet.begin(), crsSet.end());
115 48 : std::sort(crs.begin(), crs.end(), [](const ChordRest* a, const ChordRest* b) { return a->tick() > b->tick(); });
116 :
117 : // Make sure each linked group is handled only once
118 44 : std::unordered_set<const LinkedObjects*> handledLinks;
119 :
120 90 : for (ChordRest* cr : crs) {
121 46 : if (cr->links() && !handledLinks.insert(cr->links()).second) {
122 2 : continue;
123 : }
124 :
125 : // if measure rest is selected as input, then the correct initialDuration will be the
126 : // duration of the measure's time signature, else is just the ChordRest's duration
127 44 : TDuration initialDuration = cr->durationType();
128 44 : if (initialDuration == DurationType::V_MEASURE) {
129 0 : initialDuration = TDuration(cr->measure()->timesig(), true);
130 :
131 0 : if (initialDuration.fraction() < cr->measure()->timesig() && nSteps > 0) {
132 : // Duration already shortened by truncation; shorten one step less
133 0 : --nSteps;
134 : }
135 : }
136 :
137 44 : TDuration newDuration { stepDotted ? initialDuration.shiftRetainDots(nSteps, stepDotted) : initialDuration.shift(nSteps) };
138 44 : if (!newDuration.isValid()) {
139 0 : continue;
140 : }
141 :
142 44 : if (cr->isGrace()) {
143 0 : score->undoChangeChordRestLen(cr, newDuration);
144 : } else {
145 44 : score->changeCRlen(cr, newDuration);
146 : }
147 : }
148 90 : for (Note* n : notes) {
149 46 : if (score->canReselectItem(n)) {
150 4 : score->select(toEngravingItem(n), SelectType::ADD);
151 : }
152 : }
153 44 : if (is.noteEntryMode()) {
154 42 : const ChordRest* cr = crs.size() == 1 ? crs.front() : nullptr;
155 42 : IF_ASSERT_FAILED(cr) {
156 : // (At time of writing) it shouldn't be possible to have more than
157 : // one CR selected during note entry...
158 0 : return;
159 : }
160 42 : is.setDuration(cr->durationType());
161 42 : NoteInput::nextInputPos(tx, score, cr, false);
162 : }
163 44 : }
164 : }
165 :
166 : //---------------------------------------------------------
167 : // extendToNextNote
168 : //---------------------------------------------------------
169 :
170 0 : void EditDuration::extendToNextNote(Transaction& tx, Score* score)
171 : {
172 0 : Selection& selection = score->selection();
173 0 : InputState& is = score->inputState();
174 :
175 0 : const Fraction startTick = selection.tickStart();
176 0 : Fraction endTick = selection.tickEnd();
177 0 : const staff_idx_t startStaff = selection.staffStart();
178 0 : const staff_idx_t endStaff = selection.staffEnd();
179 0 : Box* startBox = selection.startBox();
180 0 : Box* endBox = selection.endBox();
181 :
182 0 : std::vector<EngravingItem*> toSelect;
183 0 : const bool wasRangeSelection = selection.isRange();
184 0 : const std::vector<Note*> initialSelection = selection.noteList();
185 :
186 0 : for (ChordRest* cr : score->getSelectedChordRests()) {
187 0 : ChordRest* ncr = Navigation::nextChordRest(cr);
188 0 : if (cr->isRest() || cr->isGrace() || cr->endTick() == score->endTick()
189 0 : || (ncr && ncr->isChord() && cr->endTick() == ncr->tick())) {
190 0 : continue;
191 : }
192 0 : std::vector<Note*> chordNotes = toChord(cr)->notes();
193 0 : std::vector<Note*> selectedChordNotes;
194 0 : for (Note* n : chordNotes) {
195 0 : if (std::find(initialSelection.begin(), initialSelection.end(), n) != initialSelection.end()) {
196 0 : selectedChordNotes.push_back(n);
197 0 : toSelect.push_back(n);
198 0 : if (LaissezVib* lv = n->laissezVib()) {
199 0 : score->undoRemoveElement(lv);
200 : }
201 : }
202 : }
203 0 : bool allNotesSelected = chordNotes.size() == selectedChordNotes.size() ? true : false;
204 :
205 0 : while (cr->endTick() != score->endTick()) { // not at end of score
206 0 : if (ncr && cr->endTick() == ncr->tick() && ncr->isChord()) {
207 0 : break;
208 : }
209 0 : if (!ncr || cr->endTick() != ncr->tick() // if voices>0 have empty measures till end OR have empty measures between cr and ncr
210 0 : || cr->tuplet() != ncr->tuplet() || !allNotesSelected) {
211 0 : std::vector<Note*> notesToExtend = !allNotesSelected ? (allNotesSelected = true, selectedChordNotes) : toChord(cr)->notes();
212 0 : is.setTrack(cr->track());
213 0 : is.setSegment(cr->segment());
214 0 : is.moveToNextInputPos();
215 0 : is.setLastSegment(is.segment());
216 :
217 0 : if (!is.cr()) {
218 0 : score->expandVoice();
219 : }
220 0 : is.setDuration(is.cr()->durationType());
221 :
222 0 : Note* nn = nullptr;
223 0 : for (size_t i = 0; i < notesToExtend.size(); i++) {
224 0 : Note* note = notesToExtend[i];
225 0 : NoteVal nval(note->noteVal());
226 0 : nn = NoteInput::addPitch(tx, score, nval, i != 0);
227 :
228 0 : Tie* tie = Factory::createTie(note);
229 0 : tie->setStartNote(note);
230 0 : tie->setTrack(note->track());
231 0 : tie->setTick(note->chord()->segment()->tick());
232 0 : tie->setEndNote(nn);
233 0 : tie->setTicks(nn->chord()->segment()->tick() - note->chord()->segment()->tick());
234 0 : score->undoAddElement(tie);
235 : }
236 0 : cr = toChordRest(nn->chord());
237 0 : toSelect.push_back(cr);
238 0 : } else {
239 0 : Fraction newDur = cr->ticks() + ncr->ticks();
240 0 : score->changeCRlen(cr, newDur);
241 0 : while (toChord(cr)->notes()[0]->tieFor()) {
242 0 : cr = Navigation::nextChordRest(cr);
243 0 : toSelect.push_back(cr);
244 : }
245 : }
246 0 : ncr = Navigation::nextChordRest(cr);
247 0 : endTick = cr->endTick() >= endTick ? cr->endTick() : endTick;
248 : }
249 0 : }
250 :
251 0 : if (wasRangeSelection) {
252 0 : selection.setRangeTicks(startTick, endTick, startStaff, endStaff, startBox, endBox);
253 0 : selection.updateSelectedElements();
254 : } else {
255 0 : for (EngravingItem* ei : toSelect) {
256 0 : if (ei->isChord()) {
257 0 : std::vector<Note*> notes = toChord(ei)->notes();
258 0 : score->select({ notes.begin(), notes.end() }, SelectType::ADD);
259 0 : } else {
260 0 : score->select(ei, SelectType::ADD);
261 : }
262 : }
263 : }
264 0 : }
|