QuasarAppLib/settings.h

121 lines
3.5 KiB
C
Raw Normal View History

2018-09-29 15:56:04 +03:00
/*
2021-01-05 13:04:05 +03:00
* Copyright (C) 2018-2021 QuasarApp.
2018-09-29 15:56:04 +03:00
* 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 <QObject>
#include <QVariant>
class QSettings;
namespace QuasarAppUtils {
2019-09-28 16:41:43 +03:00
/**
* @brief The SettingsSaveMode enum
*/
enum class SettingsSaveMode: quint64 {
/// a settings will be saved on hard disk when called the Settings::setValue method.
2019-09-28 16:41:43 +03:00
Auto,
/// a settings will be saved on hard disk when called the Settings::Sync method.
2019-09-28 16:41:43 +03:00
Manual
};
2018-09-29 15:56:04 +03:00
/**
* @brief The Settings class This is wraper of the QSettings object.
* @note This is singleton object.
2018-09-29 15:56:04 +03:00
*/
class QUASARAPPSHARED_EXPORT Settings : public QObject
{
Q_OBJECT
2018-09-29 15:56:04 +03:00
public:
/**
* @brief instance This method return instance of the settings object
* @return pointer to a settings object;
2018-09-29 15:56:04 +03:00
*/
static Settings* instance();
2018-09-29 15:56:04 +03:00
/**
* @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
2018-09-29 15:56:04 +03:00
*/
2019-10-13 18:45:16 +03:00
Q_INVOKABLE QVariant getValue(const QString &key, const QVariant& def);
2018-09-29 15:56:04 +03:00
/**
* @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
2018-09-29 15:56:04 +03:00
*/
2019-10-13 18:45:16 +03:00
Q_INVOKABLE QString getStrValue(const QString &key, const QString& def);
2018-09-29 15:56:04 +03:00
2019-09-28 16:41:43 +03:00
/**
* @brief sync This method save all setings data on a hard disk;
2019-09-28 16:41:43 +03:00
*/
void sync();
/**
* @brief getMode This method return the current mode of the settings.
* @return the current mode of the settings.
2019-09-28 16:41:43 +03:00
*/
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.
2019-09-28 16:41:43 +03:00
*/
void setMode(const SettingsSaveMode &mode);
2018-09-29 15:56:04 +03:00
public slots:
/**
2021-04-26 12:01:08 +03:00
* @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
2018-09-29 15:56:04 +03:00
*/
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
2018-09-29 15:56:04 +03:00
*/
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.
*/
2018-09-29 15:56:04 +03:00
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.
*/
2018-09-29 15:56:04 +03:00
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);
2018-09-29 15:56:04 +03:00
};
} ;
#endif // SETTINGS_H