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

Properly format stuff

parent 6dc3a933
......@@ -12,6 +12,7 @@ set(kweather_SRCS
weatherlocation.cpp
locationquerymodel.cpp
temperaturechartdata.cpp
formatter.cpp
resources.qrc
)
add_library(kweatherLib STATIC)
......
/*
* SPDX-FileCopyrightText: 2020 Han Young <hanyoung@protonmail.com>
* SPDX-FileCopyrightText: 2020 Devin Lin <espidev@gmail.com>
* SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "formatter.h"
#include <KLocalizedString>
#include "global.h"
#include "kweathersettings.h"
QString Formatter::formatTemperature(qreal temperature) const
{
return i18nc("A temperature", "%1°", Kweather::convertTemp(temperature));
}
QString Formatter::formatTemperatureRounded(qreal temperature) const
{
return i18nc("A temperature", "%1°", qRound(Kweather::convertTemp(temperature)));
}
QString Formatter::formatWindSpeed(qreal speed) const
{
if (KWeatherSettings().speedUnits() == QLatin1String("kph")) {
return i18n("%1 km/h", QString::number(speed, 'g', 1));
} else {
return i18n("%1 mph", QString::number(speed * 0.62, 'g', 1));
}
}
/*
* SPDX-FileCopyrightText: 2020 Han Young <hanyoung@protonmail.com>
* SPDX-FileCopyrightText: 2020 Devin Lin <espidev@gmail.com>
* SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <QObject>
class Formatter : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE QString formatTemperature(qreal temperature) const;
Q_INVOKABLE QString formatTemperatureRounded(qreal temperature) const;
Q_INVOKABLE QString formatWindSpeed(qreal speed) const;
};
......@@ -23,11 +23,6 @@ static double convertTemp(double temp)
}
};
static QString tempConverter(double temp)
{
return QString::number(qRound(convertTemp(temp))) + "°";
}
enum class WindDirection { N, NW, W, SW, S, SE, E, NE };
enum class Backend { NMI, OWM };
struct ResolvedWeatherDesc {
......
......@@ -24,6 +24,7 @@
#include <KLocalizedContext>
#include <KLocalizedString>
#include "formatter.h"
#include "kweathersettings.h"
#include "locationquerymodel.h"
#include "temperaturechartdata.h"
......@@ -73,6 +74,9 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
engine.rootContext()->setContextProperty("settingsModel", &settings);
engine.rootContext()->setContextProperty("locationQueryModel", locationQueryModel);
Formatter formatter;
qmlRegisterSingletonInstance<Formatter>("kweather", 1, 0, "Formatter", &formatter);
qmlRegisterType<TemperatureChartData>("kweather", 1, 0, "TemperatureChartData");
qRegisterMetaType<KWeatherCore::Sunrise>();
......
......@@ -11,6 +11,7 @@ import QtQuick.Layouts 1.2
import QtCharts 2.3
import org.kde.kirigami 2.13 as Kirigami
import "backgrounds"
import kweather 1.0
Kirigami.ScrollablePage {
id: page
......@@ -72,7 +73,7 @@ Kirigami.ScrollablePage {
color: "white"
Layout.alignment: Qt.AlignLeft
horizontalAlignment: Text.AlignLeft
text: page.currentHour.temperatureRounded
text: Formatter.formatTemperatureRounded(page.currentHour.temperature)
font.family: lightHeadingFont.name
}
Label {
......
......@@ -10,6 +10,7 @@ import QtQuick.Controls 2.4
import QtQuick.Layouts 1.2
import QtQuick.Shapes 1.12
import org.kde.kirigami 2.11 as Kirigami
import kweather 1.0
Kirigami.ScrollablePage {
id: page
......@@ -63,7 +64,7 @@ Kirigami.ScrollablePage {
font.pointSize: Kirigami.Theme.defaultFont.pointSize * 3
font.weight: Font.Light
font.family: lightHeadingFont.name
text: page.currentHour.temperatureRounded // TODO
text: Formatter.formatTemperatureRounded(page.currentHour.temperature)
}
Label {
font.pointSize: Kirigami.Theme.defaultFont.pointSize * 1.3
......@@ -151,7 +152,7 @@ Kirigami.ScrollablePage {
delegate: WeatherHourDelegate {
weather: modelData
textColor: weatherLocation.cardTextColor
textColor: Kirigami.Theme.textColor
}
}
......
......@@ -10,6 +10,7 @@ import QtQuick.Controls 2.4
import QtQuick.Layouts 1.2
import QtQuick.Shapes 1.12
import org.kde.kirigami 2.11 as Kirigami
import kweather 1.0
Rectangle {
implicitWidth: Kirigami.Units.gridUnit * 6
......@@ -53,14 +54,14 @@ Rectangle {
Label {
id: highTemp
font.pointSize: Kirigami.Theme.defaultFont.pointSize * 1.3
text: Math.round(weather.maxTemp) + "°"
text: Formatter.formatTemperatureRounded(weather.maxTemp)
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: Math.round(weather.minTemp) + "°"
text: Formatter.formatTemperatureRounded(weather.minTemp)
}
}
Label {
......
......@@ -10,6 +10,7 @@ import QtQuick.Controls 2.4
import QtQuick.Layouts 1.2
import QtQuick.Shapes 1.12
import org.kde.kirigami 2.11 as Kirigami
import kweather 1.0
Rectangle {
implicitWidth: Kirigami.Units.gridUnit * 5
......@@ -34,7 +35,7 @@ Rectangle {
Layout.preferredWidth: Kirigami.Units.iconSizes.medium
}
Label {
text: weather.temperature
text: Formatter.formatTemperature(weather.temperature)
font.pointSize: Kirigami.Theme.defaultFont.pointSize * 1.3
color: textColor
}
......@@ -45,7 +46,6 @@ Rectangle {
// precipitation
RowLayout {
visible: weather != null
Kirigami.Icon {
source: "raindrop"
Layout.preferredHeight: Kirigami.Units.iconSizes.small
......@@ -68,7 +68,7 @@ Rectangle {
}
Label {
color: settingsModel && settingsModel.forecastStyle === "Dynamic" ? KWeatherStyle.disabledTextColor : Kirigami.Theme.disabledTextColor
text: weather.windSpeed
text: Formatter.formatWindSpeed(weather.windSpeed)
}
}
......
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