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) 2024 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 <QJsonArray>
23 : #include <QJsonDocument>
24 : #include <QJsonObject>
25 :
26 : #include "engraving/infrastructure/mscreader.h"
27 :
28 : #include "notationsolomutestate.h"
29 :
30 : using namespace muse;
31 : using namespace mu::engraving;
32 : using namespace mu::notation;
33 :
34 0 : Ret NotationSoloMuteState::read(const engraving::MscReader& reader, const muse::io::path_t& pathPrefix)
35 : {
36 0 : const ByteArray json = reader.readAudioSettingsJsonFile(pathPrefix);
37 :
38 0 : if (json.empty()) {
39 0 : return make_ret(Ret::Code::UnknownError);
40 : }
41 :
42 0 : const QJsonObject rootObj = QJsonDocument::fromJson(json.toQByteArrayNoCopy()).object();
43 :
44 0 : const QJsonArray tracksArray = rootObj.value("tracks").toArray();
45 0 : for (const QJsonValueConstRef track : tracksArray) {
46 0 : const QJsonObject trackObject = track.toObject();
47 :
48 : const InstrumentTrackId id = {
49 0 : trackObject.value("partId").toString(),
50 0 : trackObject.value("instrumentId").toString()
51 0 : };
52 :
53 0 : QJsonObject soloMuteObj = trackObject.value("soloMuteState").toObject();
54 0 : SoloMuteState soloMuteState;
55 0 : soloMuteState.mute = soloMuteObj.value("mute").toBool();
56 0 : soloMuteState.solo = soloMuteObj.value("solo").toBool();
57 :
58 0 : m_trackSoloMuteStatesMap.emplace(id, std::move(soloMuteState));
59 0 : }
60 :
61 0 : return make_ret(Ret::Code::Ok);
62 0 : }
63 :
64 0 : Ret NotationSoloMuteState::write(io::IODevice* out)
65 : {
66 0 : QJsonObject rootObj;
67 0 : QJsonArray tracksArray;
68 :
69 0 : for (const auto& pair : m_trackSoloMuteStatesMap) {
70 0 : QJsonObject currentTrack;
71 0 : currentTrack["instrumentId"] = pair.first.instrumentId.toQString();
72 0 : currentTrack["partId"] = pair.first.partId.toQString();
73 :
74 0 : QJsonObject soloMuteStateObject;
75 0 : soloMuteStateObject["mute"] = pair.second.mute;
76 0 : soloMuteStateObject["solo"] = pair.second.solo;
77 :
78 0 : currentTrack["soloMuteState"] = soloMuteStateObject;
79 0 : tracksArray.append(currentTrack);
80 0 : }
81 :
82 0 : rootObj["tracks"] = tracksArray;
83 :
84 0 : out->write(QJsonDocument(rootObj).toJson());
85 :
86 0 : return make_ret(Ret::Code::Ok);
87 0 : }
88 :
89 0 : bool NotationSoloMuteState::trackSoloMuteStateExists(const engraving::InstrumentTrackId& partId) const
90 : {
91 0 : auto search = m_trackSoloMuteStatesMap.find(partId);
92 0 : return search != m_trackSoloMuteStatesMap.end();
93 : }
94 :
95 0 : const INotationSoloMuteState::SoloMuteState& NotationSoloMuteState::trackSoloMuteState(const InstrumentTrackId& partId) const
96 : {
97 0 : auto search = m_trackSoloMuteStatesMap.find(partId);
98 :
99 0 : if (search == m_trackSoloMuteStatesMap.end()) {
100 : static const SoloMuteState dummySoloMuteState;
101 0 : return dummySoloMuteState;
102 : }
103 :
104 0 : return search->second;
105 : }
106 :
107 0 : void NotationSoloMuteState::setTrackSoloMuteState(const InstrumentTrackId& partId, const SoloMuteState& state)
108 : {
109 0 : auto it = m_trackSoloMuteStatesMap.find(partId);
110 0 : if (it != m_trackSoloMuteStatesMap.end() && it->second == state) {
111 0 : return;
112 : }
113 :
114 0 : m_trackSoloMuteStatesMap.insert_or_assign(partId, state);
115 0 : m_trackSoloMuteStateChanged.send(partId, state);
116 : }
117 :
118 0 : void NotationSoloMuteState::removeTrackSoloMuteState(const engraving::InstrumentTrackId& trackId)
119 : {
120 0 : auto soloMuteSearch = m_trackSoloMuteStatesMap.find(trackId);
121 0 : if (soloMuteSearch != m_trackSoloMuteStatesMap.end()) {
122 0 : m_trackSoloMuteStatesMap.erase(soloMuteSearch);
123 : }
124 0 : }
125 :
126 0 : muse::async::Channel<InstrumentTrackId, INotationSoloMuteState::SoloMuteState> NotationSoloMuteState::trackSoloMuteStateChanged() const
127 : {
128 0 : return m_trackSoloMuteStateChanged;
129 : }
|