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 "selectinstrumentscenario.h"
23 :
24 : #include "log.h"
25 :
26 : using namespace muse;
27 : using namespace muse::async;
28 : using namespace mu::instrumentsscene;
29 : using namespace mu::notation;
30 :
31 6 : muse::async::Promise<PartInstrumentListScoreOrder> SelectInstrumentsScenario::selectInstruments(const ScoreOrder& currentOrder) const
32 : {
33 : ValMap params {
34 6 : { "canSelectMultipleInstruments", Val(true) }
35 18 : };
36 :
37 12 : return selectInstruments(params, currentOrder);
38 12 : }
39 :
40 1 : muse::async::Promise<InstrumentTemplate> SelectInstrumentsScenario::selectInstrument(const InstrumentKey& currentInstrumentKey) const
41 : {
42 : ValMap params {
43 0 : { "canSelectMultipleInstruments", Val(false) },
44 2 : { "currentInstrumentId", Val(currentInstrumentKey.instrumentId.toStdString()) }
45 4 : };
46 :
47 2 : return async::make_promise<InstrumentTemplate>([this, params](auto resolve, auto reject) {
48 1 : Promise<PartInstrumentListScoreOrder> selectedInstruments = selectInstruments(params, ScoreOrder());
49 1 : selectedInstruments.onResolve(this, [resolve](const PartInstrumentListScoreOrder& selectedInstruments) {
50 1 : const InstrumentTemplate& tpl = selectedInstruments.instruments.front().instrumentTemplate;
51 1 : (void)resolve(tpl);
52 : });
53 1 : selectedInstruments.onReject(this, [reject](int code, const std::string& msg) {
54 0 : (void)reject(code, msg);
55 : });
56 :
57 2 : return Promise<InstrumentTemplate>::dummy_result();
58 3 : });
59 3 : }
60 :
61 7 : muse::async::Promise<PartInstrumentListScoreOrder> SelectInstrumentsScenario::selectInstruments(const ValMap& params,
62 : const ScoreOrder& currentOrder) const
63 : {
64 14 : return async::make_promise<PartInstrumentListScoreOrder>([this, params, currentOrder](auto resolve, auto reject) {
65 9 : static const Uri SELECT_INSTRUMENT_URI = Uri("musescore://instruments/select");
66 7 : if (interactive()->isOpened(SELECT_INSTRUMENT_URI).val) {
67 1 : Ret ret = muse::make_ret(Ret::Code::Cancel);
68 1 : return reject(ret.code(), ret.text());
69 1 : }
70 :
71 6 : UriQuery q(SELECT_INSTRUMENT_URI);
72 6 : q.set(params);
73 :
74 6 : auto promise = interactive()->open(q);
75 6 : promise.onResolve(this, [this, currentOrder, resolve, reject](const Val& val) {
76 6 : ValMap content = val.toMap();
77 6 : const ValList instruments = content["instruments"].toList();
78 :
79 6 : IF_ASSERT_FAILED(!instruments.empty()) {
80 0 : Ret ret = muse::make_ret(Ret::Code::UnknownError);
81 0 : (void)reject(ret.code(), ret.text());
82 0 : return;
83 0 : }
84 :
85 6 : PartInstrumentListScoreOrder result;
86 6 : result.instruments.reserve(instruments.size());
87 :
88 13 : for (const Val& obj: instruments) {
89 7 : ValMap map = obj.toMap();
90 :
91 7 : PartInstrument pi;
92 14 : pi.partId = ID(map["partId"].toString());
93 14 : pi.isExistingPart = map["isExistingPart"].toBool();
94 14 : pi.isSoloist = map["isSoloist"].toBool();
95 :
96 7 : const String instrumentId = String::fromStdString(map["instrumentId"].toString());
97 7 : pi.instrumentTemplate = instrumentsRepository()->instrumentTemplate(instrumentId);
98 :
99 7 : result.instruments.push_back(pi);
100 : }
101 :
102 12 : ValMap order = content["scoreOrder"].toMap();
103 6 : const String orderId = String::fromStdString(order["id"].toString());
104 6 : const ScoreOrder& builtInOrder = instrumentsRepository()->order(orderId);
105 :
106 : // Orders that aren't built in (e.g. those that come with templates) are the score's own
107 6 : result.scoreOrder = (builtInOrder.id != orderId && currentOrder.id == orderId) ? currentOrder : builtInOrder;
108 6 : result.scoreOrder.customized = order["customized"].toBool();
109 :
110 6 : (void)resolve(result);
111 6 : });
112 :
113 6 : promise.onReject(this, [reject](int code, const std::string& msg) {
114 0 : (void)reject(code, msg);
115 : });
116 :
117 6 : return Promise<PartInstrumentListScoreOrder>::dummy_result();
118 20 : });
119 : }
|