Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

measurement.w 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. @** Measurement representation.
  2. \noindent Most of the information in a roast log will be measurements of
  3. various sorts. These could be temperature measurements, control settings,
  4. data derived from other data series, and so on. There are three key elements to
  5. a measurement: the value of the measurement, the unit that the measurement was
  6. taken in, and the time the measurement was collected.
  7. Starting in Typica 1.6 the |Measurement| class implementation was changed so
  8. that every |Measurement| is a |QVariantMap|. Additional methods are provided on
  9. top of this map so that this change is transparent to code that has already
  10. been written against the old class, but over time these methods will be
  11. depreciated and code can work directly with the |QVariantMap| interface to
  12. allow |Measurement| objects to carry much more information.
  13. As |QVariantMap| is implicitly shared and |Measurement| adds no new data
  14. members we no longer require copy and assignment operators.
  15. @<Class declarations@>=
  16. class Measurement : public QVariantMap@/
  17. {@t\1@>@/
  18. public:@/
  19. Measurement(double temperature = 0, QTime time = QTime(),@|
  20. Units::Unit sc = Units::Fahrenheit);
  21. Measurement(double temperature);
  22. double temperature() const;
  23. QTime time() const;
  24. void setTemperature(double temperature);
  25. void setTime(QTime time);
  26. void setUnit(Units::Unit scale);
  27. Units::Unit scale() const;
  28. Measurement toFahrenheit();
  29. Measurement toCelsius();
  30. Measurement toKelvin();
  31. Measurement toRankine();@t\2@>@/
  32. };
  33. @ Time, measured value, and unit are stored at well known keys.
  34. @<Measurement implementation@>=
  35. Measurement::Measurement(double temperature, QTime time, Units::Unit sc)
  36. {
  37. insert("measurement", temperature);
  38. insert("time", time);
  39. insert("unit", sc);
  40. }
  41. Measurement::Measurement(double temperature)
  42. {
  43. insert("measurement", temperature);
  44. insert("time", QTime::currentTime());
  45. insert("unit", Units::Fahrenheit);
  46. }
  47. void Measurement::setTemperature(double temperature)
  48. {
  49. insert("measurement", temperature);
  50. }
  51. void Measurement::setTime(QTime time)
  52. {
  53. insert("time", time);
  54. }
  55. void Measurement::setUnit(Units::Unit scale)
  56. {
  57. insert("unit", scale);
  58. }
  59. double Measurement::temperature() const
  60. {
  61. return value("measurement").toDouble();
  62. }
  63. QTime Measurement::time() const
  64. {
  65. return value("time").toTime();
  66. }
  67. Units::Unit Measurement::scale() const
  68. {
  69. return (Units::Unit)(value("unit").toInt());
  70. }
  71. @ Unit conversion methods will likely be the first things to be depreciated as
  72. the functionality is fully available from the |Units| class.
  73. @<Measurement implementation@>=
  74. Measurement Measurement::toFahrenheit()
  75. {
  76. return Measurement(Units::convertTemperature(this->temperature(),
  77. this->scale(), Units::Fahrenheit),
  78. this->time(), Units::Fahrenheit);
  79. }
  80. Measurement Measurement::toCelsius()
  81. {
  82. return Measurement(Units::convertTemperature(this->temperature(),
  83. this->scale(), Units::Celsius),
  84. this->time(), Units::Celsius);
  85. }
  86. Measurement Measurement::toKelvin()
  87. {
  88. return Measurement(Units::convertTemperature(this->temperature(),
  89. this->scale(), Units::Kelvin),
  90. this->time(), Units::Kelvin);
  91. }
  92. Measurement Measurement::toRankine()
  93. {
  94. return Measurement(Units::convertTemperature(this->temperature(),
  95. this->scale(), Units::Rankine),
  96. this->time(), Units::Rankine);
  97. }
  98. @ In the future we will be able to drop the |Measurement| class entirely and
  99. just pass around |QVariantMap| but until then we need to be able to pass these
  100. objects through the signals and slots mechanism. This requires that we have a
  101. public default constructor (already defined above), a public copy constructor,
  102. and a public destructor. These latter two are default generated.
  103. @<Register meta-types@>=
  104. qRegisterMetaType<Measurement>("Measurement");
  105. @ A little more is required to use |Measurement| objects in scripts.
  106. @<Class declarations@>=
  107. Q_DECLARE_METATYPE(Measurement)
  108. @ The only thing unusual here is the conversion to and from script values.
  109. @<Function prototypes for scripting@>=
  110. QScriptValue constructMeasurement(QScriptContext *context, QScriptEngine *engine);
  111. void setMeasurementProperties(QScriptValue value, QScriptEngine *engine);
  112. QScriptValue Measurement_toScriptValue(QScriptEngine *engine, const Measurement &measurement);
  113. void Measurement_fromScriptValue(const QScriptValue &value, Measurement &measurement);
  114. @ This follows much the same pattern as other types not derived from |QObject|.
  115. @<Set up the scripting engine@>=
  116. constructor = engine->newFunction(constructMeasurement);
  117. engine->globalObject().setProperty("Measurement", constructor);
  118. qScriptRegisterMetaType(engine, Measurement_toScriptValue, Measurement_fromScriptValue);
  119. @ The constructor takes two or three arguments. If the third arguement is not
  120. supplied, we will assume that the measurements are in degrees Fahrenheit.
  121. @<Functions for scripting@>=
  122. QScriptValue constructMeasurement(QScriptContext *context, QScriptEngine *engine)
  123. {
  124. QScriptValue object;
  125. if(context->argumentCount() == 2 || context->argumentCount() == 3)
  126. {
  127. double measurement = argument<double>(0, context);
  128. QTime timestamp = argument<QTime>(1, context);
  129. Units::Unit unit = Units::Fahrenheit;
  130. if(context->argumentCount() == 3)
  131. {
  132. unit = argument<Units::Unit>(2, context);
  133. }
  134. object = engine->toScriptValue<Measurement>(Measurement(measurement, timestamp, unit));
  135. setMeasurementProperties(object, engine);
  136. }
  137. else
  138. {
  139. context->throwError("Incorrect number of arguments passed to "@|
  140. "Measurement::Measurement(). This method takes two "@|
  141. "or three arguments.");
  142. }
  143. return object;
  144. }
  145. @ No additional properties are currently needed, but if they were, they would go here.
  146. @<Functions for scripting@>=
  147. void setMeasurementProperties(QScriptValue, QScriptEngine *)
  148. {
  149. /* Nothing needs to be done here. */
  150. }
  151. @ The script value conversions are reasonably straightforward.
  152. @<Functions for scripting@>=
  153. QScriptValue Measurement_toScriptValue(QScriptEngine *engine, const Measurement &measurement)
  154. {
  155. QVariant var;
  156. var.setValue(measurement);
  157. return engine->newVariant(var);
  158. }
  159. void Measurement_fromScriptValue(const QScriptValue &value, Measurement &measurement)
  160. {
  161. measurement = value.toVariant().value<Measurement>();
  162. }