diff options
Diffstat (limited to 'libsowatch')
-rw-r--r-- | libsowatch/weathernotification.cpp | 21 | ||||
-rw-r--r-- | libsowatch/weathernotification.h | 6 |
2 files changed, 27 insertions, 0 deletions
diff --git a/libsowatch/weathernotification.cpp b/libsowatch/weathernotification.cpp index fa82f3d..ddfd60b 100644 --- a/libsowatch/weathernotification.cpp +++ b/libsowatch/weathernotification.cpp @@ -6,3 +6,24 @@ WeatherNotification::WeatherNotification(QObject *parent) : Notification(parent) { } + +qreal WeatherNotification::convertTemperature(qreal temp, Unit from, Unit to) +{ + if (from == to) { + return temp; + } else if (from == Celsius && to == Fahrenheit) { + return temp * (9.0f/5.0f) + 32.0f; + } else if (from == Fahrenheit && to == Celsius) { + return (temp - 32.0f) * (5.0f/9.0f); + } +} + + +int WeatherNotification::convertTemperature(int temp, Unit from, Unit to) +{ + if (from == to) { + return temp; + } else { + return qRound(convertTemperature(static_cast<qreal>(temp), from, to)); + } +} diff --git a/libsowatch/weathernotification.h b/libsowatch/weathernotification.h index de3152a..843a844 100644 --- a/libsowatch/weathernotification.h +++ b/libsowatch/weathernotification.h @@ -32,8 +32,14 @@ public: virtual WeatherType forecast() = 0; virtual int temperature() = 0; virtual Unit temperatureUnits() = 0; + + /** Quite useful helper function. */ + static qreal convertTemperature(qreal temp, Unit from, Unit to); + static int convertTemperature(int temp, Unit from, Unit to); }; + + } #endif // WEATHERNOTIFICATION_H |