KWeatherCore

hourlyweatherforecast.cpp
1/*
2 * SPDX-FileCopyrightText: 2020-2021 Han Young <hanyoung@protonmail.com>
3 * SPDX-FileCopyrightText: 2020 Devin Lin <espidev@gmail.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7#include "hourlyweatherforecast.h"
8
9#include <QJsonObject>
10
11#include <cmath>
12
13namespace KWeatherCore
14{
15class HourlyWeatherForecastPrivate : public QSharedData
16{
17public:
19 QString weatherDescription = QStringLiteral("Unknown");
20 QString weatherIcon = QStringLiteral("weather-none-available");
21 // weather icon without time of day
22 QString neutralWeatherIcon = QStringLiteral("weather-none-available");
23 QString symbolCode;
24 double temperature = 0; // celsius
25 double pressure = 0; // hPa
26 double windDirection = NAN;
27 double windSpeed = 0; // m/s
28 double humidity = 0; // %
29 double fog = 0; // %
30 double uvIndex = 0; // 0-1
31 double precipitationAmount = 0; // mm
32};
34 : d(new HourlyWeatherForecastPrivate)
35{
36 d->date = date;
37}
38
39HourlyWeatherForecast::HourlyWeatherForecast(HourlyWeatherForecast &&other) noexcept = default;
40HourlyWeatherForecast &HourlyWeatherForecast::operator=(const HourlyWeatherForecast &other) = default;
41HourlyWeatherForecast::~HourlyWeatherForecast() = default;
42
43HourlyWeatherForecast::HourlyWeatherForecast()
44 : d(new HourlyWeatherForecastPrivate)
45{
46}
47
49HourlyWeatherForecast &HourlyWeatherForecast::operator=(HourlyWeatherForecast &&other) noexcept = default;
51{
52 QJsonObject obj;
53 obj[QLatin1String("date")] = date().toString(Qt::ISODate);
54 obj[QLatin1String("weatherDescription")] = weatherDescription();
55 obj[QLatin1String("weatherIcon")] = weatherIcon();
56 obj[QLatin1String("neutralWeatherIcon")] = neutralWeatherIcon();
57 obj[QLatin1String("temperature")] = temperature();
58 obj[QLatin1String("pressure")] = pressure();
59 obj[QLatin1String("windDirectionDegree")] = windDirectionDegree();
60 obj[QLatin1String("windSpeed")] = windSpeed();
61 obj[QLatin1String("humidity")] = humidity();
62 obj[QLatin1String("fog")] = fog();
63 obj[QLatin1String("uvIndex")] = uvIndex();
64 obj[QLatin1String("precipitationAmount")] = precipitationAmount();
65 return obj;
66}
68{
70 ret.setWeatherDescription(obj[QLatin1String("weatherDescription")].toString());
71 ret.setWeatherIcon(obj[QLatin1String("weatherIcon")].toString());
72 ret.setNeutralWeatherIcon(obj[QLatin1String("neutralWeatherIcon")].toString());
73 ret.setTemperature(obj[QLatin1String("temperature")].toDouble());
74 ret.setPressure(obj[QLatin1String("pressure")].toDouble());
75 ret.setWindDirectionDegree(obj[QLatin1String("windDirectionDegree")].toDouble(NAN));
76 ret.setWindSpeed(obj[QLatin1String("windSpeed")].toDouble());
77 ret.setHumidity(obj[QLatin1String("humidity")].toDouble());
78 ret.setFog(obj[QLatin1String("fog")].toDouble());
79 ret.setUvIndex(obj[QLatin1String("uvIndex")].toDouble());
80 ret.setPrecipitationAmount(obj[QLatin1String("precipitationAmount")].toDouble());
81 return ret;
82}
83const QDateTime &HourlyWeatherForecast::date() const
84{
85 return d->date;
86}
88{
89 d->date = std::move(date);
90}
91const QString &HourlyWeatherForecast::weatherDescription() const
92{
93 return d->weatherDescription;
94}
96{
97 d->weatherDescription = weatherDescription;
98}
99const QString &HourlyWeatherForecast::weatherIcon() const
100{
101 return d->weatherIcon;
102}
104{
105 d->weatherIcon = weatherIcon;
106}
107const QString &HourlyWeatherForecast::neutralWeatherIcon() const
108{
109 return d->neutralWeatherIcon;
110}
112{
113 d->neutralWeatherIcon = neutralWeatherIcon;
114}
116{
117 return d->symbolCode;
118}
120{
121 d->symbolCode = symbolCode;
122}
123double HourlyWeatherForecast::temperature() const
124{
125 return d->temperature;
126}
128{
129 d->temperature = temperature;
130}
131double HourlyWeatherForecast::pressure() const
132{
133 return d->pressure;
134}
136{
137 d->pressure = pressure;
138}
139
140double HourlyWeatherForecast::windDirectionDegree() const
141{
142 return d->windDirection;
143}
144
146{
147 d->windDirection = windDirection;
148}
149
150// sorted by degree for use with std::lower_bound
151struct {
152 float degree;
153 WindDirection direction;
154} static constexpr const cardinal_direction_map[] = {{22.5, WindDirection::N},
155 {67.5, WindDirection::NE},
156 {112.5, WindDirection::E},
157 {157.5, WindDirection::SE},
158 {202.5, WindDirection::S},
159 {247.5, WindDirection::SW},
160 {292.5, WindDirection::W},
161 {337.5, WindDirection::NW},
162 {360.0, WindDirection::N}};
163
164WindDirection HourlyWeatherForecast::windDirectionCardinal() const
165{
166 const auto it = std::lower_bound(std::begin(cardinal_direction_map), std::end(cardinal_direction_map), d->windDirection, [](const auto &entry, double deg) {
167 return entry.degree <= deg;
168 });
169 if (it != std::end(cardinal_direction_map)) {
170 return (*it).direction;
171 }
172 return {};
173}
174
175double HourlyWeatherForecast::windSpeed() const
176{
177 return d->windSpeed;
178}
180{
181 d->windSpeed = windSpeed;
182}
183double HourlyWeatherForecast::humidity() const
184{
185 return d->humidity;
186}
188{
189 d->humidity = humidity;
190}
191double HourlyWeatherForecast::fog() const
192{
193 return d->fog;
194}
196{
197 d->fog = fog;
198}
199double HourlyWeatherForecast::uvIndex() const
200{
201 return d->uvIndex;
202}
204{
205 d->uvIndex = uvIndex;
206}
207double HourlyWeatherForecast::precipitationAmount() const
208{
209 return d->precipitationAmount;
210}
211void HourlyWeatherForecast::setPrecipitationAmount(double precipitationAmount)
212{
213 d->precipitationAmount = precipitationAmount;
214}
216{
217 return (weatherDescription() == rhs.weatherDescription() && weatherIcon() == rhs.weatherIcon() && date() == rhs.date());
218}
219}
220
221#include "moc_hourlyweatherforecast.cpp"
Class represents weatherforecast in a hour.
void setTemperature(double temperature)
set temperature in celsius
void setPrecipitationAmount(double precipitationAmount)
set precipitation in mm
void setFog(double fog)
set fog in percentage
HourlyWeatherForecast()
HourlyWeatherForecast construct a null forecast.
void setWindSpeed(double windSpeed)
set wind speed in km/h
QJsonObject toJson() const
convert this to QJsonObject
void setWeatherDescription(const QString &weatherDescription)
set weather description
void setUvIndex(double uvIndex)
set uv index, 0-1
void setNeutralWeatherIcon(const QString &neutralWeatherIcon)
set neutral weatherIcon
bool operator==(const KWeatherCore::HourlyWeatherForecast &) const
void setHumidity(double humidity)
set humidity in percentage
void setDate(const QDateTime &date)
set date
static HourlyWeatherForecast fromJson(const QJsonObject &obj)
construct from QJsonObject
const QString & symbolCode() const
internal symbolcode from api, normally you can ignore this
void setWindDirectionDegree(double windDirection)
Sets the wind direction in degree.
void setWeatherIcon(const QString &weatherIcon)
set weather icon
void setSymbolCode(const QString &symbolCode)
set internal symbolcode from api, normally you can ignore this
void setPressure(double pressure)
set pressure in hpa
QDateTime currentDateTime()
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
QString toString(QStringView format, QCalendar cal) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.