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>
11#include <QJsonObject>
12
13namespace KWeatherCore
14{
15class DailyWeatherForecastPrivate : public QSharedData
16{
17public:
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) noexcept = default;
32DailyWeatherForecast &DailyWeatherForecast::operator=(const DailyWeatherForecast &other) = default;
33DailyWeatherForecast &DailyWeatherForecast::operator=(DailyWeatherForecast &&other) noexcept = default;
35 : d(new DailyWeatherForecastPrivate)
36{
37}
39 : d(new DailyWeatherForecastPrivate)
40{
41 d->date = date;
42}
43DailyWeatherForecast::DailyWeatherForecast(const DailyWeatherForecast &other) = default;
44
46{
47 QJsonObject obj;
48 QJsonArray hourlyArray;
49 obj[QLatin1String("maxTemp")] = maxTemp();
50 obj[QLatin1String("minTemp")] = minTemp();
51 obj[QLatin1String("precipitation")] = precipitation();
52 obj[QLatin1String("uvIndex")] = uvIndex();
53 obj[QLatin1String("humidity")] = humidity();
54 obj[QLatin1String("pressure")] = pressure();
55 obj[QLatin1String("weatherIcon")] = weatherIcon();
56 obj[QLatin1String("weatherDescription")] = weatherDescription();
57 obj[QLatin1String("date")] = date().toString(Qt::ISODate);
58 for (const auto &h : hourlyWeatherForecast()) {
59 hourlyArray.append(h.toJson());
60 }
61 obj[QLatin1String("hourly")] = hourlyArray;
62 return obj;
63}
65{
67 ret.setMaxTemp(obj[QLatin1String("maxTemp")].toDouble());
68 ret.setMinTemp(obj[QLatin1String("minTemp")].toDouble());
69 ret.setPrecipitation(obj[QLatin1String("precipitation")].toDouble());
70 ret.setUvIndex(obj[QLatin1String("uvIndex")].toDouble());
71 ret.setHumidity(obj[QLatin1String("humidity")].toDouble());
72 ret.setPressure(obj[QLatin1String("pressure")].toDouble());
73 ret.setWeatherIcon(obj[QLatin1String("weatherIcon")].toString());
74 ret.setWeatherDescription(obj[QLatin1String("weatherDescription")].toString());
75 std::vector<HourlyWeatherForecast> hourlyVec;
76 auto array = obj[QLatin1String("hourly")].toArray();
77 for (int i = 0; i < array.size(); i++) {
78 hourlyVec.push_back(HourlyWeatherForecast::fromJson(array.at(i).toObject()));
79 }
80 ret.setHourlyWeatherForecast(std::move(hourlyVec));
81 return ret;
82}
84{
85 d->maxTemp = maxTemp;
86}
88{
89 d->minTemp = minTemp;
90}
92{
93 d->precipitation = precipitation;
94}
96{
97 d->uvIndex = uvIndex;
98}
100{
101 d->humidity = humidity;
102}
104{
105 d->pressure = pressure;
106}
108{
109 d->weatherIcon = icon;
110}
112{
113 d->weatherDescription = std::move(description);
114}
116{
117 d->date = date;
118}
120{
121 d->date = date.date();
122}
123double DailyWeatherForecast::maxTemp() const
124{
125 return d->maxTemp;
126}
127double DailyWeatherForecast::minTemp() const
128{
129 return d->minTemp;
130}
131double DailyWeatherForecast::precipitation() const
132{
133 return d->precipitation;
134}
135double DailyWeatherForecast::uvIndex() const
136{
137 return d->uvIndex;
138}
139double DailyWeatherForecast::humidity() const
140{
141 return d->humidity;
142}
143double DailyWeatherForecast::pressure() const
144{
145 return d->pressure;
146}
147const QString &DailyWeatherForecast::weatherIcon() const
148{
149 return d->weatherIcon;
150}
151const QString &DailyWeatherForecast::weatherDescription() const
152{
153 return d->weatherDescription;
154}
155const QDate &DailyWeatherForecast::date() const
156{
157 return d->date;
158}
159QDateTime DailyWeatherForecast::dateTime() const
160{
161 return d->date.startOfDay();
162}
163const std::vector<HourlyWeatherForecast> &DailyWeatherForecast::hourlyWeatherForecast() const
164{
165 return d->hourlyWeatherForecast;
166}
167void DailyWeatherForecast::setHourlyWeatherForecast(std::vector<HourlyWeatherForecast> &&forecast)
168{
169 d->hourlyWeatherForecast = std::move(forecast);
170}
171
173{
174 if (!d->date.isValid()) {
175 setDate(forecast.date().date());
176 setWeatherDescription(forecast.weatherDescription());
177 setWeatherIcon(forecast.weatherIcon());
178 }
179
180 if (date().daysTo(forecast.date().date()) == 0) {
181 // set description and icon if it is higher ranked
182 if (KWeatherCorePrivate::weatherIconPriorityRank(forecast.neutralWeatherIcon()) >= KWeatherCorePrivate::weatherIconPriorityRank(weatherIcon())) {
183 setWeatherDescription(KWeatherCorePrivate::resolveAPIWeatherDesc(forecast.symbolCode() + QStringLiteral("_neutral")).desc);
184 setWeatherIcon(forecast.neutralWeatherIcon());
185 }
186 setPrecipitation(precipitation() + forecast.precipitationAmount());
187 setUvIndex(std::max(uvIndex(), forecast.uvIndex()));
188 setHumidity(std::max(humidity(), forecast.humidity()));
189 setPressure(std::max(pressure(), forecast.pressure()));
190 setMaxTemp(std::max(maxTemp(), forecast.temperature()));
191 setMinTemp(std::min(minTemp(), forecast.temperature()));
192 }
193
194 d->hourlyWeatherForecast.push_back(std::move(forecast));
195 return *this;
196}
197
199{
200 return (date() == forecast.date());
201}
202
204{
205 return date() < forecast.date();
206}
207}
208
209#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
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(std::vector< HourlyWeatherForecast > &&forecast)
overloaded version
void setMinTemp(double minTemp)
set the minimum temperature of the day
void setUvIndex(double uvIndex)
set the UvIndex of the day
DailyWeatherForecast & operator+=(HourlyWeatherForecast &&forecast)
append hourly forecast, you can append valid hourly forecast into a invalid daily forecast,...
static DailyWeatherForecast fromJson(const QJsonObject &obj)
Construct a DailyWeatherForecast from QJsonObject.
QJsonObject toJson() const
Return a QJsonObject that can be converted back with DailyWeatherForecast::fromJson.
Class represents weatherforecast in a hour.
static HourlyWeatherForecast fromJson(const QJsonObject &obj)
construct from QJsonObject
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 Fri May 24 2024 12:02:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.