MuseScore  3.4
Music composition and notation
preferences.h
Go to the documentation of this file.
1 //=============================================================================
2 // MusE Score
3 // Linux Music Score Editor
4 //
5 // Copyright (C) 2002-2016 Werner Schweer and others
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //=============================================================================
19 
20 #ifndef __PREFERENCES_H__
21 #define __PREFERENCES_H__
22 
23 /*
24  * HOW TO ADD A NEW PREFERENCE
25  * - Add a new define to the list of defines below
26  * - Add the preference to the _allPreferences map in the init() function in preferences.cpp
27  * and specify the default value for this preference
28  * - That's it. The preference is stored and retrieved automatically and can be read
29  * using getString(), getInt(), etc., and changed using setPreference()
30  */
31 
32 #include "globals.h"
33 #include "global/settings/types/preferencekeys.h"
34 
35 namespace Ms {
36 
37 extern QString mscoreGlobalShare;
38 
39 enum class SessionStart : char {
40  EMPTY, LAST, NEW, SCORE
41  };
42 
43 // midi remote control values:
44 enum {
64  };
65 
66 enum class MuseScoreStyleType : char {
67  DARK_FUSION = 0,
69  };
70 
71 // MusicXML export break values
72 enum class MusicxmlExportBreaks : char {
73  ALL, MANUAL, NO
74  };
75 
76 class PreferenceVisitor;
77 
78 //---------------------------------------------------------
79 // Preference
80 //---------------------------------------------------------
81 class Preference {
82  private:
83  QVariant _defaultValue = 0;
84  bool _showInAdvancedList = true;
85 
86  protected:
87  QMetaType::Type _type = QMetaType::UnknownType;
88  Preference(QVariant defaultValue) : _defaultValue(defaultValue) {}
89 
90  public:
91  Preference(QVariant defaultValue, QMetaType::Type type, bool showInAdvancedList = true);
92  virtual ~Preference() {}
93 
94  QVariant defaultValue() const {return _defaultValue;}
95  bool showInAdvancedList() const {return _showInAdvancedList;}
96  QMetaType::Type type() {return _type;}
97  virtual void accept(QString key, PreferenceVisitor&) = 0;
98  };
99 
100 class IntPreference : public Preference {
101  public:
102  IntPreference(int defaultValue, bool showInAdvancedList = true);
103  virtual void accept(QString key, PreferenceVisitor&);
104  };
105 
106 class DoublePreference : public Preference {
107  public:
108  DoublePreference(double defaultValue, bool showInAdvancedList = true);
109  virtual void accept(QString key, PreferenceVisitor&);
110  };
111 
112 class BoolPreference : public Preference {
113  public:
114  BoolPreference(bool defaultValue, bool showInAdvancedList = true);
115  virtual void accept(QString key, PreferenceVisitor&);
116  };
117 
119  public:
120  StringPreference(QString defaultValue, bool showInAdvancedList = true);
121  virtual void accept(QString key, PreferenceVisitor&);
122  };
123 
125  public:
126  ColorPreference(QColor defaultValue, bool showInAdvancedList = true);
127  virtual void accept(QString key, PreferenceVisitor&);
128  };
129 
130 // Support for EnumPreference is currently not fully implemented
131 class EnumPreference: public Preference {
132  public:
133  EnumPreference(QVariant defaultValue, bool showInAdvancedList = true);
134  virtual void accept(QString, PreferenceVisitor&);
135  };
136 
137 //---------------------------------------------------------
138 // Preferences
139 //---------------------------------------------------------
140 
141 class Preferences {
142  public:
143  typedef QHash<QString, Preference*> prefs_map_t;
144 
145  private:
146 
147  // Map of all preferences and their default values
148  // A preference can not be read or set if it is not present in this map
149  // This map is not used for storing a preference it is only for default values
150  prefs_map_t _allPreferences;
151  // used for storing preferences in memory when _storeInMemoryOnly is true
152  // and for storing temporary preferences
153  QHash<QString, QVariant> _inMemorySettings;
154  bool _storeInMemoryOnly = false;
155  bool _returnDefaultValues = false;
156  bool _initialized = false;
157  QSettings* _settings; // should not be used directly but through settings() accessor
158 
159  QSettings* settings() const;
160  // the following functions must be used to access and change a preference
161  // instead of using QSettings directly
162  QVariant get(const QString key) const;
163  bool has(const QString key) const;
164  void set(const QString key, QVariant value, bool temporary = false);
165  void remove(const QString key);
166 
167  QVariant preference(const QString key) const;
168  QMetaType::Type type(const QString key) const;
169  bool checkIfKeyExists(const QString key) const;
170  bool checkType(const QString key, QMetaType::Type t) const;
171 
172  // Used with workspace
173  QMap<QString, QVariant> localPreferences;
174  QMap<QString, QVariant> getDefaultLocalPreferences();
175  bool useLocalPrefs = false;
176 
177  public:
178  Preferences();
179  ~Preferences();
180  void init(bool storeInMemoryOnly = false);
181  void save();
182  // set to true to let getters return default values instead of values from QSettings
183  void setReturnDefaultValuesMode(bool returnDefaultValues) {_returnDefaultValues = returnDefaultValues;}
184 
185  const prefs_map_t& allPreferences() const {return _allPreferences;}
186 
187  // general getters
188  QVariant defaultValue(const QString key) const;
189  bool getBool(const QString key) const;
190  QColor getColor(const QString key) const;
191  QString getString(const QString key) const;
192  int getInt(const QString key) const;
193  double getDouble(const QString key) const;
194 
195  // general setters
196  void setToDefaultValue(const QString key);
197  void setPreference(const QString key, QVariant value);
198 
199  // A temporary preference is stored "in memory" only and not written to file.
200  // If there is both a "normal" preference and a temporary preference with the same
201  // key the temporary preference is used
202  void setTemporaryPreference(const QString key, QVariant value);
203 
204  /*
205  * Some preferences like enums and structs/classes are not easily read using the general set/get methods
206  * and therefore require specific getters and/or setters
207  */
208  SessionStart sessionStart() const;
209  MusicxmlExportBreaks musicxmlExportBreaks() const;
210  MuseScoreStyleType globalStyle() const;
211  bool isThemeDark() const;
212 
213  template<typename T>
214  void setCustomPreference(const QString key, T t)
215  {
216  set(key, QVariant::fromValue<T>(t));
217  }
218 
219  // The midiRemote preference requires special handling due to its complexity
220  MidiRemote midiRemote(int recordId) const;
221  void updateMidiRemote(int recordId, MidiRemoteType type, int data);
222  void clearMidiRemote(int recordId);
223 
224  QMap<QString, QVariant> getLocalPreferences() { return localPreferences; }
225  void setLocalPreference(QString key, QVariant value);
226  void setUseLocalPreferences(bool value) { useLocalPrefs = value; }
227  bool getUseLocalPreferences() { return useLocalPrefs; }
228  void updateLocalPreferences() { localPreferences = getDefaultLocalPreferences(); }
229  };
230 
231 // singleton
232 extern Preferences preferences;
233 
234 // Stream operators for enum classes
235 // enum classes don't play well with QSettings without custom serialization
236 inline QDataStream&
237 operator<<(QDataStream &out, const Ms::MuseScoreStyleType &val)
238 {
239  return out << static_cast<int>(val);
240 }
241 
242 inline QDataStream&
243 operator>>(QDataStream &in, Ms::MuseScoreStyleType &val)
244 {
245  int tmp;
246  in >> tmp;
247  val = static_cast<Ms::MuseScoreStyleType>(tmp);
248  return in;
249 }
250 
251 inline QDataStream&
252 operator<<(QDataStream &out, const Ms::SessionStart &val)
253 {
254  return out << static_cast<int>(val);
255 }
256 
257 inline QDataStream&
258 operator>>(QDataStream &in, Ms::SessionStart &val)
259 {
260  int tmp;
261  in >> tmp;
262  val = static_cast<Ms::SessionStart>(tmp);
263  return in;
264 }
265 
266 inline QDataStream&
267 operator<<(QDataStream &out, const Ms::MusicxmlExportBreaks &val)
268 {
269  return out << static_cast<int>(val);
270 }
271 
272 inline QDataStream&
273 operator>>(QDataStream &in, Ms::MusicxmlExportBreaks &val)
274 {
275  int tmp;
276  in >> tmp;
277  val = static_cast<Ms::MusicxmlExportBreaks>(tmp);
278  return in;
279 }
280 
281 
283  public:
284  virtual void visit(QString key, IntPreference*) = 0;
285  virtual void visit(QString key, DoublePreference*) = 0;
286  virtual void visit(QString key, BoolPreference*) = 0;
287  virtual void visit(QString key, StringPreference*) = 0;
288  virtual void visit(QString key, ColorPreference*) = 0;
289  };
290 
291 
292 } // namespace Ms
293 
294 Q_DECLARE_METATYPE(Ms::SessionStart);
295 Q_DECLARE_METATYPE(Ms::MusicxmlExportBreaks);
296 Q_DECLARE_METATYPE(Ms::MuseScoreStyleType);
297 
298 #endif
QMetaType::Type type()
Definition: preferences.h:96
Definition: preferences.h:58
Definition: preferences.h:56
Definition: preferences.h:59
Definition: preferences.h:53
Definition: preferences.h:50
prefs_map_t _allPreferences
Definition: preferences.h:150
Definition: preferences.h:62
Definition: preferences.h:118
Definition: preferences.h:106
const prefs_map_t & allPreferences() const
Definition: preferences.h:185
int getInt(int byte, int bits)
Definition: ove.cpp:5182
QMap< QString, QVariant > localPreferences
Definition: preferences.h:173
Definition: preferences.h:63
Definition: preferences.h:100
QSettings * _settings
Definition: preferences.h:157
Definition: preferences.h:131
void updateLocalPreferences()
Definition: preferences.h:228
void setReturnDefaultValuesMode(bool returnDefaultValues)
Definition: preferences.h:183
MidiRemoteType
Definition: globals.h:91
Definition: preferences.h:61
void setUseLocalPreferences(bool value)
Definition: preferences.h:226
Definition: preferences.h:124
Definition: preferences.h:55
QDataStream & operator>>(QDataStream &in, Ms::MuseScoreStyleType &val)
Definition: preferences.h:243
virtual ~Preference()
Definition: preferences.h:92
Definition: preferences.h:51
Definition: preferences.h:49
void setCustomPreference(const QString key, T t)
Definition: preferences.h:214
Definition: globals.h:100
QDataStream & operator<<(QDataStream &out, const Ms::MuseScoreStyleType &val)
Definition: preferences.h:237
Definition: preferences.h:46
Definition: preferences.h:52
MuseScoreStyleType
Definition: preferences.h:66
Definition: aeolus.cpp:26
Definition: preferences.h:48
bool getUseLocalPreferences()
Definition: preferences.h:227
QString mscoreGlobalShare
Definition: mscore.cpp:124
Definition: preferences.h:112
Definition: preferences.h:45
QHash< QString, QVariant > _inMemorySettings
Definition: preferences.h:153
bool showInAdvancedList() const
Definition: preferences.h:95
Definition: preferences.h:282
Definition: preferences.h:54
Preferences preferences
Definition: inspectorplugin.cpp:31
MusicxmlExportBreaks
Definition: preferences.h:72
SessionStart
Definition: preferences.h:39
Definition: preferences.h:47
Definition: inspectorplugin.h:25
Definition: preferences.h:81
QVariant defaultValue() const
Definition: preferences.h:94
QMap< QString, QVariant > getLocalPreferences()
Definition: preferences.h:224
QHash< QString, Preference * > prefs_map_t
Definition: preferences.h:143
Preference(QVariant defaultValue)
Definition: preferences.h:88
Definition: preferences.h:60
Definition: preferences.h:57