diff --git a/isettings.cpp b/isettings.cpp new file mode 100644 index 0000000..1c543b9 --- /dev/null +++ b/isettings.cpp @@ -0,0 +1,61 @@ +/* + * 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 + +using namespace QuasarAppUtils; + +ISettings::ISettings(SettingsSaveMode mode) { + _mode = mode; +} + +SettingsSaveMode ISettings::getMode() const { + return _mode; +} + +void ISettings::setMode(const SettingsSaveMode &mode) { + _mode = mode; +} + +//ISettings *ISettings::initSettings(SettingsSaveMode mode) { +// static ISettings* res = new ISettings(mode); +// return res; +//} + +//ISettings *ISettings::instance() { +// return initSettings(); +//} + +QVariant ISettings::getValue(const QString &key, const QVariant &def) { + getValueImplementation(key, def); +} + +QString ISettings::getStrValue(const QString &key, const QString &def) { + return getValue(key, QVariant(def)).toString(); +} + +void ISettings::sync() { + syncImplementation(); +} + +void ISettings::setValue(const QString key, const QVariant &value) { + 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..c10c6db --- /dev/null +++ b/isettings.h @@ -0,0 +1,142 @@ +/* + * 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; +// */ +// static ISettings* instance(); + + /** + * @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(); + + /** + * @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); + + /** + * @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); + +private: + SettingsSaveMode _mode = SettingsSaveMode::Auto; + +// static ISettings* initSettings(SettingsSaveMode mode = SettingsSaveMode::Auto); + +}; +} ; + + +#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