Typica is a free program for professional coffee roasters. https://typica.us
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

units.w 11KB

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