Skip to content

Commit c760d67

Browse files
committed
Add class for budnling project settings
Closes #168
1 parent 0d9aacf commit c760d67

File tree

5 files changed

+195
-10
lines changed

5 files changed

+195
-10
lines changed

src/db/tables/settings_table.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ void SettingsTable::removeSetting(QWidget* parent, const GenericProjectSetting*
137137
}
138138

139139

140+
/**
141+
* Removes the value from the setting in the project settings table.
142+
*
143+
* @param parent The parent window. Cannot be nullptr.
144+
* @param setting The setting to remove.
145+
*/
146+
void SettingsTable::clearAllSettings(QWidget* parent, const QString& baseKey)
147+
{
148+
for (BufferRowIndex rowIndex = BufferRowIndex(buffer.numRows()); rowIndex.isValid(); rowIndex--) {
149+
QString key = settingKeyColumn->getValueAt(rowIndex).toString();
150+
if (key.startsWith(baseKey)) {
151+
ValidItemID settingID = VALID_ITEM_ID(primaryKeyColumn->getValueAt(rowIndex));
152+
removeMatchingRows(parent, primaryKeyColumn, settingID);
153+
}
154+
}
155+
}
156+
157+
140158

141159
/**
142160
* Finds the projectSettingID (primary key) of the given setting in the database.

src/db/tables/settings_table.h

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class SettingsTable : public Table {
5353
void setSetting(QWidget* parent, const GenericProjectSetting* setting, QVariant value);
5454
void clearSetting(QWidget* parent, const GenericProjectSetting* setting);
5555
void removeSetting(QWidget* parent, const GenericProjectSetting* setting);
56+
void clearAllSettings(QWidget* parent, const QString& baseKey);
5657

5758
private:
5859
ItemID findSettingID(const GenericProjectSetting* setting, QWidget* parent = nullptr);

src/settings/project_settings.cpp

+134-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* @file project_settings.cpp
2020
*
21-
* This file defines the ProjectSetting class.
21+
* This file defines the GenericProjectSetting, ProjectSetting and ProjectMultiSetting classes.
2222
*/
2323

2424
#include "project_settings.h"
@@ -46,7 +46,7 @@ GenericProjectSetting::GenericProjectSetting(SettingsTable* table, const QString
4646
* @param parent The parent window. Can be nullptr, in which case no cleanup is performed for duplicate settings.
4747
* @return True if the setting is present in the project settings storage, false otherwise.
4848
*/
49-
bool GenericProjectSetting::isPresent(QWidget* parent)
49+
bool GenericProjectSetting::isPresent(QWidget* parent) const
5050
{
5151
return table->settingIsPresent(this, parent);
5252
}
@@ -60,7 +60,7 @@ bool GenericProjectSetting::isPresent(QWidget* parent)
6060
* @param parent The parent window. Can be nullptr, in which case no cleanup is performed for duplicate settings.
6161
* @return The current value of the setting as a QVariant.
6262
*/
63-
QVariant GenericProjectSetting::getAsQVariant(QWidget* parent)
63+
QVariant GenericProjectSetting::getAsQVariant(QWidget* parent) const
6464
{
6565
return table->getSetting(this, parent);
6666
}
@@ -154,3 +154,134 @@ T ProjectSetting<T>::getDefault() const
154154
{
155155
return defaultValue.value<T>();
156156
}
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+
}

src/settings/project_settings.h

+36-6
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
/**
1919
* @file project_settings.h
2020
*
21-
* This file declares the GenericProjectSetting and ProjectSetting classes and defines the
22-
* ProjectSettings class.
21+
* This file declares the GenericProjectSetting, ProjectSetting and ProjectMultiSetting classes
22+
* and defines the ProjectSettings class.
2323
*/
2424

2525
#ifndef PROJECT_SETTINGS_H
@@ -31,8 +31,6 @@
3131
#include <QVariant>
3232
#include <QDate>
3333

34-
class ProjectSettings;
35-
3634

3735

3836
/**
@@ -51,8 +49,8 @@ class GenericProjectSetting {
5149

5250
GenericProjectSetting(SettingsTable* table, const QString& key, QVariant defaultValue);
5351

54-
bool isPresent(QWidget* parent = nullptr);
55-
QVariant getAsQVariant(QWidget* parent = nullptr);
52+
bool isPresent(QWidget* parent = nullptr) const;
53+
QVariant getAsQVariant(QWidget* parent = nullptr) const;
5654
QVariant getDefaultAsQVariant() const;
5755

5856
void set(QWidget* parent, QVariant value);
@@ -82,6 +80,38 @@ template class ProjectSetting<QDate>;
8280

8381

8482

83+
template<typename T>
84+
class ProjectMultiSetting
85+
{
86+
/** Dynamically grown list of all settings in this group, mapped to their sub-keys. */
87+
QMap<QString, ProjectSetting<T>*> settings;
88+
89+
/** The project settings table. */
90+
SettingsTable* const table;
91+
/** The part of the key which all settings share. */
92+
const QString baseKey;
93+
/** The default value of all the settings. */
94+
const QVariant defaultValue;
95+
96+
public:
97+
ProjectMultiSetting(SettingsTable* table, const QString baseKey, QVariant defaultValue = T());
98+
99+
bool anyPresent() const;
100+
bool nonePresent() const;
101+
bool allPresent(QSet<QString> subKeys);
102+
103+
QMap<QString, T> get(const QSet<QString>& subKeys);
104+
T getDefault() const;
105+
106+
void set(QWidget* parent, const QMap<QString, T>& subKeyValueMap);
107+
void clear(QWidget* parent, SettingsTable* settingsTable) const;
108+
109+
private:
110+
void createSettingIfMissing(const QString& subKey);
111+
};
112+
113+
114+
85115
/**
86116
* A class for managing all project settings.
87117
*/

src/settings/settings.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class MultiSetting
151151

152152
public:
153153
/**
154-
* Creates a new setting with the given key and default value.
154+
* Creates a new MultiSetting with the given base key and default value.
155155
*
156156
* @param baseKey The common part of the keys under which the settings will be stored.
157157
* @param defaultValue The default value for all the settings.
@@ -260,6 +260,11 @@ class MultiSetting
260260

261261

262262
private:
263+
/**
264+
* Creates a new Setting for the given sub-key and adds it to the setttings map.
265+
*
266+
* @param subKey The sub-key for the missing setting
267+
*/
263268
inline void createSettingIfMissing(const QString& subKey)
264269
{
265270
if (!settings.contains(subKey)) {

0 commit comments

Comments
 (0)