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 : #include "notationparts.h"
23 :
24 : #include "types/translatablestring.h"
25 :
26 : #include "engraving/dom/instrument.h"
27 : #include "engraving/editing/editstavesharing.h"
28 : #include "engraving/editing/transaction/transaction.h"
29 :
30 : #include "igetscore.h"
31 : #include "inotationinteraction.h"
32 : #include "inotationnoteinput.h" // IWYU pragma: keep
33 : #include "inotationstyle.h"
34 : #include "inotationundostack.h"
35 :
36 : #include "log.h"
37 :
38 : using namespace muse;
39 : using namespace muse::async;
40 : using namespace mu::notation;
41 : using namespace mu::engraving;
42 :
43 0 : static StaffConfig makeStaffConfig(const Staff* staff, const Fraction& tick = Fraction(0, 1))
44 : {
45 0 : StaffConfig config;
46 :
47 0 : const mu::engraving::StaffType* staffType = staff->staffType(tick);
48 0 : if (!staffType) {
49 0 : return config;
50 : }
51 :
52 0 : config.visible = staff->visible();
53 0 : config.userDistance = staff->userDist();
54 0 : config.cutaway = staff->cutaway();
55 0 : config.hideSystemBarline = staff->hideSystemBarLine();
56 0 : config.mergeMatchingRests = staff->mergeMatchingRests();
57 0 : config.clefTypeList = staff->defaultClefType();
58 0 : config.reflectTranspositionInLinkedTab = staff->reflectTranspositionInLinkedTab();
59 0 : config.staffType = *staffType;
60 :
61 0 : return config;
62 0 : }
63 :
64 0 : NotationParts::NotationParts(IGetScore* getScore, INotationInteractionPtr interaction, INotationUndoStackPtr undoStack,
65 0 : INotationStylePtr style)
66 0 : : m_getScore(getScore), m_undoStack(undoStack), m_interaction(interaction), m_style(style)
67 : {
68 0 : m_getScore->scoreInited().onNotify(this, [this]() {
69 0 : listenUndoStackChanges();
70 0 : listenStyleChanges();
71 0 : });
72 0 : }
73 :
74 0 : NotifyList<const Part*> NotationParts::partList() const
75 : {
76 0 : NotifyList<const Part*> result;
77 0 : result.setNotify(m_partChangedNotifier.notify());
78 :
79 0 : if (Score* _score = score()) {
80 0 : const std::vector<Part*>& parts = _score->parts();
81 0 : result.assign(parts.begin(), parts.end());
82 : }
83 :
84 0 : return result;
85 0 : }
86 :
87 0 : NotifyList<const Staff*> NotationParts::staffList(const ID& partId) const
88 : {
89 0 : NotifyList<const Staff*> result;
90 0 : ChangedNotifier<const Staff*>& notifier = m_staffChangedNotifierMap[partId];
91 0 : result.setNotify(notifier.notify());
92 :
93 0 : const Part* part = this->part(partId);
94 0 : if (!part) {
95 0 : return result;
96 : }
97 :
98 0 : const std::vector<Staff*>& staves = part->staves();
99 0 : result.assign(staves.begin(), staves.end());
100 :
101 0 : return result;
102 0 : }
103 :
104 0 : bool NotationParts::hasParts() const
105 : {
106 0 : const Score* score = this->score();
107 0 : if (!score) {
108 0 : return false;
109 : }
110 :
111 0 : return !score->parts().empty();
112 : }
113 :
114 0 : const Part* NotationParts::part(const ID& partId) const
115 : {
116 0 : return score()->partById(partId);
117 : }
118 :
119 0 : bool NotationParts::partExists(const ID& partId) const
120 : {
121 0 : return part(partId) != nullptr;
122 : }
123 :
124 0 : const Staff* NotationParts::staff(const ID& staffId) const
125 : {
126 0 : return score()->staffById(staffId);
127 : }
128 :
129 0 : bool NotationParts::staffExists(const ID& staffId) const
130 : {
131 0 : return staff(staffId) != nullptr;
132 : }
133 :
134 0 : StaffConfig NotationParts::staffConfig(const ID& staffId, Fraction tick) const
135 : {
136 0 : const Staff* staff = score()->staffById(staffId);
137 0 : if (!staff) {
138 0 : return StaffConfig();
139 : }
140 :
141 0 : return makeStaffConfig(staff, tick);
142 : }
143 :
144 0 : ScoreOrder NotationParts::scoreOrder() const
145 : {
146 0 : return score()->scoreOrder();
147 : }
148 :
149 0 : void NotationParts::setParts(const PartInstrumentList& parts, const ScoreOrder& order)
150 : {
151 0 : TRACEFUNC;
152 :
153 0 : endInteractionWithScore();
154 :
155 0 : EditPart::SetPartsResult result;
156 :
157 0 : transaction(TranslatableString("undoableAction", "Add/remove instruments"), [&](engraving::Transaction&) {
158 0 : result = EditPart::setParts(score(), partEntries(parts), order);
159 0 : });
160 :
161 0 : const std::vector<Part*>& currentParts = score()->parts();
162 0 : for (Part* part : result.addedParts) {
163 0 : if (muse::contains(currentParts, part)) {
164 0 : notifyAboutPartAdded(part);
165 : }
166 : }
167 :
168 0 : m_scoreOrderChanged.notify();
169 0 : updatePartsAndSystemObjectStaves();
170 0 : }
171 :
172 0 : void NotationParts::setScoreOrder(const ScoreOrder& order)
173 : {
174 0 : if (score()->scoreOrder() == order) {
175 0 : return;
176 : }
177 :
178 0 : transaction(TranslatableString("undoableAction", "Set score order"), [&](engraving::Transaction&) {
179 0 : EditPart::setScoreOrder(score(), order);
180 0 : });
181 :
182 0 : m_scoreOrderChanged.notify();
183 : }
184 :
185 0 : void NotationParts::setPartVisible(const ID& partId, bool visible)
186 : {
187 0 : TRACEFUNC;
188 :
189 0 : Part* part = score()->partById(partId);
190 0 : if (!part) {
191 0 : return;
192 : }
193 :
194 0 : if (part->show() == visible) {
195 0 : return;
196 : }
197 :
198 : const TranslatableString actionName = visible
199 0 : ? TranslatableString("undoableAction", "Show instrument")
200 0 : : TranslatableString("undoableAction", "Hide instrument");
201 :
202 0 : transaction(actionName, [&](engraving::Transaction&) {
203 0 : EditPart::setPartVisible(score(), part, visible);
204 0 : });
205 :
206 0 : notifyAboutPartChanged(part);
207 0 : }
208 :
209 0 : bool NotationParts::setVoiceVisible(const ID& staffId, int voiceIndex, bool visible)
210 : {
211 0 : TRACEFUNC;
212 :
213 0 : Staff* staff = score()->staffById(staffId);
214 0 : if (!staff) {
215 0 : return false;
216 : }
217 :
218 : const TranslatableString actionName = visible
219 0 : ? TranslatableString("undoableAction", "Show voice %1").arg(voiceIndex + 1)
220 0 : : TranslatableString("undoableAction", "Hide voice %1").arg(voiceIndex + 1);
221 :
222 0 : bool result = false;
223 :
224 : //! HACK: Excerpt::setVoiceVisible recreates the staff,
225 : //! so later in listenUndoStackChanges() we will call notifyAboutStaffRemoved() and notifyAboutStaffAdded(),
226 : //! which will result in the wrong UI state in the Layout panel.
227 : //! We should not recreate the staff here, only update it
228 0 : m_ignoreUndoStackChanges = true;
229 0 : transaction(actionName, [&](engraving::Transaction&) {
230 0 : result = EditPart::setVoiceVisible(score(), staff, voiceIndex, visible);
231 0 : });
232 0 : m_ignoreUndoStackChanges = false;
233 :
234 0 : if (result) {
235 0 : updatePartsAndSystemObjectStaves();
236 0 : notifyAboutStaffChanged(score()->staffById(staffId));
237 : }
238 :
239 0 : return result;
240 0 : }
241 :
242 0 : void NotationParts::setStaffVisible(const ID& staffId, bool visible)
243 : {
244 0 : TRACEFUNC;
245 :
246 0 : Staff* staff = score()->staffById(staffId);
247 0 : if (!staff) {
248 0 : return;
249 : }
250 :
251 0 : StaffConfig config = makeStaffConfig(staff);
252 0 : if (config.visible == visible) {
253 0 : return;
254 : }
255 :
256 : const TranslatableString actionName = visible
257 0 : ? TranslatableString("undoableAction", "Show staff")
258 0 : : TranslatableString("undoableAction", "Hide staff");
259 :
260 0 : config.visible = visible;
261 :
262 0 : transaction(actionName, [&](engraving::Transaction&) {
263 0 : EditPart::setStaffConfig(score(), staff, config);
264 0 : });
265 :
266 0 : notifyAboutStaffChanged(staff);
267 0 : }
268 :
269 0 : void NotationParts::setPartSharpFlat(const ID& partId, const SharpFlat& sharpFlat)
270 : {
271 0 : TRACEFUNC;
272 :
273 0 : Part* part = score()->partById(partId);
274 0 : if (!part) {
275 0 : return;
276 : }
277 :
278 0 : int shartFlatInt = static_cast<int>(sharpFlat);
279 0 : if (part->getProperty(mu::engraving::Pid::PREFER_SHARP_FLAT) == shartFlatInt) {
280 0 : return;
281 : }
282 :
283 0 : auto calcActionName = [](const SharpFlat& sharpFlat) -> TranslatableString {
284 0 : switch (sharpFlat) {
285 0 : case SharpFlat::NONE: return TranslatableString("undoableAction", "Set sharps/flats no preference");
286 0 : case SharpFlat::FLATS: return TranslatableString("undoableAction", "Set prefer flats");
287 0 : case SharpFlat::SHARPS: return TranslatableString("undoableAction", "Set prefer sharps");
288 0 : case SharpFlat::AUTO: return TranslatableString("undoableAction", "Set sharps/flats automatic");
289 : }
290 0 : return TranslatableString("undoableAction", "Set sharps/flats preference");
291 : };
292 :
293 0 : transaction(calcActionName(sharpFlat), [&](engraving::Transaction&) {
294 0 : EditPart::setPartSharpFlat(score(), part, sharpFlat);
295 0 : });
296 :
297 0 : notifyAboutPartChanged(part);
298 0 : }
299 :
300 0 : void NotationParts::setInstrumentName(const InstrumentKey& instrumentKey, const QString& name)
301 : {
302 0 : TRACEFUNC;
303 :
304 0 : Part* part = score()->partById(instrumentKey.partId);
305 0 : if (!part) {
306 0 : return;
307 : }
308 :
309 0 : const mu::engraving::Instrument* instrument = part->instrument(instrumentKey.tick);
310 0 : if (!instrument) {
311 0 : return;
312 : }
313 :
314 0 : String newName = String::fromQString(name);
315 0 : if (instrument->longName() == newName) {
316 0 : return;
317 : }
318 :
319 0 : transaction(TranslatableString("undoableAction", "Set instrument name"), [&](engraving::Transaction&) {
320 0 : EditPart::setInstrumentName(score(), part, instrumentKey.tick, name);
321 0 : });
322 :
323 0 : notifyAboutPartChanged(part);
324 0 : }
325 :
326 0 : void NotationParts::setInstrumentAbbreviature(const InstrumentKey& instrumentKey, const QString& abbreviature)
327 : {
328 0 : TRACEFUNC;
329 :
330 0 : Part* part = score()->partById(instrumentKey.partId);
331 0 : if (!part) {
332 0 : return;
333 : }
334 :
335 0 : const mu::engraving::Instrument* instrument = part->instrument(instrumentKey.tick);
336 0 : if (!instrument) {
337 0 : return;
338 : }
339 :
340 0 : if (instrument->abbreviatureAsPlainText() == abbreviature) {
341 0 : return;
342 : }
343 :
344 0 : transaction(TranslatableString("undoableAction", "Set abbreviated instrument name"), [&](engraving::Transaction&) {
345 0 : EditPart::setInstrumentAbbreviature(score(), part, instrumentKey.tick, abbreviature);
346 0 : });
347 :
348 0 : notifyAboutPartChanged(part);
349 0 : }
350 :
351 0 : void NotationParts::setInstrumentGroupNameOptions(const std::vector<InstrumentKey>& instruments, bool useCustom, const QString& name,
352 : const QString& shortName)
353 : {
354 0 : TRACEFUNC;
355 :
356 0 : std::vector<Part*> changedParts;
357 :
358 0 : transaction(TranslatableString("undoableAction", "Set instrument custom group name"), [&](engraving::Transaction&) {
359 0 : for (const InstrumentKey& key : instruments) {
360 0 : Part* part = score()->partById(key.partId);
361 0 : if (EditPart::setInstrumentGroupNameOptions(score(), part, key.tick, useCustom, name, shortName)) {
362 0 : changedParts.push_back(part);
363 : }
364 : }
365 0 : });
366 :
367 0 : for (Part* p : changedParts) {
368 0 : notifyAboutPartChanged(p);
369 : }
370 0 : }
371 :
372 0 : void NotationParts::setInstrumentNumber(const InstrumentKey& instrumentKey, int v)
373 : {
374 0 : TRACEFUNC;
375 :
376 0 : Part* part = score()->partById(instrumentKey.partId);
377 0 : if (!part) {
378 0 : return;
379 : }
380 :
381 0 : const mu::engraving::Instrument* instrument = part->instrument(instrumentKey.tick);
382 0 : if (!instrument) {
383 0 : return;
384 : }
385 :
386 0 : if (instrument->number() == v) {
387 0 : return;
388 : }
389 :
390 0 : transaction(TranslatableString("undoableAction", "Set instrument number"), [&](engraving::Transaction&) {
391 0 : EditPart::setInstrumentNumber(score(), part, instrumentKey.tick, v);
392 0 : });
393 :
394 0 : notifyAboutPartChanged(part);
395 0 : }
396 :
397 0 : void NotationParts::setStaffType(const ID& staffId, StaffTypeId type)
398 : {
399 0 : TRACEFUNC;
400 :
401 0 : Staff* staff = score()->staffById(staffId);
402 0 : const mu::engraving::StaffType* staffType = mu::engraving::StaffType::preset(type);
403 :
404 0 : if (!staff || !staffType) {
405 0 : return;
406 : }
407 :
408 0 : if (*staff->staffType() == *staffType) {
409 0 : return;
410 : }
411 :
412 0 : transaction(TranslatableString("undoableAction", "Set staff type"), [&](engraving::Transaction&) {
413 0 : EditPart::setStaffType(score(), staff, type);
414 0 : });
415 :
416 0 : notifyAboutStaffChanged(staff);
417 0 : }
418 :
419 0 : void NotationParts::setStaffConfig(const ID& staffId, const StaffConfig& config, Fraction tick)
420 : {
421 0 : TRACEFUNC;
422 :
423 0 : Staff* staff = score()->staffById(staffId);
424 0 : if (!staff) {
425 0 : return;
426 : }
427 :
428 0 : if (makeStaffConfig(staff, tick) == config) {
429 0 : return;
430 : }
431 :
432 0 : transaction(TranslatableString("undoableAction", "Edit staff properties"), [&](engraving::Transaction&) {
433 0 : EditPart::setStaffConfig(score(), staff, config, tick);
434 0 : });
435 :
436 0 : notifyAboutStaffChanged(staff);
437 0 : }
438 :
439 0 : void NotationParts::setSharedPartEnabled(const muse::ID& partId, bool enable)
440 : {
441 0 : TRACEFUNC;
442 :
443 0 : Part* part = score()->partById(partId);
444 0 : if (!part) {
445 0 : return;
446 : }
447 :
448 0 : if (part->getProperty(Pid::SHARED_PART_ENABLED).toBool() == enable) {
449 0 : return;
450 : }
451 :
452 : const TranslatableString actionName = enable
453 0 : ? TranslatableString("undoableAction", "Enable shared staff")
454 0 : : TranslatableString("undoableAction", "Disable shared staff");
455 :
456 0 : transaction(actionName, [&](engraving::Transaction&) {
457 0 : EditPart::setSharedPartEnabled(score(), part, enable);
458 0 : });
459 :
460 0 : notifyAboutPartChanged(part);
461 0 : }
462 :
463 0 : void NotationParts::removeParts(const IDList& partsIds)
464 : {
465 0 : TRACEFUNC;
466 :
467 0 : std::vector<Part*> partsToRemove = parts(partsIds);
468 0 : if (partsToRemove.empty()) {
469 0 : return;
470 : }
471 :
472 0 : endInteractionWithScore();
473 0 : transaction(TranslatableString("undoableAction", "Remove instruments"), [&](engraving::Transaction&) {
474 0 : EditPart::removeParts(score(), partsToRemove);
475 0 : });
476 :
477 0 : for (const Part* part : partsToRemove) {
478 0 : notifyAboutPartRemoved(part);
479 : }
480 0 : }
481 :
482 0 : void NotationParts::removeStaves(const IDList& stavesIds)
483 : {
484 0 : TRACEFUNC;
485 :
486 0 : std::vector<Staff*> stavesToRemove = staves(stavesIds);
487 0 : if (stavesToRemove.empty()) {
488 0 : return;
489 : }
490 :
491 0 : endInteractionWithScore();
492 0 : transaction(TranslatableString("undoableAction", "Remove staves"), [&](engraving::Transaction&) {
493 0 : EditPart::removeStaves(score(), stavesToRemove);
494 0 : });
495 0 : }
496 :
497 0 : void NotationParts::reorderParts(const IDList& partIds, const std::vector<SystemObjectsMove>& systemObjectsMoves)
498 : {
499 0 : TRACEFUNC;
500 :
501 0 : const std::vector<Part*> orderedParts = parts(partIds);
502 0 : const std::vector<EditPart::SystemObjectsMove> moves = editPartSystemObjectsMoves(systemObjectsMoves);
503 0 : if (!EditPart::reorderPartsChangesScore(score(), orderedParts, moves)) {
504 0 : return;
505 : }
506 :
507 0 : endInteractionWithScore();
508 0 : transaction(TranslatableString("undoableAction", "Move instruments"), [&](engraving::Transaction&) {
509 0 : EditPart::reorderParts(score(), orderedParts, moves);
510 0 : });
511 0 : }
512 :
513 0 : void NotationParts::reorderStaves(const IDList& staffIds)
514 : {
515 0 : TRACEFUNC;
516 :
517 0 : const std::vector<Staff*> orderedStaves = staves(staffIds);
518 0 : if (!EditPart::reorderStavesChangesScore(orderedStaves)) {
519 0 : return;
520 : }
521 :
522 0 : endInteractionWithScore();
523 0 : transaction(TranslatableString("undoableAction", "Move staves"), [&](engraving::Transaction&) {
524 0 : EditPart::reorderStaves(score(), orderedStaves);
525 0 : });
526 0 : }
527 :
528 0 : bool NotationParts::appendStaff(Staff* staff, const ID& destinationPartId)
529 : {
530 0 : TRACEFUNC;
531 :
532 0 : IF_ASSERT_FAILED(staff) {
533 0 : return false;
534 : }
535 :
536 0 : Part* destinationPart = score()->partById(destinationPartId);
537 0 : if (!destinationPart) {
538 0 : return false;
539 : }
540 :
541 0 : transaction(TranslatableString("undoableAction", "Add staff"), [&](engraving::Transaction&) {
542 0 : EditPart::appendStaff(score(), staff, destinationPart);
543 0 : });
544 :
545 0 : return true;
546 0 : }
547 :
548 0 : bool NotationParts::appendLinkedStaff(Staff* staff, const muse::ID& sourceStaffId, const muse::ID& destinationPartId)
549 : {
550 0 : TRACEFUNC;
551 :
552 0 : IF_ASSERT_FAILED(staff) {
553 0 : return false;
554 : }
555 :
556 0 : Staff* sourceStaff = score()->staffById(sourceStaffId);
557 0 : Part* destinationPart = score()->partById(destinationPartId);
558 0 : if (!sourceStaff || !destinationPart) {
559 0 : return false;
560 : }
561 :
562 0 : transaction(TranslatableString("undoableAction", "Add linked staff"), [&](engraving::Transaction&) {
563 0 : EditPart::appendLinkedStaff(score(), staff, sourceStaff, destinationPart);
564 0 : });
565 :
566 0 : return true;
567 0 : }
568 :
569 0 : void NotationParts::insertPart(Part* part, size_t index)
570 : {
571 0 : TRACEFUNC;
572 :
573 0 : if (!part) {
574 0 : return;
575 : }
576 :
577 0 : transaction(TranslatableString("undoableAction", "Add instrument"), [&](engraving::Transaction&) {
578 0 : EditPart::insertPart(score(), part, index);
579 0 : });
580 :
581 0 : notifyAboutPartAdded(part);
582 0 : }
583 :
584 0 : void NotationParts::replacePart(const ID& partId, Part* newPart)
585 : {
586 0 : TRACEFUNC;
587 :
588 0 : Part* part = score()->partById(partId);
589 0 : if (!part || !newPart) {
590 0 : return;
591 : }
592 :
593 0 : transaction(TranslatableString("undoableAction", "Reset instrument formatting"), [&](engraving::Transaction&) {
594 0 : EditPart::replacePart(score(), part, newPart);
595 0 : });
596 :
597 0 : notifyAboutPartReplaced(part, newPart);
598 0 : }
599 :
600 0 : void NotationParts::replaceInstrument(const InstrumentKey& instrumentKey, const Instrument& newInstrument, const StaffType* newStaffType)
601 : {
602 0 : TRACEFUNC;
603 :
604 0 : Part* part = score()->partById(instrumentKey.partId);
605 0 : if (!part) {
606 0 : return;
607 : }
608 :
609 0 : bool replaced = false;
610 :
611 0 : transaction(TranslatableString("undoableAction", "Replace instrument"), [&](engraving::Transaction&) {
612 0 : replaced = EditPart::replaceInstrument(score(), part, instrumentKey.tick, newInstrument, newStaffType);
613 0 : });
614 :
615 0 : if (replaced) {
616 0 : notifyAboutPartChanged(part);
617 : }
618 0 : }
619 :
620 0 : void NotationParts::replaceDrumset(const InstrumentKey& instrumentKey, const Drumset& newDrumset, bool undoable)
621 : {
622 0 : Part* part = score()->partById(instrumentKey.partId);
623 0 : if (!part) {
624 0 : return;
625 : }
626 :
627 0 : auto replace = [this, part, &instrumentKey, &newDrumset, undoable]() {
628 0 : for (const auto& pair : part->instruments()) {
629 0 : const Instrument* instrument = pair.second;
630 0 : if (instrument && instrument->drumset() && instrument->id() == instrumentKey.instrumentId) {
631 0 : EditPart::replaceDrumset(score(), part, Fraction::fromTicks(pair.first), newDrumset, undoable);
632 : }
633 : }
634 0 : };
635 :
636 0 : if (undoable) {
637 0 : transaction(TranslatableString("undoableAction", "Edit drumset"), [&replace](engraving::Transaction&) {
638 0 : replace();
639 0 : });
640 : } else {
641 0 : replace();
642 : }
643 :
644 0 : notifyAboutPartChanged(part);
645 0 : m_interaction->noteInput()->stateChanged().notify();
646 : }
647 :
648 0 : const std::vector<Staff*>& NotationParts::systemObjectStaves() const
649 : {
650 0 : return m_systemObjectStaves;
651 : }
652 :
653 0 : muse::async::Notification NotationParts::systemObjectStavesChanged() const
654 : {
655 0 : return m_systemObjectStavesChanged;
656 : }
657 :
658 0 : void NotationParts::addSystemObjects(const muse::IDList& stavesIds)
659 : {
660 0 : std::vector<Staff*> staves = this->staves(stavesIds);
661 0 : if (staves.empty()) {
662 0 : return;
663 : }
664 :
665 0 : transaction(TranslatableString("undoableAction", "Add system markings"), [&](engraving::Transaction&) {
666 0 : EditPart::addSystemObjects(score(), staves);
667 0 : });
668 0 : }
669 :
670 0 : void NotationParts::removeSystemObjects(const IDList& stavesIds)
671 : {
672 0 : std::vector<Staff*> staves = this->staves(stavesIds);
673 0 : if (staves.empty()) {
674 0 : return;
675 : }
676 :
677 0 : transaction(TranslatableString("undoableAction", "Remove system markings"), [&](engraving::Transaction&) {
678 0 : EditPart::removeSystemObjects(score(), staves);
679 0 : });
680 0 : }
681 :
682 0 : void NotationParts::toggleStaveSharing(bool on)
683 : {
684 0 : transaction(TranslatableString("undoableAction", "Toggle stave sharing"), [&](engraving::Transaction& tx) {
685 0 : EditStaveSharing::toggleStaveSharing(tx, score(), on);
686 0 : });
687 0 : }
688 :
689 0 : Notification NotationParts::partsChanged() const
690 : {
691 0 : return m_partsChanged;
692 : }
693 :
694 0 : Notification NotationParts::scoreOrderChanged() const
695 : {
696 0 : return m_scoreOrderChanged;
697 : }
698 :
699 0 : Notification NotationParts::sharedPartsChanged() const
700 : {
701 0 : return m_sharedPartsChanged;
702 : }
703 :
704 0 : mu::engraving::Score* NotationParts::score() const
705 : {
706 0 : return m_getScore->score();
707 : }
708 :
709 0 : void NotationParts::transaction(const TranslatableString& actionName, const std::function<void(engraving::Transaction&)>& func)
710 : {
711 0 : if (m_undoStack->isLocked()) {
712 0 : func(score()->transactionManager()->currentOrDummyTransaction());
713 : } else {
714 0 : m_undoStack->transaction(actionName, func);
715 : }
716 :
717 0 : m_partsChanged.notify();
718 0 : }
719 :
720 0 : void NotationParts::listenUndoStackChanges()
721 : {
722 0 : if (!score()) {
723 0 : return;
724 : }
725 :
726 0 : updatePartsAndSystemObjectStaves();
727 :
728 0 : m_undoStack->changesChannel().onReceive(this, [this](const ScoreChanges& changes) {
729 0 : if (changes.isTextEditing || changes.changedTypes.empty() || m_ignoreUndoStackChanges) {
730 0 : return;
731 : }
732 :
733 : static const ElementTypeSet TYPES_TO_CHECK {
734 : ElementType::SCORE,
735 : ElementType::STAFF,
736 : ElementType::PART,
737 : ElementType::SHARED_PART,
738 0 : };
739 :
740 0 : for (ElementType type : TYPES_TO_CHECK) {
741 0 : if (muse::contains(changes.changedTypes, type)) {
742 0 : updatePartsAndSystemObjectStaves(changes);
743 0 : return;
744 : }
745 : }
746 : });
747 0 : }
748 :
749 0 : void NotationParts::updatePartsAndSystemObjectStaves(const mu::engraving::ScoreChanges& changes)
750 : {
751 0 : const bool partsChanged = m_parts != score()->parts();
752 0 : m_parts = score()->parts();
753 :
754 0 : std::vector<Staff*> newSystemObjectStaves = score()->systemObjectStavesWithTopStaff();
755 0 : const bool systemObjectStavesChanged = m_systemObjectStaves != newSystemObjectStaves;
756 0 : m_systemObjectStaves = std::move(newSystemObjectStaves);
757 :
758 0 : if (partsChanged) {
759 0 : m_partChangedNotifier.changed();
760 : }
761 :
762 0 : if (systemObjectStavesChanged) {
763 0 : m_systemObjectStavesChanged.notify();
764 : }
765 :
766 0 : if (muse::contains(changes.changedTypes, ElementType::SHARED_PART)) {
767 0 : m_sharedPartsChanged.notify();
768 : }
769 :
770 0 : std::vector<Staff*> removedStaves;
771 0 : std::vector<Staff*> addedStaves;
772 :
773 0 : bool stavesSorted = false;
774 :
775 0 : for (auto& pair : changes.changedObjects) {
776 0 : if (muse::contains(pair.second, CommandType::SortStaves)) {
777 0 : stavesSorted = true;
778 0 : continue;
779 : }
780 :
781 0 : if (!pair.first->isStaff()) {
782 0 : continue;
783 : }
784 :
785 0 : Staff* staff = toStaff(pair.first);
786 :
787 0 : if (muse::contains(pair.second, CommandType::RemoveStaff)) {
788 0 : removedStaves.push_back(staff);
789 0 : } else if (muse::contains(pair.second, CommandType::InsertStaff)) {
790 0 : addedStaves.push_back(staff);
791 : }
792 : }
793 :
794 0 : if (stavesSorted && !partsChanged) {
795 0 : m_partChangedNotifier.changed();
796 0 : return;
797 : }
798 :
799 0 : for (Staff* staff : removedStaves) {
800 0 : notifyAboutStaffRemoved(staff);
801 : }
802 :
803 0 : for (Staff* staff: addedStaves) {
804 0 : notifyAboutStaffAdded(staff);
805 : }
806 0 : }
807 :
808 0 : void NotationParts::listenStyleChanges()
809 : {
810 0 : if (!score()) {
811 0 : return;
812 : }
813 :
814 0 : m_style->styleChanged().onNotify(this, [this]{ m_sharedPartsChanged.notify(); });
815 : }
816 :
817 0 : std::vector<EditPart::PartEntry> NotationParts::partEntries(const PartInstrumentList& parts) const
818 : {
819 0 : std::vector<EditPart::PartEntry> entries;
820 0 : entries.reserve(parts.size());
821 :
822 0 : for (const PartInstrument& pi : parts) {
823 0 : if (pi.isExistingPart) {
824 0 : entries.push_back({ score()->partById(pi.partId), nullptr, pi.isSoloist });
825 : } else {
826 0 : entries.push_back({ nullptr, &pi.instrumentTemplate, pi.isSoloist });
827 : }
828 : }
829 :
830 0 : return entries;
831 0 : }
832 :
833 0 : std::vector<Staff*> NotationParts::staves(const IDList& stavesIds) const
834 : {
835 0 : std::vector<Staff*> staves;
836 0 : staves.reserve(stavesIds.size());
837 :
838 0 : for (const ID& staffId : stavesIds) {
839 0 : if (Staff* staff = score()->staffById(staffId)) {
840 0 : staves.push_back(staff);
841 : }
842 : }
843 :
844 0 : return staves;
845 0 : }
846 :
847 0 : std::vector<Part*> NotationParts::parts(const IDList& partsIds) const
848 : {
849 0 : std::vector<Part*> parts;
850 0 : parts.reserve(partsIds.size());
851 :
852 0 : for (const ID& partId : partsIds) {
853 0 : if (Part* part = score()->partById(partId)) {
854 0 : parts.push_back(part);
855 : }
856 : }
857 :
858 0 : return parts;
859 0 : }
860 :
861 0 : std::vector<EditPart::SystemObjectsMove> NotationParts::editPartSystemObjectsMoves(const std::vector<SystemObjectsMove>& moves) const
862 : {
863 0 : std::vector<EditPart::SystemObjectsMove> result;
864 0 : result.reserve(moves.size());
865 :
866 0 : for (const SystemObjectsMove& move : moves) {
867 0 : result.push_back({ score()->staffById(move.staffId), score()->staffById(move.destinationStaffId), move.belowBottomStaff });
868 : }
869 :
870 0 : return result;
871 0 : }
872 :
873 0 : void NotationParts::endInteractionWithScore()
874 : {
875 0 : m_interaction->clearSelection();
876 0 : m_interaction->noteInput()->endNoteInput(/*resetState=*/ true);
877 0 : }
878 :
879 0 : void NotationParts::notifyAboutPartChanged(const Part* part) const
880 : {
881 0 : IF_ASSERT_FAILED(part) {
882 0 : return;
883 : }
884 :
885 0 : m_partChangedNotifier.itemChanged(part);
886 : }
887 :
888 0 : void NotationParts::notifyAboutPartAdded(const Part* part) const
889 : {
890 0 : IF_ASSERT_FAILED(part) {
891 0 : return;
892 : }
893 :
894 0 : m_partChangedNotifier.itemAdded(part);
895 : }
896 :
897 0 : void NotationParts::notifyAboutPartRemoved(const Part* part) const
898 : {
899 0 : IF_ASSERT_FAILED(part) {
900 0 : return;
901 : }
902 :
903 0 : m_partChangedNotifier.itemRemoved(part);
904 : }
905 :
906 0 : void NotationParts::notifyAboutPartReplaced(const Part* oldPart, const Part* newPart) const
907 : {
908 0 : IF_ASSERT_FAILED(oldPart && newPart) {
909 0 : return;
910 : }
911 :
912 0 : m_partChangedNotifier.itemReplaced(oldPart, newPart);
913 : }
914 :
915 0 : void NotationParts::notifyAboutStaffChanged(const Staff* staff) const
916 : {
917 0 : IF_ASSERT_FAILED(staff && staff->part()) {
918 0 : return;
919 : }
920 :
921 0 : ChangedNotifier<const Staff*>& notifier = m_staffChangedNotifierMap[staff->part()->id()];
922 0 : notifier.itemChanged(staff);
923 : }
924 :
925 0 : void NotationParts::notifyAboutStaffAdded(const Staff* staff) const
926 : {
927 0 : IF_ASSERT_FAILED(staff && staff->part()) {
928 0 : return;
929 : }
930 :
931 0 : ChangedNotifier<const Staff*>& notifier = m_staffChangedNotifierMap[staff->part()->id()];
932 0 : notifier.itemAdded(staff);
933 : }
934 :
935 0 : void NotationParts::notifyAboutStaffRemoved(const Staff* staff) const
936 : {
937 0 : IF_ASSERT_FAILED(staff && staff->part()) {
938 0 : return;
939 : }
940 :
941 0 : ChangedNotifier<const Staff*>& notifier = m_staffChangedNotifierMap[staff->part()->id()];
942 0 : notifier.itemRemoved(staff);
943 : }
|