Commit 8cf33784 authored by Nicolas Fella's avatar Nicolas Fella Committed by Han Young
Browse files

Fix switching unit at runtime

Currently when switching units it only applies on restart

This code is a bit more verbose but it results in a binding reevaluation when the unit changes so the UI updates accordingly
parent 7502ddf1
Pipeline #77209 passed with stage
in 17 seconds
......@@ -11,21 +11,20 @@
#include <KLocalizedString>
#include "global.h"
#include "kweathersettings.h"
QString Formatter::formatTemperature(qreal temperature) const
QString Formatter::formatTemperature(qreal temperature, const QString &unit) const
{
return i18nc("A temperature", "%1°", Kweather::convertTemp(temperature));
return i18nc("A temperature", "%1°", Kweather::convertTemp(temperature, unit));
}
QString Formatter::formatTemperatureRounded(qreal temperature) const
QString Formatter::formatTemperatureRounded(qreal temperature, const QString &unit) const
{
return i18nc("A temperature", "%1°", qRound(Kweather::convertTemp(temperature)));
return i18nc("A temperature", "%1°", qRound(Kweather::convertTemp(temperature, unit)));
}
QString Formatter::formatWindSpeed(qreal speed) const
QString Formatter::formatWindSpeed(qreal speed, const QString &unit) const
{
if (KWeatherSettings::self()->speedUnits() == QLatin1String("kph")) {
if (unit == QLatin1String("kph")) {
return i18n("%1 km/h", QString::number(speed, 'g', 1));
} else {
return i18n("%1 mph", QString::number(speed * 0.62, 'g', 1));
......
......@@ -14,8 +14,8 @@ class Formatter : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE QString formatTemperature(qreal temperature) const;
Q_INVOKABLE QString formatTemperatureRounded(qreal temperature) const;
Q_INVOKABLE QString formatTemperature(qreal temperature, const QString &unit) const;
Q_INVOKABLE QString formatTemperatureRounded(qreal temperature, const QString &unit) const;
Q_INVOKABLE QString formatWindSpeed(qreal speed) const;
Q_INVOKABLE QString formatWindSpeed(qreal speed, const QString &unit) const;
};
......@@ -14,9 +14,9 @@ namespace Kweather
{
static const QString WEATHER_LOCATIONS_CFG_GROUP = QStringLiteral("WeatherLocations");
static double convertTemp(double temp)
static double convertTemp(double temp, const QString &unit)
{
if (KWeatherSettings::self()->temperatureUnits() == "Fahrenheit") {
if (unit == QLatin1String("Fahrenheit")) {
return temp * 1.8 + 32;
} else {
return temp;
......
......@@ -74,7 +74,7 @@ Kirigami.ScrollablePage {
color: "white"
Layout.alignment: Qt.AlignLeft
horizontalAlignment: Text.AlignLeft
text: Formatter.formatTemperatureRounded(page.currentHour.temperature)
text: Formatter.formatTemperatureRounded(page.currentHour.temperature, settingsModel.temperatureUnits)
font.family: lightHeadingFont.name
}
Label {
......
......@@ -65,7 +65,7 @@ Kirigami.ScrollablePage {
font.pointSize: Kirigami.Theme.defaultFont.pointSize * 3
font.weight: Font.Light
font.family: lightHeadingFont.name
text: Formatter.formatTemperatureRounded(page.currentHour.temperature)
text: Formatter.formatTemperatureRounded(page.currentHour.temperature, settingsModel.temperatureUnits)
}
Label {
font.pointSize: Kirigami.Theme.defaultFont.pointSize * 1.3
......
......@@ -11,6 +11,8 @@ import QtQuick.Controls 2.4
import QtQuick.Layouts 1.2
import org.kde.kirigami 2.12 as Kirigami
import kweather 1.0
Kirigami.ScrollablePage {
title: i18n("Locations")
......@@ -120,7 +122,7 @@ Kirigami.ScrollablePage {
Label {
Layout.alignment: Qt.AlignHCenter
font.pointSize: Kirigami.Theme.defaultFont.pointSize * 1.3
text: location.todayForecast.maxTemp
text: Formatter.formatTemperatureRounded(location.todayForecast.maxTemp, settingsModel.temperatureUnits)
}
}
......
......@@ -54,14 +54,14 @@ Rectangle {
Label {
id: highTemp
font.pointSize: Kirigami.Theme.defaultFont.pointSize * 1.3
text: Formatter.formatTemperatureRounded(weather.maxTemp)
text: Formatter.formatTemperatureRounded(weather.maxTemp, settingsModel.temperatureUnits)
color: textColor
}
Label {
anchors.baseline: highTemp.baseline
color: settingsModel && settingsModel.forecastStyle === "Dynamic" ? KWeatherStyle.disabledTextColor : Kirigami.Theme.disabledTextColor
font.pointSize: Kirigami.Theme.defaultFont.pointSize * 1
text: Formatter.formatTemperatureRounded(weather.minTemp)
text: Formatter.formatTemperatureRounded(weather.minTemp, settingsModel.temperatureUnits)
}
}
Label {
......
......@@ -35,7 +35,7 @@ Rectangle {
Layout.preferredWidth: Kirigami.Units.iconSizes.medium
}
Label {
text: Formatter.formatTemperature(weather.temperature)
text: Formatter.formatTemperature(weather.temperature, settingsModel.temperatureUnits)
font.pointSize: Kirigami.Theme.defaultFont.pointSize * 1.3
color: textColor
}
......@@ -68,7 +68,7 @@ Rectangle {
}
Label {
color: settingsModel && settingsModel.forecastStyle === "Dynamic" ? KWeatherStyle.disabledTextColor : Kirigami.Theme.disabledTextColor
text: Formatter.formatWindSpeed(weather.windSpeed)
text: Formatter.formatWindSpeed(weather.windSpeed, settingsModel.speedUnits)
}
}
......
......@@ -45,8 +45,8 @@ void TemperatureChartData::updateData()
for (const QVariant &dayVariant : qAsConst(m_weatherData)) {
const auto day = dayVariant.value<KWeatherCore::DailyWeatherForecast>();
const double dayMinTemp = Kweather::convertTemp(day.minTemp());
const double dayMaxTemp = Kweather::convertTemp(day.maxTemp());
const double dayMinTemp = Kweather::convertTemp(day.minTemp(), KWeatherSettings::self()->temperatureUnits());
const double dayMaxTemp = Kweather::convertTemp(day.maxTemp(), KWeatherSettings::self()->temperatureUnits());
result.append(QPointF(day.date().startOfDay().toMSecsSinceEpoch(), dayMaxTemp));
minTemp = std::min<double>(dayMinTemp, minTemp);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment