Browse Source

static methods in Units now invokable

Neal Wilson 11 years ago
parent
commit
a7113ee1ed
2 changed files with 71 additions and 3 deletions
  1. 10
    0
      src/typica.w
  2. 61
    3
      src/units.w

+ 10
- 0
src/typica.w View File

976
 	return qscriptvalue_cast<QModelIndex>(context->argument(arg));
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
 @ The scripting engine is informed of a number of classes defined elsewhere in
989
 @ The scripting engine is informed of a number of classes defined elsewhere in
980
 the program. Code related to scripting these classes is grouped with the code
990
 the program. Code related to scripting these classes is grouped with the code
981
 implementing the classes. Additionally, there are several classes from Qt which
991
 implementing the classes. Additionally, there are several classes from Qt which

+ 61
- 3
src/units.w View File

364
 
364
 
365
 @ This class is exposed to the host environment. Note the lack of constructor.
365
 @ This class is exposed to the host environment. Note the lack of constructor.
366
 We do not wish to create any instances, just have access to the |Unit|
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
 @<Set up the scripting engine@>=
384
 @<Set up the scripting engine@>=
370
 value = engine->newQMetaObject(&Units::staticMetaObject);
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
 engine->globalObject().setProperty("Units", value);
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
 @<Class declarations@>=
433
 @<Class declarations@>=
377
 Q_DECLARE_METATYPE(Units::Unit)
434
 Q_DECLARE_METATYPE(Units::Unit)
399
 
456
 
400
 @<Set up the scripting engine@>=
457
 @<Set up the scripting engine@>=
401
 qScriptRegisterMetaType(engine, Unit_toScriptValue, Unit_fromScriptValue);
458
 qScriptRegisterMetaType(engine, Unit_toScriptValue, Unit_fromScriptValue);
459
+

Loading…
Cancel
Save