Debian Codegraph @

src/util/dabstractunitformatter.cpp

raw
1 // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dabstractunitformatter.h"
6
7 DCORE_BEGIN_NAMESPACE
8
9 /*!
10 @~english
11 @class Dtk::Core::DAbstractUnitFormatter
12 @ingroup dutil
13 @brief DAbstractUnitFormatter is a interface which manages the data with the
14 same type
15
16 DAbstractUnitFormatter provides interfaces including: <br>
17 1. The maximum value of one unit.
18 2. The minumum value of one unit.
19 3. The convert rate to next unit.
20 4. The string to display one unit.
21 */
22
23 /*!
24 @~english
25 @fn int DAbstractUnitFormatter::unitMax() const = 0
26 @brief Get the maximum unit in the list
27 */
28 /*!
29 @~english
30 @fn int DAbstractUnitFormatter::unitMin() const = 0
31 @brief Get the minumum unit in the list
32 */
33 /*!
34 @~english
35 @fn uint DAbstractUnitFormatter::unitConvertRate(int unitId) const = 0
36 @brief Get the convert rate to next unit of unitId
37 @param[in] unitId the id of one unit
38 */
39 /*!
40 @~english
41 @fn qreal DAbstractUnitFormatter::unitValueMax(int unitId) const
42 @brief Get the maximum value of unitId
43 @param[in] unitId the id of one unit
44 */
45 /*!
46 @~english
47 @fn qreal DAbstractUnitFormatter::unitValueMin(int unitId) const
48 @brief Get the minumum value of unitId
49 @param[in] unitId the id of one unit
50 */
51 /*!
52 @~english
53 @fn QString DAbstractUnitFormatter::unitStr(int unitId) const = 0
54 @brief Get the display string of unitId
55 @param[in] unitId the id of one unit
56 */
57
58 /*!
59 @~english
60 @brief Constructor of DAbstractUnitFormatter
61
62 */
63 DAbstractUnitFormatter::DAbstractUnitFormatter() {}
64
65 /*!
66 @~english
67 @brief Destructor of DAbstractUnitFormatter
68
69 */
70 DAbstractUnitFormatter::~DAbstractUnitFormatter() {}
71
72 /*!
73 @~english
74 @brief Convert value from unit currentUnit to targetUnit. If currentUnit is
75 smaller than targetUnit, the value will zoom out until the unit is equal to
76 targetUnit. Instead it will zoom in.
77
78 @param[in] value primitive value
79 @param[in] currentUnit current unit of value
80 @param[in] targetUnit target unit to convert to
81 @return qreal the value with unit targetUnit
82 */
83 qreal DAbstractUnitFormatter::formatAs(qreal value, int currentUnit,
84 const int targetUnit) const {
85 while (currentUnit < targetUnit)
86 value /= unitConvertRate(currentUnit++);
87 while (currentUnit > targetUnit)
88 value *= unitConvertRate(--currentUnit);
89
90 return value;
91 }
92
93 /*!
94 @~english
95 @brief Convert the value to a proper unit.
96
97 If unit is larger than unitMin() or smaller than unitMax, value will be
98 converted to the unit where value is close to the unitValueMin()
99
100 @param[in] value primitive value
101 @param[in] unit current unit
102 @return QPair<qreal, int> a pair of the converted value and unit
103 */
104 QPair<qreal, int> DAbstractUnitFormatter::format(const qreal value,
105 const int unit) const {
106 // can convert to smaller unit
107 if (unit > unitMin() && value < unitValueMin(unit))
108 return format(value * unitConvertRate(unit - 1), unit - 1);
109
110 // can convert to bigger unit
111 if (unit < unitMax() && value > unitValueMax(unit))
112 return format(value / unitConvertRate(unit), unit + 1);
113
114 return QPair<qreal, int>(value, unit);
115 }
116
117 /*!
118 @~english
119 @brief A version of format() with all the convert data
120
121 @param[in] value primitive value
122 @param[in] unit current unit
123 @return QList<QPair<qreal, int>> pairs of all converted value and unit
124 */
125 QList<QPair<qreal, int>>
126 DAbstractUnitFormatter::formatAsUnitList(const qreal value, int unit) const {
127 if (qFuzzyIsNull(value))
128 return QList<QPair<qreal, int>>();
129
130 if (value < unitValueMin(unit) || unit == unitMin()) {
131 if (unit != unitMin())
132 return formatAsUnitList(value * unitConvertRate(unit - 1), unit - 1);
133 else
134 return std::move(QList<QPair<qreal, int>>()
135 << QPair<qreal, int>(value, unit));
136 }
137
138 ulong _value = ulong(value);
139 QList<QPair<qreal, int>> ret = formatAsUnitList(value - _value, unit);
140
141 while (_value && unit != unitMax()) {
142 const ulong rate = unitConvertRate(unit);
143 const ulong r = _value % rate;
144 if (r)
145 ret.push_front(QPair<qreal, int>(r, unit));
146
147 unit += 1;
148 _value /= rate;
149 }
150
151 if (_value)
152 ret.push_front(QPair<qreal, int>(_value, unit));
153
154 return ret;
155 }
156
157 DCORE_END_NAMESPACE