|
@@ -121,4 +121,75 @@ public default constructor (already defined above), a public copy constructor,
|
121
|
121
|
and a public destructor. These latter two are default generated.
|
122
|
122
|
|
123
|
123
|
@<Register meta-types@>=
|
124
|
|
-qRegisterMetaType<Measurement>("Measurement");
|
|
124
|
+qRegisterMetaType<Measurement>("Measurement");
|
|
125
|
+
|
|
126
|
+@ A little more is required to use |Measurement| objects in scripts.
|
|
127
|
+
|
|
128
|
+@<Class declarations@>=
|
|
129
|
+Q_DECLARE_METATYPE(Measurement)
|
|
130
|
+
|
|
131
|
+@ The only thing unusual here is the conversion to and from script values.
|
|
132
|
+
|
|
133
|
+@<Function prototypes for scripting@>=
|
|
134
|
+QScriptValue constructMeasurement(QScriptContext *context, QScriptEngine *engine);
|
|
135
|
+void setMeasurementProperties(QScriptValue value, QScriptEngine *engine);
|
|
136
|
+QScriptValue Measurement_toScriptValue(QScriptEngine *engine, const Measurement &measurement);
|
|
137
|
+void Measurement_fromScriptValue(const QScriptValue &value, Measurement &measurement);
|
|
138
|
+
|
|
139
|
+@ This follows much the same pattern as other types not derived from |QObject|.
|
|
140
|
+
|
|
141
|
+@<Set up the scripting engine@>=
|
|
142
|
+constructor = engine->newFunction(constructMeasurement);
|
|
143
|
+engine->globalObject().setProperty("Measurement", constructor);
|
|
144
|
+qScriptRegisterMetaType(engine, Measurement_toScriptValue, Measurement_fromScriptValue);
|
|
145
|
+
|
|
146
|
+@ The constructor takes two or three arguments. If the third arguement is not
|
|
147
|
+supplied, we will assume that the measurements are in degrees Fahrenheit.
|
|
148
|
+
|
|
149
|
+@<Functions for scripting@>=
|
|
150
|
+QScriptValue constructMeasurement(QScriptContext *context, QScriptEngine *engine)
|
|
151
|
+{
|
|
152
|
+ QScriptValue object;
|
|
153
|
+ if(context->argumentCount() == 2 || context->argumentCount() == 3)
|
|
154
|
+ {
|
|
155
|
+ double measurement = argument<double>(0, context);
|
|
156
|
+ QTime timestamp = argument<QTime>(1, context);
|
|
157
|
+ Units::Unit unit = Units::Fahrenheit;
|
|
158
|
+ if(context->argumentCount() == 3)
|
|
159
|
+ {
|
|
160
|
+ unit = argument<Units::Unit>(2, context);
|
|
161
|
+ }
|
|
162
|
+ object = engine->toScriptValue<Measurement>(Measurement(measurement, timestamp, unit));
|
|
163
|
+ setMeasurementProperties(object, engine);
|
|
164
|
+ }
|
|
165
|
+ else
|
|
166
|
+ {
|
|
167
|
+ context->throwError("Incorrect number of arguments passed to "@|
|
|
168
|
+ "Measurement::Measurement(). This method takes two "@|
|
|
169
|
+ "or three arguments.");
|
|
170
|
+ }
|
|
171
|
+ return object;
|
|
172
|
+}
|
|
173
|
+
|
|
174
|
+@ No additional properties are currently needed, but if they were, they would go here.
|
|
175
|
+
|
|
176
|
+@<Functions for scripting@>=
|
|
177
|
+void setMeasurementProperties(QScriptValue, QScriptEngine *)
|
|
178
|
+{
|
|
179
|
+ /* Nothing needs to be done here. */
|
|
180
|
+}
|
|
181
|
+
|
|
182
|
+@ The script value conversions are reasonably straightforward.
|
|
183
|
+
|
|
184
|
+@<Functions for scripting@>=
|
|
185
|
+QScriptValue Measurement_toScriptValue(QScriptEngine *engine, const Measurement &measurement)
|
|
186
|
+{
|
|
187
|
+ QVariant var;
|
|
188
|
+ var.setValue(measurement);
|
|
189
|
+ return engine->newVariant(var);
|
|
190
|
+}
|
|
191
|
+
|
|
192
|
+void Measurement_fromScriptValue(const QScriptValue &value, Measurement &measurement)
|
|
193
|
+{
|
|
194
|
+ measurement = value.toVariant().value<Measurement>();
|
|
195
|
+}
|