-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.h
192 lines (126 loc) · 5.23 KB
/
weather.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef WEATHER_H
#define WEATHER_H
#include <QObject>
#include <QString>
#include <QDebug>
#include <QStringList>
#include <QVariant>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QEventLoop>
#include <qqml.h>
#include <iostream>
#include <sstream>
#include <string>
#include <math.h>
#include "datetime.h"
#include "tasksloader.h"
#include "jsonreader.h"
class Weather : public QObject
{
Q_OBJECT
Q_PROPERTY(QString status READ status WRITE setStatus NOTIFY statusChanged)
Q_PROPERTY(double temperature READ temperature WRITE setTemperature NOTIFY temperatureChanged)
Q_PROPERTY(double humidity READ humidity WRITE setHumidity NOTIFY humidityChanged)
Q_PROPERTY(double windSpeed READ windSpeed WRITE setWindSpeed NOTIFY windSpeedChanged)
Q_PROPERTY(double uvIndex READ uvIndex WRITE setUvIndex NOTIFY uvIndexChanged)
Q_PROPERTY(QString location READ location WRITE setLocation NOTIFY locationChanged)
Q_PROPERTY(QString statusIconUrl READ statusIconUrl WRITE setStatusIconUrl NOTIFY statusIconUrlChanged)
Q_PROPERTY(DateTime *dateTime READ dateTime WRITE setDateTime NOTIFY dateTimeChanged)
Q_PROPERTY(TasksLoader *tasksLoader READ tasksLoader WRITE setTasksLoader NOTIFY tasksLoaderChanged)
Q_PROPERTY(int currentDay READ currentDay WRITE setCurrentDay NOTIFY currentDayChanged)
Q_PROPERTY(int currentHourInDay READ currentHourInDay WRITE setCurrentHourInDay NOTIFY currentHourInDayChanged)
Q_PROPERTY(QStringList weatherIconPaths READ weatherIconPaths WRITE setWeatherIconPaths NOTIFY weatherIconPathsChanged)
Q_PROPERTY(double rain READ rain WRITE setRain NOTIFY rainChanged)
QML_ELEMENT
public:
explicit Weather(QObject *parent = nullptr);
Q_INVOKABLE void returnToLoadingState(); // Set all the weather properties to 0 and weather status to "Loading..."
void sendHttpRequest(QNetworkAccessManager* networkManager, QUrl url);
static QString reformatCityToUrlCity(QString city);
static double kelvin2Celsius(double kevinTemperature);
Q_INVOKABLE void reloadWeatherFromLocation(QString city);
QString convertWeatherCodeToWeatherStatus(QString weatherCode, bool timeIsDay);
QString convertWeatherCodeToWeatherUrl(QString weatherCode, bool timeIsDay);
QString getWeatherStatusFromCode(QJsonObject weatherJsonObject);
QString getWeatherIconUrlFromCode(QJsonObject weatherJsonObject, int scale);
void setWeatherIconPathsFromEachHour(QJsonObject weatherJsonObject);
void getCurrentHourFromCurrentHourInDay();
Q_INVOKABLE QStringList getDailyWeatherIconUrlsListFromJsonData();
template <typename T>
std::string number2StdString(T number);
void setWeatherInfo(QJsonObject weatherJsonData);
void setCityInfo(QJsonObject cityJsonData);
void reformatStatusText();
const QString &status() const;
void setStatus(const QString &newStatus);
double temperature() const;
void setTemperature(double newTemperature);
double humidity() const;
void setHumidity(double newHumidity);
double windSpeed() const;
void setWindSpeed(double newWindSpeed);
double uvIndex() const;
void setUvIndex(double newUvIndex);
const QString &location() const;
void setLocation(const QString &newLocation);
const QString &statusIconUrl() const;
void setStatusIconUrl(const QString &newStatusIconName);
DateTime *dateTime() const;
void setDateTime(DateTime *newDateTime);
TasksLoader *tasksLoader() const;
void setTasksLoader(TasksLoader *newTasksLoader);
int currentDay() const;
void setCurrentDay(int newCurrentDay);
int currentHourInDay() const;
void setCurrentHourInDay(int newCurrentHourInDay);
QStringList weatherIconPaths() const;
void setWeatherIconPaths(const QStringList &newWeatherIconPaths);
double rain() const;
void setRain(double newRain);
signals:
void statusChanged();
void temperatureChanged();
void humidityChanged();
void windSpeedChanged();
void uvIndexChanged();
void locationChanged();
void statusIconUrlChanged();
void dateTimeChanged();
void tasksLoaderChanged();
void currentDayChanged();
void currentHourInDayChanged();
void weatherIconPathsChanged();
void rainChanged();
void weatherPropertiesChanged();
private:
TasksLoader* m_tasksLoader;
QString m_status; // The description about the weather
double m_temperature;
double m_humidity;
double m_windSpeed;
double m_uvIndex;
double m_rain;
int m_currentDay;
/* For example, if currentHour = 25 then the currentHourInDay = 1, understand? */
int m_currentHourInDay;
int m_currentHour;
QString m_statusIconUrl;
QString m_location;
float m_latitude, m_longitude;
QString m_currentTimezone;
DateTime* m_dateTime;
bool m_currentTimeIsDay;
QStringList m_weatherIconPaths;
QJsonObject m_oldWeatherJsonData;
QNetworkAccessManager* m_weatherNetworkManager;
QNetworkAccessManager* m_cityCoordNetworkManager;
QNetworkRequest m_networkRequest;
const std::string apiKey = "9ae7907c3148b138968051b92b60f5e0";
std::string m_apiURL;
};
#endif // WEATHER_H