| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#ifndef SETTINGSOBJECTBASE_H |
| 19 |
#define SETTINGSOBJECTBASE_H |
| 20 |
|
| 21 |
#include <QObject> |
| 22 |
#include <QStringList> |
| 23 |
#include <QJsonObject> |
| 24 |
#include <QJsonValue> |
| 25 |
#include <QJsonArray> |
| 26 |
#include <QDateTime> |
| 27 |
#include <QVariantMap> |
| 28 |
#include <QSharedPointer> |
| 29 |
#include <QScopedPointer> |
| 30 |
#include <QLockFile> |
| 31 |
#include "SettingsFileBase.h" |
| 32 |
|
| 33 |
class SettingsFileBase; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class SettingsObjectBase : public QObject |
| 43 |
{ |
| 44 |
Q_OBJECT |
| 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 |
Q_PROPERTY(QJsonObject data READ data WRITE setData NOTIFY dataChanged) |
| 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 |
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged) |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
Q_PROPERTY(bool isValid READ isValid NOTIFY dataChanged) |
| 106 |
|
| 107 |
public: |
| 108 |
explicit SettingsObjectBase(QObject *parent = 0); |
| 109 |
|
| 110 |
QJsonObject data() const; |
| 111 |
void setData(const QJsonObject &data); |
| 112 |
QString path() const; |
| 113 |
void setPath(const QString &path); |
| 114 |
bool isValid() const; |
| 115 |
bool hasKey() const { return !m_settingsKey.isEmpty(); } |
| 116 |
|
| 117 |
Q_INVOKABLE QJsonValue read(const QString &key, const QJsonValue &defaultValue = QJsonValue::Undefined) const; |
| 118 |
template<typename T> T read(const QString &key) const; |
| 119 |
Q_INVOKABLE void write(const QString &key, const QJsonValue &value); |
| 120 |
template<typename T> void write(const QString &key, const T &value); |
| 121 |
|
| 122 |
QJsonValue read(const char *key, const QJsonValue &defaultValue = QJsonValue::Undefined) const |
| 123 |
{ |
| 124 |
return read(QString::fromLatin1(key), defaultValue); |
| 125 |
} |
| 126 |
template<typename T> T read(const char *key) const |
| 127 |
{ |
| 128 |
return read<T>(QString::fromLatin1(key)); |
| 129 |
} |
| 130 |
void write(const char *key, const QJsonValue &value) |
| 131 |
{ |
| 132 |
write(QString::fromLatin1(key), value); |
| 133 |
} |
| 134 |
template<typename T> void write(const char *key, const T &value) |
| 135 |
{ |
| 136 |
write<T>(QString::fromLatin1(key), value); |
| 137 |
} |
| 138 |
|
| 139 |
Q_INVOKABLE void undefine(); |
| 140 |
|
| 141 |
signals: |
| 142 |
void pathChanged(); |
| 143 |
void dataChanged(); |
| 144 |
void modified(const QString &path, const QJsonValue &value); |
| 145 |
public slots: |
| 146 |
void modified(const QStringList &path, const QJsonValue &value); |
| 147 |
|
| 148 |
protected: |
| 149 |
virtual void createDefaultsIfNotExist(); |
| 150 |
void setSettingsKey(const QString &key); |
| 151 |
|
| 152 |
private: |
| 153 |
void setFile(QSharedPointer<SettingsFileBase> file); |
| 154 |
QSharedPointer<SettingsFileBase> m_file; |
| 155 |
QScopedPointer<QLockFile> *m_lock; |
| 156 |
QStringList m_path; |
| 157 |
QJsonObject m_object; |
| 158 |
bool m_invalid; |
| 159 |
QString m_settingsKey; |
| 160 |
|
| 161 |
}; |
| 162 |
template<typename T> inline void SettingsObjectBase::write(const QString &key, const T &value) |
| 163 |
{ |
| 164 |
write(key, QJsonValue(value)); |
| 165 |
} |
| 166 |
|
| 167 |
template<> inline QString SettingsObjectBase::read<QString>(const QString &key) const |
| 168 |
{ |
| 169 |
return read(key).toString(); |
| 170 |
} |
| 171 |
|
| 172 |
template<> inline QJsonArray SettingsObjectBase::read<QJsonArray>(const QString &key) const |
| 173 |
{ |
| 174 |
return read(key).toArray(); |
| 175 |
} |
| 176 |
|
| 177 |
template<> inline QJsonObject SettingsObjectBase::read<QJsonObject>(const QString &key) const |
| 178 |
{ |
| 179 |
return read(key).toObject(); |
| 180 |
} |
| 181 |
|
| 182 |
template<> inline double SettingsObjectBase::read<double>(const QString &key) const |
| 183 |
{ |
| 184 |
return read(key).toDouble(); |
| 185 |
} |
| 186 |
|
| 187 |
template<> inline int SettingsObjectBase::read<int>(const QString &key) const |
| 188 |
{ |
| 189 |
return read(key).toInt(); |
| 190 |
} |
| 191 |
|
| 192 |
template<> inline bool SettingsObjectBase::read<bool>(const QString &key) const |
| 193 |
{ |
| 194 |
return read(key).toBool(); |
| 195 |
} |
| 196 |
|
| 197 |
template<> inline QDateTime SettingsObjectBase::read<QDateTime>(const QString &key) const |
| 198 |
{ |
| 199 |
QString value = read(key).toString(); |
| 200 |
if (value.isEmpty()) |
| 201 |
return QDateTime(); |
| 202 |
return QDateTime::fromString(value, Qt::ISODate).toLocalTime(); |
| 203 |
} |
| 204 |
|
| 205 |
template<> inline void SettingsObjectBase::write<QDateTime>(const QString &key, const QDateTime &value) |
| 206 |
{ |
| 207 |
write(key, QJsonValue(value.toUTC().toString(Qt::ISODate))); |
| 208 |
} |
| 209 |
|
| 210 |
#endif |