swift
units.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/pq/units.h"
5 
6 #include <QCoreApplication>
7 
8 #include "misc/math/mathutils.h"
9 
10 SWIFT_DEFINE_UNIT_MIXINS(CAngleUnit)
11 SWIFT_DEFINE_UNIT_MIXINS(CLengthUnit)
12 SWIFT_DEFINE_UNIT_MIXINS(CPressureUnit)
13 SWIFT_DEFINE_UNIT_MIXINS(CFrequencyUnit)
14 SWIFT_DEFINE_UNIT_MIXINS(CMassUnit)
15 SWIFT_DEFINE_UNIT_MIXINS(CTemperatureUnit)
16 SWIFT_DEFINE_UNIT_MIXINS(CSpeedUnit)
17 SWIFT_DEFINE_UNIT_MIXINS(CTimeUnit)
18 SWIFT_DEFINE_UNIT_MIXINS(CAccelerationUnit)
19 
20 namespace swift::misc::physical_quantities
21 {
23 
24  // pin vtables to this file
25 
26  void CLengthUnit::anchor() {}
27 
28  void CFrequencyUnit::anchor() {}
29 
30  void CMassUnit::anchor() {}
31 
32  void CPressureUnit::anchor() {}
33 
34  void CTemperatureUnit::anchor() {}
35 
36  void CSpeedUnit::anchor() {}
37 
38  void CAccelerationUnit::anchor() {}
39 
40  QString CAngleUnit::makeRoundedQStringWithUnit(double value, int digits, bool withGroupSeparator, bool i18n) const
41  {
42  if (digits < 0) { digits = this->getDisplayDigits(); }
43  QString s;
44  if ((*this) == CAngleUnit::sexagesimalDeg())
45  {
46  digits -= 4;
47  Q_ASSERT(digits >= 0);
48  double de = CMathUtils::trunc(value);
49  double mi = CMathUtils::trunc((value - de) * 100.0);
50  double se = CMathUtils::trunc((value - de - mi / 100.0) * 1000000) / 100.0;
51  const char *fmt = value < 0 ? "-%1 %2 %3" : "%1 %2 %3";
52  s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
53  s = s.arg(fabs(de), 0, 'f', 0).arg(fabs(mi), 2, 'f', 0, '0').arg(fabs(se), 3 + digits, 'f', digits, '0');
54  }
55  else if ((*this) == CAngleUnit::sexagesimalDegMin())
56  {
57  digits -= 2;
58  Q_ASSERT(digits >= 0);
59  double de = CMathUtils::trunc(value);
60  double mi = CMathUtils::trunc((value - de) * 100.0);
61  const char *fmt = value < 0 ? "-%1 %2" : "%1 %2";
62  s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
63  s = s.arg(fabs(de), 0, 'f', 0).arg(fabs(mi), 3 + digits, 'f', digits, '0');
64  }
65  else { s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, withGroupSeparator, i18n); }
66  return s;
67  }
68 
69  QString CTimeUnit::makeRoundedQStringWithUnit(double value, int digits, bool withGroupSeparator, bool i18n) const
70  {
71  if (digits < 0) { digits = this->getDisplayDigits(); }
72  QString s;
73  if ((*this) == CTimeUnit::hms())
74  {
75  digits -= 4;
76  Q_ASSERT(digits >= 0);
77  double hr = CMathUtils::trunc(value);
78  double mi = CMathUtils::trunc((value - hr) * 100.0);
79  double se = CMathUtils::trunc((value - hr - mi / 100.0) * 1000000) / 100.0;
80  const char *fmt = value < 0 ? "-%1h%2m%3s" : "%1h%2m%3s";
81  s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
82  s = s.arg(fabs(hr), 2, 'f', 0, '0')
83  .arg(fabs(mi), 2, 'f', 0, '0')
84  .arg(fabs(se), 3 + digits, 'f', digits, '0');
85  }
86  else if ((*this) == CTimeUnit::hrmin())
87  {
88  digits -= 2;
89  Q_ASSERT(digits >= 0);
90  double hr = CMathUtils::trunc(value);
91  double mi = CMathUtils::trunc((value - hr) * 100.0);
92  const char *fmt = value < 0 ? "-%1h%2m" : "%1h%2m";
93  s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
94  s = s.arg(fabs(hr), 2, 'f', 0, '0').arg(fabs(mi), 3 + digits, 'f', digits, '0');
95  }
96  else if ((*this) == CTimeUnit::minsec())
97  {
98  digits -= 2;
99  Q_ASSERT(digits >= 0);
100  double mi = CMathUtils::trunc(value);
101  double se = CMathUtils::trunc((value - mi) * 100.0);
102  const char *fmt = value < 0 ? "-%2m%3s" : "%2m%3s";
103  s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
104  s = s.arg(fabs(mi), 2, 'f', 0, '0').arg(fabs(se), 3 + digits, 'f', digits, '0');
105  }
106  else { s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, withGroupSeparator, i18n); }
107  return s;
108  }
109 
110 } // namespace swift::misc::physical_quantities
static double trunc(double value, double epsilon=1e-10)
Nearest integer not greater in magnitude than value, correcting for epsilon.
Definition: mathutils.h:52
static CAngleUnit sexagesimalDegMin()
Sexagesimal degree (degrees, minutes, decimal minutes)
Definition: units.h:298
virtual QString makeRoundedQStringWithUnit(double value, int digits=-1, bool withGroupSeparator=false, bool i18n=false) const
Value rounded with unit, e.g. "5.00m", "30kHz".
Definition: units.cpp:40
static CAngleUnit sexagesimalDeg()
Sexagesimal degree (degrees, minutes, seconds, decimal seconds)
Definition: units.h:287
virtual QString makeRoundedQStringWithUnit(double value, int digits=-1, bool withGroupSeparator=false, bool i18n=false) const
Value rounded with unit, e.g. "5.00m", "30kHz".
virtual QString makeRoundedQStringWithUnit(double value, int digits=-1, bool withGroupSeparator=false, bool i18n=false) const
Value rounded with unit, e.g. "5.00m", "30kHz".
Definition: units.cpp:69
static CTimeUnit hrmin()
Hours, minutes.
Definition: units.h:1007
static CTimeUnit hms()
Hours, minutes, seconds.
Definition: units.h:996
static CTimeUnit s()
Second s.
Definition: units.h:954
static CTimeUnit minsec()
Minutes, seconds.
Definition: units.h:1018
#define SWIFT_DEFINE_UNIT_MIXINS(MU)
Explicit template definition of mixins for a CMeasurementUnit subclass.
Definition: units.h:66