Showing the changes to plugins/JSAPIExample/src/JSAPIExample.cpp (1 of 1 file). Show all
Unified · Split
| @@ -22,7 +22,7 @@ · 21 unmodified lines | ||
| 22 | 22 | #include <QtScript/QScriptable> |
| 23 | 23 | |
| 24 | 24 | #include <SettingHelpers.h> // for ::settingsFilename() |
| 25 | −#include <SharedUtil.h> // for usecTimestampNow() | |
| 25 | +#include <SharedUtil.h> // for ::usecTimestampNow() | |
| 26 | 26 | #include <shared/ScriptInitializerMixin.h> |
| 27 | 27 | |
| 28 | 28 | // NOTE: replace this with your own namespace when starting a new plugin (to avoid .so/.dll symbol clashes) |
| @@ -150,9 +150,9 @@ public slots: · 121 unmodified lines | ||
| 150 | 150 | * @example <caption>access persistent settings stored in separate .json files</caption> |
| 151 | 151 | * var settings = JSAPIExample.getScopedSettings("example"); |
| 152 | 152 | * print("example settings stored in:", settings.fileName()); |
| 153 | − * print("(before) example::timestamp", settings.value("timestamp")); | |
| 153 | + * print("(before) example::timestamp", settings.getValue("timestamp")); | |
| 154 | 154 | * settings.setValue("timestamp", Date.now()); |
| 155 | − * print("(after) example::timestamp", settings.value("timestamp")); | |
| 155 | + * print("(after) example::timestamp", settings.getValue("timestamp")); | |
| 156 | 156 | * print("all example::* keys", settings.allKeys()); |
| 157 | 157 | * settings = null; // optional best pratice; allows the object to be reclaimed ASAP by the JS garbage collector |
| 158 | 158 | */ |
| @@ -173,44 +173,24 @@ private: · 14 unmodified lines | ||
| 173 | 173 | const QString _version{ JSAPI_SEMANTIC_VERSION }; |
| 174 | 174 | }; |
| 175 | 175 | |
| 176 | −// JSSettingsHelper emulates a subset of QSetting APIs: | |
| 177 | −// fileName() -- full path to the scoped settings .json file | |
| 178 | −// allKeys() -- all previously stored keys available in the scoped settings file | |
| 179 | −// value(key, defaultValue) -- retrieve a stored value | |
| 180 | −// setValue(key, newValue) -- set/update a stored value | |
| 176 | +// JSSettingsHelper wraps a scoped (prefixed/separate) QSettings and exposes a subset of QSetting APIs as slots | |
| 181 | 177 | class JSSettingsHelper : public QObject { |
| 182 | 178 | Q_OBJECT |
| 183 | − QString _scope; | |
| 184 | − QString _fileName; | |
| 185 | − QSharedPointer<QSettings> _settings; | |
| 186 | − | |
| 187 | 179 | public: |
| 188 | − operator bool() const { return (bool)_settings; } | |
| 189 | − JSSettingsHelper(const QString& scope, QObject* parent = nullptr) : | |
| 190 | − QObject(parent), _scope(scope), _fileName(getLocalSettingsPath(scope)), | |
| 191 | − _settings(_fileName.isEmpty() ? nullptr : new QSettings(_fileName, JSON_FORMAT)) {} | |
| 192 | − ~JSSettingsHelper() { qCDebug(logger) << "~JSSettingsHelper" << _scope << _fileName << this; } | |
| 180 | + JSSettingsHelper(const QString& scope, QObject* parent = nullptr); | |
| 181 | + ~JSSettingsHelper(); | |
| 182 | + operator bool() const; | |
| 193 | 183 | public slots: |
| 194 | − QString fileName() const { return _settings ? _settings->fileName() : ""; } | |
| 195 | − QString toString() const { return QString("[JSSettingsHelper scope=%1 valid=%2]").arg(_scope).arg((bool)_settings); } | |
| 196 | − QVariant value(const QString& key, const QVariant& defaultValue = QVariant()) { | |
| 197 | − return _settings ? _settings->value(key, defaultValue) : defaultValue; | |
| 198 | − } | |
| 199 | − bool setValue(const QString& key, const QVariant& value) { | |
| 200 | − if (_settings) { | |
| 201 | − _settings->setValue(key, value); | |
| 202 | − return true; | |
| 203 | − } | |
| 204 | − return false; | |
| 205 | − } | |
| 206 | − QStringList allKeys() const { return _settings ? _settings->allKeys() : QStringList{}; } | |
| 207 | − | |
| 184 | + QString fileName() const; | |
| 185 | + QString toString() const; | |
| 186 | + QVariant getValue(const QString& key, const QVariant& defaultValue = QVariant()); | |
| 187 | + bool setValue(const QString& key, const QVariant& value); | |
| 188 | + QStringList allKeys() const; | |
| 208 | 189 | protected: |
| 209 | − QString getLocalSettingsPath(const QString& scope) const { | |
| 210 | − // generate a prefixed filename (relative to the main application's Interface.json file) | |
| 211 | − const QString fileName = QString("jsapi_%1.json").arg(scope); | |
| 212 | − return QFileInfo(::settingsFilename()).dir().filePath(fileName); | |
| 213 | − } | |
| 190 | + QString _scope; | |
| 191 | + QString _fileName; | |
| 192 | + QSharedPointer<QSettings> _settings; | |
| 193 | + QString getLocalSettingsPath(const QString& scope) const; | |
| 214 | 194 | }; |
| 215 | 195 | |
| 216 | 196 | // verifies the requested scope is sensible and creates/returns a scoped JSSettingsHelper instance |
| @@ -223,6 +203,48 @@ QObject* createScopedSettings(const QString& scope, QObject* parent, QString& er · 6 unmodified lines | ||
| 223 | 203 | return new JSSettingsHelper(scope, parent); |
| 224 | 204 | } |
| 225 | 205 | |
| 206 | +// -------------------------------------------------- | |
| 207 | +// ----- inline JSSettingsHelper implementation ----- | |
| 208 | +JSSettingsHelper::operator bool() const { | |
| 209 | + return (bool)_settings; | |
| 210 | +} | |
| 211 | +JSSettingsHelper::JSSettingsHelper(const QString& scope, QObject* parent) : | |
| 212 | + QObject(parent), _scope(scope), _fileName(getLocalSettingsPath(scope)), | |
| 213 | + _settings(_fileName.isEmpty() ? nullptr : new QSettings(_fileName, JSON_FORMAT)) { | |
| 214 | +} | |
| 215 | +JSSettingsHelper::~JSSettingsHelper() { | |
| 216 | + qCDebug(logger) << "~JSSettingsHelper" << _scope << _fileName << this; | |
| 217 | +} | |
| 218 | +QString JSSettingsHelper::fileName() const { | |
| 219 | + return _settings ? _settings->fileName() : ""; | |
| 220 | +} | |
| 221 | +QString JSSettingsHelper::toString() const { | |
| 222 | + return QString("[JSSettingsHelper scope=%1 valid=%2]").arg(_scope).arg((bool)_settings); | |
| 223 | +} | |
| 224 | +QVariant JSSettingsHelper::getValue(const QString& key, const QVariant& defaultValue) { | |
| 225 | + return _settings ? _settings->value(key, defaultValue) : defaultValue; | |
| 226 | +} | |
| 227 | +bool JSSettingsHelper::setValue(const QString& key, const QVariant& value) { | |
| 228 | + if (_settings) { | |
| 229 | + if (value.isValid()) { | |
| 230 | + _settings->setValue(key, value); | |
| 231 | + } else { | |
| 232 | + _settings->remove(key); | |
| 233 | + } | |
| 234 | + return true; | |
| 235 | + } | |
| 236 | + return false; | |
| 237 | +} | |
| 238 | +QStringList JSSettingsHelper::allKeys() const { | |
| 239 | + return _settings ? _settings->allKeys() : QStringList{}; | |
| 240 | +} | |
| 241 | +QString JSSettingsHelper::getLocalSettingsPath(const QString& scope) const { | |
| 242 | + // generate a prefixed filename (relative to the main application's Interface.json file) | |
| 243 | + const QString fileName = QString("jsapi_%1.json").arg(scope); | |
| 244 | + return QFileInfo(::settingsFilename()).dir().filePath(fileName); | |
| 245 | +} | |
| 246 | +// ----- /inline JSSettingsHelper implementation ----- | |
| 247 | + | |
| 226 | 248 | } // namespace REPLACE_ME_WITH_UNIQUE_NAME |
| 227 | 249 | |
| 228 | 250 | #include "JSAPIExample.moc" |