swift
samplesphysicalquantities.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
6 
8 
9 #include <QString>
10 #include <QTextStream>
11 
12 #include "misc/aviation/altitude.h"
13 #include "misc/pq/acceleration.h"
14 #include "misc/pq/angle.h"
15 #include "misc/pq/frequency.h"
16 #include "misc/pq/length.h"
17 #include "misc/pq/mass.h"
19 #include "misc/pq/pressure.h"
20 #include "misc/pq/speed.h"
21 #include "misc/pq/temperature.h"
22 #include "misc/pq/time.h"
23 #include "misc/pq/units.h"
24 #include "misc/stringutils.h"
25 
26 using namespace swift::misc;
27 using namespace swift::misc::aviation;
28 using namespace swift::misc::physical_quantities;
29 
30 namespace swift::sample
31 {
32  int CSamplesPhysicalQuantities::samples(QTextStream &out)
33  {
34  // standard tests
35  CLengthUnit lu1(CLengthUnit::cm());
36  CLengthUnit lu2(CLengthUnit::ft());
37  QString lu1s = lu1.toQString(true);
38  QString lu2s = lu2.toQString(true);
39  out << "units: " << lu1 << " " << lu2 << " " << lu1s << " " << lu2s << " " << lu1.getName(true) << " "
40  << lu2.getName(true) << Qt::endl;
41  const CLength l1(5.0, CLengthUnit::ft()); // 5 ft
42  CLength l2(1, CLengthUnit::NM()); // 1NM
43  CLength l3(1, CLengthUnit::km());
44  CLength l4(l3);
45 
46  out << CLengthUnit::ft() << Qt::endl;
47  out << l1 << " " << l2 << " " << l3 << " " << l4 << Qt::endl;
48  out << l1.valueRoundedWithUnit(CLengthUnit::ft(), 5) << " " << l2.valueRoundedWithUnit(CLengthUnit::km())
49  << Qt::endl;
50  out << l3.getUnit() << Qt::endl;
51 
52  l2.switchUnit(CLengthUnit::ft()); // now in ft
53  l3 += l3; // 2km now
54  l3 *= 1.5; // 3km now
55  out << l2 << " " << l3 << Qt::endl;
56 
57  l3 = l3 * 2;
58  out << "doubled l3: " << l3 << Qt::endl;
59 
60  // null test
61  CLength nullLength(0, CLengthUnit::nullUnit());
62  out << "Null PQ: " << nullLength << " converted " << nullLength.valueRoundedWithUnit(CLengthUnit::m(), 2)
63  << Qt::endl;
64 
65  // more tests
66  CFrequency f1(1E6, CFrequencyUnit::Hz()); // 1MHz
67  out << f1 << " " << f1.valueRoundedWithUnit(CFrequencyUnit::MHz()) << " "
68  << f1.valueRoundedWithUnit(CFrequencyUnit::GHz(), 3);
69 
70  CSpeed s1 = CSpeed(100, CSpeedUnit::km_h());
71  CSpeed s2 = CSpeed(1000, CSpeedUnit::ft_min());
72  CSpeed s3 = CSpeed(s2);
73  s3.switchUnit(CSpeedUnit::m_s());
74  out << s1 << " " << s1.valueRoundedWithUnit(CSpeedUnit::defaultUnit()) << " "
75  << s1.valueRoundedWithUnit(CSpeedUnit::NM_h());
76  out << s2 << " " << s3 << Qt::endl;
77 
78  CAngle a1(180, CAngleUnit::deg());
79  CAngle a2(1.5 * CAngle::PI(), CAngleUnit::rad());
80  CAngle a3(180.5, CAngleUnit::deg());
81  CAngle a4(35.4336, CAngleUnit::sexagesimalDeg()); // 35.72666
82  a1 += a2;
83  // a1 = d2; // must not work
84  out << a1;
85  a1.switchUnit(CAngleUnit::deg());
86  // a2 += d1; // must not work
87  a2 = a1 + a1;
88 
89  a2.switchUnit(CAngleUnit::deg());
90  out << a1.valueRoundedWithUnit() << " " << a1.piFactor() << Qt::endl;
91  out << a2 << Qt::endl;
92  a3.switchUnit(CAngleUnit::sexagesimalDeg());
93  a4.switchUnit(CAngleUnit::deg());
94  out << a3 << " " << a4 << Qt::endl;
95 
96  CMass w1(1, CMassUnit::tonne());
97  CMass w2(w1);
98  w2.switchUnit(CMassUnit::lb());
99  out << w1 << " " << w1.valueRoundedWithUnit(CMassUnit::kg()) << " " << w2 << Qt::endl;
100 
102  out << p1 << " " << p1.valueRoundedWithUnit(CPressureUnit::psi()) << " "
103  << p1.valueRoundedWithUnit(CPressureUnit::inHg()) << Qt::endl;
104 
105  CTemperature t1;
106  CTemperature t2(20, CTemperatureUnit::C());
107  CTemperature t3(1, CTemperatureUnit::F());
108  out << t1 << " " << t2 << " " << t2.valueRoundedWithUnit(CTemperatureUnit::defaultUnit(), -1, true);
109  out << t3.valueRoundedWithUnit(CTemperatureUnit::F(), -1, true) << " "
110  << t3.valueRoundedWithUnit(CTemperatureUnit::C(), -1, true) << " "
111  << "I18N/UTF" << Qt::endl;
112 
113  (t1 - t2).switchUnit(CTemperatureUnit::F()); // was not working since wrong return type const
114  // CLengthUnit duA(CSpeedUnit::ft_min()); // no longer possible
115  CLengthUnit duB(CLengthUnit::cm());
116  out << duB << Qt::endl;
117 
118  CTime ti1(1, CTimeUnit::h());
119  CTime ti2(ti1);
120  ti2.switchUnit(CTimeUnit::ms());
121  CTime ti3(1.0101, CTimeUnit::hms());
122  CTime ti4(1.15, CTimeUnit::hrmin());
123  CTime ti5(1.30, CTimeUnit::minsec());
124  CTime ti6("12:30");
125  CTime ti7("20s");
126  CTime ti8("12:30:40");
127 
128  out << ti1 << " " << ti2 << " " << ti3 << " " << ti4 << " " << ti5 << Qt::endl;
129  out << ti6 << " " << ti7 << " " << ti8 << Qt::endl;
130 
131  CAcceleration ac1(10, CAccelerationUnit::m_s2());
132  out << ac1 << " " << ac1.toQString(true) << " " << ac1.valueRoundedWithUnit(-1, true) << " "
133  << "I18N/UTF" << Qt::endl;
134 
135  // bye
136  out << "-----------------------------------------------" << Qt::endl;
137  return 0;
138  }
139 } // namespace swift::sample
static const physical_quantities::CPressure & standardISASeaLevelPressure()
Standard pressure 1013.25mbar/hPa.
Definition: altitude.cpp:447
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
Physical unit angle (radians, degrees)
Definition: angle.h:23
double piFactor() const
Value as factor of PI (e.g. 0.5PI)
Definition: angle.cpp:69
Physical unit length (length)
Definition: length.h:18
Specialized class for distance units (meter, foot, nautical miles).
Definition: units.h:95
QString getName(bool i18n=false) const
Name such as "meter".
PQ & switchUnit(const MU &newUnit)
Change unit, and convert value to maintain the same quantity.
QString valueRoundedWithUnit(const MU &unit, int digits=-1, bool withGroupSeparator=false, bool i18n=false) const
Value to QString with the given unit, e.g. "5.00m".
Free functions in swift::misc.