diff --git a/QuasarApp.pro b/QuasarApp.pro index 3d3ab0e..1774467 100644 --- a/QuasarApp.pro +++ b/QuasarApp.pro @@ -40,6 +40,7 @@ SOURCES += \ params.cpp \ locales.cpp \ settings.cpp \ + isettings.cpp \ global.cpp HEADERS += \ @@ -50,6 +51,7 @@ HEADERS += \ params.h \ locales.h \ settings.h \ + isettings.h \ global.h DISTFILES += \ diff --git a/isettings.cpp b/isettings.cpp new file mode 100644 index 0000000..cabc089 --- /dev/null +++ b/isettings.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2018-2021 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. +*/ + +#include "isettings.h" +#include +#include + +namespace QuasarAppUtils { + +ISettings* ISettings::_settings = nullptr; + +ISettings::ISettings(SettingsSaveMode mode) { + _mode = mode; +} + +SettingsSaveMode ISettings::getMode() const { + return _mode; +} + +void ISettings::setMode(const SettingsSaveMode &mode) { + _mode = mode; +} + +QVariant ISettings::getValue(const QString &key, const QVariant &def) { + return getValueImplementation(key, def); +} + +QString ISettings::getStrValue(const QString &key, const QString &def) { + return getValue(key, QVariant(def)).toString(); +} + +void ISettings::sync() { + return syncImplementation(); +} + +void ISettings::setValue(const QString key, const QVariant &value) { + return setValueImplementation(key, value); + + emit valueChanged(key, value); + emit valueStrChanged(key, value.toString()); + + if (_mode == SettingsSaveMode::Auto) { + sync(); + } + +} + +void ISettings::setStrValue(const QString &key, const QString &value) { + setValue(key, value); +} + +} diff --git a/isettings.h b/isettings.h new file mode 100644 index 0000000..c225118 --- /dev/null +++ b/isettings.h @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2018-2021 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 "quasarapp_global.h" +#include +#include + +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 This is wraper of the QSettings object. + * @note This is singleton object. + */ +class QUASARAPPSHARED_EXPORT ISettings : public QObject +{ + Q_OBJECT + +public: + + /** + * @brief instance This method return instance of the settings object + * @return pointer to a settings object; + */ + template + static ISettings* instance() { + static_assert (std::is_base_of::value, + "the Settingstype type must be ISettings"); + + if(!_settings){ + _settings = new SettingsType(); + } + + return _settings; + } + + /** + * @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. + * @return value of a @a key + */ + 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. + * @return value of a @a key + */ + Q_INVOKABLE QString getStrValue(const QString &key, const QString& def); + + /** + * @brief sync This method save all setings data on a hard disk; + */ + void sync(); + + /** + * @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); + +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 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; + +private: + SettingsSaveMode _mode = SettingsSaveMode::Auto; + static ISettings* _settings; +}; +} ; + + +#endif // ISETTINGS_H diff --git a/settings.cpp b/settings.cpp index 8f519ab..1f86982 100644 --- a/settings.cpp +++ b/settings.cpp @@ -1,17 +1,18 @@ /* - * Copyright (C) 2018-2021 QuasarApp. + * Copyright (C) 2021-2021 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. */ + #include "settings.h" #include #include -using namespace QuasarAppUtils; +namespace QuasarAppUtils { -Settings::Settings(SettingsSaveMode mode) { +Settings::Settings() { auto name = QCoreApplication::applicationName(); auto company = QCoreApplication::organizationName(); if (name.isEmpty()) { @@ -23,50 +24,18 @@ Settings::Settings(SettingsSaveMode mode) { } _settings = new QSettings(QSettings::IniFormat, QSettings::Scope::UserScope, company, name); - _mode = mode; } -SettingsSaveMode Settings::getMode() const { - return _mode; +void Settings::syncImplementation() { + return _settings->sync(); } -void Settings::setMode(const SettingsSaveMode &mode) { - _mode = mode; -} - -Settings *Settings::initSettings(SettingsSaveMode mode) { - static Settings* res = new Settings(mode); - return res; -} - -Settings *Settings::instance() { - return initSettings(); -} - -QVariant Settings::getValue(const QString &key, const QVariant &def) { +QVariant Settings::getValueImplementation(const QString &key, const QVariant &def) { return _settings->value(key, def); } -QString Settings::getStrValue(const QString &key, const QString &def) { - return getValue(key, QVariant(def)).toString(); +void Settings::setValueImplementation(const QString key, const QVariant &value) { + return _settings->setValue(key, value); } -void Settings::sync() { - _settings->sync(); -} - -void Settings::setValue(const QString key, const QVariant &value) { - _settings->setValue(key, value); - - emit valueChanged(key, value); - emit valueStrChanged(key, value.toString()); - - if (_mode == SettingsSaveMode::Auto) { - sync(); - } - -} - -void Settings::setStrValue(const QString &key, const QString &value) { - setValue(key, value); } diff --git a/settings.h b/settings.h index a9ad5c8..b7b1097 100644 --- a/settings.h +++ b/settings.h @@ -1,120 +1,35 @@ /* - * Copyright (C) 2018-2021 QuasarApp. + * Copyright (C) 2021-2021 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 SETTINGS_H #define SETTINGS_H #include "quasarapp_global.h" -#include -#include - -class QSettings; +#include "isettings.h" 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 This is wraper of the QSettings object. - * @note This is singleton object. - */ -class QUASARAPPSHARED_EXPORT Settings : public QObject +class QUASARAPPSHARED_EXPORT Settings: public ISettings { - Q_OBJECT - public: + Settings(); - /** - * @brief instance This method return instance of the settings object - * @return pointer to a settings object; - */ - static Settings* instance(); + // ISettings interface +protected: + void syncImplementation(); + QVariant getValueImplementation(const QString &key, const QVariant &def); + void setValueImplementation(const QString key, const QVariant &value); - /** - * @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. - * @return value of a @a key - */ - 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. - * @return value of a @a key - */ - Q_INVOKABLE QString getStrValue(const QString &key, const QString& def); - - /** - * @brief sync This method save all setings data on a hard disk; - */ - void sync(); - - /** - * @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); - -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); private: - explicit Settings(SettingsSaveMode mode = SettingsSaveMode::Auto); QSettings *_settings = nullptr; - SettingsSaveMode _mode = SettingsSaveMode::Auto; - - static Settings* initSettings(SettingsSaveMode mode = SettingsSaveMode::Auto); - }; -} ; +} #endif // SETTINGS_H