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) 2022 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 "soundprofilesmodel.h"
24 :
25 : #include "notation/imasternotation.h" // IWYU pragma: keep
26 : #include "notation/inotationplayback.h"
27 :
28 : #include "project/inotationproject.h"
29 : #include "project/iprojectaudiosettings.h" // IWYU pragma: keep
30 :
31 : using namespace mu::playback;
32 : using namespace mu::project;
33 :
34 0 : SoundProfilesModel::SoundProfilesModel(QObject* parent)
35 0 : : QAbstractListModel(parent), muse::Contextable(muse::iocCtxForQmlObject(this))
36 : {
37 0 : }
38 :
39 0 : void SoundProfilesModel::init()
40 : {
41 0 : beginResetModel();
42 :
43 0 : const SoundProfilesMap& availableProfiles = profilesRepo()->availableProfiles();
44 0 : for (const auto& pair : availableProfiles) {
45 0 : m_profiles.push_back(pair.second);
46 : }
47 :
48 0 : std::sort(m_profiles.begin(), m_profiles.end(), [this](const SoundProfile& left, const SoundProfile& right) {
49 0 : if (left.name == config()->basicSoundProfileName()
50 0 : && right.name == config()->museSoundsProfileName()) {
51 0 : return true;
52 : }
53 :
54 0 : return false;
55 : });
56 :
57 0 : if (INotationProjectPtr project = context()->project()) {
58 0 : m_activeProfile = project->audioSettings()->activeSoundProfile().toQString();
59 0 : m_currentlySelectedProfile = m_activeProfile;
60 0 : }
61 :
62 0 : m_defaultProjectsProfile = config()->defaultProfileForNewProjects().toQString();
63 :
64 0 : endResetModel();
65 0 : }
66 :
67 0 : int SoundProfilesModel::rowCount(const QModelIndex& /*parent*/) const
68 : {
69 0 : return static_cast<int>(m_profiles.size());
70 : }
71 :
72 0 : QVariant SoundProfilesModel::data(const QModelIndex& index, int role) const
73 : {
74 0 : if (!index.isValid()
75 0 : || m_profiles.empty()
76 0 : || index.row() > static_cast<int>(m_profiles.size() - 1)) {
77 0 : return QVariant();
78 : }
79 :
80 0 : const SoundProfile& profile = m_profiles.at(index.row());
81 :
82 0 : switch (role) {
83 0 : case RoleTitle:
84 0 : return profile.name.toQString();
85 0 : case RoleEnabled:
86 0 : return profile.isEnabled();
87 : }
88 :
89 0 : return QVariant();
90 : }
91 :
92 0 : QHash<int, QByteArray> SoundProfilesModel::roleNames() const
93 : {
94 : static const QHash<int, QByteArray> roles {
95 0 : { RoleTitle, "title" },
96 0 : { RoleEnabled, "isEnabled" }
97 0 : };
98 :
99 0 : return roles;
100 0 : }
101 :
102 0 : const QString& SoundProfilesModel::activeProfile() const
103 : {
104 0 : return m_activeProfile;
105 : }
106 :
107 0 : void SoundProfilesModel::setActiveProfile(const QString& newActiveProfile)
108 : {
109 0 : if (m_activeProfile == newActiveProfile) {
110 0 : return;
111 : }
112 :
113 0 : askAboutChangingSounds().onResolve(this, [this, newActiveProfile](bool change) {
114 0 : if (change) {
115 0 : applyActiveProfile(newActiveProfile);
116 : }
117 0 : });
118 : }
119 :
120 0 : void SoundProfilesModel::applyActiveProfile(const QString& newActiveProfile)
121 : {
122 0 : if (m_activeProfile == newActiveProfile) {
123 0 : return;
124 : }
125 :
126 0 : m_activeProfile = newActiveProfile;
127 :
128 0 : if (INotationProjectPtr project = context()->project()) {
129 0 : SoundProfileName activeProfileName = SoundProfileName::fromQString(newActiveProfile);
130 0 : project->audioSettings()->setActiveSoundProfile(activeProfileName);
131 0 : controller()->applyProfile(activeProfileName);
132 0 : }
133 :
134 0 : emit activeProfileChanged();
135 : }
136 :
137 0 : const QString& SoundProfilesModel::defaultProjectsProfile() const
138 : {
139 0 : return m_defaultProjectsProfile;
140 : }
141 :
142 0 : void SoundProfilesModel::setDefaultProjectsProfile(const QString& newDefaultProjectsProfile)
143 : {
144 0 : if (m_defaultProjectsProfile == newDefaultProjectsProfile) {
145 0 : return;
146 : }
147 :
148 0 : m_defaultProjectsProfile = newDefaultProjectsProfile;
149 0 : config()->setDefaultProfileForNewProjects(SoundProfileName::fromQString(newDefaultProjectsProfile));
150 :
151 0 : emit defaultProjectsProfileChanged();
152 : }
153 :
154 0 : mu::notation::INotationPlaybackPtr SoundProfilesModel::notationPlayback() const
155 : {
156 0 : project::INotationProjectPtr project = context()->project();
157 0 : return project ? project->masterNotation()->playback() : nullptr;
158 0 : }
159 :
160 0 : muse::async::Promise<bool> SoundProfilesModel::askAboutChangingSounds()
161 : {
162 0 : if (!config()->needToShowResetSoundFlagsWhenChangePlaybackProfileWarning()
163 0 : || !notationPlayback()->hasSoundFlags(notationPlayback()->existingTrackIdSet())) {
164 0 : return muse::async::make_promise<bool>([](auto resolve) {
165 0 : return resolve(true);
166 0 : });
167 : }
168 :
169 0 : return muse::async::make_promise<bool>([this](auto resolve) {
170 0 : int changeBtn = int(muse::IInteractive::Button::Apply);
171 0 : muse::IInteractive::Options options = muse::IInteractive::Option::WithIcon | muse::IInteractive::Option::WithDontShowAgainCheckBox;
172 0 : muse::IInteractive::ButtonDatas buttons = {
173 0 : interactive()->buttonData(muse::IInteractive::Button::Cancel),
174 0 : muse::IInteractive::ButtonData(changeBtn, muse::trc("playback", "Change sounds"), true /*accent*/)
175 : };
176 :
177 0 : interactive()->warning(muse::trc("playback", "Are you sure you want to change sounds?"),
178 0 : muse::IInteractive::Text(muse::trc("playback",
179 : "Sound flags may be reset, but staff text will remain. This action can’t be undone.")),
180 : buttons, changeBtn, options)
181 0 : .onResolve(this, [this, changeBtn, resolve](const muse::IInteractive::Result& result) {
182 0 : if (result.button() != changeBtn) {
183 0 : (void)resolve(false);
184 0 : return;
185 : }
186 :
187 0 : if (!result.showAgain()) {
188 0 : config()->setNeedToShowResetSoundFlagsWhenChangePlaybackProfileWarning(false);
189 : }
190 :
191 0 : (void)resolve(true);
192 : });
193 :
194 0 : return muse::async::Promise<bool>::dummy_result();
195 0 : }, muse::async::PromiseType::AsyncByBody);
196 : }
197 :
198 0 : const QString& SoundProfilesModel::currentlySelectedProfile() const
199 : {
200 0 : return m_currentlySelectedProfile;
201 : }
202 :
203 0 : void SoundProfilesModel::setCurrentlySelectedProfile(const QString& newCurrentlySelectedProfile)
204 : {
205 0 : if (m_currentlySelectedProfile == newCurrentlySelectedProfile) {
206 0 : return;
207 : }
208 0 : m_currentlySelectedProfile = newCurrentlySelectedProfile;
209 0 : emit currentlySelectedProfileChanged();
210 : }
|