• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Engines

weatherutils.cpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright (C) 2007-2008 by Shawn Starr <shawn.starr@rogers.com>           *
00003  * Copyright (C) 2008 by Teemu Rytilahti <tpr@d5k.net>                       *
00004  *                                                                           *
00005  * This library is free software; you can redistribute it and/or             *
00006  * modify it under the terms of the GNU Library General Public               *
00007  * License as published by the Free Software Foundation; either              *
00008  * version 2 of the License, or (at your option) any later version.          *
00009  *                                                                           *
00010  * This library is distributed in the hope that it will be useful,           *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00013  * Library General Public License for more details.                          *
00014  *                                                                           *
00015  * You should have received a copy of the GNU Library General Public License *
00016  * along with this library; see the file COPYING.LIB.  If not, write to      *
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
00018  * Boston, MA 02110-1301, USA.                                               *
00019  *****************************************************************************/
00020 
00021 #include "weatherutils.h"
00022 #include <math.h>
00023 #include <KLocalizedString>
00024 
00025 namespace WeatherUtils
00026 {
00027 
00028 
00029 float convert(float value, int srcUnit, int destUnit)
00030 {
00031     switch (srcUnit) {
00032     case WeatherUtils::Celsius:
00033         switch (destUnit) {
00034         case WeatherUtils::Fahrenheit:
00035             return (value * 9 / 5 + 32);
00036         case WeatherUtils::Kelvin:
00037             return (value + 273.15);
00038         };
00039 
00040     case WeatherUtils::Fahrenheit:
00041         switch (destUnit) {
00042         case WeatherUtils::Celsius:
00043             return (value - 32) * 5 / 9;
00044         case WeatherUtils::Kelvin:
00045             return (5 / 9 * (value - 32) + 273.15);
00046         };
00047 
00048     case WeatherUtils::Kelvin:
00049         switch (destUnit) {
00050         case WeatherUtils::Celsius:
00051             return (value - 273.15);
00052         case WeatherUtils::Fahrenheit:
00053             return ((value - 273.15) * 1.8) + 32;
00054         };
00055 
00056     case WeatherUtils::Kilometers:
00057     case WeatherUtils::KilometersAnHour:
00058         switch (destUnit) {
00059         case WeatherUtils::Miles:
00060             return (0.621371192 * value);
00061         case WeatherUtils::MetersPerSecond:
00062             return (value * 0.277778);
00063         case WeatherUtils::Knots:
00064             return (value * 0.539956803);
00065         case WeatherUtils::Beaufort:
00066             return kilometersToBeaufort(value);
00067         };
00068 
00069     case WeatherUtils::MetersPerSecond:
00070         switch (destUnit) {
00071         case WeatherUtils::Miles:
00072             return (value * 2.23693629);
00073         case WeatherUtils::Kilometers:
00074             return (value * 3.6);
00075         case WeatherUtils::Knots:
00076             return (value * 1.943845);
00077         case WeatherUtils::Beaufort:
00078             return metersPerSecondToBeaufort(value);
00079         };
00080 
00081     case WeatherUtils::Miles:
00082     case WeatherUtils::MilesAnHour:
00083         switch (destUnit) {
00084         case WeatherUtils::Kilometers:
00085             return (1.609344 * value);
00086         case WeatherUtils::MetersPerSecond:
00087             return (value * 0.44704);
00088         case WeatherUtils::Knots:
00089             return (value * 0.868976242);
00090         case WeatherUtils::Beaufort:
00091             return milesToBeaufort(value);
00092         };
00093 
00094     case WeatherUtils::Kilopascals:
00095         switch (destUnit) {
00096         case WeatherUtils::InchesHG:
00097             return ((0.02952997 * value) * 10);
00098         case WeatherUtils::Millibars:
00099         case WeatherUtils::Hectopascals:
00100             return (value / 0.10);
00101         };
00102 
00103     case WeatherUtils::InchesHG:
00104         switch (destUnit) {
00105         case WeatherUtils::Kilopascals:
00106             return (value * 3.386389);
00107         case WeatherUtils::Millibars:
00108         case WeatherUtils::Hectopascals:
00109             return (value * 33.8637526);
00110         };
00111 
00112     case WeatherUtils::Millibars:
00113         switch (destUnit) {
00114         case WeatherUtils::Kilopascals:
00115             return (value * 0.10);
00116         case WeatherUtils::InchesHG:
00117             return (value * 0.0295333727);
00118         };
00119 
00120     case WeatherUtils::Centimeters:
00121         switch (destUnit) {
00122         case WeatherUtils::Millimeters:
00123             return (value / 0.1);
00124         case WeatherUtils::Inches:
00125             return (value * 0.393700787);
00126         };
00127 
00128     case WeatherUtils::Millimeters:
00129         switch (destUnit) {
00130         case WeatherUtils::Centimeters:
00131             return (value * 0.1);
00132         case WeatherUtils::Inches:
00133             return (value * 0.0393700787);
00134         };
00135 
00136     case WeatherUtils::Inches:
00137         switch (destUnit) {
00138         case WeatherUtils::Centimeters:
00139             return (value * 2.54);
00140         case WeatherUtils::Millimeters:
00141             return (value * 25.4);
00142         };
00143 
00144     case WeatherUtils::Knots:
00145         switch (destUnit) {
00146         case WeatherUtils::Kilometers:
00147             return floor(value * 1.852 + 0.5);
00148         case WeatherUtils::Miles:
00149             return (value * 1.507794);
00150         case WeatherUtils::MetersPerSecond:
00151             return (value * 1.9438);
00152         case WeatherUtils::Beaufort:
00153             return knotsToBeaufort(value);
00154         };
00155     };
00156     return 0;
00157 }
00158 
00159 QString getUnitString(int unit, bool plain)
00160 {
00161     switch (unit) {
00162     case WeatherUtils::Celsius:
00163         if (plain)
00164             return QString("C");
00165         else
00166             return i18nc("Celsius, temperature unit", "⁰C");
00167 
00168     case WeatherUtils::Fahrenheit:
00169         if (plain)
00170             return QString("F");
00171         else
00172             return i18nc("Fahrenheit, temperature unit", "⁰F");
00173 
00174     case WeatherUtils::Kelvin:
00175         if (plain)
00176             return QString("K");
00177         else
00178             return i18nc("Kelvin, temperature unit", "K");
00179 
00180     case WeatherUtils::KilometersAnHour:
00181         if (plain)
00182             return QString("kmh");
00183         else
00184             return i18nc("kilometers per hour, windspeed unit", "km/h");
00185 
00186     case WeatherUtils::Kilometers:
00187         if (plain)
00188             return QString("km");
00189         else
00190             return i18nc("kilometers, distance unit", "km");
00191 
00192     case WeatherUtils::MetersPerSecond:
00193         if (plain)
00194             return QString("ms");
00195         else
00196             return i18nc("meters per second, windspeed unit", "m/s");
00197 
00198     case WeatherUtils::MilesAnHour:
00199         if (plain)
00200             return QString("mph");
00201         else
00202             return i18nc("miles per hour, windspeed unit", "mph");
00203 
00204     case WeatherUtils::Miles:
00205         if (plain)
00206             return QString("mi");
00207         else
00208             return i18nc("miles, distance unit", "mi");
00209 
00210     case WeatherUtils::Kilopascals:
00211         if (plain)
00212             return QString("kpa");
00213         else
00214             return i18nc("kilopascals, airpressure unit", "kPa");
00215 
00216     case WeatherUtils::InchesHG:
00217         if (plain)
00218             return QString("in");
00219         else
00220             return i18nc("inches hg, airpressure unit", "inHg");
00221 
00222     case WeatherUtils::Millibars:
00223         if (plain)
00224             return QString("mbar");
00225         else
00226             return i18nc("millibars, airpressure unit", "mbar");
00227 
00228     case WeatherUtils::Hectopascals:
00229         if (plain)
00230             return QString("hpa");
00231         else
00232             return i18nc("hectopascals, airpressure unit", "hPa");
00233 
00234     case WeatherUtils::Centimeters:
00235         if (plain)
00236             return QString("cm");
00237         else
00238             return i18nc("centimeters, length unit", "cm");
00239 
00240     case WeatherUtils::Millimeters:
00241         if (plain)
00242             return QString("mm");
00243         else
00244             return i18nc("millimeters, length unit", "mm");
00245 
00246     case WeatherUtils::Inches:
00247         if (plain)
00248             return QString("in");
00249         else
00250             return i18nc("inches, length unit", "in");
00251 
00252     case WeatherUtils::Knots:
00253         if (plain)
00254             return QString("kt");
00255         else
00256             return i18nc("knots, wind speed unit", "kt");
00257 
00258     case WeatherUtils::Beaufort:
00259         if (plain)
00260             return QString("bft");
00261         else
00262             return i18nc("beaufort, wind speed unit", "Bft");
00263 
00264     default:
00265         return QString();
00266     }
00267 }
00268 
00269 QString degreesToCardinal(float degrees)
00270 {
00271     QString direction;
00272     if ((degrees >= 348.75 && degrees <= 360) || (degrees > 0 && degrees <= 11.25))
00273         direction = "N";
00274     else if (degrees >= 11.25 && degrees < 33.75)
00275         direction = "NNE";
00276     else if (degrees >= 33.75 && degrees < 56.25)
00277         direction = "NE";
00278     else if (degrees >= 56.25 && degrees < 78.75)
00279         direction = "ENE";
00280     else if (degrees >= 78.75 && degrees < 101.25)
00281         direction = "E";
00282     else if (degrees >= 101.25 && degrees < 123.75)
00283         direction = "ESE";
00284     else if (degrees >= 123.75 && degrees < 146.25)
00285         direction = "SE";
00286     else if (degrees >= 146.25 && degrees < 168.75)
00287         direction = "SSE";
00288     else if (degrees >= 168.75 && degrees < 191.25)
00289         direction = "S";
00290     else if (degrees >= 191.25 && degrees < 213.75)
00291         direction = "SSW";
00292     else if (degrees >= 213.75 && degrees < 236.25)
00293         direction = "SW";
00294     else if (degrees >= 236.25 && degrees < 258.75)
00295         direction = "WSW";
00296     else if (degrees >= 258.75 && degrees < 281.25)
00297         direction = "W";
00298     else if (degrees >= 281.25 && degrees < 303.75)
00299         direction = "WNW";
00300     else if (degrees >= 303.75 && degrees < 326.25)
00301         direction = "NW";
00302     else if (degrees >= 326.25 && degrees < 248.75)
00303         direction = "NNW";
00304 
00305     if (!direction.isEmpty())
00306         return direction;
00307     else
00308         return QString();
00309 }
00310 
00311 
00312 int knotsToBeaufort(float knots)
00313 {
00314     if (knots < 1) {
00315         return 0;
00316     } else if (knots >= 1 && knots < 4) {
00317         return 1;
00318     } else if (knots >= 4 && knots < 7) {
00319         return 2;
00320     } else if (knots >= 7 && knots < 11) {
00321         return 3;
00322     } else if (knots >= 11 && knots < 16) {
00323         return 4;
00324     } else if (knots >= 16 && knots < 22) {
00325         return 5;
00326     } else if (knots >= 22 && knots < 28) {
00327         return 6;
00328     } else if (knots >= 28 && knots < 34) {
00329         return 7;
00330     } else if (knots >= 34 && knots < 41) {
00331         return 8;
00332     } else if (knots >= 41 && knots < 48) {
00333         return 9;
00334     } else if (knots >= 48 && knots < 56) {
00335         return 10;
00336     } else if (knots >= 56 && knots < 64) {
00337         return 11;
00338     } else {
00339         return 12;
00340     }
00341 }
00342 
00343 int milesToBeaufort(float miles)
00344 {
00345     return knotsToBeaufort(miles / 1.1507794);
00346 }
00347 
00348 int kilometersToBeaufort(float km)
00349 {
00350     return knotsToBeaufort(km / 1.852);
00351 }
00352 
00353 int metersPerSecondToBeaufort(float ms)
00354 {
00355     return knotsToBeaufort(ms * 1.943845);
00356 }
00357 
00358 } // namespace WeatherUtils
00359 

Engines

Skip menu "Engines"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal