changes per CR

This commit is contained in:
humbletim 2020-10-31 14:11:05 -04:00
parent dedf6a6975
commit 5c2a8bd459

View file

@ -28,12 +28,12 @@
// NOTE: replace this with your own namespace when starting a new plugin (to avoid .so/.dll symbol clashes)
namespace REPLACE_ME_WITH_UNIQUE_NAME {
static constexpr auto JSAPI_SEMANTIC_VERSION = "0.0.1";
static constexpr auto JSAPI_EXPORT_NAME = "JSAPIExample";
static constexpr auto JSAPI_SEMANTIC_VERSION = "0.0.1";
static constexpr auto JSAPI_EXPORT_NAME = "JSAPIExample";
QLoggingCategory logger{ "jsapiexample" };
QLoggingCategory logger { "jsapiexample" };
inline QVariant raiseScriptingError(QScriptContext* context, const QString& message, const QVariant& returnValue = QVariant()) {
inline QVariant raiseScriptingError(QScriptContext* context, const QString& message, const QVariant& returnValue = QVariant()) {
if (context) {
// when a QScriptContext is available throw an actual JS Exception (which can be caught using try/catch on JS side)
context->throwError(message);
@ -42,15 +42,15 @@ inline QVariant raiseScriptingError(QScriptContext* context, const QString& mess
qCWarning(logger) << "error:" << message;
}
return returnValue;
}
}
QObject* createScopedSettings(const QString& scope, QObject* parent, QString& error);
QObject* createScopedSettings(const QString& scope, QObject* parent, QString& error);
class JSAPIExample : public QObject, public QScriptable {
class JSAPIExample : public QObject, public QScriptable {
Q_OBJECT
Q_PLUGIN_METADATA(IID "JSAPIExample" FILE "plugin.json")
Q_PROPERTY(QString version MEMBER _version CONSTANT)
public:
public:
JSAPIExample() {
setObjectName(JSAPI_EXPORT_NAME);
auto scriptInit = DependencyManager::get<ScriptInitializers>();
@ -72,7 +72,7 @@ public:
// which still makes them very Qt-specific, but avoids depending directly on deprecated QtScript/QScriptValue APIs.
// (as such this plugin class and its methods remain forward-compatible with other engines like QML's QJSEngine)
public slots:
public slots:
// pretty-printed representation for logging eg: print(JSAPIExample)
// (note: Qt script engines automatically look for a ".toString" method on native classes when coercing values to strings)
QString toString() const { return QString("[%1 version=%2]").arg(objectName()).arg(_version); }
@ -158,8 +158,9 @@ public slots:
*/
QScriptValue getScopedSettings(const QString& scope) {
auto engine = QScriptable::engine();
if (!engine)
if (!engine) {
return QScriptValue::NullValue;
}
QString error;
auto cppValue = createScopedSettings(scope, engine, error);
if (!cppValue) {
@ -169,62 +170,62 @@ public slots:
return engine->newQObject(cppValue, QScriptEngine::ScriptOwnership, QScriptEngine::ExcludeDeleteLater);
}
private:
const QString _version{ JSAPI_SEMANTIC_VERSION };
};
private:
const QString _version { JSAPI_SEMANTIC_VERSION };
};
// JSSettingsHelper wraps a scoped (prefixed/separate) QSettings and exposes a subset of QSetting APIs as slots
class JSSettingsHelper : public QObject {
// JSSettingsHelper wraps a scoped (prefixed/separate) QSettings and exposes a subset of QSetting APIs as slots
class JSSettingsHelper : public QObject {
Q_OBJECT
public:
public:
JSSettingsHelper(const QString& scope, QObject* parent = nullptr);
~JSSettingsHelper();
operator bool() const;
public slots:
public slots:
QString fileName() const;
QString toString() const;
QVariant getValue(const QString& key, const QVariant& defaultValue = QVariant());
bool setValue(const QString& key, const QVariant& value);
QStringList allKeys() const;
protected:
protected:
QString _scope;
QString _fileName;
QSharedPointer<QSettings> _settings;
QString getLocalSettingsPath(const QString& scope) const;
};
};
// verifies the requested scope is sensible and creates/returns a scoped JSSettingsHelper instance
QObject* createScopedSettings(const QString& scope, QObject* parent, QString& error) {
const QRegExp VALID_SETTINGS_SCOPE{ "[-_A-Za-z0-9]{1,64}" };
// verifies the requested scope is sensible and creates/returns a scoped JSSettingsHelper instance
QObject* createScopedSettings(const QString& scope, QObject* parent, QString& error) {
const QRegExp VALID_SETTINGS_SCOPE { "[-_A-Za-z0-9]{1,64}" };
if (!VALID_SETTINGS_SCOPE.exactMatch(scope)) {
error = QString("invalid scope (expected alphanumeric <= 64 chars not '%1')").arg(scope);
return nullptr;
}
return new JSSettingsHelper(scope, parent);
}
}
// --------------------------------------------------
// ----- inline JSSettingsHelper implementation -----
JSSettingsHelper::operator bool() const {
// --------------------------------------------------
// ----- inline JSSettingsHelper implementation -----
JSSettingsHelper::operator bool() const {
return (bool)_settings;
}
JSSettingsHelper::JSSettingsHelper(const QString& scope, QObject* parent) :
}
JSSettingsHelper::JSSettingsHelper(const QString& scope, QObject* parent) :
QObject(parent), _scope(scope), _fileName(getLocalSettingsPath(scope)),
_settings(_fileName.isEmpty() ? nullptr : new QSettings(_fileName, JSON_FORMAT)) {
}
JSSettingsHelper::~JSSettingsHelper() {
}
JSSettingsHelper::~JSSettingsHelper() {
qCDebug(logger) << "~JSSettingsHelper" << _scope << _fileName << this;
}
QString JSSettingsHelper::fileName() const {
}
QString JSSettingsHelper::fileName() const {
return _settings ? _settings->fileName() : "";
}
QString JSSettingsHelper::toString() const {
}
QString JSSettingsHelper::toString() const {
return QString("[JSSettingsHelper scope=%1 valid=%2]").arg(_scope).arg((bool)_settings);
}
QVariant JSSettingsHelper::getValue(const QString& key, const QVariant& defaultValue) {
}
QVariant JSSettingsHelper::getValue(const QString& key, const QVariant& defaultValue) {
return _settings ? _settings->value(key, defaultValue) : defaultValue;
}
bool JSSettingsHelper::setValue(const QString& key, const QVariant& value) {
}
bool JSSettingsHelper::setValue(const QString& key, const QVariant& value) {
if (_settings) {
if (value.isValid()) {
_settings->setValue(key, value);
@ -234,16 +235,16 @@ bool JSSettingsHelper::setValue(const QString& key, const QVariant& value) {
return true;
}
return false;
}
QStringList JSSettingsHelper::allKeys() const {
}
QStringList JSSettingsHelper::allKeys() const {
return _settings ? _settings->allKeys() : QStringList{};
}
QString JSSettingsHelper::getLocalSettingsPath(const QString& scope) const {
}
QString JSSettingsHelper::getLocalSettingsPath(const QString& scope) const {
// generate a prefixed filename (relative to the main application's Interface.json file)
const QString fileName = QString("jsapi_%1.json").arg(scope);
return QFileInfo(::settingsFilename()).dir().filePath(fileName);
}
// ----- /inline JSSettingsHelper implementation -----
}
// ----- /inline JSSettingsHelper implementation -----
} // namespace REPLACE_ME_WITH_UNIQUE_NAME