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 0 : muse::async::Promise<PartInstrumentListScoreOrder> SelectInstrumentsScenario::selectInstruments() const
32 : {
33 : ValMap params {
34 0 : { "canSelectMultipleInstruments", Val(true) }
35 0 : };
36 :
37 0 : return selectInstruments(params);
38 0 : }
39 :
40 0 : muse::async::Promise<InstrumentTemplate> SelectInstrumentsScenario::selectInstrument(const InstrumentKey& currentInstrumentKey) const
41 : {
42 : ValMap params {
43 0 : { "canSelectMultipleInstruments", Val(false) },
44 0 : { "currentInstrumentId", Val(currentInstrumentKey.instrumentId.toStdString()) }
45 0 : };
46 :
47 0 : return async::make_promise<InstrumentTemplate>([this, params](auto resolve, auto reject) {
48 0 : Promise<PartInstrumentListScoreOrder> selectedInstruments = selectInstruments(params);
49 0 : selectedInstruments.onResolve(this, [resolve](const PartInstrumentListScoreOrder& selectedInstruments) {
50 0 : const InstrumentTemplate& tpl = selectedInstruments.instruments.first().instrumentTemplate;
51 0 : (void)resolve(tpl);
52 : });
53 0 : selectedInstruments.onReject(this, [reject](int code, const std::string& msg) {
54 0 : (void)reject(code, msg);
55 : });
56 :
57 0 : return Promise<InstrumentTemplate>::dummy_result();
58 0 : });
59 0 : }
60 :
61 0 : muse::async::Promise<PartInstrumentListScoreOrder> SelectInstrumentsScenario::selectInstruments(const ValMap& params) const
62 : {
63 0 : return async::make_promise<PartInstrumentListScoreOrder>([this, params](auto resolve, auto reject) {
64 0 : static const Uri SELECT_INSTRUMENT_URI = Uri("musescore://instruments/select");
65 0 : if (interactive()->isOpened(SELECT_INSTRUMENT_URI).val) {
66 0 : Ret ret = muse::make_ret(Ret::Code::Cancel);
67 0 : return reject(ret.code(), ret.text());
68 0 : }
69 :
70 0 : UriQuery q(SELECT_INSTRUMENT_URI);
71 0 : q.set(params);
72 :
73 0 : auto promise = interactive()->open(q);
74 0 : promise.onResolve(this, [this, resolve, reject](const Val& val) {
75 0 : ValMap content = val.toMap();
76 0 : ValList instruments = content["instruments"].toList();
77 :
78 0 : IF_ASSERT_FAILED(!instruments.empty()) {
79 0 : Ret ret = muse::make_ret(Ret::Code::UnknownError);
80 0 : (void)reject(ret.code(), ret.text());
81 0 : return;
82 0 : }
83 :
84 0 : PartInstrumentListScoreOrder result;
85 :
86 0 : for (const Val& obj: instruments) {
87 0 : ValMap map = obj.toMap();
88 0 : PartInstrument pi;
89 :
90 0 : pi.partId = ID(map["partId"].toString());
91 0 : pi.isExistingPart = map["isExistingPart"].toBool();
92 0 : pi.isSoloist = map["isSoloist"].toBool();
93 :
94 0 : String instrumentId = String::fromStdString(map["instrumentId"].toString());
95 0 : pi.instrumentTemplate = instrumentsRepository()->instrumentTemplate(instrumentId);
96 :
97 0 : result.instruments << pi;
98 : }
99 :
100 0 : ValMap order = content["scoreOrder"].toMap();
101 0 : result.scoreOrder = instrumentsRepository()->order(String::fromStdString(order["id"].toString()));
102 0 : result.scoreOrder.customized = order["customized"].toBool();
103 :
104 0 : (void)resolve(result);
105 0 : });
106 :
107 0 : promise.onReject(this, [reject](int code, const std::string& msg) {
108 0 : (void)reject(code, msg);
109 : });
110 :
111 0 : return Promise<PartInstrumentListScoreOrder>::dummy_result();
112 0 : }, PromiseType::AsyncByBody);
113 : }
|