18
18
/* *
19
19
* @file project_settings.cpp
20
20
*
21
- * This file defines the ProjectSetting class .
21
+ * This file defines the GenericProjectSetting, ProjectSetting and ProjectMultiSetting classes .
22
22
*/
23
23
24
24
#include " project_settings.h"
@@ -46,7 +46,7 @@ GenericProjectSetting::GenericProjectSetting(SettingsTable* table, const QString
46
46
* @param parent The parent window. Can be nullptr, in which case no cleanup is performed for duplicate settings.
47
47
* @return True if the setting is present in the project settings storage, false otherwise.
48
48
*/
49
- bool GenericProjectSetting::isPresent (QWidget* parent)
49
+ bool GenericProjectSetting::isPresent (QWidget* parent) const
50
50
{
51
51
return table->settingIsPresent (this , parent);
52
52
}
@@ -60,7 +60,7 @@ bool GenericProjectSetting::isPresent(QWidget* parent)
60
60
* @param parent The parent window. Can be nullptr, in which case no cleanup is performed for duplicate settings.
61
61
* @return The current value of the setting as a QVariant.
62
62
*/
63
- QVariant GenericProjectSetting::getAsQVariant (QWidget* parent)
63
+ QVariant GenericProjectSetting::getAsQVariant (QWidget* parent) const
64
64
{
65
65
return table->getSetting (this , parent);
66
66
}
@@ -154,3 +154,134 @@ T ProjectSetting<T>::getDefault() const
154
154
{
155
155
return defaultValue.value <T>();
156
156
}
157
+
158
+
159
+
160
+
161
+
162
+ /* *
163
+ * Creates a new ProjectMultiSetting with the given base key and default value.
164
+ *
165
+ * @param baseKey The common part of the keys under which the settings will be stored.
166
+ * @param defaultValue The default value for all the settings.
167
+ */
168
+ template <typename T>
169
+ ProjectMultiSetting<T>::ProjectMultiSetting(SettingsTable* table, const QString baseKey, QVariant defaultValue) :
170
+ settings (QMap<QString, ProjectSetting<T>*>()),
171
+ table(table),
172
+ baseKey(baseKey),
173
+ defaultValue(defaultValue)
174
+ {}
175
+
176
+
177
+ /* *
178
+ * Checks whether any of the settings are present in the project settings storage.
179
+ *
180
+ * @return True if any settings are stored in the settings file under the baseKey, false otherwise.
181
+ */
182
+ template <typename T>
183
+ bool ProjectMultiSetting<T>::anyPresent() const
184
+ {
185
+ for (const ProjectSetting<T>* const setting : settings) {
186
+ if (setting->isPresent ()) return true ;
187
+ }
188
+ return false ;
189
+ }
190
+
191
+ /* *
192
+ * Checks whether none of the settings are present in the project settings storage.
193
+ *
194
+ * @return True if no settings are stored in the settings file under the baseKey, false otherwise.
195
+ */
196
+ template <typename T>
197
+ bool ProjectMultiSetting<T>::nonePresent() const
198
+ {
199
+ return !anyPresent ();
200
+ }
201
+
202
+ /* *
203
+ * Checks whether al of the the setting are present in the project settings storage.
204
+ *
205
+ * @param subKeys The sub-keys of all settings to check.
206
+ * @return True if all settings given by their sub-keys are stored in the settings file under the baseKey, false otherwise.
207
+ */
208
+ template <typename T>
209
+ bool ProjectMultiSetting<T>::allPresent(QSet<QString> subKeys)
210
+ {
211
+ for (const QString& subKey : subKeys) {
212
+ createSettingIfMissing (subKey);
213
+ if (!settings[subKey]->isPresent ()) return false ;
214
+ }
215
+ return true ;
216
+ }
217
+
218
+ /* *
219
+ * Returns the values of the settings after validating them, paired with their sub-keys.
220
+ *
221
+ * If the setting is not present in the settings file or invalid, it is discarded and the
222
+ * default value is returned and written back to the settings file.
223
+ *
224
+ * @return The value of the settings as they are stored in the settings file after validation.
225
+ */
226
+ template <typename T>
227
+ QMap<QString, T> ProjectMultiSetting<T>::get(const QSet<QString>& subKeys)
228
+ {
229
+ QMap<QString, T> values = QMap<QString, T>();
230
+ for (const QString& subKey : subKeys) {
231
+ createSettingIfMissing (subKey);
232
+ values[subKey] = settings.value (subKey)->get ();
233
+ }
234
+ return values;
235
+ }
236
+
237
+ /* *
238
+ * Returns the default value of the settings.
239
+ *
240
+ * @return The default value of the settings.
241
+ */
242
+ template <typename T>
243
+ T ProjectMultiSetting<T>::getDefault() const
244
+ {
245
+ return defaultValue.value <T>();
246
+ }
247
+
248
+
249
+ /* *
250
+ * Sets the value of the setting.
251
+ *
252
+ * @param values The new values for the settings.
253
+ */
254
+ template <typename T>
255
+ void ProjectMultiSetting<T>::set(QWidget* parent, const QMap<QString, T>& subKeyValueMap)
256
+ {
257
+ for (auto iter = subKeyValueMap.cbegin (); iter != subKeyValueMap.cend (); iter++) {
258
+ const QString& subKey = iter.key ();
259
+ const T& value = iter.value ();
260
+
261
+ createSettingIfMissing (subKey);
262
+ settings[subKey]->set (parent, value);
263
+ }
264
+ }
265
+
266
+ /* *
267
+ * Removes all of the settings from the project settings storage.
268
+ */
269
+ template <typename T>
270
+ void ProjectMultiSetting<T>::clear(QWidget* parent, SettingsTable* settingsTable) const
271
+ {
272
+ settingsTable->clearAllSettings (parent, baseKey);
273
+ }
274
+
275
+
276
+ /* *
277
+ * Creates a new Setting for the given sub-key and adds it to the setttings map.
278
+ *
279
+ * @param subKey The sub-key for the missing setting
280
+ */
281
+ template <typename T>
282
+ void ProjectMultiSetting<T>::createSettingIfMissing(const QString& subKey)
283
+ {
284
+ if (!settings.contains (subKey)) {
285
+ settings.insert (subKey, new ProjectSetting<T>(table, baseKey + " /" + subKey, defaultValue));
286
+ }
287
+ }
0 commit comments