1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#ifndef CFGENTRY_H
#define CFGENTRY_H
#include "coreSQLiteStudio_global.h"
#include <QString>
#include <QVariant>
#include <QObject>
class CfgCategory;
class CfgMain;
class API_EXPORT CfgEntry : public QObject
{
Q_OBJECT
friend class CfgCategory;
public:
typedef QVariant (*DefaultValueProviderFunc)();
explicit CfgEntry(const CfgEntry& other);
CfgEntry(const QString& name, const QVariant& defValue, const QString& title);
virtual ~CfgEntry();
QVariant get() const;
QVariant getDefaultValue() const;
void set(const QVariant& value);
operator QString() const;
void defineDefaultValueFunction(DefaultValueProviderFunc func);
QString getFullKey() const;
QString getTitle() const;
QString getName() const;
void translateTitle();
void reset();
bool isPersistable() const;
bool isPersisted() const;
void savepoint(bool transaction = false);
void begin();
void restore();
void release();
void commit();
void rollback();
CfgCategory* getCategory() const;
CfgMain* getMain() const;
/**
* @brief operator CfgEntry *
*
* Allows implict casting from value object into pointer. It simply returns "this".
* It's useful to use config objects directly in QObject::connect() arguments,
* cause it accepts pointers, not values, but CfgEntry is usually accessed by value.
*/
operator CfgEntry*();
protected:
bool persistable = true;
CfgCategory* parent = nullptr;
QString name;
QVariant defValue;
QString title;
QVariant backup;
bool transaction = false;
mutable bool cached = false;
mutable QVariant cachedValue;
DefaultValueProviderFunc defValueFunc = nullptr;
signals:
void changed(const QVariant& newValue);
void persisted(const QVariant& newValue);
};
template <class T>
class CfgTypedEntry : public CfgEntry
{
public:
CfgTypedEntry(const QString& name, DefaultValueProviderFunc func, const QString& title) :
CfgEntry(name, QVariant(), title)
{
defineDefaultValueFunction(func);
}
CfgTypedEntry(const QString& name, const T& defValue, const QString& title) :
CfgEntry(name, QVariant::fromValue(defValue), title) {}
CfgTypedEntry(const QString& name, DefaultValueProviderFunc func) :
CfgTypedEntry(name, func, QString()) {}
CfgTypedEntry(const QString& name, const T& defValue, bool persistable) :
CfgTypedEntry(name, defValue, QString())
{
this->persistable = persistable;
}
CfgTypedEntry(const QString& name, DefaultValueProviderFunc func, bool persistable) :
CfgTypedEntry(name, func, QString())
{
this->persistable = persistable;
}
CfgTypedEntry(const QString& name, const T& defValue) :
CfgTypedEntry(name, defValue, QString()) {}
CfgTypedEntry(const QString& name) :
CfgEntry(name, QVariant(), QString()) {}
CfgTypedEntry(const CfgTypedEntry& other) :
CfgEntry(other) {}
T get()
{
QVariant v = CfgEntry::get();
return v.value<T>();
}
void set(const T& value)
{
CfgEntry::set(QVariant::fromValue<T>(value));
}
};
typedef CfgTypedEntry<QString> CfgStringEntry;
Q_DECLARE_METATYPE(CfgEntry*)
#endif // CFGENTRY_H
|