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 "editstavesharing.h"
24 :
25 : #include "dom/factory.h"
26 : #include "dom/instrtemplate.h"
27 : #include "dom/part.h"
28 : #include "dom/score.h"
29 : #include "dom/sharedpart.h"
30 : #include "dom/staff.h"
31 : #include "dom/stavesharingchange.h"
32 : #include "dom/system.h"
33 :
34 : #include "transaction/transaction.h"
35 : #include "transaction/undoablecommand.h"
36 :
37 : using namespace mu::engraving;
38 :
39 : namespace {
40 : //-------------------------------------------------------------------
41 : // Undo commands
42 : //-------------------------------------------------------------------
43 :
44 : class ConnectSharedPart : public UndoableCommand
45 : {
46 78 : OBJECT_ALLOCATOR(engraving, ConnectSharedPart)
47 :
48 : SharedPart* sharedPart = nullptr;
49 : Part* originPart = nullptr;
50 :
51 : public:
52 40 : ConnectSharedPart(SharedPart* s, Part* o)
53 40 : : sharedPart(s), originPart(o) {}
54 :
55 8 : void undo() override
56 : {
57 8 : sharedPart->removeOriginPart(originPart);
58 8 : }
59 :
60 42 : void redo() override
61 : {
62 42 : sharedPart->addOriginPart(originPart);
63 42 : }
64 :
65 50 : UNDO_TYPE(CommandType::ConnectSharedPart)
66 0 : UNDO_NAME("Connect shared part")
67 : };
68 :
69 : class DisconnectSharedPart : public UndoableCommand
70 : {
71 6 : OBJECT_ALLOCATOR(engraving, DisconnectSharedPart)
72 :
73 : SharedPart* sharedPart = nullptr;
74 : Part* originPart = nullptr;
75 :
76 : public:
77 4 : DisconnectSharedPart(SharedPart* s, Part* o)
78 4 : : sharedPart(s), originPart(o) {}
79 :
80 2 : void undo() override
81 : {
82 2 : sharedPart->addOriginPart(originPart);
83 2 : }
84 :
85 4 : void redo() override
86 : {
87 4 : sharedPart->removeOriginPart(originPart);
88 4 : }
89 :
90 6 : UNDO_TYPE(CommandType::DisconnectSharedPart)
91 0 : UNDO_NAME("Disconnect shared part")
92 : };
93 :
94 : class EditStaveSharingState : public UndoableCommand
95 : {
96 : protected:
97 : SharedPart* m_sharedPart = nullptr;
98 : Fraction m_tick = Fraction();
99 :
100 35 : EditStaveSharingState(SharedPart* s, const Fraction& t)
101 35 : : m_sharedPart(s), m_tick(t) {}
102 :
103 24 : std::optional<StaveSharingState> currentState() const
104 : {
105 24 : return m_sharedPart->exactStaveSharingStateAtTick(m_tick);
106 : }
107 :
108 35 : void setState(const StaveSharingState& state)
109 : {
110 35 : m_sharedPart->setStaveSharingStateAtTick(state, m_tick);
111 35 : triggerLayout();
112 35 : }
113 :
114 12 : void removeState()
115 : {
116 12 : m_sharedPart->removeStaveSharingStateAtTick(m_tick);
117 12 : triggerLayout();
118 12 : }
119 :
120 : private:
121 47 : void triggerLayout()
122 : {
123 47 : const staff_idx_t staff = m_sharedPart->staves().front()->idx();
124 47 : const Fraction& tick2 = m_sharedPart->nextUserModifiedStaveSharingTick(m_tick);
125 47 : m_sharedPart->score()->setLayout(m_tick, tick2, staff, staff);
126 47 : }
127 : };
128 :
129 : class AddStaveSharingChange : public EditStaveSharingState
130 : {
131 22 : OBJECT_ALLOCATOR(engraving, AddStaveSharingChange)
132 :
133 : StaveSharingState m_staveSharingState = StaveSharingState();
134 :
135 : public:
136 11 : AddStaveSharingChange(SharedPart* s, const Fraction& t, const StaveSharingState& m)
137 11 : : EditStaveSharingState(s, t), m_staveSharingState(m) {}
138 :
139 5 : void undo() override
140 : {
141 5 : removeState();
142 5 : }
143 :
144 11 : void redo() override
145 : {
146 11 : setState(m_staveSharingState);
147 11 : }
148 :
149 16 : UNDO_TYPE(CommandType::AddStaveSharingChange)
150 0 : UNDO_NAME("Add stave sharing change")
151 : };
152 :
153 : class ChangeStaveSharingState : public EditStaveSharingState
154 : {
155 34 : OBJECT_ALLOCATOR(engraving, ChangeStaveSharingState)
156 :
157 : StaveSharingState m_staveSharingState = StaveSharingState();
158 :
159 17 : void flip() override
160 : {
161 17 : std::optional<StaveSharingState> oldState = currentState();
162 17 : IF_ASSERT_FAILED(oldState) {
163 0 : return;
164 : }
165 17 : setState(m_staveSharingState);
166 17 : m_staveSharingState = *oldState;
167 17 : }
168 :
169 : public:
170 : // Only for ticks with an existing state - use AddStaveSharingChange otherwise
171 17 : ChangeStaveSharingState(SharedPart* s, const Fraction& t, const StaveSharingState& m)
172 17 : : EditStaveSharingState(s, t), m_staveSharingState(m) {}
173 :
174 17 : UNDO_TYPE(CommandType::ChangeStaveSharingState)
175 0 : UNDO_NAME("Change stave sharing state")
176 : };
177 :
178 : class ChangeStaveSharingIsReset : public EditStaveSharingState
179 : {
180 0 : OBJECT_ALLOCATOR(engraving, ChangeStaveSharingIsReset)
181 :
182 : bool m_isReset = false;
183 :
184 0 : void flip() override
185 : {
186 0 : std::optional<StaveSharingState> state = currentState();
187 0 : IF_ASSERT_FAILED(state) {
188 0 : return;
189 : }
190 0 : bool oldIsReset = state->isReset();
191 0 : state->setIsReset(m_isReset);
192 0 : setState(*state);
193 0 : m_isReset = oldIsReset;
194 0 : }
195 :
196 : public:
197 : // Only for ticks with an existing state
198 0 : ChangeStaveSharingIsReset(SharedPart* s, const Fraction& t, bool r)
199 0 : : EditStaveSharingState(s, t), m_isReset(r) {}
200 :
201 0 : UNDO_TYPE(CommandType::ChangeStaveSharingIsReset)
202 0 : UNDO_NAME("Change stave sharing is reset")
203 : };
204 :
205 : class RemoveStaveSharingChange : public EditStaveSharingState
206 : {
207 14 : OBJECT_ALLOCATOR(engraving, RemoveStaveSharingChange)
208 :
209 : StaveSharingState m_staveSharingState = StaveSharingState();
210 :
211 : public:
212 7 : RemoveStaveSharingChange(SharedPart* s, const Fraction& t)
213 7 : : EditStaveSharingState(s, t) {}
214 :
215 7 : void undo() override
216 : {
217 7 : setState(m_staveSharingState);
218 7 : }
219 :
220 7 : void redo() override
221 : {
222 7 : std::optional<StaveSharingState> oldState = currentState();
223 7 : IF_ASSERT_FAILED(oldState) {
224 0 : return;
225 : }
226 7 : m_staveSharingState = *oldState;
227 7 : removeState();
228 7 : }
229 :
230 14 : UNDO_TYPE(CommandType::RemoveStaveSharingChange)
231 0 : UNDO_NAME("Remove stave sharing change")
232 : };
233 :
234 28 : void undoSetStaveSharingState(Transaction& tx, SharedPart* sharedPart, const Fraction& tick, const StaveSharingState& state)
235 : {
236 28 : if (sharedPart->exactStaveSharingStateAtTick(tick)) {
237 17 : tx.push(new ChangeStaveSharingState(sharedPart, tick, state));
238 : } else {
239 11 : tx.push(new AddStaveSharingChange(sharedPart, tick, state));
240 : }
241 28 : }
242 : }
243 :
244 : //-------------------------------------------------------------------
245 : // EditStaveSharing
246 : //-------------------------------------------------------------------
247 :
248 23 : void EditStaveSharing::toggleStaveSharing(Transaction& tx, Score* score, bool on)
249 : {
250 23 : score->undoChangeStyleVal(Sid::enableStaveSharing, on);
251 :
252 23 : if (on && score->sharedParts().empty()) {
253 18 : cmdCreateSharedStaves(tx, score);
254 : }
255 :
256 23 : score->setBracketsAndBarlines();
257 :
258 23 : score->update();
259 23 : }
260 :
261 0 : void EditStaveSharing::cmdRemoveSharedStaves(Score* score)
262 : {
263 0 : std::vector<Part*> parts = score->parts(); // COPY because we're about to remove elements
264 :
265 0 : for (Part* part : parts) {
266 0 : if (part->isSharedPart()) {
267 0 : score->cmdRemovePart(part);
268 : }
269 : }
270 0 : }
271 :
272 18 : void EditStaveSharing::cmdCreateSharedStaves(Transaction& tx, Score* score)
273 : {
274 18 : StaveSharingGroups staveSharingGroups = computeGroups(score);
275 :
276 18 : createSharedParts(tx, staveSharingGroups, score);
277 18 : }
278 :
279 18 : StaveSharingGroups EditStaveSharing::computeGroups(Score* score)
280 : {
281 18 : const std::vector<Part*>& parts = score->parts();
282 :
283 18 : StaveSharingGroups staveSharingGroups;
284 :
285 66 : auto excludePart = [](Part* part) {
286 66 : if (part->nstaves() > 1 || part->isSharedPart()) {
287 0 : return true;
288 : }
289 :
290 66 : Staff* firstStaff = part->staves().front();
291 66 : const StaffType* staffType = firstStaff->staffType();
292 66 : if (staffType->group() != StaffGroup::STANDARD && staffType->group() != StaffGroup::JIANPU) {
293 0 : return true;
294 : }
295 :
296 66 : return false;
297 : };
298 :
299 39 : for (size_t partIdx = 0; partIdx < parts.size();) {
300 21 : Part* part = parts[partIdx];
301 21 : if (excludePart(part)) {
302 0 : ++partIdx;
303 0 : continue;
304 : }
305 :
306 21 : Instrument* instr = part->instrument();
307 21 : String instrGroup = instr->group();
308 21 : if (instrGroup != "woodwinds" && instrGroup != "brass") { // TODO: styles
309 0 : ++partIdx;
310 0 : continue;
311 : }
312 :
313 21 : StaveSharingGroup staveSharingGroup;
314 :
315 21 : size_t nextPartIdx = partIdx;
316 63 : while (nextPartIdx < parts.size()) {
317 45 : Part* nextPart = parts[nextPartIdx];
318 45 : if (excludePart(nextPart)) {
319 3 : break;
320 : }
321 45 : Instrument* nextInstr = nextPart->instrument();
322 45 : if (nextPart != part && nextInstr->id() != instr->id()) {
323 3 : break;
324 : }
325 :
326 42 : staveSharingGroup.push_back(nextPart);
327 42 : ++nextPartIdx;
328 : }
329 :
330 21 : if (staveSharingGroup.size() > 1) {
331 19 : staveSharingGroups.push_back(staveSharingGroup);
332 : }
333 :
334 21 : partIdx = nextPartIdx;
335 21 : }
336 :
337 18 : return staveSharingGroups;
338 0 : }
339 :
340 18 : void EditStaveSharing::createSharedParts(Transaction& tx, const StaveSharingGroups& groups, Score* score)
341 : {
342 37 : for (const StaveSharingGroup& group : groups) {
343 19 : SharedPart* sharedPart = nullptr;
344 59 : for (Part* part : group) {
345 40 : if (part->sharedPart() && !sharedPart) {
346 0 : sharedPart = part->sharedPart();
347 0 : break;
348 : }
349 : }
350 :
351 19 : Part* firstPart = group.front();
352 19 : const Instrument* instr = firstPart->instrument();
353 :
354 19 : if (!sharedPart) {
355 19 : size_t idx = muse::indexOf(score->parts(), firstPart);
356 19 : sharedPart = createSharedPart(score, idx, instr);
357 : }
358 :
359 59 : for (Part* originPart : group) {
360 40 : SharedPart* existingSharedPart = originPart->sharedPart();
361 40 : if (existingSharedPart && existingSharedPart != sharedPart) {
362 0 : disconnectSharedPart(tx, existingSharedPart, originPart);
363 0 : connectSharedPart(tx, sharedPart, originPart);
364 40 : } else if (!existingSharedPart) {
365 40 : connectSharedPart(tx, sharedPart, originPart);
366 : }
367 : }
368 : }
369 18 : }
370 :
371 19 : SharedPart* EditStaveSharing::createSharedPart(Score* score, size_t idx, const Instrument* instr)
372 : {
373 19 : SharedPart* sharedPart = new SharedPart(score);
374 19 : score->undoInsertPart(sharedPart, idx);
375 :
376 19 : Instrument* instrument = new Instrument(*instr);
377 19 : InstrumentLabel& label = instrument->instrumentLabel();
378 19 : label.setAllowGroupName(false);
379 19 : label.setNumber(0);
380 19 : label.setShowNumberLong(false);
381 19 : label.setShowNumberShort(false);
382 :
383 19 : sharedPart->setInstrument(instrument);
384 :
385 19 : return sharedPart;
386 : }
387 :
388 40 : void EditStaveSharing::addStaffToSharedPart(SharedPart* sharedPart, const Part* originPart, const KeyList& keyList,
389 : const StaffType* staffType)
390 : {
391 40 : Staff* staff = Factory::createStaff(sharedPart);
392 40 : staff->setStaffType(Fraction(0, 1), *staffType);
393 40 : staff->setPart(sharedPart);
394 :
395 40 : Score* score = sharedPart->score();
396 40 : staff->setScore(score);
397 :
398 40 : staff_idx_t relStaffIdx = sharedPart->nstaves();
399 40 : staff->setDefaultClefType(originPart->instrument()->clefType(0));
400 :
401 40 : score->undoInsertStaff(staff, relStaffIdx);
402 :
403 40 : staff_idx_t absStaffIdx = track2staff(sharedPart->trackRange().startTrack) + relStaffIdx;
404 40 : score->adjustKeySigs(absStaffIdx, absStaffIdx + 1, keyList);
405 40 : }
406 :
407 40 : void EditStaveSharing::connectSharedPart(Transaction& tx, SharedPart* sharedPart, Part* originPart)
408 : {
409 40 : Score* score = originPart->score();
410 40 : tx.push(new ConnectSharedPart(sharedPart, originPart));
411 40 : addStaffToSharedPart(sharedPart, originPart, score->keyList(), originPart->staff(0)->staffType());
412 40 : }
413 :
414 2 : void EditStaveSharing::disconnectSharedPart(Transaction& tx, SharedPart* sharedPart, Part* originPart)
415 : {
416 2 : Score* score = originPart->score();
417 2 : tx.push(new DisconnectSharedPart(sharedPart, originPart));
418 2 : score->undoRemoveStaff(sharedPart->staves().back());
419 2 : }
420 :
421 14 : void EditStaveSharing::handleRemovePart(Transaction& tx, Part* part)
422 : {
423 14 : Score* score = part->score();
424 :
425 14 : if (SharedPart* sharedPart = part->sharedPart()) {
426 2 : disconnectSharedPart(tx, sharedPart, part);
427 2 : if (sharedPart->originParts().empty()) {
428 1 : score->cmdRemovePart(sharedPart);
429 : }
430 12 : } else if (part->isSharedPart()) {
431 2 : sharedPart = toSharedPart(part);
432 2 : std::vector<Part*> originParts = sharedPart->originParts();
433 4 : for (Part* originPart : originParts) {
434 2 : tx.push(new DisconnectSharedPart(sharedPart, originPart));
435 : }
436 2 : }
437 14 : }
438 :
439 6 : void EditStaveSharing::addStaveSharingChange(Transaction& tx, Segment* seg, Part* part, bool reset)
440 : {
441 6 : Score* score = seg ? seg->score() : nullptr;
442 6 : IF_ASSERT_FAILED(part && part->isSharedPart()) {
443 0 : return;
444 : }
445 6 : SharedPart* sharedPart = toSharedPart(part);
446 :
447 : // Use current state to initialise map
448 6 : StaveSharingState newStaveSharingState = sharedPart->staveSharingStateAtTick(seg->tick());
449 6 : newStaveSharingState.setUserTrackMap(newStaveSharingState.sharedTrackMap());
450 6 : newStaveSharingState.setIsReset(reset);
451 :
452 6 : undoSetStaveSharingState(tx, sharedPart, seg->tick(), newStaveSharingState);
453 :
454 : // When adding a stave sharing change, reset at the start of the next system if there is no user change there already
455 6 : const System* curSys = !reset ? seg->system() : nullptr;
456 6 : const Fraction nextSysTick = curSys ? curSys->endTick() : score->endTick();
457 6 : if (nextSysTick < score->endTick()) {
458 2 : std::optional<StaveSharingState> existingState = sharedPart->exactStaveSharingStateAtTick(nextSysTick);
459 2 : if (!existingState || !existingState->isUserModified()) {
460 2 : StaveSharingState resetState = existingState ? existingState.value() : newStaveSharingState;
461 2 : resetState.setUserTrackMap(resetState.sharedTrackMap());
462 2 : resetState.setIsReset(true);
463 :
464 2 : undoSetStaveSharingState(tx, sharedPart, nextSysTick, resetState);
465 2 : }
466 2 : }
467 6 : }
468 :
469 0 : void EditStaveSharing::addStaveSharingChangeToRange(Transaction& tx, Segment* startSeg, Segment* endSeg, staff_idx_t startStaff,
470 : staff_idx_t endStaffIdx, bool reset)
471 : {
472 0 : const Score* score = startSeg->score();
473 0 : const Fraction& startTick = startSeg->tick();
474 :
475 0 : for (staff_idx_t staffIdx = startStaff; staffIdx < endStaffIdx;) {
476 0 : Staff* staff = score->staff(staffIdx);
477 0 : Part* part = staff->part();
478 0 : if (!part->isSharedPart()) {
479 0 : staffIdx = track2staff(part->trackRange().endTrack);
480 0 : continue;
481 : }
482 0 : SharedPart* sharedPart = toSharedPart(part);
483 :
484 : // Create change
485 0 : StaveSharingState prevStaveSharingState = sharedPart->staveSharingStateAtTick(startTick);
486 :
487 0 : StaveSharingState newStaveSharingState = prevStaveSharingState;
488 0 : newStaveSharingState.setUserTrackMap(newStaveSharingState.sharedTrackMap());
489 0 : newStaveSharingState.setIsReset(reset);
490 :
491 0 : undoSetStaveSharingState(tx, sharedPart, startTick, newStaveSharingState);
492 :
493 : // No end state needed if the range runs to the end of the score
494 0 : std::optional<StaveSharingState> existingState = endSeg ? sharedPart->exactStaveSharingStateAtTick(endSeg->tick()) : std::nullopt;
495 0 : if (endSeg && (!existingState || !existingState->isUserModified())) {
496 0 : const Fraction& endTick = endSeg->tick();
497 0 : if (prevStaveSharingState.isUserModified() && !prevStaveSharingState.isReset()) {
498 : // Resume previous user sharing state
499 0 : undoSetStaveSharingState(tx, sharedPart, endTick, prevStaveSharingState);
500 0 : } else if (!reset) {
501 : // Create reset
502 0 : StaveSharingState nextState = existingState ? existingState.value() : newStaveSharingState;
503 0 : nextState.setUserTrackMap(nextState.sharedTrackMap());
504 0 : nextState.setIsReset(true);
505 0 : undoSetStaveSharingState(tx, sharedPart, endTick, nextState);
506 0 : }
507 : }
508 :
509 : // Next part
510 0 : staffIdx = track2staff(part->trackRange().endTrack);
511 0 : }
512 0 : }
513 :
514 15 : void EditStaveSharing::setTrackMapping(Transaction& tx, Segment* seg, Part* part, track_idx_t originTrack, track_idx_t sharedTrack)
515 : {
516 15 : IF_ASSERT_FAILED(part && part->isSharedPart()) {
517 0 : return;
518 : }
519 15 : SharedPart* sharedPart = toSharedPart(part);
520 :
521 15 : StaveSharingState newStaveSharingState = sharedPart->staveSharingStateAtTick(seg->tick());
522 :
523 15 : SharedTrackMap map = newStaveSharingState.userTrackMap().value_or(newStaveSharingState.sharedTrackMap());
524 15 : map[originTrack] = sharedTrack;
525 15 : newStaveSharingState.setUserTrackMap(map);
526 15 : newStaveSharingState.setIsReset(false);
527 :
528 15 : undoSetStaveSharingState(tx, sharedPart, seg->tick(), newStaveSharingState);
529 15 : }
530 :
531 0 : void EditStaveSharing::setIsReset(Transaction& tx, StaveSharingChange* staveSharingChange, bool reset)
532 : {
533 0 : Part* part = staveSharingChange ? staveSharingChange->part() : nullptr;
534 0 : IF_ASSERT_FAILED(part && part->isSharedPart()) {
535 0 : return;
536 : }
537 :
538 0 : SharedPart* sharedPart = toSharedPart(part);
539 0 : const Fraction tick = staveSharingChange->tick();
540 0 : std::optional<StaveSharingState> state = sharedPart->exactStaveSharingStateAtTick(tick);
541 0 : IF_ASSERT_FAILED(state) {
542 0 : return;
543 : }
544 :
545 0 : if (state->isReset() == reset) {
546 0 : return;
547 : }
548 :
549 0 : tx.push(new ChangeStaveSharingIsReset(sharedPart, tick, reset));
550 0 : }
551 :
552 0 : void EditStaveSharing::removeStaveSharingChange(Transaction& tx, StaveSharingChange* staveSharingChange)
553 : {
554 0 : Part* part = staveSharingChange ? staveSharingChange->part() : nullptr;
555 0 : IF_ASSERT_FAILED(part && part->isSharedPart()) {
556 0 : return;
557 : }
558 :
559 0 : SharedPart* sharedPart = toSharedPart(part);
560 :
561 0 : tx.push(new RemoveStaveSharingChange(sharedPart, staveSharingChange->tick()));
562 : }
563 :
564 116 : void EditStaveSharing::removeStaveSharingStatesOnRemoveTime(Transaction& tx, Score* score, const Fraction& startTick,
565 : const Fraction& endTick)
566 : {
567 125 : for (SharedPart* sharedPart : score->sharedParts()) {
568 20 : for (const auto& [tick, state] : sharedPart->staveSharingStatesBetweenTicks(startTick, endTick)) {
569 11 : if (state.isUserModified()) {
570 7 : tx.push(new RemoveStaveSharingChange(sharedPart, tick));
571 : }
572 9 : }
573 116 : }
574 116 : }
575 :
576 55 : StaveSharingStatesByPart EditStaveSharing::userStaveSharingStatesBetweenTicks(Score* score, const Fraction& startTick,
577 : const Fraction& endTick)
578 : {
579 55 : StaveSharingStatesByPart result;
580 :
581 58 : for (SharedPart* sharedPart : score->sharedParts()) {
582 10 : for (const auto& [tick, state] : sharedPart->staveSharingStatesBetweenTicks(startTick, endTick)) {
583 7 : if (state.isUserModified()) {
584 5 : result[sharedPart].emplace(tick, state);
585 : }
586 3 : }
587 55 : }
588 :
589 55 : return result;
590 0 : }
591 :
592 54 : void EditStaveSharing::restoreStaveSharingStates(Transaction& tx, const StaveSharingStatesByPart& states)
593 : {
594 57 : for (const auto& [sharedPart, statesByTick] : states) {
595 8 : for (const auto& [tick, state] : statesByTick) {
596 5 : undoSetStaveSharingState(tx, sharedPart, tick, state);
597 : }
598 : }
599 54 : }
|