swift
Loading...
Searching...
No Matches
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
9
11SWIFT_DEFINE_UNIT_MIXINS(CLengthUnit)
12SWIFT_DEFINE_UNIT_MIXINS(CPressureUnit)
15SWIFT_DEFINE_UNIT_MIXINS(CTemperatureUnit)
18SWIFT_DEFINE_UNIT_MIXINS(CAccelerationUnit)
19
20namespace swift::misc::physical_quantities
21{
22 using swift::misc::math::CMathUtils;
23
24 QString CAngleUnit::makeRoundedQStringWithUnit(double value, int digits, bool withGroupSeparator, bool i18n) const
25 {
26 if (digits < 0)
27 {
28 digits = this->getDisplayDigits();
29 }
30 QString s;
31 if ((*this) == CAngleUnit::sexagesimalDeg())
32 {
33 digits -= 4;
34 Q_ASSERT(digits >= 0);
35 double de = CMathUtils::trunc(value);
36 double mi = CMathUtils::trunc((value - de) * 100.0);
37 double se = CMathUtils::trunc((value - de - mi / 100.0) * 1000000) / 100.0;
38 const char *fmt = value < 0 ? "-%1 %2 %3" : "%1 %2 %3";
39 s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
40 s = s.arg(fabs(de), 0, 'f', 0).arg(fabs(mi), 2, 'f', 0, '0').arg(fabs(se), 3 + digits, 'f', digits, '0');
41 }
42 else if ((*this) == CAngleUnit::sexagesimalDegMin())
43 {
44 digits -= 2;
45 Q_ASSERT(digits >= 0);
46 double de = CMathUtils::trunc(value);
47 double mi = CMathUtils::trunc((value - de) * 100.0);
48 const char *fmt = value < 0 ? "-%1 %2" : "%1 %2";
49 s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
50 s = s.arg(fabs(de), 0, 'f', 0).arg(fabs(mi), 3 + digits, 'f', digits, '0');
51 }
52 else
53 {
54 s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, withGroupSeparator, i18n);
55 }
56 return s;
57 }
58
59 QString CTimeUnit::makeRoundedQStringWithUnit(double value, int digits, bool withGroupSeparator, bool i18n) const
60 {
61 if (digits < 0)
62 {
63 digits = this->getDisplayDigits();
64 }
65 QString s;
66 if ((*this) == CTimeUnit::hms())
67 {
68 digits -= 4;
69 Q_ASSERT(digits >= 0);
70 double hr = CMathUtils::trunc(value);
71 double mi = CMathUtils::trunc((value - hr) * 100.0);
72 double se = CMathUtils::trunc((value - hr - mi / 100.0) * 1000000) / 100.0;
73 const char *fmt = value < 0 ? "-%1h%2m%3s" : "%1h%2m%3s";
74 s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
75 s = s.arg(fabs(hr), 2, 'f', 0, '0')
76 .arg(fabs(mi), 2, 'f', 0, '0')
77 .arg(fabs(se), 3 + digits, 'f', digits, '0');
78 }
79 else if ((*this) == CTimeUnit::hrmin())
80 {
81 digits -= 2;
82 Q_ASSERT(digits >= 0);
83 double hr = CMathUtils::trunc(value);
84 double mi = CMathUtils::trunc((value - hr) * 100.0);
85 const char *fmt = value < 0 ? "-%1h%2m" : "%1h%2m";
86 s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
87 s = s.arg(fabs(hr), 2, 'f', 0, '0').arg(fabs(mi), 3 + digits, 'f', digits, '0');
88 }
89 else if ((*this) == CTimeUnit::minsec())
90 {
91 digits -= 2;
92 Q_ASSERT(digits >= 0);
93 double mi = CMathUtils::trunc(value);
94 double se = CMathUtils::trunc((value - mi) * 100.0);
95 const char *fmt = value < 0 ? "-%2m%3s" : "%2m%3s";
96 s = i18n ? QCoreApplication::translate("CMeasurementUnit", fmt) : fmt;
97 s = s.arg(fabs(mi), 2, 'f', 0, '0').arg(fabs(se), 3 + digits, 'f', digits, '0');
98 }
99 else
100 {
101 s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, withGroupSeparator, i18n);
102 }
103 return s;
104 }
105
106} // namespace swift::misc::physical_quantities
Specialized class for angles (degrees, radian).
Definition units.h:231
Specialized class for frequency (hertz, mega hertz, kilo hertz).
Definition units.h:334
static CAngleUnit sexagesimalDegMin()
Sexagesimal degree (degrees, minutes, decimal minutes).
Definition units.h:294
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
static double trunc(double value, double epsilon=1e-10)
Nearest integer not greater in magnitude than value, correcting for epsilon.
Definition mathutils.h:52
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:59
static CTimeUnit hrmin()
Hours, minutes.
Definition units.h:989
static CTimeUnit hms()
Hours, minutes, seconds.
Definition units.h:980
static CTimeUnit s()
Second s.
Definition units.h:938
static CTimeUnit minsec()
Minutes, seconds.
Definition units.h:998
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