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 "fuel_efficiency_p.h"
9#include "unit_p.h"
10
11#include <KLocalizedString>
12
13namespace KUnitConversion
14{
15class FuelUnitPrivate : public UnitPrivate
16{
17public:
18 FuelUnitPrivate(CategoryId categoryId,
19 UnitId id,
20 qreal multiplier,
21 const QString &symbol,
22 const QString &description,
23 const QString &matchString,
24 const KLocalizedString &symbolString,
25 const KLocalizedString &realString,
26 const KLocalizedString &integerString,
27 const bool isReciprocalToDefaultUnit = false)
28 : UnitPrivate(categoryId, id, multiplier, symbol, description, matchString, symbolString, realString, integerString)
29 , m_isReciprocalToDefaultUnit(isReciprocalToDefaultUnit)
30 {
31 }
32
33 qreal toDefault(qreal value) const override
34 {
35 if (m_isReciprocalToDefaultUnit) {
36 return unitMultiplier() / value;
37 }
38 return UnitPrivate::toDefault(value);
39 }
40
41 qreal fromDefault(qreal value) const override
42 {
43 if (m_isReciprocalToDefaultUnit) {
44 return unitMultiplier() / value;
45 }
46 return UnitPrivate::fromDefault(value);
47 }
48
49private:
50 bool m_isReciprocalToDefaultUnit; /* l/100 km (fuel per given distance) is inverse
51 to MPG and kmpl (distance per given amount of fuel).
52 Converter has to account for this. */
53};
54
55UnitCategory FuelEfficiency::makeCategory()
56{
57 auto c = UnitCategoryPrivate::makeCategory(id: FuelEfficiencyCategory, i18n("Fuel Efficiency"), i18n("Fuel Efficiency"));
58 auto d = UnitCategoryPrivate::get(category: c);
59 KLocalizedString symbolString = ki18nc("%1 value, %2 unit symbol (fuel efficiency)", "%1 %2");
60
61 d->addDefaultUnit(
62 unit: UnitPrivate::makeUnit(dd: new FuelUnitPrivate(FuelEfficiencyCategory,
63 LitersPer100Kilometers,
64 1,
65 i18nc("fuelefficiency unit symbol", "l/100 km"),
66 i18nc("unit description in lists", "liters per 100 kilometers"),
67 i18nc("unit synonyms for matching user input", "liters per 100 kilometers;liters per 100 kilometers;l/100 km;L/100 km"),
68 symbolString,
69 ki18nc("amount in units (real)", "%1 liters per 100 kilometers"),
70 ki18ncp("amount in units (integer)", "%1 liters per 100 kilometers", "%1 liters per 100 kilometers"))));
71
72 d->addCommonUnit(unit: UnitPrivate::makeUnit(dd: new FuelUnitPrivate(FuelEfficiencyCategory,
73 MilePerUsGallon,
74 235.215,
75 i18nc("fuelefficiency unit symbol", "mpg"),
76 i18nc("unit description in lists", "miles per US gallon"),
77 i18nc("unit synonyms for matching user input", "mile per US gallon;miles per US gallon;mpg"),
78 symbolString,
79 ki18nc("amount in units (real)", "%1 miles per US gallon"),
80 ki18ncp("amount in units (integer)", "%1 mile per US gallon", "%1 miles per US gallon"),
81 true)));
82
83 d->addCommonUnit(unit: UnitPrivate::makeUnit(dd: new FuelUnitPrivate(
84 FuelEfficiencyCategory,
85 MilePerImperialGallon,
86 282.481,
87 i18nc("fuelefficiency unit symbol", "mpg (imperial)"),
88 i18nc("unit description in lists", "miles per imperial gallon"),
89 i18nc("unit synonyms for matching user input", "mile per imperial gallon;miles per imperial gallon;mpg (imperial);imp mpg;mpg (imp)"),
90 symbolString,
91 ki18nc("amount in units (real)", "%1 miles per imperial gallon"),
92 ki18ncp("amount in units (integer)", "%1 mile per imperial gallon", "%1 miles per imperial gallon"),
93 true)));
94
95 d->addCommonUnit(unit: UnitPrivate::makeUnit(dd: new FuelUnitPrivate(FuelEfficiencyCategory,
96 KilometrePerLitre,
97 100.0,
98 i18nc("fuelefficiency unit symbol", "kmpl"),
99 i18nc("unit description in lists", "kilometers per liter"),
100 i18nc("unit synonyms for matching user input", "kilometer per liter;kilometers per liter;kmpl;km/l"),
101 symbolString,
102 ki18nc("amount in units (real)", "%1 kilometers per liter"),
103 ki18ncp("amount in units (integer)", "%1 kilometer per liter", "%1 kilometers per liter"),
104 true)));
105
106 return c;
107}
108
109} // KUnitConversion namespace
110

source code of kunitconversion/src/fuel_efficiency.cpp