Selaa lähdekoodia

Work in progress. Do not mess with this branch.

Neal Wilson 11 vuotta sitten
vanhempi
commit
cfdb23e557
3 muutettua tiedostoa jossa 111 lisäystä ja 197 poistoa
  1. 33
    31
      src/Typica.pro
  2. 65
    160
      src/typica.w
  3. 13
    6
      src/units.w

+ 33
- 31
src/Typica.pro Näytä tiedosto

@@ -1,31 +1,33 @@
1
-######################################################################
2
-# Automatically generated by qmake (2.01a) Sat May 23 23:41:13 2009
3
-######################################################################
4
-
5
-QT += script
6
-QT += xml
7
-QT += sql
8
-QT += xmlpatterns
9
-QT += scripttools
10
-QT += webkit
11
-
12
-include(3rdparty/qextserialport/src/qextserialport.pri)
13
-
14
-TEMPLATE = app
15
-TARGET =
16
-DEPENDPATH += .
17
-
18
-# Input
19
-HEADERS += moc_typica.cpp \
20
-    helpmenu.h \
21
-    abouttypica.h
22
-SOURCES += typica.cpp \
23
-    helpmenu.cpp \
24
-    abouttypica.cpp
25
-
26
-RESOURCES += \
27
-    resources.qrc
28
-
29
-RC_FILE = typica.rc
30
-ICON = resources/icons/appicons/logo.icns
31
-QMAKE_INFO_PLIST = resources/Info.plist
1
+######################################################################
2
+# Automatically generated by qmake (2.01a) Sat May 23 23:41:13 2009
3
+######################################################################
4
+
5
+QT += script
6
+QT += xml
7
+QT += sql
8
+QT += xmlpatterns
9
+QT += scripttools
10
+QT += webkit
11
+
12
+include(3rdparty/qextserialport/src/qextserialport.pri)
13
+
14
+TEMPLATE = app
15
+TARGET =
16
+DEPENDPATH += .
17
+
18
+# Input
19
+HEADERS += moc_typica.cpp \
20
+    helpmenu.h \
21
+    abouttypica.h \
22
+    units.h
23
+SOURCES += typica.cpp \
24
+    helpmenu.cpp \
25
+    abouttypica.cpp \
26
+    units.cpp
27
+
28
+RESOURCES += \
29
+    resources.qrc
30
+
31
+RC_FILE = typica.rc
32
+ICON = resources/icons/appicons/logo.icns
33
+QMAKE_INFO_PLIST = resources/Info.plist

+ 65
- 160
src/typica.w Näytä tiedosto

@@ -5705,6 +5705,11 @@ Measurement times are represented as instances of |QTime|.
5705 5705
 
5706 5706
 @i units.w
5707 5707
 
5708
+@ We will require the |units.h| header.
5709
+
5710
+@<Header files to include@>=
5711
+#include "units.h"
5712
+
5708 5713
 @ The declaration of |Measurement| is reasonably straightforward. Measurements
5709 5714
 will often be passed around to a number of different objects through the signals
5710 5715
 and slots mechanism of Qt. The simplest way to make this work is to provide a
@@ -5718,21 +5723,9 @@ This can almost certainly be handled better.
5718 5723
 @<Class declarations@>=
5719 5724
 class Measurement@/
5720 5725
 {
5721
-	public:@/
5722
-		enum TemperatureUnits
5723
-		{
5724
-			Fahrenheit = 10144,
5725
-			Celsius = 10143,
5726
-			Kelvin = 10325,
5727
-			Rankine = 10145
5728
-		};
5729
-	private:@/
5730
-		double theTemperature;
5731
-		QTime theTime;
5732
-		TemperatureUnits unit;
5733 5726
 	public:@;
5734 5727
 		Measurement(double temperature = 0, QTime time = QTime(),@|
5735
-		            TemperatureUnits sc = Fahrenheit);
5728
+		            Units::Unit sc = Units::Fahrenheit);
5736 5729
 		Measurement(double temperature);
5737 5730
 		Measurement(const Measurement &x);
5738 5731
 		Measurement& operator=(Measurement &x);
@@ -5741,12 +5734,16 @@ class Measurement@/
5741 5734
 		QTime time() const;
5742 5735
 		void setTemperature(double temperature);
5743 5736
 		void setTime(QTime time);
5744
-		void setUnit(TemperatureUnits scale);
5745
-		TemperatureUnits scale();
5737
+		void setUnit(Units::Unit scale);
5738
+		Units::Unit scale();
5746 5739
 		Measurement toFahrenheit();
5747 5740
 		Measurement toCelsius();
5748 5741
 		Measurement toKelvin();
5749 5742
 		Measurement toRankine();
5743
+	private:@/
5744
+		double theTemperature;
5745
+		QTime theTime;
5746
+		Units::Unit unit;
5750 5747
 };
5751 5748
 
5752 5749
 @ There are two constructors that can create |Measurement| objects. The
@@ -5757,7 +5754,7 @@ current time.
5757 5754
 
5758 5755
 @<Measurement implementation@>=
5759 5756
 Measurement::Measurement(double temperature, QTime time,
5760
-                         TemperatureUnits sc) :@;
5757
+                         Units::Unit sc) :@;
5761 5758
 	@t\kern2em@>theTemperature(temperature), theTime(time), unit(sc)@;
5762 5759
 {
5763 5760
 	/* Nothing has to be done here. */
@@ -5765,7 +5762,7 @@ Measurement::Measurement(double temperature, QTime time,
5765 5762
 
5766 5763
 Measurement::Measurement(double temperature) :@;
5767 5764
 	@t\kern2em@>theTemperature(temperature), theTime(QTime::currentTime()),
5768
-	unit(Fahrenheit)@;
5765
+	unit(Units::Fahrenheit)@;
5769 5766
 {
5770 5767
 	/* Nothing has to be done here. */
5771 5768
 }@;
@@ -5840,122 +5837,47 @@ unit to another, use the appropriate |to[Fahrenheit||Celsius||Kelvin||Rankine]|
5840 5837
 method.
5841 5838
 
5842 5839
 @<Measurement implementation@>=
5843
-void Measurement::setUnit(TemperatureUnits scale)
5840
+void Measurement::setUnit(Units::Unit scale)
5844 5841
 {
5845 5842
 	unit = scale;
5846 5843
 }
5847 5844
 
5848
-Measurement::TemperatureUnits Measurement::scale()
5845
+Units::Unit Measurement::scale()
5849 5846
 {
5850 5847
 	return unit;
5851 5848
 }
5852 5849
 
5853 5850
 @ Four methods create a new |Measurement| from the object the method is called
5854
-on representing the same measurement expressed in different units. The
5855
-conversion formul\ae{} come from Wikipedia.\nfnote{%
5856
-\pdfURL{Temperature Conversion}{%
5857
-http://en.wikipedia.org/%
5858
-w/index.php?title=Temperature_conversion&oldid=226047163}}
5859
-
5860
-The |toFahrenheit()| method converts a measurement to Fahrenheit.
5851
+on representing the same measurement expressed in different units. The value
5852
+conversions are handled in the |Units| class.
5861 5853
 
5862 5854
 @<Measurement implementation@>=
5863 5855
 Measurement Measurement::toFahrenheit()
5864 5856
 {
5865
-	switch(unit)
5866
-	{
5867
-		case Celsius:
5868
-			return Measurement(this->temperature() * 9 / 5 + 32, this->time(),
5869
-			                   Fahrenheit);
5870
-			break;
5871
-		case Kelvin:
5872
-			return Measurement(this->temperature() * 5 / 9 - 459.67,
5873
-			                   this->time(), Fahrenheit);
5874
-			break;
5875
-		case Rankine:
5876
-			return Measurement(this->temperature() - 459.67, this->time(),
5877
-			                   Fahrenheit);
5878
-			break;
5879
-		default:
5880
-			return Measurement(this->temperature(), this->time(), Fahrenheit);
5881
-			break;
5882
-	}
5857
+	return Measurement(Units::convertTemperature(this->temperature(),
5858
+	                                             this->unit, Units::Fahrenheit),
5859
+	                   this->time(), Units::Fahrenheit);
5883 5860
 }
5884 5861
 
5885
-@ Similarly, the following converts to Celsius.
5886
-
5887
-@<Measurement implementation@>=
5888 5862
 Measurement Measurement::toCelsius()
5889 5863
 {
5890
-	switch(unit)
5891
-	{
5892
-		case Fahrenheit:
5893
-			return Measurement((this->temperature() - 32) * 5 / 9, this->time(),
5894
-			                   Celsius);
5895
-			break;
5896
-		case Kelvin:
5897
-			return Measurement(this->temperature() - 273.15, this->time(),
5898
-			                   Celsius);
5899
-			break;
5900
-		case Rankine:
5901
-			return Measurement((this->temperature() - 491.67) * 5 / 9,
5902
-			                   this->time(), Celsius);
5903
-			break;
5904
-		default:
5905
-			return Measurement(this->temperature(), this->time(), Celsius);
5906
-			break;
5907
-	}
5864
+	return Measurement(Units::convertTemperature(this->temperature(),
5865
+	                                             this->unit, Units::Celsius),
5866
+	                   this->time(), Units::Celsius);
5908 5867
 }
5909 5868
 
5910
-@ For those who prefer absolute scales, a method is provided to convert to
5911
-Kelvin.
5912
-
5913
-@<Measurement implementation@>=
5914 5869
 Measurement Measurement::toKelvin()
5915 5870
 {
5916
-	switch(unit)
5917
-	{
5918
-		case Fahrenheit:
5919
-			return Measurement((this->temperature() + 459.67) * 5 / 9,
5920
-			                   this->time(), Kelvin);
5921
-			break;
5922
-		case Celsius:
5923
-			return Measurement(this->temperature() + 273.15, this->time(),
5924
-			                   Kelvin);
5925
-			break;
5926
-		case Rankine:
5927
-			return Measurement(this->temperature() * 5 / 9, this->time(),
5928
-			                   Kelvin);
5929
-			break;
5930
-		default:
5931
-			return Measurement(this->temperature(), this->time(), Kelvin);
5932
-			break;
5933
-	}
5871
+	return Measurement(Units::convertTemperature(this->temperature(),
5872
+	                                             this->unit, Units::Kelvin),
5873
+	                   this->time(), Units::Kelvin);
5934 5874
 }
5935 5875
 
5936
-@ Finally, conversion to Rankine is also allowed.
5937
-
5938
-@<Measurement implementation@>=
5939 5876
 Measurement Measurement::toRankine()
5940 5877
 {
5941
-	switch(unit)
5942
-	{
5943
-		case Fahrenheit:
5944
-			return Measurement(this->temperature() + 459.67, this->time(),
5945
-			                   Rankine);
5946
-			break;
5947
-		case Celsius:
5948
-			return Measurement((this->temperature() + 273.15) * 9 / 5,
5949
-			                   this->time(), Rankine);
5950
-			break;
5951
-		case Kelvin:
5952
-			return Measurement(this->temperature() * 9 / 5, this->time(),
5953
-			                   Rankine);
5954
-			break;
5955
-		default:
5956
-			return Measurement(this->temperature(), this->time(), Rankine);
5957
-			break;
5958
-	}
5878
+	return Measurement(Units::convertTemperature(this->temperature(),
5879
+	                                             this->unit, Units::Rankine),
5880
+	                   this->time(), Units::Rankine);
5959 5881
 }
5960 5882
 
5961 5883
 @** The Main Measurement Pipeline.
@@ -6024,7 +5946,6 @@ class DAQImplementation;@/
6024 5946
 class DAQ : public QObject@;@/
6025 5947
 {@t\1@>@/
6026 5948
 	Q_OBJECT@/
6027
-	Q_ENUMS(TemperatureUnits)@/
6028 5949
 	Q_ENUMS(ThermocoupleType)@;@/
6029 5950
 	DAQImplementation *imp;@/
6030 5951
 	@t\4@>private slots@t\kern-3pt@>:@/
@@ -6036,13 +5957,6 @@ class DAQ : public QObject@;@/
6036 5957
 		@[Q_INVOKABLE@,@, void@]@, setClockRate(double Hz);@t\2\2@>@/
6037 5958
 		@[Q_INVOKABLE@,@, void@]@, start();@t\2\2@>@/
6038 5959
 		@[Q_INVOKABLE@]@, void stop();
6039
-		enum TemperatureUnits@/
6040
-		{
6041
-			@!Fahrenheit = 10144,
6042
-			@!Celsius = 10143,
6043
-			@!Kelvin = 10325,
6044
-			@!Rankine = 10145
6045
-		};
6046 5960
 		enum ThermocoupleType@/
6047 5961
 		{
6048 5962
 			@!TypeJ = 10072,
@@ -6108,7 +6022,7 @@ int error;
6108 6022
 int channels;
6109 6023
 bool ready;
6110 6024
 QLibrary driver;
6111
-QVector<Measurement::TemperatureUnits> unitMap;
6025
+QVector<Units::Unit> unitMap;
6112 6026
 
6113 6027
 @ Most of the interesting work associated with the |DAQ| class is handled in
6114 6028
 the |measure()| method of |DAQImplementation|. This function will block until a
@@ -6331,7 +6245,7 @@ Channel* DAQ::newChannel(int units, int thermocouple)
6331 6245
 {
6332 6246
 	Channel *retval = new Channel();
6333 6247
 	imp->channelMap[imp->channels] = retval;
6334
-	imp->unitMap[imp->channels] = (Measurement::TemperatureUnits)units;
6248
+	imp->unitMap[imp->channels] = (Units::Unit)units;
6335 6249
 	imp->channels++;
6336 6250
 	if(imp->ready)
6337 6251
 	{
@@ -7179,23 +7093,14 @@ This is a specialization of |QLCDNumber|.
7179 7093
 class TemperatureDisplay : public QLCDNumber@/
7180 7094
 {@t\1@>@/
7181 7095
 	Q_OBJECT@;
7182
-	Q_ENUMS(DisplayUnits)
7183 7096
 	int unit;
7184 7097
 	public:@/
7185
-		enum DisplayUnits
7186
-		{
7187
-			Auto = -1,
7188
-			Fahrenheit = 10144,
7189
-			Celsius = 10143,
7190
-			Kelvin = 10325,
7191
-			Rankine = 10145
7192
-		};
7193 7098
 		TemperatureDisplay(QWidget *parent = NULL);
7194 7099
 		~TemperatureDisplay();@/
7195 7100
 	@t\4@>public slots@t\kern-3pt@>:@/
7196 7101
 		void setValue(Measurement temperature);
7197 7102
 		void invalidate();
7198
-		void setDisplayUnits(DisplayUnits scale);@t\2@>@/
7103
+		void setDisplayUnits(Units::Unit scale);@t\2@>@/
7199 7104
 };
7200 7105
 
7201 7106
 @ Displaying a temperature is a simple matter of taking the temperature
@@ -7215,47 +7120,47 @@ void TemperatureDisplay::setValue(Measurement temperature)
7215 7120
 	QString number;
7216 7121
 	switch(unit)
7217 7122
 	{
7218
-		case Auto:
7219
-			switch(temperature.scale())
7220
-			{
7221
-				case Fahrenheit:
7222
-					display(QString("%1'F").
7223
-						arg(number.setNum(temperature.temperature(), 'f', 2)));
7224
-					break;
7225
-				case Celsius:
7226
-					display(QString("%1'C").
7227
-						arg(number.setNum(temperature.temperature(), 'f', 2)));
7228
-					break;
7229
-				case Kelvin:
7230
-					display(QString("%1").
7231
-						arg(number.setNum(temperature.temperature(), 'f', 2)));
7232
-					break;
7233
-				case Rankine:
7234
-					display(QString("%1'r").
7235
-						arg(number.setNum(temperature.temperature(), 'f', 2)));
7236
-					break;
7237
-			}
7238
-			break;
7239
-		case Fahrenheit:
7123
+		case Units::Fahrenheit:
7240 7124
 			display(QString("%1'F").
7241 7125
 				arg(number.setNum(temperature.toFahrenheit().temperature(), 'f',
7242 7126
 				                  2)));
7243 7127
 			break;
7244
-		case Celsius:
7128
+		case Units::Celsius:
7245 7129
 			display(QString("%1'C").
7246 7130
 				arg(number.setNum(temperature.toCelsius().temperature(), 'f',
7247 7131
 				                  2)));
7248 7132
 			break;
7249
-		case Kelvin:
7133
+		case Units::Kelvin:
7250 7134
 			display(QString("%1").
7251 7135
 				arg(number.setNum(temperature.toKelvin().temperature(), 'f',
7252 7136
 				                  2)));
7253 7137
 			break;
7254
-		case Rankine:
7138
+		case Units::Rankine:
7255 7139
 			display(QString("%1'r").
7256 7140
 				arg(number.setNum(temperature.toRankine().temperature(), 'f',
7257 7141
 				                  2)));
7258 7142
 			break;
7143
+		default:
7144
+			switch(temperature.scale())
7145
+			{
7146
+				case Units::Fahrenheit:
7147
+					display(QString("%1'F").
7148
+						arg(number.setNum(temperature.temperature(), 'f', 2)));
7149
+					break;
7150
+				case Units::Celsius:
7151
+					display(QString("%1'C").
7152
+						arg(number.setNum(temperature.temperature(), 'f', 2)));
7153
+					break;
7154
+				case Units::Kelvin:
7155
+					display(QString("%1").
7156
+						arg(number.setNum(temperature.temperature(), 'f', 2)));
7157
+					break;
7158
+				case Units::Rankine:
7159
+					display(QString("%1'r").
7160
+						arg(number.setNum(temperature.temperature(), 'f', 2)));
7161
+					break;
7162
+			}
7163
+			break;
7259 7164
 	}
7260 7165
 }
7261 7166
 
@@ -7271,7 +7176,7 @@ the usual |QLCDNumber| methods.
7271 7176
 
7272 7177
 @<TemperatureDisplay Implementation@>=
7273 7178
 TemperatureDisplay::TemperatureDisplay(QWidget *parent) :
7274
-	QLCDNumber(8, parent), unit(Auto)@/
7179
+	QLCDNumber(8, parent), unit(Units::Fahrenheit)@/
7275 7180
 {
7276 7181
 	setSegmentStyle(Filled);
7277 7182
 	display("---.--'F");
@@ -7298,7 +7203,7 @@ the scale to a different supported scale and convert measurements to that scale
7298 7203
 prior to display.
7299 7204
 
7300 7205
 @<TemperatureDisplay Implementation@>=
7301
-void TemperatureDisplay::setDisplayUnits(DisplayUnits scale)
7206
+void TemperatureDisplay::setDisplayUnits(Units::Unit scale)
7302 7207
 {
7303 7208
 	unit = scale;
7304 7209
 }
@@ -17306,8 +17211,8 @@ if(!unitIsF)
17306 17211
 	pvOut = pvOut * 9 / 5 + 32;
17307 17212
 	svOut = svOut * 9 / 5 + 32;
17308 17213
 }
17309
-Measurement pvm(pvOut, time, Measurement::Fahrenheit);
17310
-Measurement svm(svOut, time, Measurement::Fahrenheit);
17214
+Measurement pvm(pvOut, time, Units::Fahrenheit);
17215
+Measurement svm(svOut, time, Units::Fahrenheit);
17311 17216
 channels.at(0)->input(pvm);
17312 17217
 channels.at(1)->input(svm);
17313 17218
 
@@ -17331,15 +17236,15 @@ if(!unitIsF)
17331 17236
 }
17332 17237
 if(!svenabled)
17333 17238
 {
17334
-	Measurement vm(valueOut, time, Measurement::Fahrenheit);
17239
+	Measurement vm(valueOut, time, Units::Fahrenheit);
17335 17240
 	channels.at(0)->input(vm);
17336 17241
 }
17337 17242
 else
17338 17243
 {
17339 17244
 	if(readingsv)
17340 17245
 	{
17341
-		Measurement pvm(savedpv, time, Measurement::Fahrenheit);
17342
-		Measurement svm(valueOut, time, Measurement::Fahrenheit);
17246
+		Measurement pvm(savedpv, time, Units::Fahrenheit);
17247
+		Measurement svm(valueOut, time, Units::Fahrenheit);
17343 17248
 		channels.at(0)->input(pvm);
17344 17249
 		channels.at(1)->input(svm);
17345 17250
 		readingsv = false;

+ 13
- 6
src/units.w Näytä tiedosto

@@ -6,26 +6,33 @@ take advantage of the meta-object system. There are no data members, however
6 6
 there are some convenience methods for working with unit data.
7 7
 
8 8
 @(units.h@>=
9
+#include <QObject>
10
+
11
+#ifndef TypicaUnitsIncluded
12
+#define TypicaUnitsIncluded
13
+
9 14
 class Units: public QObject
10 15
 {
11 16
 	Q_OBJECT
12
-	Q_ENUMS(Units)
17
+	Q_ENUMS(Unit)
13 18
 	public:
14
-		enum Units
19
+		enum Unit
15 20
 		{
16 21
 			Fahrenheit = 10144,
17 22
 			Celsius = 10143,
18 23
 			Kelvin = 10325,
19 24
 			Rankine = 10145
20 25
 		};
21
-		double convertTemperature(double value, Unit fromUnit, Unit toUnit);
22
-		bool isTemperatureUnit(Unit unit);
23
-}
26
+		static double convertTemperature(double value, Unit fromUnit, Unit toUnit);
27
+		static bool isTemperatureUnit(Unit unit);
28
+};
29
+
30
+#endif
24 31
 
25 32
 @ Methods are implemented in a separate file.
26 33
 
27 34
 @(units.cpp@>=
28
-#inculde "units.h"
35
+#include "units.h"
29 36
 #include "moc_units.cpp"
30 37
 
31 38
 @ The |isTemperatureUnit()| method may seem counter-intuitive while the enum

Loading…
Peruuta
Tallenna