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  QString CAngleUnit::makeRoundedQStringWithUnit(double value, int digits, bool withGroupSeparator, bool i18n) const
25  {
26  if (digits < 0) { digits = this->getDisplayDigits(); }
27  QString s;
28  if ((*this) == CAngleUnit::sexagesimalDeg())
29  {
30  digits -= 4;
31  Q_ASSERT(digits >= 0);
32  double de = CMathUtils::trunc(value);
33  double mi = CMathUtils::trunc((value - de) * 100.0);
34  double se = CMathUtils::trunc((value - de - mi / 100.0) * 1000000) / 100.0;
35  const char *fmt = value < 0 ? "-%1 %2 %3" : "%1 %2 %3";
36  s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
37  s = s.arg(fabs(de), 0, 'f', 0).arg(fabs(mi), 2, 'f', 0, '0').arg(fabs(se), 3 + digits, 'f', digits, '0');
38  }
39  else if ((*this) == CAngleUnit::sexagesimalDegMin())
40  {
41  digits -= 2;
42  Q_ASSERT(digits >= 0);
43  double de = CMathUtils::trunc(value);
44  double mi = CMathUtils::trunc((value - de) * 100.0);
45  const char *fmt = value < 0 ? "-%1 %2" : "%1 %2";
46  s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
47  s = s.arg(fabs(de), 0, 'f', 0).arg(fabs(mi), 3 + digits, 'f', digits, '0');
48  }
49  else { s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, withGroupSeparator, i18n); }
50  return s;
51  }
52 
53  QString CTimeUnit::makeRoundedQStringWithUnit(double value, int digits, bool withGroupSeparator, bool i18n) const
54  {
55  if (digits < 0) { digits = this->getDisplayDigits(); }
56  QString s;
57  if ((*this) == CTimeUnit::hms())
58  {
59  digits -= 4;
60  Q_ASSERT(digits >= 0);
61  double hr = CMathUtils::trunc(value);
62  double mi = CMathUtils::trunc((value - hr) * 100.0);
63  double se = CMathUtils::trunc((value - hr - mi / 100.0) * 1000000) / 100.0;
64  const char *fmt = value < 0 ? "-%1h%2m%3s" : "%1h%2m%3s";
65  s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
66  s = s.arg(fabs(hr), 2, 'f', 0, '0')
67  .arg(fabs(mi), 2, 'f', 0, '0')
68  .arg(fabs(se), 3 + digits, 'f', digits, '0');
69  }
70  else if ((*this) == CTimeUnit::hrmin())
71  {
72  digits -= 2;
73  Q_ASSERT(digits >= 0);
74  double hr = CMathUtils::trunc(value);
75  double mi = CMathUtils::trunc((value - hr) * 100.0);
76  const char *fmt = value < 0 ? "-%1h%2m" : "%1h%2m";
77  s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
78  s = s.arg(fabs(hr), 2, 'f', 0, '0').arg(fabs(mi), 3 + digits, 'f', digits, '0');
79  }
80  else if ((*this) == CTimeUnit::minsec())
81  {
82  digits -= 2;
83  Q_ASSERT(digits >= 0);
84  double mi = CMathUtils::trunc(value);
85  double se = CMathUtils::trunc((value - mi) * 100.0);
86  const char *fmt = value < 0 ? "-%2m%3s" : "%2m%3s";
87  s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
88  s = s.arg(fabs(mi), 2, 'f', 0, '0').arg(fabs(se), 3 + digits, 'f', digits, '0');
89  }
90  else { s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, withGroupSeparator, i18n); }
91  return s;
92  }
93 
94 } // 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:296
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:24
static CAngleUnit sexagesimalDeg()
Sexagesimal degree (degrees, minutes, seconds, decimal seconds)
Definition: units.h:285
virtual QString makeRoundedQStringWithUnit(double value, int digits=-1, bool withGroupSeparator=false, bool i18n=false) const
Value rounded with unit, e.g. "5.00m", "30kHz".
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:53
static CTimeUnit hrmin()
Hours, minutes.
Definition: units.h:995
static CTimeUnit hms()
Hours, minutes, seconds.
Definition: units.h:984
static CTimeUnit s()
Second s.
Definition: units.h:942
static CTimeUnit minsec()
Minutes, seconds.
Definition: units.h:1006
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
QString arg(Args &&... args) const const
#define SWIFT_DEFINE_UNIT_MIXINS(MU)
Explicit template definition of mixins for a CMeasurementUnit subclass.
Definition: units.h:66