forked from sailfishapps/weatherfish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrentweathermodel.cpp
238 lines (219 loc) · 6.3 KB
/
currentweathermodel.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright (C) 2013 Lucien XU <[email protected]>
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * The names of its contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/
#include "currentweathermodel.h"
#include <QtCore/QDebug>
#include <QtCore/QTime>
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonArray>
#include <QtCore/QJsonObject>
CurrentWeatherModel::CurrentWeatherModel(QObject *parent) :
AbstractOpenWeatherModel(parent)
{
}
City * CurrentWeatherModel::city() const
{
return m_city;
}
void CurrentWeatherModel::setCity(City *city)
{
if (m_city != city) {
m_city = city;
emit cityChanged();
}
}
QString CurrentWeatherModel::temperature() const
{
return m_temperature;
}
QString CurrentWeatherModel::icon() const
{
return m_icon;
}
int CurrentWeatherModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 0;
}
QVariant CurrentWeatherModel::data(const QModelIndex &index, int role) const
{
Q_UNUSED(index)
Q_UNUSED(role)
return QVariant();
}
void CurrentWeatherModel::refresh()
{
if (!m_city) {
m_icon.clear();
m_temperature.clear();
emit iconChanged();
emit temperatureChanged();
return;
}
QVariantMap arguments;
arguments.insert(QLatin1String("id"), m_city->identifier());
request(QLatin1String("weather"), arguments);
}
QHash<int, QByteArray> CurrentWeatherModel::roleNames() const
{
return QHash<int, QByteArray>();
}
void CurrentWeatherModel::clear()
{
m_icon.clear();
m_temperature.clear();
}
AbstractOpenWeatherModel::Status CurrentWeatherModel::handleFinished(const QByteArray &reply)
{
QJsonDocument document = QJsonDocument::fromJson(reply);
if (document.isNull()) {
return Error;
}
QJsonObject object = document.object();
QJsonObject weather = object.value("weather").toArray().first().toObject();
int id = weather.value("id").toVariant().toInt();
QJsonObject main = object.value("main").toObject();
QString icon = QString("../icons/%1").arg(getIconFromCode(id));
if (m_icon != icon) {
m_icon = icon;
emit iconChanged();
}
QString temperature = QString("%1°").arg(int (main.value("temp").toDouble()));
if (m_temperature != temperature) {
m_temperature = temperature;
emit temperatureChanged();
}
return Idle;
}
QString CurrentWeatherModel::getIconFromCode(int code)
{
int hour = QTime::currentTime().hour();
bool night = hour < 7 || hour >= 19;
switch (code) {
// Storms
case 200:
case 210:
case 221:
case 230:
return !night ? QString("weather-storm-day.png") : QString("weather-storm-night.png");
break;
case 201:
case 202:
case 211:
case 212:
case 231:
case 232:
return QString("weather-storm.png");
break;
// Drizzle
case 300:
case 301:
case 302:
case 310:
case 311:
case 312:
case 321:
return QString("weather-showers-scattered.png");
break;
// Rain
case 500:
case 501:
return !night ? QString("weather-showers-scattered-day.png")
: QString("weather-showers-scattered-night.png");
break;
case 502:
case 503:
case 504:
return !night ? QString("weather-showers-day.png")
: QString("weather-showers-night.png");
break;
case 511:
return QString("weather-freezing-rain");
break;
case 520:
return QString("weather-showers-scattered.png");
break;
case 521:
case 522:
return QString("weather-showers.png");
break;
case 600:
return QString("weather-snow-scattered");
break;
// Snow
case 601:
case 602:
return QString("weather-snow.png");
break;
case 611:
return QString("weather-snow-rain.png");
break;
case 621:
return QString("weather-snow.png");
break;
// Atmosphere
case 701:
case 711:
case 721:
case 731:
case 741:
return QString("weather-mist.png");
break;
// Clouds
case 800:
return !night ? QString("weather-clear.png") : QString("weather-clear-night.png");
break;
case 801:
case 802:
return !night ? QString("weather-few-clouds.png") : QString("weather-few-clouds-night.png");
break;
case 803:
return !night ? QString("weather-clouds.png") : QString("weather-clouds-night.png");
break;
case 804:
return QString("weather-many-clouds.png");
break;
// Extreme
case 900:
case 901:
case 902:
case 903:
case 904:
case 905:
return QString("weather-none-available.png");
break;
case 906:
return QString("weather-hail.png");
break;
default:
return QString("weather-none-available.png");
break;
}
}