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
7373 // (as such this plugin class and its methods remain forward-compatible with other engines like QML's QJSEngine)
7474
7575 public slots:
76− // returns a pretty-printed representation for logging eg: print(JSAPIExample)
77− inline QString toString() const { return QString("[%1 version=%2]").arg(objectName()).arg(_version); }
76+ // pretty-printed representation for logging eg: print(JSAPIExample)
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); }
7879
7980 /**jsdoc
8081 * Returns current microseconds (usecs) since Epoch. note: 1000usecs == 1ms
@@ -100,12 +101,16 @@ public slots: · 19 unmodified lines
100101 }
101102 return out;
102103 }
104+
103105 /**jsdoc
104106 * Example of returning a JS Array result
105107 * @example <caption>emulate Object.values(keyValues)</caption>
106108 * print(JSON.stringify(JSAPIExample.values({ "a": 1, "b": 2 }))); // [1,2]
107109 */
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+ }
109114
110115 /**jsdoc
111116 * Another example of returning JS Array data
@@ -129,10 +134,14 @@ public slots: · 17 unmodified lines
129134 * var u = JSAPIExample.qUncompressString(z); // u will be a String value
130135 * print(JSON.stringify({ input: data, compressed: z.byteLength, output: u, uncompressed: u.length }));
131136 */
132− QVariant qCompressString(const QString& data, int compress_level = -1) const {
133− return qCompress(data.toUtf8(), compress_level);
137+ QVariant qCompressString(const QString& jsString, int compress_level = -1) const {
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;
134144 }
135− QString qUncompressString(const QByteArray& data) const { return QString::fromUtf8(qUncompress(data)); }
136145
137146 /**
138147 * Example of exposing a custom "managed" C++ QObject to JS
@@ -149,12 +158,13 @@ public slots: · 10 unmodified lines
149158 */
150159 QScriptValue getScopedSettings(const QString& scope) {
151160 auto engine = QScriptable::engine();
152− if (!engine) return QScriptValue::NullValue;
161+ if (!engine)
162+ return QScriptValue::NullValue;
153163 QString error;
154164 auto cppValue = createScopedSettings(scope, engine, error);
155165 if (!cppValue) {
156− raiseScriptingError(context(), "error creating scoped settings instance: " + error);
157− return QScriptValue::NullValue;
166+ raiseScriptingError(context(), "error creating scoped settings instance: " + error);
167+ return QScriptValue::NullValue;
158168 }
159169 return engine->newQObject(cppValue, QScriptEngine::ScriptOwnership, QScriptEngine::ExcludeDeleteLater);
160170 }
@@ -163,7 +173,6 @@ private: · 2 unmodified lines
163173 const QString _version{ JSAPI_SEMANTIC_VERSION };
164174 };
165175
166−// Example of how to create a QObject class that can have multiple instances created from the JS side
167176 // JSSettingsHelper emulates a subset of QSetting APIs:
168177 // fileName() -- full path to the scoped settings .json file
169178 // allKeys() -- all previously stored keys available in the scoped settings file
@@ -204,6 +213,7 @@ protected: · 34 unmodified lines
204213 }
205214 };
206215
216+// verifies the requested scope is sensible and creates/returns a scoped JSSettingsHelper instance
207217 QObject* createScopedSettings(const QString& scope, QObject* parent, QString& error) {
208218 const QRegExp VALID_SETTINGS_SCOPE{ "[-_A-Za-z0-9]{1,64}" };
209219 if (!VALID_SETTINGS_SCOPE.exactMatch(scope)) {