Changeset View
Changeset View
Standalone View
Standalone View
src/power.cpp
Show All 16 Lines | |||||
17 | * Free Software Foundation, Inc., | 17 | * Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "power_p.h" | 21 | #include "power_p.h" | ||
22 | #include "unit_p.h" | 22 | #include "unit_p.h" | ||
23 | 23 | | |||
24 | #include <KI18n/klocalizedstring.h> | 24 | #include <KI18n/klocalizedstring.h> | ||
25 | #include <QtMath> | ||||
25 | 26 | | |||
26 | namespace KUnitConversion | 27 | namespace KUnitConversion | ||
27 | { | 28 | { | ||
28 | 29 | | |||
30 | class DecibelUnitPrivate : public UnitPrivate | ||||
31 | { | ||||
32 | public: | ||||
33 | DecibelUnitPrivate(CategoryId categoryId, UnitId id, qreal multiplier, | ||||
34 | const QString &symbol, const QString &description, | ||||
35 | const QString &matchString, const KLocalizedString &symbolString, | ||||
36 | const KLocalizedString &realString, const KLocalizedString &integerString) | ||||
37 | : UnitPrivate(categoryId, id, multiplier, | ||||
38 | symbol, description, | ||||
39 | matchString, symbolString, | ||||
40 | realString, integerString) | ||||
41 | {} | ||||
42 | | ||||
43 | qreal toDefault(qreal value) const override | ||||
44 | { | ||||
45 | // y[W] = 10 ^ (value[dBW] / 10) | ||||
46 | return qPow(10, value / 10) * m_multiplier; | ||||
47 | } | ||||
48 | | ||||
49 | qreal fromDefault(qreal value) const override | ||||
50 | { | ||||
51 | // y[dBW] = 10 * log10(value[W]) | ||||
52 | // QMath only provides natural log function (qLn) and constant M_LN10 = ln(10) | ||||
53 | // We use the logarithm change of base: log10(x) = ln(x) / ln(10) | ||||
54 | return 10 * qLn(value / m_multiplier) / M_LN10; | ||||
55 | } | ||||
56 | }; | ||||
57 | | ||||
58 | | ||||
29 | Power::Power() : CustomCategory(PowerCategory, i18n("Power"), i18n("Power")) | 59 | Power::Power() : CustomCategory(PowerCategory, i18n("Power"), i18n("Power")) | ||
30 | { | 60 | { | ||
31 | KLocalizedString symbolString = ki18nc("%1 value, %2 unit symbol (power)", "%1 %2"); | 61 | KLocalizedString symbolString = ki18nc("%1 value, %2 unit symbol (power)", "%1 %2"); | ||
32 | 62 | | |||
33 | addUnit(CustomUnit(PowerCategory, Yottawatt, 1e+24, | 63 | addUnit(CustomUnit(PowerCategory, Yottawatt, 1e+24, | ||
34 | i18nc("power unit symbol", "YW"), | 64 | i18nc("power unit symbol", "YW"), | ||
35 | i18nc("unit description in lists", "yottawatts"), | 65 | i18nc("unit description in lists", "yottawatts"), | ||
36 | i18nc("unit synonyms for matching user input", "yottawatt;yottawatts;YW"), | 66 | i18nc("unit synonyms for matching user input", "yottawatt;yottawatts;YW"), | ||
▲ Show 20 Lines • Show All 163 Lines • ▼ Show 20 Line(s) | |||||
200 | 230 | | |||
201 | addCommonUnit(CustomUnit(PowerCategory, Horsepower, 735.499, | 231 | addCommonUnit(CustomUnit(PowerCategory, Horsepower, 735.499, | ||
202 | i18nc("power unit symbol", "hp"), | 232 | i18nc("power unit symbol", "hp"), | ||
203 | i18nc("unit description in lists", "horsepowers"), | 233 | i18nc("unit description in lists", "horsepowers"), | ||
204 | i18nc("unit synonyms for matching user input", "horsepower;horsepowers;hp"), | 234 | i18nc("unit synonyms for matching user input", "horsepower;horsepowers;hp"), | ||
205 | symbolString, | 235 | symbolString, | ||
206 | ki18nc("amount in units (real)", "%1 horsepowers"), | 236 | ki18nc("amount in units (real)", "%1 horsepowers"), | ||
207 | ki18ncp("amount in units (integer)", "%1 horsepower", "%1 horsepowers"))); | 237 | ki18ncp("amount in units (integer)", "%1 horsepower", "%1 horsepowers"))); | ||
238 | | ||||
239 | //Logarithmic Power Units (http://en.wikipedia.org/wiki/Decibel) | ||||
240 | | ||||
241 | addUnit(CustomUnit(new DecibelUnitPrivate(PowerCategory, DecibelKilowatt, 1000, | ||||
242 | i18nc("power unit symbol", "dBk"), | ||||
243 | i18nc("unit description in lists", "decibel kilowatts"), | ||||
244 | i18nc("unit synonyms for matching user input", "dBk;dBkW;dB(kW)"), | ||||
245 | symbolString, | ||||
246 | ki18nc("amount in units (real)", "%1 decibel kilowatts"), | ||||
247 | ki18ncp("amount in units (integer)", "%1 decibel kilowatt", "%1 decibel kilowatts")))); | ||||
248 | | ||||
249 | addUnit(CustomUnit(new DecibelUnitPrivate(PowerCategory, DecibelWatt, 1, | ||||
250 | i18nc("power unit symbol", "dBW"), | ||||
251 | i18nc("unit description in lists", "decibel watts"), | ||||
252 | i18nc("unit synonyms for matching user input", "dBW;dB(W)"), | ||||
253 | symbolString, | ||||
254 | ki18nc("amount in units (real)", "%1 decibel watts"), | ||||
255 | ki18ncp("amount in units (integer)", "%1 decibel watt", "%1 decibel watts")))); | ||||
256 | | ||||
257 | addCommonUnit(CustomUnit(new DecibelUnitPrivate(PowerCategory, DecibelMilliwatt, 0.001, | ||||
258 | i18nc("power unit symbol", "dBm"), | ||||
259 | i18nc("unit description in lists", "decibel milliwatts"), | ||||
260 | i18nc("unit synonyms for matching user input", "dBm;dBmW;dB(mW)"), | ||||
261 | symbolString, | ||||
262 | ki18nc("amount in units (real)", "%1 decibel milliwatts"), | ||||
263 | ki18ncp("amount in units (integer)", "%1 decibel milliwatt", "%1 decibel milliwatts")))); | ||||
264 | | ||||
265 | addUnit(CustomUnit(new DecibelUnitPrivate(PowerCategory, DecibelMicrowatt, 1e-6, | ||||
266 | i18nc("power unit symbol", "dBµW"), | ||||
267 | i18nc("unit description in lists", "decibel microwatts"), | ||||
268 | i18nc("unit synonyms for matching user input", "dBuW;dBµW;dB(uW);dB(µW)"), | ||||
269 | symbolString, | ||||
270 | ki18nc("amount in units (real)", "%1 decibel microwatts"), | ||||
271 | ki18ncp("amount in units (integer)", "%1 decibel microwatt", "%1 decibel microwatts")))); | ||||
208 | } | 272 | } | ||
209 | 273 | | |||
210 | } // KUnitConversion namespace | 274 | } // KUnitConversion namespace |