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 :
23 : #include "instrumentsactionscontroller.h"
24 :
25 : #include "engraving/dom/instrchange.h"
26 :
27 : #include "notation/imasternotation.h"
28 : #include "notation/inotation.h"
29 : #include "notation/inotationinteraction.h"
30 : #include "notation/inotationparts.h" // IWYU pragma: keep
31 : #include "notation/inotationselection.h" // IWYU pragma: keep
32 :
33 : #include "../instrumentscommands.h"
34 : #include "types/ret.h"
35 :
36 : using namespace mu::instrumentsscene;
37 : using namespace mu::notation;
38 : using namespace muse;
39 :
40 : static const muse::Uri NOTATION_REVIEW_PAGE_URI("musescore://notation/review");
41 :
42 0 : void InstrumentsActionsController::init()
43 : {
44 0 : commandDispatcher()->onRequest(this, INSTRUMENTS_SELECT_COMMAND, [this]() { selectInstruments(); return muse::make_ok(); });
45 0 : commandDispatcher()->onRequest(this, INSTRUMENTS_CHANGE_COMMAND, [this]() { changeInstrument(); return muse::make_ok(); });
46 0 : }
47 :
48 0 : void InstrumentsActionsController::selectInstruments()
49 : {
50 0 : IMasterNotationPtr master = context()->masterNotation();
51 0 : IF_ASSERT_FAILED(master) {
52 0 : return;
53 : }
54 :
55 0 : async::Promise<PartInstrumentListScoreOrder> selectedInstruments = selectInstrumentsScenario()->selectInstruments();
56 0 : selectedInstruments.onResolve(this, [master](const PartInstrumentListScoreOrder& sel) {
57 0 : master->parts()->setParts(sel.instruments, sel.scoreOrder);
58 0 : });
59 0 : }
60 :
61 0 : void InstrumentsActionsController::changeInstrument()
62 : {
63 0 : IMasterNotationPtr master = context()->masterNotation();
64 0 : IF_ASSERT_FAILED(master) {
65 0 : return;
66 : }
67 :
68 0 : INotationPtr notation = context()->currentNotation();
69 0 : IF_ASSERT_FAILED(notation) {
70 0 : return;
71 : }
72 :
73 0 : const mu::engraving::EngravingItem* element = notation->interaction()->hitElementContext().element;
74 0 : if (!element) {
75 0 : element = notation->interaction()->selection()->element();
76 : }
77 :
78 0 : if (!element || !element->isInstrumentChange()) {
79 0 : return;
80 : }
81 :
82 0 : const mu::engraving::InstrumentChange* instrumentChange = mu::engraving::toInstrumentChange(element);
83 :
84 0 : InstrumentKey key;
85 0 : key.instrumentId = instrumentChange->instrument()->id();
86 0 : key.partId = instrumentChange->part()->id();
87 0 : key.tick = instrumentChange->tick();
88 :
89 0 : async::Promise<InstrumentTemplate> templ = selectInstrumentsScenario()->selectInstrument(key);
90 0 : templ.onResolve(this, [master, key](const InstrumentTemplate& val) {
91 0 : Instrument instrument = Instrument::fromTemplate(&val);
92 0 : master->parts()->replaceInstrument(key, instrument);
93 0 : });
94 0 : }
|