KWeatherCore

weatherforecast.cpp
1 /*
2  * SPDX-FileCopyrightText: 2020-2021 Han Young <[email protected]>
3  * SPDX-FileCopyrightText: 2020 Devin Lin <[email protected]>
4  *
5  * SPDX-License-Identifier: LGPL-2.0-or-later
6  */
7 #include "weatherforecast.h"
8 #include <QJsonArray>
9 namespace KWeatherCore
10 {
11 class WeatherForecast::WeatherForecastPrivate : public QSharedData
12 {
13 public:
14  std::vector<DailyWeatherForecast> dailyWeatherForecast;
16  double latitude, longitude;
18 };
19 
21  : d(new WeatherForecastPrivate)
22 {
23 }
24 
26  : d(other.d)
27 {
28 }
29 
30 WeatherForecast &WeatherForecast::operator=(const WeatherForecast &other)
31 {
32  if (this != &other) {
33  d = other.d;
34  }
35 
36  return *this;
37 }
38 
39 WeatherForecast::~WeatherForecast()
40 {
41 }
42 
44 {
45  QJsonObject obj;
46  QJsonArray dayArray;
47  for (auto d : dailyWeatherForecast()) {
48  dayArray.append(d.toJson());
49  }
50  obj[QStringLiteral("day")] = dayArray;
51  obj[QStringLiteral("lat")] = latitude();
52  obj[QStringLiteral("lon")] = longitude();
53  obj[QStringLiteral("timezone")] = timezone();
54  obj[QStringLiteral("createdTime")] = createdTime().toString(Qt::ISODate);
55  return obj;
56 }
57 
59 {
61  std::vector<DailyWeatherForecast> dayVec;
62  const auto &array = obj[QStringLiteral("day")].toArray();
63  for (const auto &d : array) {
64  dayVec.push_back(DailyWeatherForecast::fromJson(d.toObject()));
65  }
66  w.setDailyWeatherForecast(dayVec);
67  w.setCoordinate(obj[QStringLiteral("lat")].toDouble(), obj[QStringLiteral("lon")].toDouble());
68  w.setTimezone(obj[QStringLiteral("timezone")].toString());
69  w.setCreatedTime(QDateTime::fromString(obj[QStringLiteral("createdTime")].toString(), Qt::ISODate));
70  return w;
71 }
72 const std::vector<DailyWeatherForecast> &WeatherForecast::dailyWeatherForecast() const
73 {
74  return d->dailyWeatherForecast;
75 }
76 double WeatherForecast::latitude() const
77 {
78  return d->latitude;
79 }
80 double WeatherForecast::longitude() const
81 {
82  return d->longitude;
83 }
85 {
86  return d->createdTime;
87 }
89 {
90  return d->timezone;
91 }
92 void WeatherForecast::setCoordinate(double latitude, double longitude)
93 {
94  d->latitude = latitude;
95  d->longitude = longitude;
96 }
98 {
99  d->timezone = std::move(timezone);
100 }
101 void WeatherForecast::setDailyWeatherForecast(const std::vector<DailyWeatherForecast> &forecast)
102 {
103  d->dailyWeatherForecast = forecast;
104 }
105 void WeatherForecast::setDailyWeatherForecast(std::vector<DailyWeatherForecast> &&forecast)
106 {
107  d->dailyWeatherForecast = std::move(forecast);
108 }
109 void WeatherForecast::setSunriseForecast(const std::vector<Sunrise> &sunrise)
110 {
111  int i = 0;
112  int range = sunrise.size();
113  for (auto &day : d->dailyWeatherForecast) {
114  if (i >= range) {
115  break;
116  }
117  day.setSunrise(sunrise.at(i));
118  ++i;
119  }
120 }
121 void WeatherForecast::setSunriseForecast(std::vector<Sunrise> &&sunrise)
122 {
123  int i = 0;
124  int range = sunrise.size();
125  for (auto day : d->dailyWeatherForecast) {
126  if (i >= range) {
127  break;
128  }
129  // if on the same day, add sunrise to day
130  if (day.date().daysTo(sunrise.at(i).sunRise().date()) == 0) {
131  day.setSunrise(std::move(sunrise[i]));
132  ++i;
133  }
134  }
135 }
137 {
138  for (int i = dailyWeatherForecast().size() - 1; i >= 0; --i) {
139  if (dailyWeatherForecast().at(i) == forecast) {
140  d->dailyWeatherForecast[i] += forecast;
141  return *this;
142  }
143  }
144 
145  // if not find, append it at end
146  d->dailyWeatherForecast.push_back(forecast);
147  return *this;
148 }
150 {
151  for (int i = dailyWeatherForecast().size() - 1; i >= 0; --i) {
152  if (dailyWeatherForecast().at(i) == forecast) {
153  d->dailyWeatherForecast[i] += forecast;
154  return *this;
155  }
156  }
157 
158  // if not find, append it at end
159  d->dailyWeatherForecast.push_back(std::move(forecast));
160  return *this;
161 }
163 {
164  for (int i = dailyWeatherForecast().size() - 1; i >= 0; --i) {
165  if (dailyWeatherForecast().at(i).date().isValid() && dailyWeatherForecast().at(i).date().daysTo(forecast.date().date()) == 0) {
166  d->dailyWeatherForecast[i] += forecast;
167  return *this;
168  } else {
169  break;
170  }
171  }
172 
173  // if not find, append it at end
174  DailyWeatherForecast newDay;
175  newDay += forecast;
176  d->dailyWeatherForecast.push_back(std::move(newDay));
177  return *this;
178 }
180 {
181  for (int i = dailyWeatherForecast().size() - 1; i >= 0; --i) {
182  if (dailyWeatherForecast().at(i).date().daysTo(forecast.date().date()) == 0) {
183  d->dailyWeatherForecast[i] += std::move(forecast);
184  return *this;
185  }
186  }
187 
188  // if not find, append it at end
189  auto newDay = DailyWeatherForecast();
190  newDay += forecast;
191  d->dailyWeatherForecast.push_back(std::move(newDay));
192  return *this;
193 }
194 void WeatherForecast::setCreatedTime(const QDateTime &date)
195 {
196  d->createdTime = date;
197 }
198 }
QString toString(Qt::DateFormat format) const const
void setSunriseForecast(const std::vector< Sunrise > &sunrise)
the vector should be sorted
WeatherForecast & operator+=(const DailyWeatherForecast &forecast)
merge DailyWeatherForecast
WeatherForecast()
construct an empty object
void setTimezone(QString timezone)
Class represents weatherforecast in a day.
QJsonObject toJson() const
convert to QJsonObject
const QString & timezone() const
IANA Time Zone ID.
void setCoordinate(double latitude, double longitude)
setCoordinate
const std::vector< DailyWeatherForecast > & dailyWeatherForecast() const
The WeatherForecast class contains the weather information of one location for days.
Class represents weatherforecast in a hour.
static WeatherForecast fromJson(QJsonObject obj)
construct from json
void append(const QJsonValue &value)
QDateTime fromString(const QString &string, Qt::DateFormat format)
QDateTime currentDateTime()
QDate date() const const
const QDateTime & createdTime() const
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.