KUnitConversion

power.cpp
1/*
2 * SPDX-FileCopyrightText: 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 "power_p.h"
9#include "unit_p.h"
10
11#include <KLocalizedString>
12#include <QtMath>
13
14namespace KUnitConversion
15{
16class DecibelUnitPrivate : public UnitPrivate
17{
18public:
19 DecibelUnitPrivate(CategoryId categoryId,
20 UnitId id,
21 qreal multiplier,
22 const QString &symbol,
23 const QString &description,
24 const QString &matchString,
25 const KLocalizedString &symbolString,
26 const KLocalizedString &realString,
27 const KLocalizedString &integerString)
28 : UnitPrivate(categoryId, id, multiplier, symbol, description, matchString, symbolString, realString, integerString)
29 {
30 }
31
32 qreal toDefault(qreal value) const override
33 {
34 // y[W] = 10 ^ (value[dBW] / 10)
35 return qPow(10, value / 10) * m_multiplier;
36 }
37
38 qreal fromDefault(qreal value) const override
39 {
40 // y[dBW] = 10 * log10(value[W])
41 // QMath only provides natural log function (qLn) and constant M_LN10 = ln(10)
42 // We use the logarithm change of base: log10(x) = ln(x) / ln(10)
43 return 10 * qLn(value / m_multiplier) / M_LN10;
44 }
45};
46
47UnitCategory Power::makeCategory()
48{
49 auto c = UnitCategoryPrivate::makeCategory(PowerCategory, i18n("Power"), i18n("Power"));
50 auto d = UnitCategoryPrivate::get(c);
51 KLocalizedString symbolString = ki18nc("%1 value, %2 unit symbol (power)", "%1 %2");
52
53 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
54 Yottawatt,
55 1e+24,
56 i18nc("power unit symbol", "YW"),
57 i18nc("unit description in lists", "yottawatts"),
58 i18nc("unit synonyms for matching user input", "yottawatt;yottawatts;YW"),
59 symbolString,
60 ki18nc("amount in units (real)", "%1 yottawatts"),
61 ki18ncp("amount in units (integer)", "%1 yottawatt", "%1 yottawatts")));
62
63 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
64 Zettawatt,
65 1e+21,
66 i18nc("power unit symbol", "ZW"),
67 i18nc("unit description in lists", "zettawatts"),
68 i18nc("unit synonyms for matching user input", "zettawatt;zettawatts;ZW"),
69 symbolString,
70 ki18nc("amount in units (real)", "%1 zettawatts"),
71 ki18ncp("amount in units (integer)", "%1 zettawatt", "%1 zettawatts")));
72
73 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
74 Exawatt,
75 1e+18,
76 i18nc("power unit symbol", "EW"),
77 i18nc("unit description in lists", "exawatts"),
78 i18nc("unit synonyms for matching user input", "exawatt;exawatts;EW"),
79 symbolString,
80 ki18nc("amount in units (real)", "%1 exawatts"),
81 ki18ncp("amount in units (integer)", "%1 exawatt", "%1 exawatts")));
82
83 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
84 Petawatt,
85 1e+15,
86 i18nc("power unit symbol", "PW"),
87 i18nc("unit description in lists", "petawatts"),
88 i18nc("unit synonyms for matching user input", "petawatt;petawatts;PW"),
89 symbolString,
90 ki18nc("amount in units (real)", "%1 petawatts"),
91 ki18ncp("amount in units (integer)", "%1 petawatt", "%1 petawatts")));
92
93 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
94 Terawatt,
95 1e+12,
96 i18nc("power unit symbol", "TW"),
97 i18nc("unit description in lists", "terawatts"),
98 i18nc("unit synonyms for matching user input", "terawatt;terawatts;TW"),
99 symbolString,
100 ki18nc("amount in units (real)", "%1 terawatts"),
101 ki18ncp("amount in units (integer)", "%1 terawatt", "%1 terawatts")));
102
103 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
104 Gigawatt,
105 1e+09,
106 i18nc("power unit symbol", "GW"),
107 i18nc("unit description in lists", "gigawatts"),
108 i18nc("unit synonyms for matching user input", "gigawatt;gigawatts;GW"),
109 symbolString,
110 ki18nc("amount in units (real)", "%1 gigawatts"),
111 ki18ncp("amount in units (integer)", "%1 gigawatt", "%1 gigawatts")));
112
113 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
114 Megawatt,
115 1e+06,
116 i18nc("power unit symbol", "MW"),
117 i18nc("unit description in lists", "megawatts"),
118 i18nc("unit synonyms for matching user input", "megawatt;megawatts;MW"),
119 symbolString,
120 ki18nc("amount in units (real)", "%1 megawatts"),
121 ki18ncp("amount in units (integer)", "%1 megawatt", "%1 megawatts")));
122
123 d->addCommonUnit(UnitPrivate::makeUnit(PowerCategory,
124 Kilowatt,
125 1000,
126 i18nc("power unit symbol", "kW"),
127 i18nc("unit description in lists", "kilowatts"),
128 i18nc("unit synonyms for matching user input", "kilowatt;kilowatts;kW"),
129 symbolString,
130 ki18nc("amount in units (real)", "%1 kilowatts"),
131 ki18ncp("amount in units (integer)", "%1 kilowatt", "%1 kilowatts")));
132
133 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
134 Hectowatt,
135 100,
136 i18nc("power unit symbol", "hW"),
137 i18nc("unit description in lists", "hectowatts"),
138 i18nc("unit synonyms for matching user input", "hectowatt;hectowatts;hW"),
139 symbolString,
140 ki18nc("amount in units (real)", "%1 hectowatts"),
141 ki18ncp("amount in units (integer)", "%1 hectowatt", "%1 hectowatts")));
142
143 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
144 Decawatt,
145 10,
146 i18nc("power unit symbol", "daW"),
147 i18nc("unit description in lists", "decawatts"),
148 i18nc("unit synonyms for matching user input", "decawatt;decawatts;daW"),
149 symbolString,
150 ki18nc("amount in units (real)", "%1 decawatts"),
151 ki18ncp("amount in units (integer)", "%1 decawatt", "%1 decawatts")));
152
153 d->addDefaultUnit(UnitPrivate::makeUnit(PowerCategory,
154 Watt,
155 1,
156 i18nc("power unit symbol", "W"),
157 i18nc("unit description in lists", "watts"),
158 i18nc("unit synonyms for matching user input", "watt;watts;W"),
159 symbolString,
160 ki18nc("amount in units (real)", "%1 watts"),
161 ki18ncp("amount in units (integer)", "%1 watt", "%1 watts")));
162
163 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
164 Deciwatt,
165 0.1,
166 i18nc("power unit symbol", "dW"),
167 i18nc("unit description in lists", "deciwatts"),
168 i18nc("unit synonyms for matching user input", "deciwatt;deciwatts;dW"),
169 symbolString,
170 ki18nc("amount in units (real)", "%1 deciwatts"),
171 ki18ncp("amount in units (integer)", "%1 deciwatt", "%1 deciwatts")));
172
173 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
174 Centiwatt,
175 0.01,
176 i18nc("power unit symbol", "cW"),
177 i18nc("unit description in lists", "centiwatts"),
178 i18nc("unit synonyms for matching user input", "centiwatt;centiwatts;cW"),
179 symbolString,
180 ki18nc("amount in units (real)", "%1 centiwatts"),
181 ki18ncp("amount in units (integer)", "%1 centiwatt", "%1 centiwatts")));
182
183 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
184 Milliwatt,
185 0.001,
186 i18nc("power unit symbol", "mW"),
187 i18nc("unit description in lists", "milliwatts"),
188 i18nc("unit synonyms for matching user input", "milliwatt;milliwatts;mW"),
189 symbolString,
190 ki18nc("amount in units (real)", "%1 milliwatts"),
191 ki18ncp("amount in units (integer)", "%1 milliwatt", "%1 milliwatts")));
192
193 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
194 Microwatt,
195 1e-06,
196 i18nc("power unit symbol", "µW"),
197 i18nc("unit description in lists", "microwatts"),
198 i18nc("unit synonyms for matching user input", "microwatt;microwatts;µW;uW"),
199 symbolString,
200 ki18nc("amount in units (real)", "%1 microwatts"),
201 ki18ncp("amount in units (integer)", "%1 microwatt", "%1 microwatts")));
202
203 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
204 Nanowatt,
205 1e-09,
206 i18nc("power unit symbol", "nW"),
207 i18nc("unit description in lists", "nanowatts"),
208 i18nc("unit synonyms for matching user input", "nanowatt;nanowatts;nW"),
209 symbolString,
210 ki18nc("amount in units (real)", "%1 nanowatts"),
211 ki18ncp("amount in units (integer)", "%1 nanowatt", "%1 nanowatts")));
212
213 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
214 Picowatt,
215 1e-12,
216 i18nc("power unit symbol", "pW"),
217 i18nc("unit description in lists", "picowatts"),
218 i18nc("unit synonyms for matching user input", "picowatt;picowatts;pW"),
219 symbolString,
220 ki18nc("amount in units (real)", "%1 picowatts"),
221 ki18ncp("amount in units (integer)", "%1 picowatt", "%1 picowatts")));
222
223 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
224 Femtowatt,
225 1e-15,
226 i18nc("power unit symbol", "fW"),
227 i18nc("unit description in lists", "femtowatts"),
228 i18nc("unit synonyms for matching user input", "femtowatt;femtowatts;fW"),
229 symbolString,
230 ki18nc("amount in units (real)", "%1 femtowatts"),
231 ki18ncp("amount in units (integer)", "%1 femtowatt", "%1 femtowatts")));
232
233 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
234 Attowatt,
235 1e-18,
236 i18nc("power unit symbol", "aW"),
237 i18nc("unit description in lists", "attowatts"),
238 i18nc("unit synonyms for matching user input", "attowatt;attowatts;aW"),
239 symbolString,
240 ki18nc("amount in units (real)", "%1 attowatts"),
241 ki18ncp("amount in units (integer)", "%1 attowatt", "%1 attowatts")));
242
243 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
244 Zeptowatt,
245 1e-21,
246 i18nc("power unit symbol", "zW"),
247 i18nc("unit description in lists", "zeptowatts"),
248 i18nc("unit synonyms for matching user input", "zeptowatt;zeptowatts;zW"),
249 symbolString,
250 ki18nc("amount in units (real)", "%1 zeptowatts"),
251 ki18ncp("amount in units (integer)", "%1 zeptowatt", "%1 zeptowatts")));
252
253 d->addUnit(UnitPrivate::makeUnit(PowerCategory,
254 Yoctowatt,
255 1e-24,
256 i18nc("power unit symbol", "yW"),
257 i18nc("unit description in lists", "yoctowatts"),
258 i18nc("unit synonyms for matching user input", "yoctowatt;yoctowatts;yW"),
259 symbolString,
260 ki18nc("amount in units (real)", "%1 yoctowatts"),
261 ki18ncp("amount in units (integer)", "%1 yoctowatt", "%1 yoctowatts")));
262
263 d->addCommonUnit(UnitPrivate::makeUnit(PowerCategory,
264 Horsepower,
265 735.499,
266 i18nc("power unit symbol", "hp"),
267 i18nc("unit description in lists", "horsepowers"),
268 i18nc("unit synonyms for matching user input", "horsepower;horsepowers;hp"),
269 symbolString,
270 ki18nc("amount in units (real)", "%1 horsepowers"),
271 ki18ncp("amount in units (integer)", "%1 horsepower", "%1 horsepowers")));
272
273 // Logarithmic Power Units (http://en.wikipedia.org/wiki/Decibel)
274
275 d->addUnit(UnitPrivate::makeUnit(new DecibelUnitPrivate(PowerCategory,
276 DecibelKilowatt,
277 1000,
278 i18nc("power unit symbol", "dBk"),
279 i18nc("unit description in lists", "decibel kilowatts"),
280 i18nc("unit synonyms for matching user input", "dBk;dBkW;dB(kW)"),
281 symbolString,
282 ki18nc("amount in units (real)", "%1 decibel kilowatts"),
283 ki18ncp("amount in units (integer)", "%1 decibel kilowatt", "%1 decibel kilowatts"))));
284
285 d->addUnit(UnitPrivate::makeUnit(new DecibelUnitPrivate(PowerCategory,
286 DecibelWatt,
287 1,
288 i18nc("power unit symbol", "dBW"),
289 i18nc("unit description in lists", "decibel watts"),
290 i18nc("unit synonyms for matching user input", "dBW;dB(W)"),
291 symbolString,
292 ki18nc("amount in units (real)", "%1 decibel watts"),
293 ki18ncp("amount in units (integer)", "%1 decibel watt", "%1 decibel watts"))));
294
295 d->addCommonUnit(UnitPrivate::makeUnit(new DecibelUnitPrivate(PowerCategory,
296 DecibelMilliwatt,
297 0.001,
298 i18nc("power unit symbol", "dBm"),
299 i18nc("unit description in lists", "decibel milliwatts"),
300 i18nc("unit synonyms for matching user input", "dBm;dBmW;dB(mW)"),
301 symbolString,
302 ki18nc("amount in units (real)", "%1 decibel milliwatts"),
303 ki18ncp("amount in units (integer)", "%1 decibel milliwatt", "%1 decibel milliwatts"))));
304
305 d->addUnit(UnitPrivate::makeUnit(new DecibelUnitPrivate(PowerCategory,
306 DecibelMicrowatt,
307 1e-6,
308 i18nc("power unit symbol", "dBµW"),
309 i18nc("unit description in lists", "decibel microwatts"),
310 i18nc("unit synonyms for matching user input", "dBuW;dBµW;dB(uW);dB(µW)"),
311 symbolString,
312 ki18nc("amount in units (real)", "%1 decibel microwatts"),
313 ki18ncp("amount in units (integer)", "%1 decibel microwatt", "%1 decibel microwatts"))));
314
315 return c;
316}
317
318} // KUnitConversion namespace
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KLocalizedString KI18N_EXPORT ki18ncp(const char *context, const char *singular, const char *plural)
KLocalizedString KI18N_EXPORT ki18nc(const char *context, const char *text)
QString i18n(const char *text, const TYPE &arg...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.