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>
9#include <QJsonObject>
10
11namespace KWeatherCore
12{
13class WeatherForecast::WeatherForecastPrivate : public QSharedData
14{
15public:
16 std::vector<DailyWeatherForecast> dailyWeatherForecast;
17 QString timezone;
18 double latitude, longitude;
20};
21
23 : d(new WeatherForecastPrivate)
24{
25}
26
29
30WeatherForecast &WeatherForecast::operator=(const WeatherForecast &) = default;
31WeatherForecast &WeatherForecast::operator=(WeatherForecast &&) noexcept = default;
32
34
36{
37 QJsonObject obj;
38 QJsonArray dayArray;
39 for (auto d : dailyWeatherForecast()) {
40 dayArray.append(d.toJson());
41 }
42 obj[QLatin1String("day")] = dayArray;
43 obj[QLatin1String("lat")] = latitude();
44 obj[QLatin1String("lon")] = longitude();
45 obj[QLatin1String("timezone")] = timezone();
46 obj[QLatin1String("createdTime")] = createdTime().toString(Qt::ISODate);
47 return obj;
48}
49
51{
53 std::vector<DailyWeatherForecast> dayVec;
54 const auto &array = obj[QLatin1String("day")].toArray();
55 for (const auto &d : array) {
56 dayVec.push_back(DailyWeatherForecast::fromJson(d.toObject()));
57 }
58 w.setDailyWeatherForecast(std::move(dayVec));
59 w.setCoordinate(obj[QLatin1String("lat")].toDouble(), obj[QLatin1String("lon")].toDouble());
60 w.setTimezone(obj[QLatin1String("timezone")].toString());
61 w.setCreatedTime(QDateTime::fromString(obj[QLatin1String("createdTime")].toString(), Qt::ISODate));
62 return w;
63}
64const std::vector<DailyWeatherForecast> &WeatherForecast::dailyWeatherForecast() const
65{
66 return d->dailyWeatherForecast;
67}
68double WeatherForecast::latitude() const
69{
70 return d->latitude;
71}
72double WeatherForecast::longitude() const
73{
74 return d->longitude;
75}
77{
78 return d->createdTime;
79}
81{
82 return d->timezone;
83}
84void WeatherForecast::setCoordinate(double latitude, double longitude)
85{
86 d->latitude = latitude;
87 d->longitude = longitude;
88}
90{
91 d->timezone = std::move(timezone);
92}
93void WeatherForecast::setDailyWeatherForecast(std::vector<DailyWeatherForecast> &&forecast)
94{
95 d->dailyWeatherForecast = std::move(forecast);
96}
98{
99 for (int i = dailyWeatherForecast().size() - 1; i >= 0; --i) {
100 if (dailyWeatherForecast().at(i).date() == forecast.date().date()) {
101 d->dailyWeatherForecast[i] += std::move(forecast);
102 return *this;
103 }
104 }
105
106 // if not find, append it at end
107 auto newDay = DailyWeatherForecast();
108 newDay += std::move(forecast);
109 d->dailyWeatherForecast.push_back(std::move(newDay));
110 return *this;
111}
112void WeatherForecast::setCreatedTime(const QDateTime &date)
113{
114 d->createdTime = date;
115}
116}
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.
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)
void append(const QJsonValue &value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.