-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConnectionsPage.qml
282 lines (243 loc) · 9.2 KB
/
ConnectionsPage.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
import QtQuick 1.1
import com.nokia.symbian 1.1
import "../components"
import "../js/utils.js" as Utils
Page {
id: page
anchors.fill: parent
property alias origin: connectionsModel.origin
property alias destination: connectionsModel.destination
property alias datetime: connectionsModel.datetime
property alias departure: connectionsModel.departure
property bool lockDatetime
onStatusChanged: {
if (status === PageStatus.Activating) {
connectionsModel.update(false)
} else if (status === PageStatus.Inactive && !pageStack.find(function(_page) { return (_page === page) } )) {
origin = ""
destination = ""
datetime = new Date()
lockDatetime = true
departure = true
}
}
//
// Contents
//
Column {
id: information
width: parent.width
anchors {
top: parent.top
bottomMargin: platformStyle.paddingLarge
margins: platformStyle.paddingMedium
}
Row {
width: parent.width
Text {
text: qsTr("From: ")
color: platformStyle.colorDisabledLight
font.pixelSize: platformStyle.fontSizeLarge
}
Text {
text: {
if (connectionsModel.valid && connectionsModel.count > 0)
return connectionsModel.get(0).origin
else
return origin
}
color: platformStyle.colorNormalLight
font.pixelSize: platformStyle.fontSizeLarge
}
}
Row {
width: parent.width
Text {
text: qsTr("To: ")
color: platformStyle.colorDisabledLight
font.pixelSize: platformStyle.fontSizeLarge
}
Text {
text: {
if (connectionsModel.valid && connectionsModel.count > 0)
return connectionsModel.get(0).destination
else
return destination
}
color: platformStyle.colorNormalLight
font.pixelSize: platformStyle.fontSizeLarge
}
}
}
ListView {
id: connectionsView
width: parent.width
anchors {
top: information.bottom
bottom: parent.bottom
margins: platformStyle.paddingMedium
}
visible: if (connectionsModel.valid) true; else false
clip: true
model: connectionsModel
delegate: connectionsDelegate
header: connectionsHeader
}
Text {
id: connectionsStatus
anchors.centerIn: connectionsView
visible: if (!connectionsModel.valid || connectionsModel.count <= 0) true; else false
text: {
// WORKAROUND
if (connectionsModel.loading)
return ""
else if (connectionsModel.error)
return qsTr("Error!")
switch (connectionsModel.status) {
case XmlListModel.Error:
return qsTr("Error!")
case XmlListModel.Ready:
return qsTr("No results")
default:
return ""
}
}
color: platformStyle.colorDisabledLight
font.pixelSize: platformStyle.fontSizeLarge
}
Component {
id: connectionsHeader
PullDownHeader {
view: connectionsView
onPulled: {
if (!lockDatetime)
datetime = new Date()
connectionsModel.update(true)
}
}
}
BusyIndicator {
anchors.centerIn: connectionsView
visible: if (connectionsModel.status === XmlListModel.Loading || connectionsModel.loading) true; else false
running: true
height: connectionsView.height / 10
width: height
}
//
// Data
//
XmlListModel {
id: connectionsModel
property string origin
property string destination
property date datetime
property bool departure
property string _source: ""
function update(forceReload) {
if (origin !== "" && destination !== "") {
var source = "http://data.irail.be/NMBS/Connections/" + origin + "/" + destination + "/" + Utils.generateDateUrl(datetime) + ".xml"
if (!departure)
source = source + "?timeSel=arrival"
// WORKAROUND: this is a work-around, using a separate XMLHttpRequest object so we actually
// have access to the downloaded source afterwards (in order to pass it to the ConnectionPage).
// FIX 1: get access to the model its xml property after having used the source property
// FIX 2: use a XPath function to acquire the source of a connection
// FIX 3: use a hierarchical ListView (TreeView)
if (forceReload || source !== _source) {
var doc = new XMLHttpRequest();
doc.onreadystatechange = function() {
if (doc.readyState === XMLHttpRequest.DONE) {
if (doc.status != 200) {
error = true
loading = false
} else {
error = false
loading = false
connectionsModel.xml = doc.responseText
}
}
}
loading = true
doc.open("GET", source);
doc.send();
// NOTE: this is to prevent redundant fetches when an identical source is configured
// (the XmlListModel doesn't refetch either when passed an identical source, does it?)
_source = source
}
}
}
// WORKAROUND: properties indicating the status of the XML fetch, as we can't (due to our
// workaround) check the XmlListModel for them
property bool loading: false
property bool error: false
property bool valid
valid: if (origin !== "" && destination !== "" && status === XmlListModel.Ready && !loading && !error) true; else false;
query: "/connections/Connections"
XmlRole { name: "origin"; query: "departure/station/name/string()"; isKey: true }
XmlRole { name: "departure"; query: "departure/time/string()"; isKey: true }
XmlRole { name: "delay"; query: "departure/delay/number()"; }
XmlRole { name: "vias"; query: "count(via)"; }
XmlRole { name: "destination"; query: "arrival/station/name/string()"; isKey: true }
XmlRole { name: "arrival"; query: "arrival/time/string()"; isKey: true }
XmlRole { name: "duration"; query: "duration/number()"; }
}
Component {
id: connectionsDelegate
ListItem {
id: item
subItemIndicator: if (vias > 0) true; else false;
Column {
anchors.fill: item.paddingItem
id: column1
ListItemText {
id: viaText
mode: item.mode
role: "Title"
text: (vias > 0 ? qsTr("Via %n other(s)", "", vias) : qsTr("Direct connection"))
}
ListItemText {
id: durationText
mode: item.mode
role: "SubTitle"
text: qsTr("%1 en route").arg(Utils.readableDuration(duration))
}
}
Column {
id: column2
anchors {
top: column1.top
right: parent.right
rightMargin: platformStyle.graphicSizeSmall + platformStyle.paddingSmall
}
width: Math.max(timeText.width, delayText.width)
ListItemText {
id: timeText
anchors.horizontalCenter: parent.horizontalCenter
mode: item.mode
role: "Title"
text: Utils.readableTime(Utils.getDateTime(departure))
}
ListItemText {
id: delayText
anchors.horizontalCenter: parent.horizontalCenter
color: "red"
mode: item.mode
role: "SubTitle"
visible: if (delay > 0) true; else false
text: "+" + Utils.readableDuration(delay)
}
}
onClicked: {
if (vias === 0)
return;
if (!viaPage)
viaPage = Utils.loadObjectByPath("pages/ViaPage.qml", page)
pageStack.push(viaPage, {xml: connectionsModel.xml, id: connectionsView.currentIndex});
}
}
}
//
// Objects
//
property variant viaPage
}