File indexing completed on 2024-04-21 03:58:31

0001 /*
0002  *   SPDX-FileCopyrightText: 2009 Petri Damstén <damu@iki.fi>
0003  *   SPDX-FileCopyrightText: 2014 John Layt <jlayt@kde.org>
0004  *
0005  *   SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "power_p.h"
0009 #include "unit_p.h"
0010 
0011 #include <KLocalizedString>
0012 #include <QtMath>
0013 
0014 namespace KUnitConversion
0015 {
0016 class DecibelUnitPrivate : public UnitPrivate
0017 {
0018 public:
0019     DecibelUnitPrivate(CategoryId categoryId,
0020                        UnitId id,
0021                        qreal multiplier,
0022                        const QString &symbol,
0023                        const QString &description,
0024                        const QString &matchString,
0025                        const KLocalizedString &symbolString,
0026                        const KLocalizedString &realString,
0027                        const KLocalizedString &integerString)
0028         : UnitPrivate(categoryId, id, multiplier, symbol, description, matchString, symbolString, realString, integerString)
0029     {
0030     }
0031 
0032     qreal toDefault(qreal value) const override
0033     {
0034         // y[W] = 10 ^ (value[dBW] / 10)
0035         return qPow(10, value / 10) * m_multiplier;
0036     }
0037 
0038     qreal fromDefault(qreal value) const override
0039     {
0040         // y[dBW] = 10 * log10(value[W])
0041         // QMath only provides natural log function (qLn) and constant M_LN10 = ln(10)
0042         // We use the logarithm change of base: log10(x) = ln(x) / ln(10)
0043         return 10 * qLn(value / m_multiplier) / M_LN10;
0044     }
0045 };
0046 
0047 UnitCategory Power::makeCategory()
0048 {
0049     auto c = UnitCategoryPrivate::makeCategory(PowerCategory, i18n("Power"), i18n("Power"));
0050     auto d = UnitCategoryPrivate::get(c);
0051     KLocalizedString symbolString = ki18nc("%1 value, %2 unit symbol (power)", "%1 %2");
0052 
0053     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0054                        Yottawatt,
0055                        1e+24,
0056                        i18nc("power unit symbol", "YW"),
0057                        i18nc("unit description in lists", "yottawatts"),
0058                        i18nc("unit synonyms for matching user input", "yottawatt;yottawatts;YW"),
0059                        symbolString,
0060                        ki18nc("amount in units (real)", "%1 yottawatts"),
0061                        ki18ncp("amount in units (integer)", "%1 yottawatt", "%1 yottawatts")));
0062 
0063     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0064                        Zettawatt,
0065                        1e+21,
0066                        i18nc("power unit symbol", "ZW"),
0067                        i18nc("unit description in lists", "zettawatts"),
0068                        i18nc("unit synonyms for matching user input", "zettawatt;zettawatts;ZW"),
0069                        symbolString,
0070                        ki18nc("amount in units (real)", "%1 zettawatts"),
0071                        ki18ncp("amount in units (integer)", "%1 zettawatt", "%1 zettawatts")));
0072 
0073     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0074                        Exawatt,
0075                        1e+18,
0076                        i18nc("power unit symbol", "EW"),
0077                        i18nc("unit description in lists", "exawatts"),
0078                        i18nc("unit synonyms for matching user input", "exawatt;exawatts;EW"),
0079                        symbolString,
0080                        ki18nc("amount in units (real)", "%1 exawatts"),
0081                        ki18ncp("amount in units (integer)", "%1 exawatt", "%1 exawatts")));
0082 
0083     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0084                        Petawatt,
0085                        1e+15,
0086                        i18nc("power unit symbol", "PW"),
0087                        i18nc("unit description in lists", "petawatts"),
0088                        i18nc("unit synonyms for matching user input", "petawatt;petawatts;PW"),
0089                        symbolString,
0090                        ki18nc("amount in units (real)", "%1 petawatts"),
0091                        ki18ncp("amount in units (integer)", "%1 petawatt", "%1 petawatts")));
0092 
0093     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0094                        Terawatt,
0095                        1e+12,
0096                        i18nc("power unit symbol", "TW"),
0097                        i18nc("unit description in lists", "terawatts"),
0098                        i18nc("unit synonyms for matching user input", "terawatt;terawatts;TW"),
0099                        symbolString,
0100                        ki18nc("amount in units (real)", "%1 terawatts"),
0101                        ki18ncp("amount in units (integer)", "%1 terawatt", "%1 terawatts")));
0102 
0103     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0104                        Gigawatt,
0105                        1e+09,
0106                        i18nc("power unit symbol", "GW"),
0107                        i18nc("unit description in lists", "gigawatts"),
0108                        i18nc("unit synonyms for matching user input", "gigawatt;gigawatts;GW"),
0109                        symbolString,
0110                        ki18nc("amount in units (real)", "%1 gigawatts"),
0111                        ki18ncp("amount in units (integer)", "%1 gigawatt", "%1 gigawatts")));
0112 
0113     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0114                        Megawatt,
0115                        1e+06,
0116                        i18nc("power unit symbol", "MW"),
0117                        i18nc("unit description in lists", "megawatts"),
0118                        i18nc("unit synonyms for matching user input", "megawatt;megawatts;MW"),
0119                        symbolString,
0120                        ki18nc("amount in units (real)", "%1 megawatts"),
0121                        ki18ncp("amount in units (integer)", "%1 megawatt", "%1 megawatts")));
0122 
0123     d->addCommonUnit(UnitPrivate::makeUnit(PowerCategory,
0124                              Kilowatt,
0125                              1000,
0126                              i18nc("power unit symbol", "kW"),
0127                              i18nc("unit description in lists", "kilowatts"),
0128                              i18nc("unit synonyms for matching user input", "kilowatt;kilowatts;kW"),
0129                              symbolString,
0130                              ki18nc("amount in units (real)", "%1 kilowatts"),
0131                              ki18ncp("amount in units (integer)", "%1 kilowatt", "%1 kilowatts")));
0132 
0133     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0134                        Hectowatt,
0135                        100,
0136                        i18nc("power unit symbol", "hW"),
0137                        i18nc("unit description in lists", "hectowatts"),
0138                        i18nc("unit synonyms for matching user input", "hectowatt;hectowatts;hW"),
0139                        symbolString,
0140                        ki18nc("amount in units (real)", "%1 hectowatts"),
0141                        ki18ncp("amount in units (integer)", "%1 hectowatt", "%1 hectowatts")));
0142 
0143     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0144                        Decawatt,
0145                        10,
0146                        i18nc("power unit symbol", "daW"),
0147                        i18nc("unit description in lists", "decawatts"),
0148                        i18nc("unit synonyms for matching user input", "decawatt;decawatts;daW"),
0149                        symbolString,
0150                        ki18nc("amount in units (real)", "%1 decawatts"),
0151                        ki18ncp("amount in units (integer)", "%1 decawatt", "%1 decawatts")));
0152 
0153     d->addDefaultUnit(UnitPrivate::makeUnit(PowerCategory,
0154                               Watt,
0155                               1,
0156                               i18nc("power unit symbol", "W"),
0157                               i18nc("unit description in lists", "watts"),
0158                               i18nc("unit synonyms for matching user input", "watt;watts;W"),
0159                               symbolString,
0160                               ki18nc("amount in units (real)", "%1 watts"),
0161                               ki18ncp("amount in units (integer)", "%1 watt", "%1 watts")));
0162 
0163     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0164                        Deciwatt,
0165                        0.1,
0166                        i18nc("power unit symbol", "dW"),
0167                        i18nc("unit description in lists", "deciwatts"),
0168                        i18nc("unit synonyms for matching user input", "deciwatt;deciwatts;dW"),
0169                        symbolString,
0170                        ki18nc("amount in units (real)", "%1 deciwatts"),
0171                        ki18ncp("amount in units (integer)", "%1 deciwatt", "%1 deciwatts")));
0172 
0173     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0174                        Centiwatt,
0175                        0.01,
0176                        i18nc("power unit symbol", "cW"),
0177                        i18nc("unit description in lists", "centiwatts"),
0178                        i18nc("unit synonyms for matching user input", "centiwatt;centiwatts;cW"),
0179                        symbolString,
0180                        ki18nc("amount in units (real)", "%1 centiwatts"),
0181                        ki18ncp("amount in units (integer)", "%1 centiwatt", "%1 centiwatts")));
0182 
0183     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0184                        Milliwatt,
0185                        0.001,
0186                        i18nc("power unit symbol", "mW"),
0187                        i18nc("unit description in lists", "milliwatts"),
0188                        i18nc("unit synonyms for matching user input", "milliwatt;milliwatts;mW"),
0189                        symbolString,
0190                        ki18nc("amount in units (real)", "%1 milliwatts"),
0191                        ki18ncp("amount in units (integer)", "%1 milliwatt", "%1 milliwatts")));
0192 
0193     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0194                        Microwatt,
0195                        1e-06,
0196                        i18nc("power unit symbol", "µW"),
0197                        i18nc("unit description in lists", "microwatts"),
0198                        i18nc("unit synonyms for matching user input", "microwatt;microwatts;µW;uW"),
0199                        symbolString,
0200                        ki18nc("amount in units (real)", "%1 microwatts"),
0201                        ki18ncp("amount in units (integer)", "%1 microwatt", "%1 microwatts")));
0202 
0203     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0204                        Nanowatt,
0205                        1e-09,
0206                        i18nc("power unit symbol", "nW"),
0207                        i18nc("unit description in lists", "nanowatts"),
0208                        i18nc("unit synonyms for matching user input", "nanowatt;nanowatts;nW"),
0209                        symbolString,
0210                        ki18nc("amount in units (real)", "%1 nanowatts"),
0211                        ki18ncp("amount in units (integer)", "%1 nanowatt", "%1 nanowatts")));
0212 
0213     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0214                        Picowatt,
0215                        1e-12,
0216                        i18nc("power unit symbol", "pW"),
0217                        i18nc("unit description in lists", "picowatts"),
0218                        i18nc("unit synonyms for matching user input", "picowatt;picowatts;pW"),
0219                        symbolString,
0220                        ki18nc("amount in units (real)", "%1 picowatts"),
0221                        ki18ncp("amount in units (integer)", "%1 picowatt", "%1 picowatts")));
0222 
0223     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0224                        Femtowatt,
0225                        1e-15,
0226                        i18nc("power unit symbol", "fW"),
0227                        i18nc("unit description in lists", "femtowatts"),
0228                        i18nc("unit synonyms for matching user input", "femtowatt;femtowatts;fW"),
0229                        symbolString,
0230                        ki18nc("amount in units (real)", "%1 femtowatts"),
0231                        ki18ncp("amount in units (integer)", "%1 femtowatt", "%1 femtowatts")));
0232 
0233     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0234                        Attowatt,
0235                        1e-18,
0236                        i18nc("power unit symbol", "aW"),
0237                        i18nc("unit description in lists", "attowatts"),
0238                        i18nc("unit synonyms for matching user input", "attowatt;attowatts;aW"),
0239                        symbolString,
0240                        ki18nc("amount in units (real)", "%1 attowatts"),
0241                        ki18ncp("amount in units (integer)", "%1 attowatt", "%1 attowatts")));
0242 
0243     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0244                        Zeptowatt,
0245                        1e-21,
0246                        i18nc("power unit symbol", "zW"),
0247                        i18nc("unit description in lists", "zeptowatts"),
0248                        i18nc("unit synonyms for matching user input", "zeptowatt;zeptowatts;zW"),
0249                        symbolString,
0250                        ki18nc("amount in units (real)", "%1 zeptowatts"),
0251                        ki18ncp("amount in units (integer)", "%1 zeptowatt", "%1 zeptowatts")));
0252 
0253     d->addUnit(UnitPrivate::makeUnit(PowerCategory,
0254                        Yoctowatt,
0255                        1e-24,
0256                        i18nc("power unit symbol", "yW"),
0257                        i18nc("unit description in lists", "yoctowatts"),
0258                        i18nc("unit synonyms for matching user input", "yoctowatt;yoctowatts;yW"),
0259                        symbolString,
0260                        ki18nc("amount in units (real)", "%1 yoctowatts"),
0261                        ki18ncp("amount in units (integer)", "%1 yoctowatt", "%1 yoctowatts")));
0262 
0263     d->addCommonUnit(UnitPrivate::makeUnit(PowerCategory,
0264                              Horsepower,
0265                              735.499,
0266                              i18nc("power unit symbol", "hp"),
0267                              i18nc("unit description in lists", "horsepowers"),
0268                              i18nc("unit synonyms for matching user input", "horsepower;horsepowers;hp"),
0269                              symbolString,
0270                              ki18nc("amount in units (real)", "%1 horsepowers"),
0271                              ki18ncp("amount in units (integer)", "%1 horsepower", "%1 horsepowers")));
0272 
0273     // Logarithmic Power Units (http://en.wikipedia.org/wiki/Decibel)
0274 
0275     d->addUnit(UnitPrivate::makeUnit(new DecibelUnitPrivate(PowerCategory,
0276                                               DecibelKilowatt,
0277                                               1000,
0278                                               i18nc("power unit symbol", "dBk"),
0279                                               i18nc("unit description in lists", "decibel kilowatts"),
0280                                               i18nc("unit synonyms for matching user input", "dBk;dBkW;dB(kW)"),
0281                                               symbolString,
0282                                               ki18nc("amount in units (real)", "%1 decibel kilowatts"),
0283                                               ki18ncp("amount in units (integer)", "%1 decibel kilowatt", "%1 decibel kilowatts"))));
0284 
0285     d->addUnit(UnitPrivate::makeUnit(new DecibelUnitPrivate(PowerCategory,
0286                                               DecibelWatt,
0287                                               1,
0288                                               i18nc("power unit symbol", "dBW"),
0289                                               i18nc("unit description in lists", "decibel watts"),
0290                                               i18nc("unit synonyms for matching user input", "dBW;dB(W)"),
0291                                               symbolString,
0292                                               ki18nc("amount in units (real)", "%1 decibel watts"),
0293                                               ki18ncp("amount in units (integer)", "%1 decibel watt", "%1 decibel watts"))));
0294 
0295     d->addCommonUnit(UnitPrivate::makeUnit(new DecibelUnitPrivate(PowerCategory,
0296                                                     DecibelMilliwatt,
0297                                                     0.001,
0298                                                     i18nc("power unit symbol", "dBm"),
0299                                                     i18nc("unit description in lists", "decibel milliwatts"),
0300                                                     i18nc("unit synonyms for matching user input", "dBm;dBmW;dB(mW)"),
0301                                                     symbolString,
0302                                                     ki18nc("amount in units (real)", "%1 decibel milliwatts"),
0303                                                     ki18ncp("amount in units (integer)", "%1 decibel milliwatt", "%1 decibel milliwatts"))));
0304 
0305     d->addUnit(UnitPrivate::makeUnit(new DecibelUnitPrivate(PowerCategory,
0306                                               DecibelMicrowatt,
0307                                               1e-6,
0308                                               i18nc("power unit symbol", "dBµW"),
0309                                               i18nc("unit description in lists", "decibel microwatts"),
0310                                               i18nc("unit synonyms for matching user input", "dBuW;dBµW;dB(uW);dB(µW)"),
0311                                               symbolString,
0312                                               ki18nc("amount in units (real)", "%1 decibel microwatts"),
0313                                               ki18ncp("amount in units (integer)", "%1 decibel microwatt", "%1 decibel microwatts"))));
0314 
0315     return c;
0316 }
0317 
0318 } // KUnitConversion namespace