-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.qml
344 lines (295 loc) · 10.5 KB
/
main.qml
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import QtQuick 2.6
import "CommonData.js" as Data
import QmlBanner 1.0
Item {
id: root
anchors.fill: parent
// Auto update weather state
property bool updateState: true
// Properties for Date and Time
property date currentDate: new Date()
property string dateTimeString
// The list of code
property variant codeList: []
Component.onCompleted: {
// Load the list into the screen
for (var i = 0 ; i < Data.list_location.length; i+=2 ) {
id_leftLocation.append({"name": Data.list_location[i], "index": i})
id_rightLocation.append({"name": Data.list_location[i+1], "index": i+1})
}
// Load Banner
id_banner.loadBanner()
}
QmlBanner {
id: id_banner
unitId: "ca-app-pub-3940256099942544/6300978111"
bannerSize: QmlBanner.BANNER
}
// Global Timer to update weather and Date
Timer {
interval: 2000
repeat: true
running: true
onTriggered: {
// Get weather information of all locations
for (var i = 0 ; i < Data.list_location.length; i++ ) {
getCondition(Data.list_location[i], i)
}
// Announce to update weather state for left&right list
if(updateState) updateState = false
else updateState = true
currentDate = new Date()
dateTimeString = currentDate.toLocaleDateString() + "\n" + currentDate.getHours() + ":" + currentDate.getMinutes()
}
}
// Background
Rectangle {
anchors.fill: root
gradient: Gradient {
GradientStop { position: 0.0; color: "#E8F5E9" }
GradientStop { position: 0.1; color: "#C8E6C9" }
GradientStop { position: 0.2; color: "#A5D6A7" }
GradientStop { position: 0.3; color: "#81C784" }
GradientStop { position: 0.4; color: "#66BB6A" }
GradientStop { position: 0.5; color: "#4CAF50" }
GradientStop { position: 0.6; color: "#43A047" }
GradientStop { position: 0.7; color: "#388E3C" }
GradientStop { position: 0.8; color: "#2E7D32" }
GradientStop { position: 0.9; color: "#1B5E20" }
GradientStop { position: 1.0; color: "#B9F6CA" }
}
}
// Top Area
Rectangle {
id: topArea
anchors {
top: root.top
left: root.left
right: root.right
}
height: root.height * 0.15
color: "transparent"
Text {
anchors.centerIn: parent
text: "Weather Information"
font.pixelSize: root.height * 0.08
font.bold: true
}
Text {
anchors {
top: parent.top
right: parent.right
}
text: dateTimeString
font.pixelSize: root.height * 0.05
}
}
// Bottom Area
Rectangle {
id: bottomArea
anchors {
left: root.left
right: root.right
bottom: root.bottom
}
height: root.height * 0.1
color: "transparent"
Image {
anchors {
right: parent.right
bottom: parent.bottom
top: parent.top
}
fillMode: Image.PreserveAspectFit
source: 'https://poweredby.yahoo.com/purple.png'
}
}
// List of location on left
ListModel {
id: id_leftLocation
}
// List of location on right
ListModel {
id: id_rightLocation
}
// Display items on left
Component {
id: id_leftDelegate
Item {
height: id_container.height / (Data.list_location.length / 2)
width: root.width / 2
property int isUpdate: root.updateState
Rectangle {
anchors {
right: parent.right
rightMargin: parent.width * 0.05
top: parent.top
topMargin: parent.height * 0.1
bottom: parent.bottom
bottomMargin: parent.height * 0.1
}
width: parent.width * 0.6
gradient: Gradient {
GradientStop { position: 0.0; color: "#FFF59D" }
GradientStop { position: 0.3; color: "#FFEE58" }
GradientStop { position: 0.6; color: "#FDD835" }
GradientStop { position: 1.0; color: "#F9A825" }
}
// Name of location
Text {
anchors.centerIn: parent
text: name
font.pixelSize: root.height * 0.05
}
MouseArea {
anchors.fill: parent
onClicked: {
id_loader.source = ""
id_loader.source = "Forecast.qml"
// Set input for Forecast.qml
var scene = null
scene = id_loader.item
scene.parent = root
scene.anchors.fill = root
scene.searchFor = name // Location
scene.init() // Call init function
scene.back.connect(closeScene) // Connect back signal to function closeScene
}
}
}
// Weather state of location
Image {
id: id_leftState
source: "images/3200.png"
anchors {
left: parent.left
top: parent.top
bottom: parent.bottom
}
width: parent.width * 0.3
fillMode: Image.PreserveAspectFit
}
// Change images when weather state changes
onIsUpdateChanged: {
id_leftState.source = "images/" + codeList[index] + ".png"
}
}
}
// Display items on right
Component {
id: id_rightDelegate
Item {
height: id_container.height / (Data.list_location.length / 2)
width: root.width / 2
property int isUpdate: root.updateState
Rectangle {
anchors {
left: parent.left
leftMargin: parent.width * 0.05
top: parent.top
topMargin: parent.height * 0.1
bottom: parent.bottom
bottomMargin: parent.height * 0.1
}
width: parent.width * 0.6
gradient: Gradient {
GradientStop { position: 0.0; color: "#FFF59D" }
GradientStop { position: 0.3; color: "#FFEE58" }
GradientStop { position: 0.6; color: "#FDD835" }
GradientStop { position: 1.0; color: "#F9A825" }
}
// Name of location
Text {
anchors.centerIn: parent
text: name
font.pixelSize: root.height * 0.05
}
MouseArea {
anchors.fill: parent
onClicked: {
id_loader.source = ""
id_loader.source = "Forecast.qml"
// Set input for Forecast.qml
var scene = null
scene = id_loader.item
scene.parent = root
scene.anchors.fill = root
scene.searchFor = name // Location
scene.init() // Call init function
scene.back.connect(closeScene) // Connect back signal to function closeScene
}
}
}
// Weather state of location
Image {
id: id_rightState
source: "images/3200.png"
anchors {
right: parent.right
top: parent.top
bottom: parent.bottom
}
width: parent.width * 0.3
fillMode: Image.PreserveAspectFit
}
// Change images when weather state changes
onIsUpdateChanged: {
id_rightState.source = "images/" + codeList[index] + ".png"
}
}
}
// Container to store left and right list
Flickable {
id: id_container
anchors {
left: root.left
top: topArea.bottom
right: root.right
bottom: bottomArea.top
}
// 2 columns in 1 row
Row {
anchors.fill: parent
Column {
Repeater {
model: id_leftLocation
delegate: id_leftDelegate
}
}
Column {
Repeater {
model: id_rightLocation
delegate: id_rightDelegate
}
}
}
}
function getCondition(location, index) {
var res
var url = 'https://query.yahooapis.com/v1/public/yql?q=select item.condition from weather.forecast where woeid in (select woeid from geo.places(1) where text="' + location + '")' + '&format=json'
var doc = new XMLHttpRequest()
doc.onreadystatechange = function() {
if (doc.readyState == XMLHttpRequest.DONE) {
res = doc.responseText
//parse data
var obj = JSON.parse(res)
if (typeof(obj) == 'object') {
if (obj.hasOwnProperty('query')) {
var ch = obj.query.results.channel
var item = ch.item
codeList[index] = item.condition["code"]
}
}
}
}
doc.open('GET', url, true)
doc.send()
}
// Using to open Forecast.qml
Loader {
id: id_loader
}
function closeScene() {
id_loader.source = ""
}
}