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.

units.w 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. @* Unit information.
  2. \noindent As unit information is required in several places, the enumeration
  3. listing these is moved to an external class. This derives from QObject only to
  4. take advantage of the meta-object system. There are no data members, however
  5. there are some convenience methods for working with unit data.
  6. @(units.h@>=
  7. #include <QObject>
  8. #ifndef TypicaUnitsIncluded
  9. #define TypicaUnitsIncluded
  10. class Units: public QObject@/
  11. {@/
  12. @[Q_OBJECT@]@;
  13. @[Q_ENUMS(Unit)@]@;
  14. public:@/
  15. enum Unit@/
  16. {@/
  17. Unitless = 0,
  18. Fahrenheit = 10144,
  19. Celsius = 10143,
  20. Kelvin = 10325,
  21. Rankine = 10145,
  22. Pound = 15876,
  23. Kilogram = 15877,
  24. Ounce = 1,
  25. Gram = 2
  26. };@/
  27. static double convertTemperature(double value, Unit fromUnit, Unit toUnit);
  28. static double convertRelativeTemperature(double value, Unit fromUnit, Unit toUnit);
  29. static bool isTemperatureUnit(Unit unit);
  30. static double convertWeight(double value, Unit fromUnit, Unit toUnit);
  31. static bool isWeightUnit(Unit unit);@/
  32. };
  33. #endif
  34. @ Methods are implemented in a separate file.
  35. @(units.cpp@>=
  36. #include "units.h"
  37. @ The |isTemperatureUnit()| method may seem counter-intuitive while the enum
  38. only contains represenations of temperature measurements, but there are plans
  39. to extend this later to extend hardware support to devices that do not directly
  40. measure temperature values and also to support the measurement of properties
  41. that cannot sensibly be represented as temperatures such as fuel pressure and
  42. control settings.
  43. @(units.cpp@>=
  44. bool Units::isTemperatureUnit(Unit unit)
  45. {
  46. return (unit == Fahrenheit ||
  47. unit == Celsius ||
  48. unit == Kelvin ||
  49. unit == Rankine);
  50. }
  51. @ Temperature conversions can be performed by the |Units| class and should
  52. eliminate the need to have conversion code in multiple places. This method
  53. takes the measurement value, the unit that value is in, and the unit that we
  54. would like to have the measurement converted into. It returns the value as it
  55. would be expressed after unit conversion. A value of 0 is presently returned
  56. if either or both of the units to convert are not temperature units, however
  57. that behavior may change in the future. Any code activating that branch should
  58. be considered flawed.
  59. @(units.cpp@>=
  60. double Units::convertTemperature(double value, Unit fromUnit, Unit toUnit)@/
  61. {
  62. if(!(isTemperatureUnit(fromUnit) && isTemperatureUnit(toUnit)))
  63. {
  64. return 0;
  65. }
  66. switch(fromUnit)@/
  67. {@t\1@>@/
  68. case Fahrenheit:@/
  69. switch(toUnit)@/
  70. {@t\1@>@/
  71. case Fahrenheit:@/
  72. return value;
  73. break;
  74. case Celsius:@/
  75. return (value - 32.0) * 5.0 / 9.0;
  76. break;
  77. case Kelvin:@/
  78. return (value + 459.67) * 5.0 / 9.0;
  79. break;
  80. case Rankine:@/
  81. return value + 459.67;
  82. break;@t\2@>@/
  83. }@/
  84. break;
  85. case Celsius:
  86. switch(toUnit)@/
  87. {@t\1@>@/
  88. case Fahrenheit:@/
  89. return value * 9.0 / 5.0 + 32.0;
  90. break;
  91. case Celsius:@/
  92. return value;
  93. break;
  94. case Kelvin:@/
  95. return value + 273.15;
  96. break;
  97. case Rankine:@/
  98. return (value + 273.15) * 9.0 / 5.0;
  99. break;@t\2@>@/
  100. }@/
  101. break;
  102. case Kelvin:
  103. switch(toUnit)@/
  104. {@t\1@>@/
  105. case Fahrenheit:@/
  106. return value * 5.0 / 9.0 - 459.67;
  107. break;
  108. case Celsius:@/
  109. return value - 273.15;
  110. break;
  111. case Kelvin:@/
  112. return value;
  113. break;
  114. case Rankine:@/
  115. return value * 9.0 / 5.0;
  116. break;@t\2@>@/
  117. }@/
  118. break;
  119. case Rankine:
  120. switch(toUnit)@/
  121. {@t\1@>@/
  122. case Fahrenheit:@/
  123. return value - 457.67;
  124. break;
  125. case Celsius:@/
  126. return (value - 491.67) * 5.0 / 9.0;
  127. break;
  128. case Kelvin:@/
  129. return value * 5.0 / 9.0;
  130. break;
  131. case Rankine:@/
  132. return value;
  133. break;@t\2@>@/
  134. }@/
  135. break;
  136. default:@/
  137. return 0;
  138. break;@t\2@>@/
  139. }
  140. return 0;
  141. }
  142. @ Conversions are also provided for relative temperature measurements.
  143. @(units.cpp@>=
  144. double Units::convertRelativeTemperature(double value, Unit fromUnit, Unit toUnit)
  145. {
  146. if(!(isTemperatureUnit(fromUnit) && isTemperatureUnit(toUnit)))@/
  147. {
  148. return 0;
  149. }
  150. switch(fromUnit)@/
  151. {@t\1@>@/
  152. case Fahrenheit:
  153. switch(toUnit)@/
  154. {@t\1@>@/
  155. case Fahrenheit:@/
  156. return value;
  157. break;
  158. case Celsius:@/
  159. return value * (5.0 / 9.0);
  160. break;
  161. case Kelvin:@/
  162. return value * (5.0 / 9.0);
  163. break;
  164. case Rankine:@/
  165. return value;
  166. break;
  167. default:@/
  168. return 0;
  169. break;@t\2@>@/
  170. }
  171. break;
  172. case Celsius:
  173. switch(toUnit)@/
  174. {@t\1@>@/
  175. case Fahrenheit:@/
  176. return value * (9.0 / 5.0);
  177. break;
  178. case Celsius:@/
  179. return value;
  180. break;
  181. case Kelvin:@/
  182. return value;
  183. break;
  184. case Rankine:@/
  185. return value * (9.0 / 5.0);
  186. break;
  187. default:@/
  188. return 0;
  189. break;@t\2@>@/
  190. }
  191. break;
  192. case Kelvin:
  193. switch(toUnit)@/
  194. {@t\1@>@/
  195. case Fahrenheit:@/
  196. return value * (5.0 / 9.0);
  197. break;
  198. case Celsius:@/
  199. return value;
  200. break;
  201. case Kelvin:@/
  202. return value;
  203. break;
  204. case Rankine:@/
  205. return value * (9.0 / 5.0);
  206. break;
  207. default:@/
  208. return 0;
  209. break;@t\2@>@/
  210. }
  211. break;
  212. case Rankine:
  213. switch(toUnit)@/
  214. {@t\1@>@/
  215. case Fahrenheit:@/
  216. return value;
  217. break;
  218. case Celsius:@/
  219. return value * (5.0 / 9.0);
  220. break;
  221. case Kelvin:@/
  222. return value * (5.0 / 9.0);
  223. break;
  224. case Rankine:@/
  225. return value;
  226. break;
  227. default:
  228. return 0;
  229. break;@t\2@>@/
  230. }
  231. break;
  232. default:@/
  233. return 0;
  234. break;
  235. }
  236. return 0;
  237. }
  238. @ Units of force are handled similarly to units of temperature.
  239. @(units.cpp@>=
  240. double Units::convertWeight(double value, Unit fromUnit, Unit toUnit)
  241. {
  242. if(isWeightUnit(fromUnit) && isWeightUnit(toUnit))@/
  243. {
  244. switch(fromUnit)@/
  245. {@t\1@>@/
  246. case Pound:
  247. switch(toUnit)@/
  248. {@t\1@>@/
  249. case Pound:@/
  250. return value;
  251. break;
  252. case Kilogram:@/
  253. return value / 2.2;
  254. break;
  255. case Ounce:@/
  256. return value * 16.0;
  257. break;
  258. case Gram:@/
  259. return value / 0.0022;
  260. break;
  261. default:@/
  262. return 0;
  263. break;@t\2@>@/
  264. }
  265. break;
  266. case Kilogram:
  267. switch(toUnit)@/
  268. {@t\1@>@/
  269. case Pound:@/
  270. return value * 2.2;
  271. break;
  272. case Kilogram:@/
  273. return value;
  274. break;
  275. case Ounce:@/
  276. return value * 35.2;
  277. break;
  278. case Gram:@/
  279. return value * 1000.0;
  280. break;
  281. default:@/
  282. return 0;
  283. break;@t\2@>@/
  284. }
  285. break;
  286. case Ounce:
  287. switch(toUnit)@/
  288. {@t\1@>@/
  289. case Pound:@/
  290. return value / 16.0;
  291. break;
  292. case Kilogram:@/
  293. return value / 35.2;
  294. break;
  295. case Ounce:@/
  296. return value;
  297. break;
  298. case Gram:@/
  299. return value / 0.0352;
  300. break;
  301. default:@/
  302. return 0;
  303. break;@t\2@>@/
  304. }
  305. break;
  306. case Gram:
  307. switch(toUnit)@/
  308. {@t\1@>@/
  309. case Pound:@/
  310. return value * 0.0022;
  311. break;
  312. case Kilogram:@/
  313. return value / 1000.0;
  314. break;
  315. case Ounce:@/
  316. return value * 0.0352;
  317. break;
  318. case Gram:@/
  319. return value;
  320. break;
  321. default:@/
  322. return 0;
  323. break;@t\2@>@/
  324. }
  325. break;
  326. default:@/
  327. return 0;
  328. break;@t\2@>@/
  329. }
  330. }
  331. return 0;
  332. }
  333. bool Units::isWeightUnit(Unit unit)
  334. {
  335. return (unit == Pound ||
  336. unit == Kilogram ||
  337. unit == Ounce ||
  338. unit == Gram);
  339. }
  340. @ This class is exposed to the host environment. Note the lack of constructor.
  341. We do not wish to create any instances, just have access to the |Unit|
  342. enumeration and conversion methods.
  343. Unfortunately, marking a static method |Q_INVOKABLE| will not work as per Qt
  344. bug \#18840. There seems to be no intention to correct this deficiency so
  345. instead of having something that just works, we must resort to the following
  346. hackery.
  347. @<Function prototypes for scripting@>=
  348. QScriptValue Units_convertTemperature(QScriptContext *context, QScriptEngine *engine);
  349. QScriptValue Units_convertRelativeTemperature(QScriptContext *context,
  350. QScriptEngine *engine);
  351. QScriptValue Units_isTemperatureUnit(QScriptContext *context, QScriptEngine *engine);
  352. QScriptValue Units_convertWeight(QScriptContext *context, QScriptEngine *engine);
  353. QScriptValue Units_isWeightUnit(QScriptContext *context, QScriptEngine *engine);
  354. @ These functions are added as properties for the host environment.
  355. @<Set up the scripting engine@>=
  356. value = engine->newQMetaObject(&Units::staticMetaObject);
  357. value.setProperty("convertTemperature", engine->newFunction(Units_convertTemperature));
  358. value.setProperty("convertRelativeTemperature",
  359. engine->newFunction(Units_convertRelativeTemperature));
  360. value.setProperty("isTemperatureUnit", engine->newFunction(Units_isTemperatureUnit));
  361. value.setProperty("convertWeight", engine->newFunction(Units_convertWeight));
  362. value.setProperty("isWeightUnit", engine->newFunction(Units_isWeightUnit));
  363. engine->globalObject().setProperty("Units", value);
  364. @ The implementation of these functions is trivial.
  365. @<Functions for scripting@>=
  366. QScriptValue Units_convertTemperature(QScriptContext *context, QScriptEngine *)
  367. {
  368. return QScriptValue(Units::convertTemperature(argument<double>(0, context),
  369. argument<Units::Unit>(1, context),
  370. argument<Units::Unit>(2, context)));
  371. }
  372. QScriptValue Units_convertRelativeTemperature(QScriptContext *context,
  373. QScriptEngine *)
  374. {
  375. return QScriptValue(Units::convertRelativeTemperature(
  376. argument<double>(0, context),
  377. argument<Units::Unit>(1, context),
  378. argument<Units::Unit>(2, context)));
  379. }
  380. QScriptValue Units_isTemperatureUnit(QScriptContext *context, QScriptEngine *)
  381. {
  382. return QScriptValue(Units::isTemperatureUnit(argument<Units::Unit>(0, context)));
  383. }
  384. QScriptValue Units_convertWeight(QScriptContext *context, QScriptEngine *)
  385. {
  386. return QScriptValue(Units::convertWeight(argument<double>(0, context),
  387. argument<Units::Unit>(1, context),
  388. argument<Units::Unit>(2, context)));
  389. }
  390. QScriptValue Units_isWeightUnit(QScriptContext *context, QScriptEngine *)
  391. {
  392. return QScriptValue(Units::isWeightUnit(argument<Units::Unit>(0, context)));
  393. }
  394. @ To pass unit data in some circumstances, the inner enumeration must be
  395. registered as a meta-type with script conversions.
  396. @<Class declarations@>=
  397. Q_DECLARE_METATYPE(Units::Unit)
  398. @ A pair of conversion methods is required.
  399. @<Function prototypes for scripting@>=
  400. QScriptValue Unit_toScriptValue(QScriptEngine *engine, const Units::Unit &value);
  401. void Unit_fromScriptValue(const QScriptValue &sv, Units::Unit &value);
  402. @ These are implemented thusly.
  403. @<Functions for scripting@>=
  404. QScriptValue Unit_toScriptValue(QScriptEngine *engine, const Units::Unit &value)
  405. {
  406. return engine->newVariant(QVariant(value));
  407. }
  408. void Unit_fromScriptValue(const QScriptValue &sv, Units::Unit &value)
  409. {
  410. value = sv.toVariant().value<Units::Unit>();
  411. }
  412. @ These conversion functions are registered.
  413. @<Set up the scripting engine@>=
  414. qScriptRegisterMetaType(engine, Unit_toScriptValue, Unit_fromScriptValue);