History

further cleanup per peer review (thanks fluffy!)

humbletim committed 6 years ago

Commit dedf6a61 parent 7fb01731 file · +58 −36Browse files

Showing the changes to plugins/JSAPIExample/src/JSAPIExample.cpp (1 of 1 file). Show all

Unified · Split

plugins/JSAPIExample/src/JSAPIExample.cpp+58−36
@@ -22,7 +22,7 @@ · 21 unmodified lines
2222 #include <QtScript/QScriptable>
2323
2424 #include <SettingHelpers.h> // for ::settingsFilename()
25−#include <SharedUtil.h> // for usecTimestampNow()
25+#include <SharedUtil.h> // for ::usecTimestampNow()
2626 #include <shared/ScriptInitializerMixin.h>
2727
2828 // 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
150150 * @example <caption>access persistent settings stored in separate .json files</caption>
151151 * var settings = JSAPIExample.getScopedSettings("example");
152152 * print("example settings stored in:", settings.fileName());
153− * print("(before) example::timestamp", settings.value("timestamp"));
153+ * print("(before) example::timestamp", settings.getValue("timestamp"));
154154 * settings.setValue("timestamp", Date.now());
155− * print("(after) example::timestamp", settings.value("timestamp"));
155+ * print("(after) example::timestamp", settings.getValue("timestamp"));
156156 * print("all example::* keys", settings.allKeys());
157157 * settings = null; // optional best pratice; allows the object to be reclaimed ASAP by the JS garbage collector
158158 */
@@ -173,44 +173,24 @@ private: · 14 unmodified lines
173173 const QString _version{ JSAPI_SEMANTIC_VERSION };
174174 };
175175
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
181177 class JSSettingsHelper : public QObject {
182178 Q_OBJECT
183− QString _scope;
184− QString _fileName;
185− QSharedPointer<QSettings> _settings;
186−
187179 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;
193183 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;
208189 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;
214194 };
215195
216196 // 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
223203 return new JSSettingsHelper(scope, parent);
224204 }
225205
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+
226248 } // namespace REPLACE_ME_WITH_UNIQUE_NAME
227249
228250 #include "JSAPIExample.moc"