Parcourir la source

static methods in Units now invokable

Neal Wilson il y a 11 ans
Parent
révision
a7113ee1ed
2 fichiers modifiés avec 71 ajouts et 3 suppressions
  1. 10
    0
      src/typica.w
  2. 61
    3
      src/units.w

+ 10
- 0
src/typica.w Voir le fichier

@@ -976,6 +976,16 @@ template<> QModelIndex argument(int arg, QScriptContext *context)
976 976
 	return qscriptvalue_cast<QModelIndex>(context->argument(arg));
977 977
 }
978 978
 
979
+template<> double argument(int arg, QScriptContext *context)
980
+{
981
+	return (double)(context->argument(arg).toNumber());
982
+}
983
+
984
+template<> Units::Unit argument(int arg, QScriptContext *context)
985
+{
986
+	return (Units::Unit)(context->argument(arg).toInt32());
987
+}
988
+
979 989
 @ The scripting engine is informed of a number of classes defined elsewhere in
980 990
 the program. Code related to scripting these classes is grouped with the code
981 991
 implementing the classes. Additionally, there are several classes from Qt which

+ 61
- 3
src/units.w Voir le fichier

@@ -364,14 +364,71 @@ bool Units::isWeightUnit(Unit unit)
364 364
 
365 365
 @ This class is exposed to the host environment. Note the lack of constructor.
366 366
 We do not wish to create any instances, just have access to the |Unit|
367
-enumeration.
367
+enumeration and conversion methods.
368
+
369
+Unfortunately, marking a static method |Q_INVOKABLE| will not work as per Qt
370
+bug #18840. There seems to be no intention to correct this deficiency so
371
+instead of having something that just works, we must resort to the following
372
+hackery.
373
+
374
+@<Function prototypes for scripting@>=
375
+QScriptValue Units_convertTemperature(QScriptContext *context, QScriptEngine *engine);
376
+QScriptValue Units_convertRelativeTemperature(QScriptContext *context,
377
+                                              QScriptEngine *engine);
378
+QScriptValue Units_isTemperatureUnit(QScriptContext *context, QScriptEngine *engine);
379
+QScriptValue Units_convertWeight(QScriptContext *context, QScriptEngine *engine);
380
+QScriptValue Units_isWeightUnit(QScriptContext *context, QScriptEngine *engine);
381
+
382
+@ These functions are added as properties for the host environment.
368 383
 
369 384
 @<Set up the scripting engine@>=
370 385
 value = engine->newQMetaObject(&Units::staticMetaObject);
386
+value.setProperty("convertTemperature", engine->newFunction(Units_convertTemperature));
387
+value.setProperty("convertRelativeTemperature",
388
+                  engine->newFunction(Units_convertRelativeTemperature));
389
+value.setProperty("isTemperatureUnit", engine->newFunction(Units_isTemperatureUnit));
390
+value.setProperty("convertWeight", engine->newFunction(Units_convertWeight));
391
+value.setProperty("isWeightUnit", engine->newFunction(Units_isWeightUnit));
371 392
 engine->globalObject().setProperty("Units", value);
372 393
 
373
-@ While the above is sufficient for some uses, others require some additional
374
-work.
394
+@ The implementation of these functions is trivial.
395
+
396
+@<Functions for scripting@>=
397
+QScriptValue Units_convertTemperature(QScriptContext *context, QScriptEngine *engine)
398
+{
399
+	return QScriptValue(Units::convertTemperature(argument<double>(0, context),
400
+	                                              argument<Units::Unit>(1, context),
401
+	                                              argument<Units::Unit>(2, context)));
402
+}
403
+
404
+QScriptValue Units_convertRelativeTemperature(QScriptContext *context,
405
+                                              QScriptEngine *engine)
406
+{
407
+	return QScriptValue(Units::convertRelativeTemperature(
408
+	                         argument<double>(0, context),
409
+	                         argument<Units::Unit>(1, context),
410
+	                         argument<Units::Unit>(2, context)));
411
+}
412
+
413
+QScriptValue Units_isTemperatureUnit(QScriptContext *context, QScriptEngine *engine)
414
+{
415
+	return QScriptValue(Units::isTemperatureUnit(argument<Units::Unit>(0, context)));
416
+}
417
+
418
+QScriptValue Units_convertWeight(QScriptContext *context, QScriptEngine *engine)
419
+{
420
+	return QScriptValue(Units::convertWeight(argument<double>(0, context),
421
+	                                         argument<Units::Unit>(1, context),
422
+	                                         argument<Units::Unit>(2, context)));
423
+}
424
+
425
+QScriptValue Units_isWeightUnit(QScriptContext *context, QScriptEngine *engine)
426
+{
427
+	return QScriptValue(Units::isWeightUnit(argument<Units::Unit>(0, context)));
428
+}
429
+
430
+@ To pass unit data in some circumstances, the inner enumeration must be
431
+registered as a meta-type with script conversions.
375 432
 
376 433
 @<Class declarations@>=
377 434
 Q_DECLARE_METATYPE(Units::Unit)
@@ -399,3 +456,4 @@ void Unit_fromScriptValue(const QScriptValue &sv, Units::Unit &value)
399 456
 
400 457
 @<Set up the scripting engine@>=
401 458
 qScriptRegisterMetaType(engine, Unit_toScriptValue, Unit_fromScriptValue);
459
+

Chargement…
Annuler
Enregistrer