-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathisettings.h
230 lines (190 loc) · 7.49 KB
/
isettings.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright (C) 2018-2025 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#ifndef ISETTINGS_H
#define ISETTINGS_H
#include "qaservice.h"
#include "quasarapp_global.h"
#include <QObject>
#include <QVariant>
class QSettings;
namespace QuasarAppUtils {
/**
* @brief The SettingsSaveMode enum
*/
enum class SettingsSaveMode: quint64 {
/// a settings will be saved on hard disk when called the Settings::setValue method.
Auto,
/// a settings will be saved on hard disk when called the Settings::Sync method.
Manual
};
/**
* @brief The Settings class base interface for implementation settings backends.
* Available implementations:
* Setting (based on QSettings backend)
* @note This is singleton object.
*
* @note The all child classes should be initialized before used.
*
* @code{cpp}
* auto settingsInstance = Setting::initService<Setting>();
* @endcode
*
* @see ISettings::init method.
*
*/
class QUASARAPPSHARED_EXPORT ISettings : public QObject, public Service<ISettings>
{
Q_OBJECT
public:
~ISettings() override;
/**
* @brief getValue This method return the value of the settings.
* @param key This is name of the required settings value.
* @param def This is default value if a value is not finded. If this params will be skipped, then The Settngs model try find default value in the defaultSettings map.
* @return value of a @a key
* @see ISettings::defaultSettings
*/
Q_INVOKABLE QVariant getValue(const QString &key, const QVariant& def = {});
/**
* @brief getStrValue some as getValue but convert result object to QString type.
* @param key This is name of the required settings value.
* @param def This is default value if a value is not finded. If this params will be skipped, then The Settngs model try find default value in the defaultSettings map.
* @return value of a @a key
* @see ISettings::defaultSettings
* @warning If you set a def arguments to empty string then this method will return default value from the defaultSettings map.
*/
Q_INVOKABLE QString getStrValue(const QString &key, const QString& def = {});
/**
* @brief resetToDefault This method reset all settings to default values.
*/
Q_INVOKABLE void resetToDefault();
/**
* @brief ignoreToRest This method should be returns true if the @a key setting is not be reset on the resetToDefault method.
* @param key This is keuy value.
* @return true if the @a key option can't be reset to default.
* The default implementaion alwayes return false.
*/
virtual bool ignoreToRest(const QString& key) const;
/**
* @brief sync This method save all setings data on a hard disk;
*/
void sync();
/**
* @brief forceReloadCache This method force reload settings data from disk.
* @note Cache will be refreshed
*/
void forceReloadCache();
/**
* @brief getMode This method return the current mode of the settings.
* @return the current mode of the settings.
*/
SettingsSaveMode getMode() const;
/**
* @brief setMode This method sets a new value of the settings mode.
* @param mode This is a new value of the settings mode.
*/
void setMode(const SettingsSaveMode &mode);
/**
* @brief instance This method returns pointer to current settings object.
* @return pointer to current settings object.
* @see Service::instance
*/
static ISettings* instance();
/**
* @brief initService This method initialize the global settings object.
* @param obj This is prepared settings object. You should create a your object monyaly, and add to initialization
* @code{cpp}
* bool result = initService(std::make_unique<MySettings>());
* @endcode
* @return true if initialization finished successful else false.
* @see Service::initService
*/
static bool initService(std::unique_ptr<ISettings> obj);
public slots:
/**
* @brief setValue This slot sets new value for a @a key setting
* @param key This is name of the changed setting.
* @param value This is a new value of the setting
*/
void setValue(const QString &key, const QVariant& value);
/**
* @brief setStrValue This is some as setValue but working with the QString type.
* @param key This is name of the changed setting.
* @param value This is a new value of the setting
*/
void setStrValue(const QString& key, const QString& value);
signals:
/**
* @brief valueChanged This signal when value of the @a key settings changed
* @param key This is name of change setting.
* @param value This is a new value of @a key.
*/
void valueChanged(QString key, QVariant value);
/**
* @brief valueStrChanged some as valueChanged(QString key, QVariant value) but value has ben converted to the QString type.
* @param key This is name of change setting.
* @param value This is a new value of @a key.
*/
void valueStrChanged(QString key, QString value);
protected:
explicit ISettings(SettingsSaveMode mode = SettingsSaveMode::Auto);
/**
* @brief defaultSettings This method must be return default map of the settings and them values. If the default value argument in a getValue method will be skipped, then settings model try find a default value in this map.
* @return The default settings map.
* @see ISettings::getValue
* @example example of implementation of this method:
*
* @code{cpp}
*
QHash<QString, QVariant> SettingsModel::defaultSettings() {
QHash<QString, QVariant> settings;
settings["colorTheme"] = "#ff6b01";
settings["shareName"] = true;
settings["devSettingEnable"] = false;
settings["host"] = "";
settings["APIVersion"] = 2;
return settings;
}
* @endcode
*/
virtual QHash<QString, QVariant> defaultSettings() = 0;
/**
* @brief syncImplementation This method should save all configuration data to the hard drive;
*/
virtual void syncImplementation() = 0;
/**
* @brief getValueImplementation This method will return the value of the settings.
* @param key This is name of the required settings value.
* @param def This is default value if a value is not finded.
* @return value of a @a key
*/
virtual QVariant getValueImplementation(const QString &key, const QVariant& def) = 0;
/**
* @brief setValueImplementation This slot will set a new value for the @a key parameter.
* @param key This is name of the changed setting.
* @param value This is a new value of the setting
*/
virtual void setValueImplementation(const QString key, const QVariant& value) = 0;
/**
* @brief clearCache This method clear all data from cache.
*/
void clearCache();
/**
* @brief settingsMap This method returns initialized settings map.
* Settings map contains pairs with settings key and default value.
* @return initialized settings map.
* @see ISettings::defaultSettings method
*/
QHash<QString, QVariant>& settingsMap();
private:
SettingsSaveMode _mode = SettingsSaveMode::Auto;
QHash<QString, QVariant> _cache;
QHash<QString, QVariant> *_defaultConfig = nullptr;
friend class Service<ISettings>;
};
};
#endif // ISETTINGS_H