KWeatherCore

weatherforecast.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 "weatherforecast.h"
8#include <QJsonArray>
9namespace KWeatherCore
10{
11class WeatherForecast::WeatherForecastPrivate : public QSharedData
12{
13public:
14 std::vector<DailyWeatherForecast> dailyWeatherForecast;
15 QString timezone;
16 double latitude, longitude;
18};
19
21 : d(new WeatherForecastPrivate)
22{
23}
24
26 : d(other.d)
27{
28}
29
30WeatherForecast &WeatherForecast::operator=(const WeatherForecast &other)
31{
32 if (this != &other) {
33 d = other.d;
34 }
35
36 return *this;
37}
38
39WeatherForecast::~WeatherForecast()
40{
41}
42
44{
45 QJsonObject obj;
46 QJsonArray dayArray;
47 for (auto d : dailyWeatherForecast()) {
48 dayArray.append(d.toJson());
49 }
50 obj[QLatin1String("day")] = dayArray;
51 obj[QLatin1String("lat")] = latitude();
52 obj[QLatin1String("lon")] = longitude();
53 obj[QLatin1String("timezone")] = timezone();
54 obj[QLatin1String("createdTime")] = createdTime().toString(Qt::ISODate);
55 return obj;
56}
57
59{
61 std::vector<DailyWeatherForecast> dayVec;
62 const auto &array = obj[QLatin1String("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[QLatin1String("lat")].toDouble(), obj[QLatin1String("lon")].toDouble());
68 w.setTimezone(obj[QLatin1String("timezone")].toString());
69 w.setCreatedTime(QDateTime::fromString(obj[QLatin1String("createdTime")].toString(), Qt::ISODate));
70 return w;
71}
72const std::vector<DailyWeatherForecast> &WeatherForecast::dailyWeatherForecast() const
73{
74 return d->dailyWeatherForecast;
75}
76double WeatherForecast::latitude() const
77{
78 return d->latitude;
79}
80double WeatherForecast::longitude() const
81{
82 return d->longitude;
83}
85{
86 return d->createdTime;
87}
89{
90 return d->timezone;
91}
92void WeatherForecast::setCoordinate(double latitude, double longitude)
93{
94 d->latitude = latitude;
95 d->longitude = longitude;
96}
98{
99 d->timezone = std::move(timezone);
100}
101void WeatherForecast::setDailyWeatherForecast(const std::vector<DailyWeatherForecast> &forecast)
102{
103 d->dailyWeatherForecast = forecast;
104}
105void WeatherForecast::setDailyWeatherForecast(std::vector<DailyWeatherForecast> &&forecast)
106{
107 d->dailyWeatherForecast = std::move(forecast);
108}
110{
111 for (int i = dailyWeatherForecast().size() - 1; i >= 0; --i) {
112 if (dailyWeatherForecast().at(i).date() == forecast.date().date()) {
113 d->dailyWeatherForecast[i] += std::move(forecast);
114 return *this;
115 }
116 }
117
118 // if not find, append it at end
119 auto newDay = DailyWeatherForecast();
120 newDay += forecast;
121 d->dailyWeatherForecast.push_back(std::move(newDay));
122 return *this;
123}
124void WeatherForecast::setCreatedTime(const QDateTime &date)
125{
126 d->createdTime = date;
127}
128}
Class represents weatherforecast in a day.
static DailyWeatherForecast fromJson(const QJsonObject &obj)
Construct a DailyWeatherForecast from QJsonObject.
Class represents weatherforecast in a hour.
The WeatherForecast class contains the weather information of one location for days.
QJsonObject toJson() const
convert to QJsonObject
const QString & timezone() const
IANA Time Zone ID.
WeatherForecast()
construct an empty object
static WeatherForecast fromJson(const QJsonObject &obj)
construct from json
WeatherForecast & operator+=(HourlyWeatherForecast &&forecast)
merge HourlyWeatherForecast, new day is created when required
void setTimezone(QString timezone)
const QDateTime & createdTime() const
void setCoordinate(double latitude, double longitude)
setCoordinate
const std::vector< DailyWeatherForecast > & dailyWeatherForecast() const
QDateTime currentDateTime()
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
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 3 2024 11:51:32 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.