1/*
2 * SPDX-FileCopyrightText: 2007-2009 Petri Damstén <damu@iki.fi>
3 * SPDX-FileCopyrightText: 2014 John Layt <jlayt@kde.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8#include "unit.h"
9#include "unit_p.h"
10#include "unitcategory.h"
11
12#include <KLocalizedString>
13
14namespace KUnitConversion
15{
16UnitPrivate::UnitPrivate()
17 : m_categoryId(InvalidCategory)
18 , m_id(InvalidUnit)
19 , m_multiplier(1.0)
20{
21}
22
23UnitPrivate::UnitPrivate(CategoryId categoryId,
24 UnitId id,
25 qreal multiplier,
26 const QString &symbol,
27 const QString &description,
28 const QString &matchString,
29 const KLocalizedString &symbolString,
30 const KLocalizedString &realString,
31 const KLocalizedString &integerString)
32 : m_categoryId(categoryId)
33 , m_id(id)
34 , m_multiplier(multiplier)
35 , m_symbol(symbol)
36 , m_description(description)
37 , m_matchString(matchString)
38 , m_symbolString(symbolString)
39 , m_realString(realString)
40 , m_integerString(integerString)
41{
42}
43
44UnitPrivate::~UnitPrivate()
45{
46}
47
48UnitPrivate *UnitPrivate::clone()
49{
50 return new UnitPrivate(*this);
51}
52
53bool UnitPrivate::operator==(const UnitPrivate &other) const
54{
55 return (m_id == other.m_id && m_symbol == other.m_symbol);
56}
57
58bool UnitPrivate::operator!=(const UnitPrivate &other) const
59{
60 return !(*this == other);
61}
62
63void UnitPrivate::setUnitMultiplier(qreal multiplier)
64{
65 m_multiplier = multiplier;
66}
67
68qreal UnitPrivate::unitMultiplier() const
69{
70 return m_multiplier;
71}
72
73qreal UnitPrivate::toDefault(qreal value) const
74{
75 return value * m_multiplier;
76}
77
78qreal UnitPrivate::fromDefault(qreal value) const
79{
80 return value / m_multiplier;
81}
82
83Unit::Unit()
84 : d(nullptr)
85{
86}
87
88Unit::Unit(UnitPrivate *dd)
89 : d(dd)
90{
91}
92
93Unit::Unit(const Unit &other)
94 : d(other.d)
95{
96}
97
98Unit::~Unit()
99{
100}
101
102Unit &Unit::operator=(const Unit &other)
103{
104 d = other.d;
105 return *this;
106}
107
108Unit &Unit::operator=(Unit &&other)
109{
110 d.swap(other&: other.d);
111 return *this;
112}
113
114bool Unit::operator==(const Unit &other) const
115{
116 if (d && other.d) {
117 return (*d == *other.d);
118 } else {
119 return (d == other.d);
120 }
121}
122
123bool Unit::operator!=(const Unit &other) const
124{
125 if (d && other.d) {
126 return (*d != *other.d);
127 } else {
128 return (d != other.d);
129 }
130}
131
132bool Unit::isNull() const
133{
134 return !d;
135}
136
137bool Unit::isValid() const
138{
139 return (d && !d->m_symbol.isEmpty());
140}
141
142UnitId Unit::id() const
143{
144 if (d) {
145 return d->m_id;
146 }
147 return InvalidUnit;
148}
149
150CategoryId Unit::categoryId() const
151{
152 if (d) {
153 return d->m_categoryId;
154 }
155 return InvalidCategory;
156}
157
158UnitCategory Unit::category() const
159{
160 if (d) {
161 return UnitCategory(d->m_category);
162 }
163 return UnitCategory();
164}
165
166QString Unit::description() const
167{
168 if (d) {
169 return d->m_description;
170 }
171 return QString();
172}
173
174QString Unit::symbol() const
175{
176 if (d) {
177 return d->m_symbol;
178 }
179 return QString();
180}
181
182void Unit::setUnitMultiplier(qreal multiplier)
183{
184 if (d) {
185 d->setUnitMultiplier(multiplier);
186 }
187}
188
189qreal Unit::toDefault(qreal value) const
190{
191 if (d) {
192 return d->toDefault(value);
193 }
194 return 0;
195}
196
197qreal Unit::fromDefault(qreal value) const
198{
199 if (d) {
200 return d->fromDefault(value);
201 }
202 return 0;
203}
204
205QString Unit::toString(qreal value, int fieldWidth, char format, int precision, const QChar &fillChar) const
206{
207 if (isNull()) {
208 return QString();
209 }
210 if ((int)value == value && precision < 1) {
211 return d->m_integerString.subs(a: (int)value).toString();
212 }
213 return d->m_realString.subs(a: value, fieldWidth, format, precision, fillChar).toString();
214}
215
216QString Unit::toSymbolString(qreal value, int fieldWidth, char format, int precision, const QChar &fillChar) const
217{
218 if (d) {
219 return d->m_symbolString.subs(a: value, fieldWidth, format, precision, fillChar).subs(a: d->m_symbol).toString();
220 }
221 return QString();
222}
223
224}
225

source code of kunitconversion/src/unit.cpp