1 | /* |
2 | * SPDX-FileCopyrightText: 2007-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 "temperature_p.h" |
9 | #include "unit_p.h" |
10 | |
11 | #include <KLocalizedString> |
12 | |
13 | namespace KUnitConversion |
14 | { |
15 | class CelsiusUnitPrivate : public UnitPrivate |
16 | { |
17 | public: |
18 | CelsiusUnitPrivate(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 | : UnitPrivate(categoryId, id, multiplier, symbol, description, matchString, symbolString, realString, integerString) |
28 | { |
29 | } |
30 | |
31 | qreal toDefault(qreal value) const override |
32 | { |
33 | return value + 273.15; |
34 | } |
35 | |
36 | qreal fromDefault(qreal value) const override |
37 | { |
38 | return value - 273.15; |
39 | } |
40 | }; |
41 | |
42 | class FahrenheitUnitPrivate : public UnitPrivate |
43 | { |
44 | public: |
45 | FahrenheitUnitPrivate(CategoryId categoryId, |
46 | UnitId id, |
47 | qreal multiplier, |
48 | const QString &symbol, |
49 | const QString &description, |
50 | const QString &matchString, |
51 | const KLocalizedString &symbolString, |
52 | const KLocalizedString &realString, |
53 | const KLocalizedString &integerString) |
54 | : UnitPrivate(categoryId, id, multiplier, symbol, description, matchString, symbolString, realString, integerString) |
55 | { |
56 | } |
57 | |
58 | qreal toDefault(qreal value) const override |
59 | { |
60 | return (value + 459.67) * 5.0 / 9.0; |
61 | } |
62 | |
63 | qreal fromDefault(qreal value) const override |
64 | { |
65 | return (value * 9.0 / 5.0) - 459.67; |
66 | } |
67 | }; |
68 | |
69 | class DelisleUnitPrivate : public UnitPrivate |
70 | { |
71 | public: |
72 | DelisleUnitPrivate(CategoryId categoryId, |
73 | UnitId id, |
74 | qreal multiplier, |
75 | const QString &symbol, |
76 | const QString &description, |
77 | const QString &matchString, |
78 | const KLocalizedString &symbolString, |
79 | const KLocalizedString &realString, |
80 | const KLocalizedString &integerString) |
81 | : UnitPrivate(categoryId, id, multiplier, symbol, description, matchString, symbolString, realString, integerString) |
82 | { |
83 | } |
84 | |
85 | qreal toDefault(qreal value) const override |
86 | { |
87 | return 373.15 - (value * 2.0 / 3.0); |
88 | } |
89 | |
90 | qreal fromDefault(qreal value) const override |
91 | { |
92 | return (373.15 - value) * 3.0 / 2.0; |
93 | } |
94 | }; |
95 | |
96 | class NewtonUnitPrivate : public UnitPrivate |
97 | { |
98 | public: |
99 | NewtonUnitPrivate(CategoryId categoryId, |
100 | UnitId id, |
101 | qreal multiplier, |
102 | const QString &symbol, |
103 | const QString &description, |
104 | const QString &matchString, |
105 | const KLocalizedString &symbolString, |
106 | const KLocalizedString &realString, |
107 | const KLocalizedString &integerString) |
108 | : UnitPrivate(categoryId, id, multiplier, symbol, description, matchString, symbolString, realString, integerString) |
109 | { |
110 | } |
111 | |
112 | qreal toDefault(qreal value) const override |
113 | { |
114 | return (value * 100.0 / 33.0) + 273.15; |
115 | } |
116 | |
117 | qreal fromDefault(qreal value) const override |
118 | { |
119 | return (value - 273.15) * 33.0 / 100.0; |
120 | } |
121 | }; |
122 | |
123 | class ReaumurUnitPrivate : public UnitPrivate |
124 | { |
125 | public: |
126 | ReaumurUnitPrivate(CategoryId categoryId, |
127 | UnitId id, |
128 | qreal multiplier, |
129 | const QString &symbol, |
130 | const QString &description, |
131 | const QString &matchString, |
132 | const KLocalizedString &symbolString, |
133 | const KLocalizedString &realString, |
134 | const KLocalizedString &integerString) |
135 | : UnitPrivate(categoryId, id, multiplier, symbol, description, matchString, symbolString, realString, integerString) |
136 | { |
137 | } |
138 | |
139 | qreal toDefault(qreal value) const override |
140 | { |
141 | return (value * 5.0 / 4.0) + 273.15; |
142 | } |
143 | |
144 | qreal fromDefault(qreal value) const override |
145 | { |
146 | return (value - 273.15) * 4.0 / 5.0; |
147 | } |
148 | }; |
149 | |
150 | class RomerUnitPrivate : public UnitPrivate |
151 | { |
152 | public: |
153 | RomerUnitPrivate(CategoryId categoryId, |
154 | UnitId id, |
155 | qreal multiplier, |
156 | const QString &symbol, |
157 | const QString &description, |
158 | const QString &matchString, |
159 | const KLocalizedString &symbolString, |
160 | const KLocalizedString &realString, |
161 | const KLocalizedString &integerString) |
162 | : UnitPrivate(categoryId, id, multiplier, symbol, description, matchString, symbolString, realString, integerString) |
163 | { |
164 | } |
165 | |
166 | qreal toDefault(qreal value) const override |
167 | { |
168 | return (value - 7.5) * 40.0 / 21.0 + 273.15; |
169 | } |
170 | |
171 | qreal fromDefault(qreal value) const override |
172 | { |
173 | return (value - 273.15) * 21.0 / 40.0 + 7.5; |
174 | } |
175 | }; |
176 | |
177 | UnitCategory Temperature::makeCategory() |
178 | { |
179 | auto c = UnitCategoryPrivate::makeCategory(id: TemperatureCategory, i18n("Temperature" ), i18n("Temperature" )); |
180 | auto d = UnitCategoryPrivate::get(category: c); |
181 | KLocalizedString symbolString = ki18nc("%1 value, %2 unit symbol (temperature)" , "%1 %2" ); |
182 | |
183 | d->addDefaultUnit(unit: UnitPrivate::makeUnit(categoryId: TemperatureCategory, |
184 | id: Kelvin, |
185 | multiplier: 1, |
186 | i18nc("temperature unit symbol" , "K" ), |
187 | i18nc("unit description in lists" , "kelvins" ), |
188 | i18nc("unit synonyms for matching user input" , "kelvin;kelvins;K" ), |
189 | symbolString, |
190 | ki18nc("amount in units (real)" , "%1 kelvins" ), |
191 | ki18ncp("amount in units (integer)" , "%1 kelvin" , "%1 kelvins" ))); |
192 | |
193 | d->addCommonUnit(unit: UnitPrivate::makeUnit(dd: new CelsiusUnitPrivate(TemperatureCategory, |
194 | Celsius, |
195 | 1, |
196 | i18nc("temperature unit symbol" , "°C" ), |
197 | i18nc("unit description in lists" , "Celsius" ), |
198 | i18nc("unit synonyms for matching user input" , "Celsius;°C;C" ), |
199 | symbolString, |
200 | ki18nc("amount in units (real)" , "%1 degrees Celsius" ), |
201 | ki18ncp("amount in units (integer)" , "%1 degree Celsius" , "%1 degrees Celsius" )))); |
202 | |
203 | d->addCommonUnit(unit: UnitPrivate::makeUnit(dd: new FahrenheitUnitPrivate(TemperatureCategory, |
204 | Fahrenheit, |
205 | 1, |
206 | i18nc("temperature unit symbol" , "°F" ), |
207 | i18nc("unit description in lists" , "Fahrenheit" ), |
208 | i18nc("unit synonyms for matching user input" , "Fahrenheit;°F;F" ), |
209 | symbolString, |
210 | ki18nc("amount in units (real)" , "%1 degrees Fahrenheit" ), |
211 | ki18ncp("amount in units (integer)" , "%1 degree Fahrenheit" , "%1 degrees Fahrenheit" )))); |
212 | |
213 | d->addUnit(unit: UnitPrivate::makeUnit(categoryId: TemperatureCategory, |
214 | id: Rankine, |
215 | multiplier: 0.555556, |
216 | i18nc("temperature unit symbol" , "R" ), |
217 | i18nc("unit description in lists" , "Rankine" ), |
218 | i18nc("unit synonyms for matching user input" , "Rankine;°R;R;Ra" ), |
219 | symbolString, |
220 | ki18nc("amount in units (real)" , "%1 Rankine" ), |
221 | ki18ncp("amount in units (integer)" , "%1 Rankine" , "%1 Rankine" ))); |
222 | |
223 | d->addUnit(unit: UnitPrivate::makeUnit(dd: new DelisleUnitPrivate(TemperatureCategory, |
224 | Delisle, |
225 | 1, |
226 | i18nc("temperature unit symbol" , "°De" ), |
227 | i18nc("unit description in lists" , "Delisle" ), |
228 | i18nc("unit synonyms for matching user input" , "Delisle;°De;De" ), |
229 | symbolString, |
230 | ki18nc("amount in units (real)" , "%1 degrees Delisle" ), |
231 | ki18ncp("amount in units (integer)" , "%1 degree Delisle" , "%1 degrees Delisle" )))); |
232 | |
233 | d->addUnit(unit: UnitPrivate::makeUnit(dd: new NewtonUnitPrivate(TemperatureCategory, |
234 | TemperatureNewton, |
235 | 1, |
236 | i18nc("temperature unit symbol" , "°N" ), |
237 | i18nc("unit description in lists" , "Newton" ), |
238 | i18nc("unit synonyms for matching user input" , "Newton;°N;N" ), |
239 | symbolString, |
240 | ki18nc("amount in units (real)" , "%1 degrees Newton" ), |
241 | ki18ncp("amount in units (integer)" , "%1 degree Newton" , "%1 degrees Newton" )))); |
242 | |
243 | d->addUnit(unit: UnitPrivate::makeUnit(dd: new ReaumurUnitPrivate(TemperatureCategory, |
244 | Reaumur, |
245 | 1, |
246 | i18nc("temperature unit symbol" , "°Ré" ), |
247 | i18nc("unit description in lists" , "Réaumur" ), |
248 | i18nc("unit synonyms for matching user input" , "Réaumur;°Ré;Ré;Reaumur;°Re;Re" ), |
249 | symbolString, |
250 | ki18nc("amount in units (real)" , "%1 degrees Réaumur" ), |
251 | ki18ncp("amount in units (integer)" , "%1 degree Réaumur" , "%1 degrees Réaumur" )))); |
252 | |
253 | d->addUnit(unit: UnitPrivate::makeUnit(dd: new RomerUnitPrivate(TemperatureCategory, |
254 | Romer, |
255 | 1, |
256 | i18nc("temperature unit symbol" , "°Rø" ), |
257 | i18nc("unit description in lists" , "Rømer" ), |
258 | i18nc("unit synonyms for matching user input" , "Rømer;°Rø;Rø;Romer;°Ro;Ro" ), |
259 | symbolString, |
260 | ki18nc("amount in units (real)" , "%1 degrees Rømer" ), |
261 | ki18ncp("amount in units (integer)" , "%1 degree Rømer" , "%1 degrees Rømer" )))); |
262 | |
263 | return c; |
264 | } |
265 | |
266 | } // KUnitConversion namespace |
267 | |