KWeatherCore

dailyweatherforecast.cpp
1 /*
2  * SPDX-FileCopyrightText: 2020-2021 Han Young <[email protected]>
3  * SPDX-FileCopyrightText: 2020-2021 Devin Lin <[email protected]>
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 namespace KWeatherCore
12 {
13 class DailyWeatherForecast::DailyWeatherForecastPrivate
14 {
15 public:
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  Sunrise sunrise;
29  std::vector<HourlyWeatherForecast> hourlyWeatherForecast;
30 };
31 DailyWeatherForecast::~DailyWeatherForecast() = default;
33 DailyWeatherForecast &DailyWeatherForecast::operator=(DailyWeatherForecast &&other) = default;
35  : d(std::make_unique<DailyWeatherForecastPrivate>())
36 {
37 }
39  : d(std::make_unique<DailyWeatherForecastPrivate>())
40 {
41  d->date = date;
42  d->isValid = false;
43 }
45  : d(std::make_unique<DailyWeatherForecastPrivate>())
46 {
47  *d = *other.d;
48 }
49 
51 {
52  QJsonObject obj;
53  QJsonArray hourlyArray;
54  obj[QStringLiteral("maxTemp")] = maxTemp();
55  obj[QStringLiteral("minTemp")] = minTemp();
56  obj[QStringLiteral("precipitation")] = precipitation();
57  obj[QStringLiteral("uvIndex")] = uvIndex();
58  obj[QStringLiteral("humidity")] = humidity();
59  obj[QStringLiteral("pressure")] = pressure();
60  obj[QStringLiteral("weatherIcon")] = weatherIcon();
61  obj[QStringLiteral("weatherDescription")] = weatherDescription();
62  obj[QStringLiteral("date")] = date().toString(Qt::ISODate);
63  for (const auto &h : hourlyWeatherForecast()) {
64  hourlyArray.append(h.toJson());
65  }
66  obj[QStringLiteral("hourly")] = hourlyArray;
67  obj[QStringLiteral("sunrise")] = sunrise().toJson();
68  return obj;
69 }
71 {
72  DailyWeatherForecast ret(QDate::fromString(obj[QStringLiteral("date")].toString(), Qt::ISODate));
73  ret.setMaxTemp(obj[QStringLiteral("maxTemp")].toDouble());
74  ret.setMinTemp(obj[QStringLiteral("minTemp")].toDouble());
75  ret.setPrecipitation(obj[QStringLiteral("precipitation")].toDouble());
76  ret.setUvIndex(obj[QStringLiteral("uvIndex")].toDouble());
77  ret.setHumidity(obj[QStringLiteral("humidity")].toDouble());
78  ret.setPressure(obj[QStringLiteral("pressure")].toDouble());
79  ret.setWeatherIcon(obj[QStringLiteral("weatherIcon")].toString());
80  ret.setWeatherDescription(obj[QStringLiteral("weatherDescription")].toString());
81  ret.setSunrise(Sunrise::fromJson(obj[QStringLiteral("sunrise")].toObject()));
82  std::vector<HourlyWeatherForecast> hourlyVec;
83  auto array = obj[QStringLiteral("hourly")].toArray();
84  for (int i = 0; i < array.size(); i++) {
85  hourlyVec.push_back(HourlyWeatherForecast::fromJson(array.at(i).toObject()));
86  }
87  ret.setHourlyWeatherForecast(hourlyVec);
88  return ret;
89 }
91 {
92  return d->isValid;
93 }
95 {
96  d->maxTemp = maxTemp;
97 }
99 {
100  d->minTemp = minTemp;
101 }
103 {
104  d->precipitation = precipitation;
105 }
107 {
108  d->uvIndex = uvIndex;
109 }
111 {
112  d->humidity = humidity;
113 }
115 {
116  d->pressure = pressure;
117 }
119 {
120  d->weatherIcon = icon;
121 }
123 {
124  d->weatherDescription = std::move(description);
125 }
127 {
128  d->date = date;
129 }
131 {
132  d->date = date.date();
133 }
134 double DailyWeatherForecast::maxTemp() const
135 {
136  return d->maxTemp;
137 }
138 double DailyWeatherForecast::minTemp() const
139 {
140  return d->minTemp;
141 }
143 {
144  return d->precipitation;
145 }
146 double DailyWeatherForecast::uvIndex() const
147 {
148  return d->uvIndex;
149 }
150 double DailyWeatherForecast::humidity() const
151 {
152  return d->humidity;
153 }
154 double DailyWeatherForecast::pressure() const
155 {
156  return d->pressure;
157 }
159 {
160  return d->weatherIcon;
161 }
163 {
164  return d->weatherDescription;
165 }
166 const QDate &DailyWeatherForecast::date() const
167 {
168  return d->date;
169 }
170 QDateTime DailyWeatherForecast::dateTime() const
171 {
172  return d->date.startOfDay();
173 }
175 {
176  return d->sunrise;
177 }
178 const std::vector<HourlyWeatherForecast> &DailyWeatherForecast::hourlyWeatherForecast() const
179 {
180  return d->hourlyWeatherForecast;
181 }
183 {
184  d->sunrise = std::move(sunrise);
185 }
186 void DailyWeatherForecast::setHourlyWeatherForecast(const std::vector<HourlyWeatherForecast> &forecast)
187 {
188  d->hourlyWeatherForecast = forecast;
189 }
190 void DailyWeatherForecast::setHourlyWeatherForecast(std::vector<HourlyWeatherForecast> &&forecast)
191 {
192  d->hourlyWeatherForecast = std::move(forecast);
193 }
195 {
196  if (date().isNull()) {
197  setDate(forecast.date());
198  setWeatherDescription(forecast.weatherDescription());
199  setWeatherIcon(forecast.weatherIcon());
200  d->isValid = false;
201  }
202 
203  if (*this == forecast) {
204  setPrecipitation(precipitation() + forecast.precipitation());
205  setUvIndex(std::max(uvIndex(), forecast.uvIndex()));
206  setHumidity(std::max(humidity(), forecast.humidity()));
207  setPressure(std::max(pressure(), forecast.pressure()));
208  setMaxTemp(std::max(maxTemp(), forecast.maxTemp()));
209  setMinTemp(std::min(minTemp(), forecast.minTemp()));
210  }
211 
212  return *this;
213 }
214 
216 {
217  return *this + forecast;
218 }
219 
221 {
222  if (isValid()) {
223  setDate(forecast.date().date());
224  setWeatherDescription(forecast.weatherDescription());
225  setWeatherIcon(forecast.weatherIcon());
226  d->isValid = false;
227  }
228 
229  if (date().daysTo(forecast.date().date()) == 0) {
230  // set description and icon if it is higher ranked
231  if (self()->weatherIconPriorityRank(forecast.neutralWeatherIcon()) >= self()->weatherIconPriorityRank(weatherIcon())) {
232  setWeatherDescription(self()->resolveAPIWeatherDesc(forecast.symbolCode() + QStringLiteral("_neutral")).desc);
233  setWeatherIcon(forecast.neutralWeatherIcon());
234  }
235  setPrecipitation(precipitation() + forecast.precipitationAmount());
236  setUvIndex(std::max(uvIndex(), forecast.uvIndex()));
237  setHumidity(std::max(humidity(), forecast.humidity()));
238  setPressure(std::max(pressure(), forecast.pressure()));
239  setMaxTemp(std::max(maxTemp(), forecast.temperature()));
240  setMinTemp(std::min(minTemp(), forecast.temperature()));
241  }
242 
243  d->hourlyWeatherForecast.push_back(forecast);
244  return *this;
245 }
246 
248 {
249  return (date() == forecast.date());
250 }
251 
253 {
254  return date() < forecast.date();
255 }
256 DailyWeatherForecast &DailyWeatherForecast::operator=(const DailyWeatherForecast &other)
257 {
258  *d = *other.d;
259  return *this;
260 }
261 }
qint64 daysTo(const QDate &d) const const
void setDate(const QDate &date)
set the date this object represents
DailyWeatherForecast()
Creates a invalid DailyWeatherForecast.
QString toString(Qt::DateFormat format) const const
double humidity() const
return humidity
const QString & symbolCode() const
internal symbolcode from api, normally you can ignore this
double minTemp() const
return minimum temperature
The Sunrise class contains the information of sunrise/set on a day and more.
Definition: sunrise.h:24
void setWeatherIcon(const QString &icon)
set the weather icon of the day
Class represents weatherforecast in a day.
static HourlyWeatherForecast fromJson(QJsonObject obj)
construct from QJsonObject
const QString & weatherIcon() const
return weather icon
void setHourlyWeatherForecast(const std::vector< HourlyWeatherForecast > &forecast)
set the hourly forecast of the day
void setUvIndex(double uvIndex)
set the UvIndex of the day
const QString & weatherDescription() const
return weather description
QDate fromString(const QString &string, Qt::DateFormat format)
void setHumidity(double humidity)
set the humidity of the day
void setMinTemp(double minTemp)
set the minimum temperature of the day
QJsonObject toJson() const
convert to json
Definition: sunrise.cpp:62
void setSunrise(Sunrise sunrise)
set sunrise
const Sunrise & sunrise() const
return sunrise data
Class represents weatherforecast in a hour.
void setPressure(double pressure)
set the pressure of the day
void setWeatherDescription(const QString &description)
set the weather description of the day
bool operator<(const DailyWeatherForecast &forecast) const
if this is earlier than
QJsonObject toJson()
Return a QJsonObject that can be converted back with DailyWeatherForecast::fromJson.
void append(const QJsonValue &value)
double precipitation() const
return precipitation
const std::vector< HourlyWeatherForecast > & hourlyWeatherForecast() const
returns all HourlyWeathreForecast belonged to this day
QDate date() const const
bool operator==(const DailyWeatherForecast &forecast) const
if on the same day
double pressure() const
return pressure
void setPrecipitation(double precipitation)
set the precipitation of the day
DailyWeatherForecast & operator+=(const DailyWeatherForecast &forecast)
merge two daily forecast, note the hourly forecast is unchanged, daily forecast becomes valid afterwa...
DailyWeatherForecast & operator+(const DailyWeatherForecast &forecast)
merge two daily forecast, note the hourly forecast is unchanged
double uvIndex() const
return uvIndex
static Sunrise fromJson(QJsonObject obj)
construct from json
Definition: sunrise.cpp:44
const QDate & date() const
return date this object represents
void setMaxTemp(double maxTemp)
set the maximum temperature of the day
double maxTemp() const
return maximum temperature
static DailyWeatherForecast fromJson(QJsonObject obj)
Construct a DailyWeatherForecast from QJsonObject.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Oct 21 2021 23:05:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.