fix settings save

This commit is contained in:
Andrei Yankovich 2019-09-28 16:41:43 +03:00
parent c8378b7ec3
commit 85f9ba6c7d
2 changed files with 53 additions and 8 deletions

View File

@ -11,7 +11,7 @@
using namespace QuasarAppUtils;
Settings::Settings() {
Settings::Settings(SettingsSaveMode mode) {
auto name = QCoreApplication::applicationName();
auto company = QCoreApplication::organizationName();
if (name.isEmpty()) {
@ -23,11 +23,21 @@ Settings::Settings() {
}
_settings = new QSettings(QSettings::IniFormat, QSettings::Scope::UserScope, company, name);
_mode = mode;
}
Settings *Settings::initSettings() {
static Settings* res = new Settings();
SettingsSaveMode Settings::getMode() const
{
return _mode;
}
void Settings::setMode(const SettingsSaveMode &mode)
{
_mode = mode;
}
Settings *Settings::initSettings(SettingsSaveMode mode) {
static Settings* res = new Settings(mode);
return res;
}
@ -47,12 +57,20 @@ QString Settings::getStrValue(const QString &key, const QString &def) {
return getValue(key, QVariant(def)).toString();
}
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) {

View File

@ -16,6 +16,16 @@ class QSettings;
namespace QuasarAppUtils {
/**
* @brief The SettingsSaveMode enum
* Auto - value save on hard disk when calling method "value"
* manual - save all data on hard disk when calling method Settings::sync
*/
enum class SettingsSaveMode: quint64 {
Auto,
Manual
};
/**
* @brief The Settings class - singleton for QSettings
*/
@ -23,11 +33,11 @@ class QUASARAPPSHARED_EXPORT Settings : public QObject
{
Q_OBJECT
private:
explicit Settings();
QSettings *_settings;
static Settings* initSettings();
explicit Settings(SettingsSaveMode mode = SettingsSaveMode::Auto);
QSettings *_settings = nullptr;
SettingsSaveMode _mode = SettingsSaveMode::Auto;
static Settings* initSettings(SettingsSaveMode mode = SettingsSaveMode::Auto);
public:
/**
@ -55,6 +65,23 @@ public:
*/
QString getStrValue(const QString &key, const QString& def);
/**
* @brief sync - save all data on hard disk;
*/
void sync();
/**
* @brief getMode
* @return
*/
SettingsSaveMode getMode() const;
/**
* @brief setMode
* @param mode
*/
void setMode(const SettingsSaveMode &mode);
public slots:
/**
* @brief setValue - set new value of key