1 /*****************************************************************************
2 * Copyright (C) 2007-2008 by Shawn Starr <shawn.starr@rogers.com> *
3 * Copyright (C) 2008 by Teemu Rytilahti <tpr@d5k.net> *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Library General Public *
7 * License as published by the Free Software Foundation; either *
8 * version 2 of the License, or (at your option) any later version. *
10 * This library is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * Library General Public License for more details. *
15 * You should have received a copy of the GNU Library General Public License *
16 * along with this library; see the file COPYING.LIB. If not, write to *
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
18 * Boston, MA 02110-1301, USA. *
19 *****************************************************************************/
21 #include "weatherutils.h"
23 #include <KLocalizedString>
25 namespace WeatherUtils
29 float convert(float value
, int srcUnit
, int destUnit
)
32 case WeatherUtils::Celsius
:
34 case WeatherUtils::Fahrenheit
:
35 return (value
* 9 / 5 + 32);
36 case WeatherUtils::Kelvin
:
37 return (value
+ 273.15);
40 case WeatherUtils::Fahrenheit
:
42 case WeatherUtils::Celsius
:
43 return (value
- 32) * 5 / 9;
44 case WeatherUtils::Kelvin
:
45 return (5 / 9 * (value
- 32) + 273.15);
48 case WeatherUtils::Kelvin
:
50 case WeatherUtils::Celsius
:
51 return (value
- 273.15);
52 case WeatherUtils::Fahrenheit
:
53 return ((value
- 273.15) * 1.8) + 32;
56 case WeatherUtils::Kilometers
:
57 case WeatherUtils::KilometersAnHour
:
59 case WeatherUtils::Miles
:
60 return (0.621371192 * value
);
61 case WeatherUtils::MetersPerSecond
:
62 return (value
* 0.277778);
63 case WeatherUtils::Knots
:
64 return (value
* 0.539956803);
65 case WeatherUtils::Beaufort
:
66 return kilometersToBeaufort(value
);
69 case WeatherUtils::MetersPerSecond
:
71 case WeatherUtils::Miles
:
72 return (value
* 2.23693629);
73 case WeatherUtils::Kilometers
:
75 case WeatherUtils::Knots
:
76 return (value
* 1.943845);
77 case WeatherUtils::Beaufort
:
78 return metersPerSecondToBeaufort(value
);
81 case WeatherUtils::Miles
:
82 case WeatherUtils::MilesAnHour
:
84 case WeatherUtils::Kilometers
:
85 return (1.609344 * value
);
86 case WeatherUtils::MetersPerSecond
:
87 return (value
* 0.44704);
88 case WeatherUtils::Knots
:
89 return (value
* 0.868976242);
90 case WeatherUtils::Beaufort
:
91 return milesToBeaufort(value
);
94 case WeatherUtils::Kilopascals
:
96 case WeatherUtils::InchesHG
:
97 return ((0.02952997 * value
) * 10);
98 case WeatherUtils::Millibars
:
99 case WeatherUtils::Hectopascals
:
100 return (value
/ 0.10);
103 case WeatherUtils::InchesHG
:
105 case WeatherUtils::Kilopascals
:
106 return (value
* 3.386389);
107 case WeatherUtils::Millibars
:
108 case WeatherUtils::Hectopascals
:
109 return (value
* 33.8637526);
112 case WeatherUtils::Millibars
:
114 case WeatherUtils::Kilopascals
:
115 return (value
* 0.10);
116 case WeatherUtils::InchesHG
:
117 return (value
* 0.0295333727);
120 case WeatherUtils::Centimeters
:
122 case WeatherUtils::Millimeters
:
123 return (value
/ 0.1);
124 case WeatherUtils::Inches
:
125 return (value
* 0.393700787);
128 case WeatherUtils::Millimeters
:
130 case WeatherUtils::Centimeters
:
131 return (value
* 0.1);
132 case WeatherUtils::Inches
:
133 return (value
* 0.0393700787);
136 case WeatherUtils::Inches
:
138 case WeatherUtils::Centimeters
:
139 return (value
* 2.54);
140 case WeatherUtils::Millimeters
:
141 return (value
* 25.4);
144 case WeatherUtils::Knots
:
146 case WeatherUtils::Kilometers
:
147 return floor(value
* 1.852 + 0.5);
148 case WeatherUtils::Miles
:
149 return (value
* 1.507794);
150 case WeatherUtils::MetersPerSecond
:
151 return (value
* 1.9438);
152 case WeatherUtils::Beaufort
:
153 return knotsToBeaufort(value
);
159 QString
getUnitString(int unit
, bool plain
)
162 case WeatherUtils::Celsius
:
166 return i18nc("Celsius, temperature unit", "⁰C");
168 case WeatherUtils::Fahrenheit
:
172 return i18nc("Fahrenheit, temperature unit", "⁰F");
174 case WeatherUtils::Kelvin
:
178 return i18nc("Kelvin, temperature unit", "K");
180 case WeatherUtils::KilometersAnHour
:
182 return QString("kmh");
184 return i18nc("kilometers per hour, windspeed unit", "km/h");
186 case WeatherUtils::Kilometers
:
188 return QString("km");
190 return i18nc("kilometers, distance unit", "km");
192 case WeatherUtils::MetersPerSecond
:
194 return QString("ms");
196 return i18nc("meters per second, windspeed unit", "m/s");
198 case WeatherUtils::MilesAnHour
:
200 return QString("mph");
202 return i18nc("miles per hour, windspeed unit", "mph");
204 case WeatherUtils::Miles
:
206 return QString("mi");
208 return i18nc("miles, distance unit", "mi");
210 case WeatherUtils::Kilopascals
:
212 return QString("kpa");
214 return i18nc("kilopascals, airpressure unit", "kPa");
216 case WeatherUtils::InchesHG
:
218 return QString("in");
220 return i18nc("inches hg, airpressure unit", "inHg");
222 case WeatherUtils::Millibars
:
224 return QString("mbar");
226 return i18nc("millibars, airpressure unit", "mbar");
228 case WeatherUtils::Hectopascals
:
230 return QString("hpa");
232 return i18nc("hectopascals, airpressure unit", "hPa");
234 case WeatherUtils::Centimeters
:
236 return QString("cm");
238 return i18nc("centimeters, length unit", "cm");
240 case WeatherUtils::Millimeters
:
242 return QString("mm");
244 return i18nc("millimeters, length unit", "mm");
246 case WeatherUtils::Inches
:
248 return QString("in");
250 return i18nc("inches, length unit", "in");
252 case WeatherUtils::Knots
:
254 return QString("kt");
256 return i18nc("knots, wind speed unit", "kt");
258 case WeatherUtils::Beaufort
:
260 return QString("bft");
262 return i18nc("beaufort, wind speed unit", "Bft");
269 QString
degreesToCardinal(float degrees
)
272 if ((degrees
>= 348.75 && degrees
<= 360) || (degrees
> 0 && degrees
<= 11.25))
274 else if (degrees
>= 11.25 && degrees
< 33.75)
276 else if (degrees
>= 33.75 && degrees
< 56.25)
278 else if (degrees
>= 56.25 && degrees
< 78.75)
280 else if (degrees
>= 78.75 && degrees
< 101.25)
282 else if (degrees
>= 101.25 && degrees
< 123.75)
284 else if (degrees
>= 123.75 && degrees
< 146.25)
286 else if (degrees
>= 146.25 && degrees
< 168.75)
288 else if (degrees
>= 168.75 && degrees
< 191.25)
290 else if (degrees
>= 191.25 && degrees
< 213.75)
292 else if (degrees
>= 213.75 && degrees
< 236.25)
294 else if (degrees
>= 236.25 && degrees
< 258.75)
296 else if (degrees
>= 258.75 && degrees
< 281.25)
298 else if (degrees
>= 281.25 && degrees
< 303.75)
300 else if (degrees
>= 303.75 && degrees
< 326.25)
302 else if (degrees
>= 326.25 && degrees
< 248.75)
305 if (!direction
.isEmpty())
312 int knotsToBeaufort(float knots
)
316 } else if (knots
>= 1 && knots
< 4) {
318 } else if (knots
>= 4 && knots
< 7) {
320 } else if (knots
>= 7 && knots
< 11) {
322 } else if (knots
>= 11 && knots
< 16) {
324 } else if (knots
>= 16 && knots
< 22) {
326 } else if (knots
>= 22 && knots
< 28) {
328 } else if (knots
>= 28 && knots
< 34) {
330 } else if (knots
>= 34 && knots
< 41) {
332 } else if (knots
>= 41 && knots
< 48) {
334 } else if (knots
>= 48 && knots
< 56) {
336 } else if (knots
>= 56 && knots
< 64) {
343 int milesToBeaufort(float miles
)
345 return knotsToBeaufort(miles
/ 1.1507794);
348 int kilometersToBeaufort(float km
)
350 return knotsToBeaufort(km
/ 1.852);
353 int metersPerSecondToBeaufort(float ms
)
355 return knotsToBeaufort(ms
* 1.943845);
358 } // namespace WeatherUtils