KWeatherCore

dailyweatherforecast.cpp
1/*
2 * SPDX-FileCopyrightText: 2020-2021 Han Young <hanyoung@protonmail.com>
3 * SPDX-FileCopyrightText: 2020-2021 Devin Lin <espidev@gmail.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7#include "dailyweatherforecast.h"
8#include "kweathercore_p.h"
9#include "pendingweatherforecast_p.h"
10#include <QJsonArray>
11namespace KWeatherCore
12{
13class DailyWeatherForecast::DailyWeatherForecastPrivate
14{
15public:
16 bool isValid = true;
17
18 double maxTemp = std::numeric_limits<double>::lowest();
19 double minTemp = std::numeric_limits<double>::max();
20 double precipitation = 0; // mm
21 double uvIndex = 0; // 0-1
22 double humidity = 0; // %
23 double pressure = 0; // hPa
24 QString weatherIcon = QStringLiteral("weather-none-available");
25 QString weatherDescription = QStringLiteral("Unknown");
26 QDate date;
27
28 std::vector<HourlyWeatherForecast> hourlyWeatherForecast;
29};
30DailyWeatherForecast::~DailyWeatherForecast() = default;
31DailyWeatherForecast::DailyWeatherForecast(DailyWeatherForecast &&other) = default;
32DailyWeatherForecast &DailyWeatherForecast::operator=(DailyWeatherForecast &&other) = default;
34 : d(std::make_unique<DailyWeatherForecastPrivate>())
35{
36}
38 : d(std::make_unique<DailyWeatherForecastPrivate>())
39{
40 d->date = date;
41 d->isValid = false;
42}
43DailyWeatherForecast::DailyWeatherForecast(const DailyWeatherForecast &other)
44 : d(std::make_unique<DailyWeatherForecastPrivate>())
45{
46 *d = *other.d;
47}
48
50{
51 QJsonObject obj;
52 QJsonArray hourlyArray;
53 obj[QLatin1String("maxTemp")] = maxTemp();
54 obj[QLatin1String("minTemp")] = minTemp();
55 obj[QLatin1String("precipitation")] = precipitation();
56 obj[QLatin1String("uvIndex")] = uvIndex();
57 obj[QLatin1String("humidity")] = humidity();
58 obj[QLatin1String("pressure")] = pressure();
59 obj[QLatin1String("weatherIcon")] = weatherIcon();
60 obj[QLatin1String("weatherDescription")] = weatherDescription();
61 obj[QLatin1String("date")] = date().toString(Qt::ISODate);
62 for (const auto &h : hourlyWeatherForecast()) {
63 hourlyArray.append(h.toJson());
64 }
65 obj[QLatin1String("hourly")] = hourlyArray;
66 return obj;
67}
69{
71 ret.setMaxTemp(obj[QLatin1String("maxTemp")].toDouble());
72 ret.setMinTemp(obj[QLatin1String("minTemp")].toDouble());
73 ret.setPrecipitation(obj[QLatin1String("precipitation")].toDouble());
74 ret.setUvIndex(obj[QLatin1String("uvIndex")].toDouble());
75 ret.setHumidity(obj[QLatin1String("humidity")].toDouble());
76 ret.setPressure(obj[QLatin1String("pressure")].toDouble());
77 ret.setWeatherIcon(obj[QLatin1String("weatherIcon")].toString());
78 ret.setWeatherDescription(obj[QLatin1String("weatherDescription")].toString());
79 std::vector<HourlyWeatherForecast> hourlyVec;
80 auto array = obj[QLatin1String("hourly")].toArray();
81 for (int i = 0; i < array.size(); i++) {
82 hourlyVec.push_back(HourlyWeatherForecast::fromJson(array.at(i).toObject()));
83 }
84 ret.setHourlyWeatherForecast(hourlyVec);
85 return ret;
86}
88{
89 return d->isValid;
90}
92{
93 d->maxTemp = maxTemp;
94}
96{
97 d->minTemp = minTemp;
98}
100{
101 d->precipitation = precipitation;
102}
104{
105 d->uvIndex = uvIndex;
106}
108{
109 d->humidity = humidity;
110}
112{
113 d->pressure = pressure;
114}
116{
117 d->weatherIcon = icon;
118}
120{
121 d->weatherDescription = std::move(description);
122}
124{
125 d->date = date;
126}
128{
129 d->date = date.date();
130}
131double DailyWeatherForecast::maxTemp() const
132{
133 return d->maxTemp;
134}
135double DailyWeatherForecast::minTemp() const
136{
137 return d->minTemp;
138}
139double DailyWeatherForecast::precipitation() const
140{
141 return d->precipitation;
142}
143double DailyWeatherForecast::uvIndex() const
144{
145 return d->uvIndex;
146}
147double DailyWeatherForecast::humidity() const
148{
149 return d->humidity;
150}
151double DailyWeatherForecast::pressure() const
152{
153 return d->pressure;
154}
155const QString &DailyWeatherForecast::weatherIcon() const
156{
157 return d->weatherIcon;
158}
159const QString &DailyWeatherForecast::weatherDescription() const
160{
161 return d->weatherDescription;
162}
163const QDate &DailyWeatherForecast::date() const
164{
165 return d->date;
166}
167QDateTime DailyWeatherForecast::dateTime() const
168{
169 return d->date.startOfDay();
170}
171const std::vector<HourlyWeatherForecast> &DailyWeatherForecast::hourlyWeatherForecast() const
172{
173 return d->hourlyWeatherForecast;
174}
175void DailyWeatherForecast::setHourlyWeatherForecast(const std::vector<HourlyWeatherForecast> &forecast)
176{
177 d->hourlyWeatherForecast = forecast;
178}
179void DailyWeatherForecast::setHourlyWeatherForecast(std::vector<HourlyWeatherForecast> &&forecast)
180{
181 d->hourlyWeatherForecast = std::move(forecast);
182}
183
185{
186 if (isValid()) {
187 setDate(forecast.date().date());
188 setWeatherDescription(forecast.weatherDescription());
189 setWeatherIcon(forecast.weatherIcon());
190 d->isValid = false;
191 }
192
193 if (date().daysTo(forecast.date().date()) == 0) {
194 // set description and icon if it is higher ranked
195 if (KWeatherCorePrivate::weatherIconPriorityRank(forecast.neutralWeatherIcon()) >= KWeatherCorePrivate::weatherIconPriorityRank(weatherIcon())) {
196 setWeatherDescription(KWeatherCorePrivate::resolveAPIWeatherDesc(forecast.symbolCode() + QStringLiteral("_neutral")).desc);
197 setWeatherIcon(forecast.neutralWeatherIcon());
198 }
199 setPrecipitation(precipitation() + forecast.precipitationAmount());
200 setUvIndex(std::max(uvIndex(), forecast.uvIndex()));
201 setHumidity(std::max(humidity(), forecast.humidity()));
202 setPressure(std::max(pressure(), forecast.pressure()));
203 setMaxTemp(std::max(maxTemp(), forecast.temperature()));
204 setMinTemp(std::min(minTemp(), forecast.temperature()));
205 }
206
207 d->hourlyWeatherForecast.push_back(forecast);
208 return *this;
209}
210
212{
213 return (date() == forecast.date());
214}
215
217{
218 return date() < forecast.date();
219}
220DailyWeatherForecast &DailyWeatherForecast::operator=(const DailyWeatherForecast &other)
221{
222 *d = *other.d;
223 return *this;
224}
225}
226
227#include "moc_dailyweatherforecast.cpp"
Class represents weatherforecast in a day.
void setPrecipitation(double precipitation)
set the precipitation of the day
void setHumidity(double humidity)
set the humidity of the day
const std::vector< HourlyWeatherForecast > & hourlyWeatherForecast() const
returns all HourlyWeathreForecast belonged to this day
bool operator<(const DailyWeatherForecast &forecast) const
if this is earlier than
void setWeatherIcon(const QString &icon)
set the weather icon of the day
void setPressure(double pressure)
set the pressure of the day
DailyWeatherForecast()
Creates a invalid DailyWeatherForecast.
void setDate(const QDate &date)
set the date this object represents
QJsonObject toJson()
Return a QJsonObject that can be converted back with DailyWeatherForecast::fromJson.
void setWeatherDescription(const QString &description)
set the weather description of the day
bool operator==(const DailyWeatherForecast &forecast) const
if on the same day
void setMaxTemp(double maxTemp)
set the maximum temperature of the day
void setHourlyWeatherForecast(const std::vector< HourlyWeatherForecast > &forecast)
set the hourly forecast of the day
DailyWeatherForecast & operator+=(const HourlyWeatherForecast &forecast)
append hourly forecast, you can append valid hourly forecast into a invalid daily forecast,...
void setMinTemp(double minTemp)
set the minimum temperature of the day
void setUvIndex(double uvIndex)
set the UvIndex of the day
static DailyWeatherForecast fromJson(const QJsonObject &obj)
Construct a DailyWeatherForecast from QJsonObject.
Class represents weatherforecast in a hour.
static HourlyWeatherForecast fromJson(const QJsonObject &obj)
construct from QJsonObject
const QString & symbolCode() const
internal symbolcode from api, normally you can ignore this
QDate fromString(QStringView string, QStringView format, QCalendar cal)
QDate date() const const
QString toString(QStringView format, QCalendar cal) const const
void append(const QJsonValue &value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:42 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.