QuasarAppLib
isettings.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2025 QuasarApp.
3 * Distributed under the lgplv3 software license, see the accompanying
4 * Everyone is permitted to copy and distribute verbatim copies
5 * of this license document, but changing it is not allowed.
6*/
7
8#include "isettings.h"
9#include <QSettings>
10#include <QCoreApplication>
11#include "qaglobalutils.h"
12
13namespace QuasarAppUtils {
14
16 _mode = mode;
17}
18
20 if (_defaultConfig)
21 delete _defaultConfig;
22}
23
25 _cache.clear();
26}
27
28QHash<QString, QVariant> &ISettings::settingsMap() {
29 if (!_defaultConfig)
30 _defaultConfig = new QHash<QString, QVariant>(defaultSettings());
31
32 return *_defaultConfig;
33}
34
36 return _mode;
37}
38
40 _mode = mode;
41}
42
46
47bool ISettings::initService(std::unique_ptr<ISettings> obj) {
48 return Service<ISettings>::initService(std::move(obj));
49}
50
51QVariant ISettings::getValue(const QString &key, const QVariant &def) {
52 debug_assert(key.size(), "You can't use the empty key value!");
53
54 if (!_cache.contains(key)) {
55
56 QVariant defVal = def;
57 if (defVal.isNull()) {
58 defVal = settingsMap().value(key);
59 }
60
61 _cache[key] = getValueImplementation(key, def);
62 }
63
64 return _cache[key];
65}
66
67QString ISettings::getStrValue(const QString &key, const QString &def) {
68 if (def.isEmpty()) {
69 return getValue(key).toString();
70
71 }
72
73 return getValue(key, QVariant(def)).toString();
74}
75
77 auto &defaultConfig = settingsMap();
78
79 for (auto it = defaultConfig.begin(); it != defaultConfig.end(); ++it) {
80 if (!ignoreToRest(it.key()))
81 setValue(it.key(), defaultConfig.value(it.key()));
82 }
83}
84
85bool ISettings::ignoreToRest(const QString &) const {
86 return false;
87}
88
90 for (auto it = _cache.begin(); it != _cache.end(); ++it) {
91 setValueImplementation(it.key(), it.value());
92 }
93
94 return syncImplementation();
95}
96
98 auto &defaultConfig = settingsMap();
99
100 for (auto it = defaultConfig.begin(); it != defaultConfig.end(); ++it) {
101 setValue(it.key(), getValueImplementation(it.key(), it.value()));
102 }
103}
104
105void ISettings::setValue(const QString &key, const QVariant &value) {
106
107 debug_assert(key.size(), "You can't use the empty key value!");
108
109 if (_cache.contains(key) && _cache.value(key) == value) {
110 return;
111 }
112
113 _cache[key] = value;
114
115 emit valueChanged(key, value);
116 emit valueStrChanged(key, value.toString());
117
118 if (_mode == SettingsSaveMode::Auto) {
119 setValueImplementation(key, value);
120 }
121
122}
123
124void ISettings::setStrValue(const QString &key, const QString &value) {
125 setValue(key, value);
126}
127
128}
The Settings class base interface for implementation settings backends. Available implementations: Se...
Definition isettings.h:46
virtual bool ignoreToRest(const QString &key) const
ignoreToRest This method should be returns true if the key setting is not be reset on the resetToDefa...
Definition isettings.cpp:85
void sync()
sync This method save all setings data on a hard disk;
Definition isettings.cpp:89
void valueStrChanged(QString key, QString value)
valueStrChanged some as valueChanged(QString key, QVariant value) but value has ben converted to the ...
QHash< QString, QVariant > & settingsMap()
settingsMap This method returns initialized settings map. Settings map contains pairs with settings k...
Definition isettings.cpp:28
static ISettings * instance()
instance This method returns pointer to current settings object.
Definition isettings.cpp:43
virtual void syncImplementation()=0
syncImplementation This method should save all configuration data to the hard drive;
void setValue(const QString &key, const QVariant &value)
setValue This slot sets new value for a key setting
void valueChanged(QString key, QVariant value)
valueChanged This signal when value of the key settings changed
virtual QVariant getValueImplementation(const QString &key, const QVariant &def)=0
getValueImplementation This method will return the value of the settings.
virtual void setValueImplementation(const QString key, const QVariant &value)=0
setValueImplementation This slot will set a new value for the key parameter.
Q_INVOKABLE QString getStrValue(const QString &key, const QString &def={})
getStrValue some as getValue but convert result object to QString type.
Definition isettings.cpp:67
void clearCache()
clearCache This method clear all data from cache.
Definition isettings.cpp:24
ISettings(SettingsSaveMode mode=SettingsSaveMode::Auto)
Definition isettings.cpp:15
Q_INVOKABLE void resetToDefault()
resetToDefault This method reset all settings to default values.
Definition isettings.cpp:76
void forceReloadCache()
forceReloadCache This method force reload settings data from disk.
Definition isettings.cpp:97
void setMode(const SettingsSaveMode &mode)
setMode This method sets a new value of the settings mode.
Definition isettings.cpp:39
Q_INVOKABLE QVariant getValue(const QString &key, const QVariant &def={})
getValue This method return the value of the settings.
Definition isettings.cpp:51
SettingsSaveMode getMode() const
getMode This method return the current mode of the settings.
Definition isettings.cpp:35
virtual QHash< QString, QVariant > defaultSettings()=0
void setStrValue(const QString &key, const QString &value)
setStrValue This is some as setValue but working with the QString type.
static Base * instance()
instance This method return pointerer to current service object.
Definition qaservice.h:115
static std::unique_ptr< ISettings > & initService()
initService This method initialize the Base object as a service.
Definition qaservice.h:81
The QuasaraAppUtils class This lib include base functions for the all applications of QuasarApp group...
Definition helpdata.cpp:18
SettingsSaveMode
The SettingsSaveMode enum.
Definition isettings.h:23
@ Auto
a settings will be saved on hard disk when called the Settings::setValue method.
#define debug_assert(C, M)