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 "advancedpreferencesmodel.h"
23 :
24 : #include "settings.h"
25 :
26 : using namespace muse;
27 : using namespace mu;
28 : using namespace mu::preferences;
29 :
30 0 : AdvancedPreferencesModel::AdvancedPreferencesModel(QObject* parent)
31 0 : : QAbstractListModel(parent)
32 : {
33 0 : }
34 :
35 0 : QVariant AdvancedPreferencesModel::data(const QModelIndex& index, int role) const
36 : {
37 0 : if (!index.isValid() || index.row() >= rowCount()) {
38 0 : return QVariant();
39 : }
40 :
41 0 : const Settings::Item& item = m_items.at(index.row());
42 0 : switch (role) {
43 0 : case KeyRole: return QString::fromStdString(item.key.key);
44 0 : case DescriptionRole: return QString::fromStdString(item.description);
45 0 : case TypeRole: return typeToString(item.value.type());
46 0 : case ValueRole: return item.value.toQVariant();
47 0 : case MinValueRole: return !item.minValue.isNull() ? item.minValue.toQVariant() : -1000;
48 0 : case MaxValueRole: return !item.maxValue.isNull() ? item.maxValue.toQVariant() : 1000;
49 : }
50 0 : return QVariant();
51 : }
52 :
53 0 : bool AdvancedPreferencesModel::setData(const QModelIndex& index, const QVariant& value, int role)
54 : {
55 0 : if (!index.isValid() || index.row() >= rowCount()) {
56 0 : return false;
57 : }
58 :
59 0 : switch (role) {
60 0 : case ValueRole:
61 0 : changeVal(index.row(), Val::fromQVariant(value));
62 0 : emit dataChanged(index, index, { ValueRole });
63 0 : return true;
64 0 : default:
65 0 : return false;
66 : }
67 : }
68 :
69 0 : int AdvancedPreferencesModel::rowCount(const QModelIndex&) const
70 : {
71 0 : return m_items.count();
72 : }
73 :
74 0 : QHash<int, QByteArray> AdvancedPreferencesModel::roleNames() const
75 : {
76 : static const QHash<int, QByteArray> roles = {
77 0 : { KeyRole, "keyRole" },
78 0 : { DescriptionRole, "descriptionRole" },
79 0 : { TypeRole, "typeRole" },
80 0 : { ValueRole, "valueRole" },
81 0 : { MinValueRole, "minValueRole" },
82 0 : { MaxValueRole, "maxValueRole" }
83 0 : };
84 :
85 0 : return roles;
86 0 : }
87 :
88 0 : void AdvancedPreferencesModel::load()
89 : {
90 0 : beginResetModel();
91 :
92 0 : m_items.clear();
93 :
94 0 : Settings::Items items = settings()->items();
95 :
96 0 : for (auto it = items.cbegin(); it != items.cend(); ++it) {
97 0 : if (it->second.canBeManuallyEdited) {
98 0 : m_items << it->second;
99 :
100 0 : muse::Settings::Key key = it->second.key;
101 0 : settings()->valueChanged(key).onReceive(this, [this, key](const Val& val) {
102 0 : QModelIndex index = findIndex(key);
103 0 : if (!index.isValid() || m_items[index.row()].value == val) {
104 0 : return;
105 : }
106 :
107 0 : changeModelVal(m_items[index.row()], val);
108 0 : emit dataChanged(index, index, { ValueRole });
109 : });
110 0 : }
111 : }
112 :
113 0 : endResetModel();
114 0 : }
115 :
116 0 : QModelIndex AdvancedPreferencesModel::findIndex(const muse::Settings::Key& key)
117 : {
118 0 : for (int i = 0; i < m_items.size(); ++i) {
119 0 : if (m_items[i].key == key) {
120 0 : return index(i, 0);
121 : }
122 : }
123 0 : return QModelIndex();
124 : }
125 :
126 0 : void AdvancedPreferencesModel::changeVal(int index, const Val& newVal)
127 : {
128 0 : Settings::Item& item = m_items[index];
129 0 : changeModelVal(item, newVal);
130 0 : settings()->setSharedValue(item.key, item.value);
131 0 : }
132 :
133 0 : void AdvancedPreferencesModel::changeModelVal(Settings::Item& item, const Val& newVal)
134 : {
135 0 : if (item.value == newVal) {
136 0 : return;
137 : }
138 0 : Val::Type type = item.value.type();
139 0 : item.value = newVal;
140 0 : item.value.setType(type);
141 : }
142 :
143 0 : void AdvancedPreferencesModel::resetToDefault()
144 : {
145 0 : beginResetModel();
146 :
147 0 : for (int i = 0; i < m_items.size(); ++i) {
148 0 : changeVal(i, m_items[i].defaultValue);
149 : }
150 :
151 0 : endResetModel();
152 0 : }
153 :
154 0 : QString AdvancedPreferencesModel::typeToString(Val::Type type) const
155 : {
156 0 : switch (type) {
157 0 : case Val::Type::Undefined: return "Undefined";
158 0 : case Val::Type::Bool: return "Bool";
159 0 : case Val::Type::Int: return "Int";
160 0 : case Val::Type::Int64: return "Int";
161 0 : case Val::Type::Double: return "Double";
162 0 : case Val::Type::String: return "String";
163 0 : case Val::Type::Color: return "Color";
164 0 : case Val::Type::List: return "List";
165 0 : case Val::Type::Map: return "Map";
166 : }
167 0 : return "Undefined";
168 : }
|