4
0
mirror of https://github.com/QuasarApp/QuasarAppLib.git synced 2025-05-01 12:09:45 +00:00

addded more features to the settings model

This commit is contained in:
Andrei Yankovich 2022-06-24 23:54:04 +03:00
parent 093ea662ee
commit 3cf69bb5a3
4 changed files with 73 additions and 4 deletions

@ -8,7 +8,7 @@
#include "isettings.h"
#include <QSettings>
#include <QCoreApplication>
#include "qaglobalutils.h""
#include "qaglobalutils.h"
namespace QuasarAppUtils {
@ -75,10 +75,15 @@ void ISettings::resetToDefault() {
auto &defaultConfig = settingsMap();
for (auto it = defaultConfig.begin(); it != defaultConfig.end(); ++it) {
setValue(it.key(), defaultConfig.value(it.key()));
if (!ignoreToRest(it.key()))
setValue(it.key(), defaultConfig.value(it.key()));
}
}
bool ISettings::ignoreToRest(const QString &) const {
return false;
}
void ISettings::sync() {
for (auto it = _cache.begin(); it != _cache.end(); ++it) {
setValueImplementation(it.key(), it.value());
@ -109,7 +114,7 @@ void ISettings::setValue(const QString &key, const QVariant &value) {
emit valueStrChanged(key, value.toString());
if (_mode == SettingsSaveMode::Auto) {
sync();
setValueImplementation(key, value);
}
}

@ -98,6 +98,14 @@ public:
*/
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;
*/
@ -204,10 +212,19 @@ protected:
* @brief clearCache This method clear all data from cache.
*/
void clearCache();
private:
/**
* @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;

@ -32,6 +32,10 @@ void Settings::syncImplementation() {
}
QVariant Settings::getValueImplementation(const QString &key, const QVariant &def) {
if (isBool(key)) {
return _settings->value(key, def).toBool();
}
return _settings->value(key, def);
}
@ -39,4 +43,18 @@ void Settings::setValueImplementation(const QString key, const QVariant &value)
return _settings->setValue(key, value);
}
bool Settings::isBool(const QString &) const {
return false;
}
const QSet<QString> &Settings::boolOptions() const
{
return _boolOptions;
}
void Settings::setBoolOptions(const QSet<QString> &newBoolOptions)
{
_boolOptions = newBoolOptions;
}
}

@ -11,6 +11,7 @@
#include "quasarapp_global.h"
#include "isettings.h"
#include <QSet>
namespace QuasarAppUtils {
@ -32,16 +33,44 @@ public:
Settings();
// ISettings interface
/**
* @brief boolOptions returns current map with keys that has a bool type.
* @return current map with keys that has a bool type
* @see Settings::setBoolOptions
* @see Settings::isBool
*/
const QSet<QString> &boolOptions() const;
/**
* @brief setBoolOptions This method sets new map of the boolean options.
* @param newBoolOptions This is new map of the boolean options.
* @see Settings::boolOptions
* @see Settings::isBool
*/
void setBoolOptions(const QSet<QString> &newBoolOptions);
protected:
void syncImplementation();
QVariant getValueImplementation(const QString &key, const QVariant &def);
void setValueImplementation(const QString key, const QVariant &value);
/**
* @brief isBool This method should be return true if the key's type is bool.
* This is needed because QVariant will be converted alvays to true value in a qml code.
* @param key This is key of checks setting.
* @return true if the key is boolean variable.
* The default implementation check key in the inner map.
*
* @see Settings::setBoolOptions
* @see Settings::boolOptions
*/
virtual bool isBool(const QString& key) const;
void setGroup(const QString&);
private:
QSettings *_settings = nullptr;
QSet<QString> _boolOptions;
};
}