mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-05-01 04:04:42 +00:00
fix settings save
This commit is contained in:
parent
c8378b7ec3
commit
85f9ba6c7d
26
settings.cpp
26
settings.cpp
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
using namespace QuasarAppUtils;
|
using namespace QuasarAppUtils;
|
||||||
|
|
||||||
Settings::Settings() {
|
Settings::Settings(SettingsSaveMode mode) {
|
||||||
auto name = QCoreApplication::applicationName();
|
auto name = QCoreApplication::applicationName();
|
||||||
auto company = QCoreApplication::organizationName();
|
auto company = QCoreApplication::organizationName();
|
||||||
if (name.isEmpty()) {
|
if (name.isEmpty()) {
|
||||||
@ -23,11 +23,21 @@ Settings::Settings() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_settings = new QSettings(QSettings::IniFormat, QSettings::Scope::UserScope, company, name);
|
_settings = new QSettings(QSettings::IniFormat, QSettings::Scope::UserScope, company, name);
|
||||||
|
_mode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
Settings *Settings::initSettings() {
|
SettingsSaveMode Settings::getMode() const
|
||||||
static Settings* res = new Settings();
|
{
|
||||||
|
return _mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Settings::setMode(const SettingsSaveMode &mode)
|
||||||
|
{
|
||||||
|
_mode = mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
Settings *Settings::initSettings(SettingsSaveMode mode) {
|
||||||
|
static Settings* res = new Settings(mode);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,12 +57,20 @@ QString Settings::getStrValue(const QString &key, const QString &def) {
|
|||||||
return getValue(key, QVariant(def)).toString();
|
return getValue(key, QVariant(def)).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Settings::sync() {
|
||||||
|
_settings->sync();
|
||||||
|
}
|
||||||
|
|
||||||
void Settings::setValue(const QString key, const QVariant &value) {
|
void Settings::setValue(const QString key, const QVariant &value) {
|
||||||
_settings->setValue(key, value);
|
_settings->setValue(key, value);
|
||||||
|
|
||||||
emit valueChanged(key, value);
|
emit valueChanged(key, value);
|
||||||
emit valueStrChanged(key, value.toString());
|
emit valueStrChanged(key, value.toString());
|
||||||
|
|
||||||
|
if (_mode == SettingsSaveMode::Auto) {
|
||||||
|
sync();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setStrValue(const QString &key, const QString &value) {
|
void Settings::setStrValue(const QString &key, const QString &value) {
|
||||||
|
35
settings.h
35
settings.h
@ -16,6 +16,16 @@ class QSettings;
|
|||||||
|
|
||||||
namespace QuasarAppUtils {
|
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
|
* @brief The Settings class - singleton for QSettings
|
||||||
*/
|
*/
|
||||||
@ -23,11 +33,11 @@ class QUASARAPPSHARED_EXPORT Settings : public QObject
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private:
|
private:
|
||||||
explicit Settings();
|
explicit Settings(SettingsSaveMode mode = SettingsSaveMode::Auto);
|
||||||
QSettings *_settings;
|
QSettings *_settings = nullptr;
|
||||||
|
SettingsSaveMode _mode = SettingsSaveMode::Auto;
|
||||||
static Settings* initSettings();
|
|
||||||
|
|
||||||
|
static Settings* initSettings(SettingsSaveMode mode = SettingsSaveMode::Auto);
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -55,6 +65,23 @@ public:
|
|||||||
*/
|
*/
|
||||||
QString getStrValue(const QString &key, const QString& def);
|
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:
|
public slots:
|
||||||
/**
|
/**
|
||||||
* @brief setValue - set new value of key
|
* @brief setValue - set new value of key
|
||||||
|
Loading…
x
Reference in New Issue
Block a user