| 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 | |
| 14 | namespace KUnitConversion |
| 15 | { |
| 16 | class DecibelUnitPrivate : public UnitPrivate |
| 17 | { |
| 18 | public: |
| 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(x: 10, y: 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(v: value / m_multiplier) / M_LN10; |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | UnitCategory Power::makeCategory() |
| 48 | { |
| 49 | auto c = UnitCategoryPrivate::makeCategory(id: PowerCategory, i18n("Power" ), i18n("Power" )); |
| 50 | auto d = UnitCategoryPrivate::get(category: c); |
| 51 | KLocalizedString symbolString = ki18nc("%1 value, %2 unit symbol (power)" , "%1 %2" ); |
| 52 | |
| 53 | d->addUnit(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 54 | id: Yottawatt, |
| 55 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 64 | id: Zettawatt, |
| 65 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 74 | id: Exawatt, |
| 75 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 84 | id: Petawatt, |
| 85 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 94 | id: Terawatt, |
| 95 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 104 | id: Gigawatt, |
| 105 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 114 | id: Megawatt, |
| 115 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 124 | id: Kilowatt, |
| 125 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 134 | id: Hectowatt, |
| 135 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 144 | id: Decawatt, |
| 145 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 154 | id: Watt, |
| 155 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 164 | id: Deciwatt, |
| 165 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 174 | id: Centiwatt, |
| 175 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 184 | id: Milliwatt, |
| 185 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 194 | id: Microwatt, |
| 195 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 204 | id: Nanowatt, |
| 205 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 214 | id: Picowatt, |
| 215 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 224 | id: Femtowatt, |
| 225 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 234 | id: Attowatt, |
| 235 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 244 | id: Zeptowatt, |
| 245 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 254 | id: Yoctowatt, |
| 255 | multiplier: 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(unit: UnitPrivate::makeUnit(categoryId: PowerCategory, |
| 264 | id: Horsepower, |
| 265 | multiplier: 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(unit: UnitPrivate::makeUnit(dd: 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(unit: UnitPrivate::makeUnit(dd: 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(unit: UnitPrivate::makeUnit(dd: 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(unit: UnitPrivate::makeUnit(dd: 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 |
| 319 | |