History

clarifications per peer review

humbletim committed 6 years ago

Commit 7fb01731 parent f54b1c51 file · +20 −10Browse files

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

Unified · Split

plugins/JSAPIExample/src/JSAPIExample.cpp+20−10
@@ -73,8 +73,9 @@ public: · 72 unmodified lines
73 // (as such this plugin class and its methods remain forward-compatible with other engines like QML's QJSEngine)73 // (as such this plugin class and its methods remain forward-compatible with other engines like QML's QJSEngine)
74 74
75public slots:75public slots:
76 // returns a pretty-printed representation for logging eg: print(JSAPIExample)76 // pretty-printed representation for logging eg: print(JSAPIExample)
77 inline QString toString() const { return QString("[%1 version=%2]").arg(objectName()).arg(_version); }77 // (note: Qt script engines automatically look for a ".toString" method on native classes when coercing values to strings)
78 QString toString() const { return QString("[%1 version=%2]").arg(objectName()).arg(_version); }
78 79
79 /**jsdoc80 /**jsdoc
80 * Returns current microseconds (usecs) since Epoch. note: 1000usecs == 1ms81 * Returns current microseconds (usecs) since Epoch. note: 1000usecs == 1ms
@@ -100,12 +101,16 @@ public slots: · 19 unmodified lines
100 }101 }
101 return out;102 return out;
102 }103 }
104
103 /**jsdoc105 /**jsdoc
104 * Example of returning a JS Array result106 * Example of returning a JS Array result
105 * @example <caption>emulate Object.values(keyValues)</caption>107 * @example <caption>emulate Object.values(keyValues)</caption>
106 * print(JSON.stringify(JSAPIExample.values({ "a": 1, "b": 2 }))); // [1,2]108 * print(JSON.stringify(JSAPIExample.values({ "a": 1, "b": 2 }))); // [1,2]
107 */109 */
108 QVariant values(const QVariantMap& keyValues) const { return keyValues.values(); }110 QVariant values(const QVariantMap& keyValues) const {
111 QVariantList values = keyValues.values();
112 return values;
113 }
109 114
110 /**jsdoc115 /**jsdoc
111 * Another example of returning JS Array data116 * Another example of returning JS Array data
@@ -129,10 +134,14 @@ public slots: · 17 unmodified lines
129 * var u = JSAPIExample.qUncompressString(z); // u will be a String value134 * var u = JSAPIExample.qUncompressString(z); // u will be a String value
130 * print(JSON.stringify({ input: data, compressed: z.byteLength, output: u, uncompressed: u.length }));135 * print(JSON.stringify({ input: data, compressed: z.byteLength, output: u, uncompressed: u.length }));
131 */136 */
132 QVariant qCompressString(const QString& data, int compress_level = -1) const {137 QVariant qCompressString(const QString& jsString, int compress_level = -1) const {
133 return qCompress(data.toUtf8(), compress_level);138 QByteArray arrayBuffer = qCompress(jsString.toUtf8(), compress_level);
139 return arrayBuffer;
140 }
141 QVariant qUncompressString(const QByteArray& arrayBuffer) const {
142 QString jsString = QString::fromUtf8(qUncompress(arrayBuffer));
143 return jsString;
134 }144 }
135 QString qUncompressString(const QByteArray& data) const { return QString::fromUtf8(qUncompress(data)); }
136 145
137 /**146 /**
138 * Example of exposing a custom "managed" C++ QObject to JS147 * Example of exposing a custom "managed" C++ QObject to JS
@@ -149,12 +158,13 @@ public slots: · 10 unmodified lines
149 */158 */
150 QScriptValue getScopedSettings(const QString& scope) {159 QScriptValue getScopedSettings(const QString& scope) {
151 auto engine = QScriptable::engine();160 auto engine = QScriptable::engine();
152 if (!engine) return QScriptValue::NullValue;161 if (!engine)
162 return QScriptValue::NullValue;
153 QString error;163 QString error;
154 auto cppValue = createScopedSettings(scope, engine, error);164 auto cppValue = createScopedSettings(scope, engine, error);
155 if (!cppValue) {165 if (!cppValue) {
156 raiseScriptingError(context(), "error creating scoped settings instance: " + error);166 raiseScriptingError(context(), "error creating scoped settings instance: " + error);
157 return QScriptValue::NullValue;167 return QScriptValue::NullValue;
158 }168 }
159 return engine->newQObject(cppValue, QScriptEngine::ScriptOwnership, QScriptEngine::ExcludeDeleteLater);169 return engine->newQObject(cppValue, QScriptEngine::ScriptOwnership, QScriptEngine::ExcludeDeleteLater);
160 }170 }
@@ -163,7 +173,6 @@ private: · 2 unmodified lines
163 const QString _version{ JSAPI_SEMANTIC_VERSION };173 const QString _version{ JSAPI_SEMANTIC_VERSION };
164};174};
165 175
166// Example of how to create a QObject class that can have multiple instances created from the JS side
167// JSSettingsHelper emulates a subset of QSetting APIs:176// JSSettingsHelper emulates a subset of QSetting APIs:
168// fileName() -- full path to the scoped settings .json file177// fileName() -- full path to the scoped settings .json file
169// allKeys() -- all previously stored keys available in the scoped settings file178// allKeys() -- all previously stored keys available in the scoped settings file
@@ -204,6 +213,7 @@ protected: · 34 unmodified lines
204 }213 }
205};214};
206 215
216// verifies the requested scope is sensible and creates/returns a scoped JSSettingsHelper instance
207QObject* createScopedSettings(const QString& scope, QObject* parent, QString& error) {217QObject* createScopedSettings(const QString& scope, QObject* parent, QString& error) {
208 const QRegExp VALID_SETTINGS_SCOPE{ "[-_A-Za-z0-9]{1,64}" };218 const QRegExp VALID_SETTINGS_SCOPE{ "[-_A-Za-z0-9]{1,64}" };
209 if (!VALID_SETTINGS_SCOPE.exactMatch(scope)) {219 if (!VALID_SETTINGS_SCOPE.exactMatch(scope)) {