Typica is a free program for professional coffee roasters. https://typica.us
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

units.w 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. };
  23. static double convertTemperature(double value, Unit fromUnit, Unit toUnit);
  24. static bool isTemperatureUnit(Unit unit);
  25. };
  26. #endif
  27. @ Methods are implemented in a separate file.
  28. @(units.cpp@>=
  29. #include "units.h"
  30. #ifndef Q_OS_WIN32
  31. #include "moc_units.cpp"
  32. #endif
  33. @ The |isTemperatureUnit()| method may seem counter-intuitive while the enum
  34. only contains represenations of temperature measurements, but there are plans
  35. to extend this later to extend hardware support to devices that do not directly
  36. measure temperature values and also to support the measurement of properties
  37. that cannot sensibly be represented as temperatures such as fuel pressure and
  38. control settings.
  39. @(units.cpp@>=
  40. bool Units::isTemperatureUnit(Unit unit)
  41. {
  42. if(unit == Fahrenheit ||
  43. unit == Celsius ||
  44. unit == Kelvin ||
  45. unit == Rankine)
  46. {
  47. return true;
  48. }
  49. return false;
  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) == false)
  63. {
  64. return 0;
  65. }
  66. switch(fromUnit)
  67. {
  68. case Fahrenheit:
  69. switch(toUnit)
  70. {
  71. case Fahrenheit:
  72. return value;
  73. break;
  74. case Celsius:
  75. return (value - 32) * 5 / 9;
  76. break;
  77. case Kelvin:
  78. return (value + 459.67) * 5 / 9;
  79. break;
  80. case Rankine:
  81. return value + 459.67;
  82. break;
  83. }
  84. break;
  85. case Celsius:
  86. switch(toUnit)
  87. {
  88. case Fahrenheit:
  89. return value * 9 / 5 + 32;
  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 / 5;
  99. break;
  100. }
  101. break;
  102. case Kelvin:
  103. switch(toUnit)
  104. {
  105. case Fahrenheit:
  106. return value * 5 / 9 - 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 / 5;
  116. break;
  117. }
  118. break;
  119. case Rankine:
  120. switch(toUnit)
  121. {
  122. case Fahrenheit:
  123. return value - 457.67;
  124. break;
  125. case Celsius:
  126. return (value - 491.67) * 5 / 9;
  127. break;
  128. case Kelvin:
  129. return value * 5 / 9;
  130. break;
  131. case Rankine:
  132. return value;
  133. break;
  134. }
  135. break;
  136. default:
  137. return 0;
  138. break;
  139. }
  140. return 0;
  141. }
  142. @ This class is exposed to the host environment. Note the lack of constructor.
  143. We do not wish to create any instances, just have access to the |Unit|
  144. enumeration.
  145. @<Set up the scripting engine@>=
  146. value = engine->newQMetaObject(&Units::staticMetaObject);
  147. engine->globalObject().setProperty("Units", value);