delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / workspace / plasma / dataengines / weather / ions / weatherutils.cpp
blobfcec3d53ee6023fc479701f12edb2d9db5e1bd7d
1 /*****************************************************************************
2 * Copyright (C) 2007-2008 by Shawn Starr <shawn.starr@rogers.com> *
3 * Copyright (C) 2008 by Teemu Rytilahti <tpr@d5k.net> *
4 * *
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. *
9 * *
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. *
14 * *
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"
22 #include <math.h>
23 #include <KLocalizedString>
25 namespace WeatherUtils
29 float convert(float value, int srcUnit, int destUnit)
31 switch (srcUnit) {
32 case WeatherUtils::Celsius:
33 switch (destUnit) {
34 case WeatherUtils::Fahrenheit:
35 return (value * 9 / 5 + 32);
36 case WeatherUtils::Kelvin:
37 return (value + 273.15);
40 case WeatherUtils::Fahrenheit:
41 switch (destUnit) {
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:
49 switch (destUnit) {
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:
58 switch (destUnit) {
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:
70 switch (destUnit) {
71 case WeatherUtils::Miles:
72 return (value * 2.23693629);
73 case WeatherUtils::Kilometers:
74 return (value * 3.6);
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:
83 switch (destUnit) {
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:
95 switch (destUnit) {
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:
104 switch (destUnit) {
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:
113 switch (destUnit) {
114 case WeatherUtils::Kilopascals:
115 return (value * 0.10);
116 case WeatherUtils::InchesHG:
117 return (value * 0.0295333727);
120 case WeatherUtils::Centimeters:
121 switch (destUnit) {
122 case WeatherUtils::Millimeters:
123 return (value / 0.1);
124 case WeatherUtils::Inches:
125 return (value * 0.393700787);
128 case WeatherUtils::Millimeters:
129 switch (destUnit) {
130 case WeatherUtils::Centimeters:
131 return (value * 0.1);
132 case WeatherUtils::Inches:
133 return (value * 0.0393700787);
136 case WeatherUtils::Inches:
137 switch (destUnit) {
138 case WeatherUtils::Centimeters:
139 return (value * 2.54);
140 case WeatherUtils::Millimeters:
141 return (value * 25.4);
144 case WeatherUtils::Knots:
145 switch (destUnit) {
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);
156 return 0;
159 QString getUnitString(int unit, bool plain)
161 switch (unit) {
162 case WeatherUtils::Celsius:
163 if (plain)
164 return QString("C");
165 else
166 return i18nc("Celsius, temperature unit", "⁰C");
168 case WeatherUtils::Fahrenheit:
169 if (plain)
170 return QString("F");
171 else
172 return i18nc("Fahrenheit, temperature unit", "⁰F");
174 case WeatherUtils::Kelvin:
175 if (plain)
176 return QString("K");
177 else
178 return i18nc("Kelvin, temperature unit", "K");
180 case WeatherUtils::KilometersAnHour:
181 if (plain)
182 return QString("kmh");
183 else
184 return i18nc("kilometers per hour, windspeed unit", "km/h");
186 case WeatherUtils::Kilometers:
187 if (plain)
188 return QString("km");
189 else
190 return i18nc("kilometers, distance unit", "km");
192 case WeatherUtils::MetersPerSecond:
193 if (plain)
194 return QString("ms");
195 else
196 return i18nc("meters per second, windspeed unit", "m/s");
198 case WeatherUtils::MilesAnHour:
199 if (plain)
200 return QString("mph");
201 else
202 return i18nc("miles per hour, windspeed unit", "mph");
204 case WeatherUtils::Miles:
205 if (plain)
206 return QString("mi");
207 else
208 return i18nc("miles, distance unit", "mi");
210 case WeatherUtils::Kilopascals:
211 if (plain)
212 return QString("kpa");
213 else
214 return i18nc("kilopascals, airpressure unit", "kPa");
216 case WeatherUtils::InchesHG:
217 if (plain)
218 return QString("in");
219 else
220 return i18nc("inches hg, airpressure unit", "inHg");
222 case WeatherUtils::Millibars:
223 if (plain)
224 return QString("mbar");
225 else
226 return i18nc("millibars, airpressure unit", "mbar");
228 case WeatherUtils::Hectopascals:
229 if (plain)
230 return QString("hpa");
231 else
232 return i18nc("hectopascals, airpressure unit", "hPa");
234 case WeatherUtils::Centimeters:
235 if (plain)
236 return QString("cm");
237 else
238 return i18nc("centimeters, length unit", "cm");
240 case WeatherUtils::Millimeters:
241 if (plain)
242 return QString("mm");
243 else
244 return i18nc("millimeters, length unit", "mm");
246 case WeatherUtils::Inches:
247 if (plain)
248 return QString("in");
249 else
250 return i18nc("inches, length unit", "in");
252 case WeatherUtils::Knots:
253 if (plain)
254 return QString("kt");
255 else
256 return i18nc("knots, wind speed unit", "kt");
258 case WeatherUtils::Beaufort:
259 if (plain)
260 return QString("bft");
261 else
262 return i18nc("beaufort, wind speed unit", "Bft");
264 default:
265 return QString();
269 QString degreesToCardinal(float degrees)
271 QString direction;
272 if ((degrees >= 348.75 && degrees <= 360) || (degrees > 0 && degrees <= 11.25))
273 direction = "N";
274 else if (degrees >= 11.25 && degrees < 33.75)
275 direction = "NNE";
276 else if (degrees >= 33.75 && degrees < 56.25)
277 direction = "NE";
278 else if (degrees >= 56.25 && degrees < 78.75)
279 direction = "ENE";
280 else if (degrees >= 78.75 && degrees < 101.25)
281 direction = "E";
282 else if (degrees >= 101.25 && degrees < 123.75)
283 direction = "ESE";
284 else if (degrees >= 123.75 && degrees < 146.25)
285 direction = "SE";
286 else if (degrees >= 146.25 && degrees < 168.75)
287 direction = "SSE";
288 else if (degrees >= 168.75 && degrees < 191.25)
289 direction = "S";
290 else if (degrees >= 191.25 && degrees < 213.75)
291 direction = "SSW";
292 else if (degrees >= 213.75 && degrees < 236.25)
293 direction = "SW";
294 else if (degrees >= 236.25 && degrees < 258.75)
295 direction = "WSW";
296 else if (degrees >= 258.75 && degrees < 281.25)
297 direction = "W";
298 else if (degrees >= 281.25 && degrees < 303.75)
299 direction = "WNW";
300 else if (degrees >= 303.75 && degrees < 326.25)
301 direction = "NW";
302 else if (degrees >= 326.25 && degrees < 248.75)
303 direction = "NNW";
305 if (!direction.isEmpty())
306 return direction;
307 else
308 return QString();
312 int knotsToBeaufort(float knots)
314 if (knots < 1) {
315 return 0;
316 } else if (knots >= 1 && knots < 4) {
317 return 1;
318 } else if (knots >= 4 && knots < 7) {
319 return 2;
320 } else if (knots >= 7 && knots < 11) {
321 return 3;
322 } else if (knots >= 11 && knots < 16) {
323 return 4;
324 } else if (knots >= 16 && knots < 22) {
325 return 5;
326 } else if (knots >= 22 && knots < 28) {
327 return 6;
328 } else if (knots >= 28 && knots < 34) {
329 return 7;
330 } else if (knots >= 34 && knots < 41) {
331 return 8;
332 } else if (knots >= 41 && knots < 48) {
333 return 9;
334 } else if (knots >= 48 && knots < 56) {
335 return 10;
336 } else if (knots >= 56 && knots < 64) {
337 return 11;
338 } else {
339 return 12;
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