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 <QCursor>
24 : #include <QGuiApplication>
25 :
26 : #include "preferencesmodel.h"
27 :
28 : #include "translation.h"
29 : #include "ui/view/iconcodes.h"
30 :
31 : #include "muse_framework_config.h"
32 :
33 : using namespace mu::preferences;
34 : using namespace muse::ui;
35 :
36 0 : PreferencesModel::PreferencesModel(QObject* parent)
37 0 : : QAbstractListModel(parent), muse::Contextable(muse::iocCtxForQmlObject(this))
38 : {
39 0 : }
40 :
41 0 : PreferencesModel::~PreferencesModel()
42 : {
43 0 : cancel();
44 0 : }
45 :
46 0 : int PreferencesModel::rowCount(const QModelIndex& parent) const
47 : {
48 0 : return parent.isValid() ? 0 : m_items.size();
49 : }
50 :
51 0 : QVariant PreferencesModel::data(const QModelIndex& index, int role) const
52 : {
53 0 : if (!index.isValid() || index.row() >= m_items.size() || role != ItemRole) {
54 0 : return QVariant();
55 : }
56 :
57 0 : return QVariant::fromValue(qobject_cast<QObject*>(m_items.at(index.row())));
58 : }
59 :
60 0 : QHash<int, QByteArray> PreferencesModel::roleNames() const
61 : {
62 0 : return { { ItemRole, "item" } };
63 0 : }
64 :
65 0 : QString PreferencesModel::currentPageId() const
66 : {
67 0 : return m_currentPageId;
68 : }
69 :
70 0 : void PreferencesModel::load(const QString& currentPageId)
71 : {
72 0 : configuration()->startEditSettings();
73 :
74 0 : beginResetModel();
75 :
76 0 : if (!currentPageId.isEmpty()) {
77 0 : setCurrentPageId(currentPageId);
78 : } else {
79 0 : const QString& lastOpenedPageId = configuration()->preferencesDialogLastOpenedPageId();
80 0 : if (lastOpenedPageId.isEmpty()) {
81 0 : setCurrentPageId("general");
82 : } else {
83 0 : setCurrentPageId(lastOpenedPageId);
84 : }
85 : }
86 :
87 0 : qDeleteAll(m_items);
88 :
89 0 : m_items = {
90 0 : makeItem("general", QT_TRANSLATE_NOOP("preferences", "General"), IconCode::Code::SETTINGS_COG,
91 : "Preferences/GeneralPreferencesPage.qml"),
92 :
93 0 : makeItem("appearance", QT_TRANSLATE_NOOP("preferences", "Appearance"), IconCode::Code::EYE_OPEN,
94 : "Preferences/AppearancePreferencesPage.qml"),
95 :
96 0 : makeItem("canvas", QT_TRANSLATE_NOOP("preferences", "Canvas"), IconCode::Code::NEW_FILE,
97 : "Preferences/CanvasPreferencesPage.qml"),
98 :
99 0 : makeItem("cloud", QT_TRANSLATE_NOOP("preferences", "Save & publish"), IconCode::Code::CLOUD_FILE,
100 : "Preferences/SaveAndPublishPreferencesPage.qml"),
101 :
102 0 : makeItem("note-input", QT_TRANSLATE_NOOP("preferences", "Note input"), IconCode::Code::EDIT,
103 : "Preferences/NoteInputPreferencesPage.qml"),
104 :
105 0 : makeItem("score", QT_TRANSLATE_NOOP("preferences", "Score"), IconCode::Code::SCORE,
106 : "Preferences/ScorePreferencesPage.qml"),
107 :
108 0 : makeItem("audio-midi", QT_TRANSLATE_NOOP("preferences", "Audio & MIDI"), IconCode::Code::AUDIO,
109 : "Preferences/AudioMidiPreferencesPage.qml"),
110 :
111 : #ifdef MUSE_MODULE_MIDIREMOTE
112 0 : makeItem("midi-device-mapping", QT_TRANSLATE_NOOP("preferences", "MIDI mappings"), IconCode::Code::MIDI_INPUT,
113 : "Preferences/MidiDeviceMappingPreferencesPage.qml"),
114 : #endif
115 :
116 0 : makeItem("video", QT_TRANSLATE_NOOP("preferences", "Video"), IconCode::Code::VIDEO,
117 : "Preferences/VideoPreferencesPage.qml"),
118 :
119 0 : makeItem("percussion", QT_TRANSLATE_NOOP("preferences", "Percussion"), IconCode::Code::PERCUSSION,
120 : "Preferences/PercussionPreferencesPage.qml"),
121 :
122 0 : makeItem("import", QT_TRANSLATE_NOOP("preferences", "Import"), IconCode::Code::IMPORT,
123 : "Preferences/ImportPreferencesPage.qml"),
124 :
125 0 : makeItem("shortcuts", QT_TRANSLATE_NOOP("preferences", "Shortcuts"), IconCode::Code::SHORTCUTS,
126 : "Preferences/ShortcutsPreferencesPage.qml"),
127 :
128 0 : makeItem("general-folders", QT_TRANSLATE_NOOP("preferences", "Folders"), IconCode::Code::OPEN_FILE,
129 : "Preferences/FoldersPreferencesPage.qml"),
130 :
131 0 : makeItem("advanced", QT_TRANSLATE_NOOP("preferences", "Advanced"), IconCode::Code::CONFIGURE,
132 : "Preferences/AdvancedPreferencesPage.qml"),
133 :
134 0 : makeItem("braille", QT_TRANSLATE_NOOP("preferences", "Braille"), IconCode::Code::BRAILLE,
135 : "Preferences/BraillePreferencesPage.qml")
136 0 : };
137 :
138 0 : endResetModel();
139 0 : }
140 :
141 0 : void PreferencesModel::askForConfirmationOfPreferencesReset()
142 : {
143 0 : std::string title = muse::trc("appshell", "Are you sure you want to reset preferences?");
144 : std::string question = muse::trc("appshell", "This action will reset all your app preferences and delete all custom shortcuts. "
145 : "It will not delete any of your scores.\n\n"
146 0 : "This action cannot be undone.");
147 :
148 0 : muse::IInteractive::ButtonData cancelBtn = interactive()->buttonData(muse::IInteractive::Button::Cancel);
149 0 : muse::IInteractive::ButtonData resetBtn = interactive()->buttonData(muse::IInteractive::Button::Reset);
150 0 : cancelBtn.accent = true;
151 :
152 0 : interactive()->warning(title, question, { cancelBtn, resetBtn }, cancelBtn.btn,
153 : { muse::IInteractive::Option::WithIcon },
154 0 : muse::trc("appshell", "Reset preferences"))
155 0 : .onResolve(this, [this](const muse::IInteractive::Result& res) {
156 0 : if (res.isButton(muse::IInteractive::Button::Reset)) {
157 0 : emit userAgreesToResetPreferences();
158 : }
159 0 : });
160 0 : }
161 :
162 0 : void PreferencesModel::resetFactorySettings()
163 : {
164 0 : QGuiApplication::setOverrideCursor(Qt::WaitCursor);
165 0 : QGuiApplication::processEvents();
166 :
167 0 : commandShortcutsRegister()->resetShortcuts();
168 : #ifdef MUSE_MODULE_MIDIREMOTE
169 0 : midiRemote()->resetMidiMappings();
170 : #endif
171 :
172 : static constexpr bool KEEP_DEFAULT_SETTINGS = true;
173 0 : configuration()->revertToFactorySettings(KEEP_DEFAULT_SETTINGS);
174 :
175 : // Unreset the "First Launch Completed" setting so the first-time launch wizard does not appear.
176 0 : configuration()->setHasCompletedFirstLaunchSetup(true);
177 :
178 0 : configuration()->startEditSettings();
179 0 : QGuiApplication::restoreOverrideCursor();
180 0 : }
181 :
182 0 : void PreferencesModel::apply()
183 : {
184 0 : configuration()->applySettings();
185 0 : }
186 :
187 0 : void PreferencesModel::cancel()
188 : {
189 0 : configuration()->rollbackSettings();
190 0 : }
191 :
192 0 : void PreferencesModel::selectRow(int row)
193 : {
194 0 : if (row < 0 || row >= m_items.size()) {
195 0 : return;
196 : }
197 :
198 0 : setCurrentPageId(m_items.at(row)->id());
199 : }
200 :
201 0 : QVariantList PreferencesModel::availablePages() const
202 : {
203 0 : QVariantList result;
204 :
205 0 : for (const PreferencePageItem* item : m_items) {
206 0 : QVariantMap page;
207 0 : page["id"] = item->id();
208 0 : page["path"] = item->path();
209 0 : result << page;
210 0 : }
211 :
212 0 : return result;
213 0 : }
214 :
215 0 : void PreferencesModel::setCurrentPageId(QString currentPageId)
216 : {
217 0 : if (m_currentPageId == currentPageId) {
218 0 : return;
219 : }
220 :
221 0 : m_currentPageId = currentPageId;
222 0 : configuration()->setPreferencesDialogLastOpenedPageId(currentPageId);
223 0 : emit currentPageIdChanged(m_currentPageId);
224 : }
225 :
226 0 : PreferencePageItem* PreferencesModel::makeItem(const QString& id, const QString& title, muse::ui::IconCode::Code icon,
227 : const QString& path)
228 : {
229 0 : PreferencePageItem* item = new PreferencePageItem(this);
230 0 : item->setId(id);
231 0 : item->setTitle(title);
232 0 : item->setIcon(icon);
233 0 : item->setPath(path);
234 :
235 0 : return item;
236 : }
|