~brunogirin/+junk/journey-planner

« back to all changes in this revision

Viewing changes to PlannerTab.qml

  • Committer: Bruno Girin
  • Date: 2013-01-27 18:29:11 UTC
  • Revision ID: brunogirin@gmail.com-20130127182911-tr5mzaeb81yma7cc
Migrated to tab interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import QtQuick 2.0
 
2
import QtQuick.XmlListModel 2.0
 
3
import Ubuntu.Components 0.1
 
4
import Ubuntu.Components.Popups 0.1
 
5
import Ubuntu.Components.ListItems 0.1 as ListItem
 
6
import "datetime.js" as Datetime
 
7
import "utils.js" as Utils
 
8
 
 
9
Item {
 
10
    id: tabRoot
 
11
    anchors.fill: parent
 
12
 
 
13
    property real margins: parent.margins
 
14
    property string coreUrl: "http://journeyplanner.tfl.gov.uk/user/XML_TRIP_REQUEST2"
 
15
 
 
16
    Column {
 
17
        id: searchQuery
 
18
 
 
19
        anchors {
 
20
            top: parent.top
 
21
            left: parent.left
 
22
            right: parent.right
 
23
            margins: tabRoot.margins
 
24
        }
 
25
        spacing: units.gu(1)
 
26
 
 
27
        Grid {
 
28
            columns: 3
 
29
            spacing: units.gu(1)
 
30
 
 
31
            Label {
 
32
                text: i18n.tr("From")
 
33
            }
 
34
            TextField {
 
35
                id: originInput
 
36
                KeyNavigation.tab: destinationInput
 
37
            }
 
38
            Button {
 
39
                id: originPopoverTrigger
 
40
                text: "..."
 
41
                enabled: originModel.count > 1
 
42
 
 
43
                onClicked: {
 
44
                    PopupUtils.open(originPopover)
 
45
                }
 
46
            }
 
47
 
 
48
            Label {
 
49
                text: i18n.tr("To")
 
50
            }
 
51
            TextField {
 
52
                id: destinationInput
 
53
                KeyNavigation.backtab: originInput
 
54
            }
 
55
            Button {
 
56
                id: destinationPopoverTrigger
 
57
                text: "..."
 
58
                enabled: destinationModel.count > 1
 
59
 
 
60
                onClicked: {
 
61
                    PopupUtils.open(destinationPopover)
 
62
                }
 
63
            }
 
64
        }
 
65
 
 
66
        Row {
 
67
            spacing: units.gu(1)
 
68
 
 
69
            Button {
 
70
                id: searchButton
 
71
                text: i18n.tr("Search")
 
72
                width: units.gu(12)
 
73
 
 
74
                property bool busy: false
 
75
 
 
76
                onClicked: {
 
77
                    searchButton.busy = true
 
78
                    var now = new Date()
 
79
 
 
80
                    var doc = new XMLHttpRequest();
 
81
                    doc.onreadystatechange = function() {
 
82
                        if (doc.readyState === XMLHttpRequest.DONE) {
 
83
                            //console.log(doc.responseText)
 
84
                            tripModel.xml = doc.responseText
 
85
                            tripModel.reload()
 
86
                            searchButton.busy = false
 
87
                        }
 
88
                    }
 
89
                    doc.open("GET", tabRoot.coreUrl +
 
90
                             '?language=en&sessionID=0&type_origin=' +
 
91
                             Utils.inferInputType(originInput.text) +
 
92
                             '&name_origin=' +
 
93
                             originInput.text +
 
94
                             '&type_destination=' +
 
95
                             Utils.inferInputType(destinationInput.text) +
 
96
                             '&name_destination=' +
 
97
                             destinationInput.text +
 
98
                             '&itdDate=' + Datetime.iso8601DateString(now) +
 
99
                             '&itdTime=' + Datetime.iso8601TimeString(now))
 
100
                    doc.send()
 
101
                }
 
102
            }
 
103
 
 
104
            ActivityIndicator {
 
105
                running: searchButton.busy === true || tripModel.status === XmlListModel.Loading ||
 
106
                         originModel.status === XmlListModel.Loading || destinationModel.status === XmlListModel.Loading
 
107
            }
 
108
        }
 
109
    }
 
110
 
 
111
    Item {
 
112
        Rectangle {
 
113
            color: "white"
 
114
            anchors.fill: parent
 
115
        }
 
116
 
 
117
        anchors {
 
118
            top: searchQuery.bottom
 
119
            left: parent.left
 
120
            right: parent.right
 
121
            bottom: parent.bottom
 
122
            topMargin: tabRoot.margins
 
123
        }
 
124
 
 
125
        PageStack {
 
126
            id: searchResults
 
127
            anchors.fill: parent
 
128
 
 
129
            Component.onCompleted: push(tripPage)
 
130
 
 
131
            Page {
 
132
                id: tripPage
 
133
                title: "Trip"
 
134
 
 
135
                ListView {
 
136
                    id: tripList
 
137
                    anchors.fill: parent
 
138
                    model: tripModel
 
139
                    delegate: tripDelegate
 
140
                }
 
141
            }
 
142
 
 
143
            Page {
 
144
                id: journeyPage
 
145
                title: "Journey"
 
146
 
 
147
                ListView {
 
148
                    id: journeyList
 
149
                    anchors.fill: parent
 
150
                    model: journeyModel
 
151
                    delegate: journeyDelegate
 
152
                }
 
153
            }
 
154
        }
 
155
    }
 
156
 
 
157
 
 
158
    XmlListModel {
 
159
        id: tripModel
 
160
        xml: ""
 
161
        query: "/itdRequest/itdTripRequest/itdItinerary/itdRouteList/itdRoute"
 
162
 
 
163
        XmlRole {
 
164
            name: "changes"
 
165
            query: "@changes/string()"
 
166
        }
 
167
        XmlRole {
 
168
            name: "duration"
 
169
            query: "@publicDuration/string()"
 
170
        }
 
171
        XmlRole {
 
172
            name: "departHour"
 
173
            query: "itdPartialRouteList/itdPartialRoute[1]/itdPoint[1]/itdDateTime/itdTime/@hour/string()"
 
174
        }
 
175
        XmlRole {
 
176
            name: "departMinute"
 
177
            query: "itdPartialRouteList/itdPartialRoute[1]/itdPoint[1]/itdDateTime/itdTime/@minute/string()"
 
178
        }
 
179
        XmlRole {
 
180
            name: "routeTripIndex"
 
181
            query: "@routeTripIndex/string()"
 
182
        }
 
183
 
 
184
        onStatusChanged: {
 
185
            if (status === XmlListModel.Loading) {
 
186
                originModel.reload()
 
187
                destinationModel.reload()
 
188
            }
 
189
        }
 
190
    }
 
191
 
 
192
    Component {
 
193
        id: tripDelegate
 
194
 
 
195
        ListItem.Empty {
 
196
            Row {
 
197
                spacing: units.gu(2)
 
198
 
 
199
                Label {
 
200
                    text: Datetime.displayTimeString(departHour, departMinute)
 
201
                    fontSize: "x-large"
 
202
                }
 
203
                Grid {
 
204
                    columns: 2
 
205
                    spacing: units.gu(1)
 
206
                    Label {
 
207
                        text: "Changes"
 
208
                    }
 
209
                    Label {
 
210
                        text: changes
 
211
                    }
 
212
                    Label {
 
213
                        text: "Duration"
 
214
                    }
 
215
                    Label {
 
216
                        text: duration
 
217
                    }
 
218
                }
 
219
            }
 
220
 
 
221
            onClicked: {
 
222
                searchResults.push(journeyPage)
 
223
                journeyModel.pageIndex = routeTripIndex
 
224
                journeyModel.reload()
 
225
            }
 
226
        }
 
227
    }
 
228
 
 
229
    XmlListModel {
 
230
        id: journeyModel
 
231
        xml: tripModel.xml
 
232
        property int pageIndex
 
233
        query: "/itdRequest/itdTripRequest/itdItinerary/itdRouteList/itdRoute["+pageIndex+"]/itdPartialRouteList/itdPartialRoute"
 
234
 
 
235
        XmlRole {
 
236
            name: "departLocation"
 
237
            query: "itdPoint[1]/@name/string()"
 
238
        }
 
239
        XmlRole {
 
240
            name: "departHour"
 
241
            query: "itdPoint[1]/itdDateTime/itdTime/@hour/string()"
 
242
        }
 
243
        XmlRole {
 
244
            name: "departMinute"
 
245
            query: "itdPoint[1]/itdDateTime/itdTime/@minute/string()"
 
246
        }
 
247
        XmlRole {
 
248
            name: "arriveLocation"
 
249
            query: "itdPoint[2]/@name/string()"
 
250
        }
 
251
        XmlRole {
 
252
            name: "arriveHour"
 
253
            query: "itdPoint[2]/itdDateTime/itdTime/@hour/string()"
 
254
        }
 
255
        XmlRole {
 
256
            name: "arriveMinute"
 
257
            query: "itdPoint[2]/itdDateTime/itdTime/@minute/string()"
 
258
        }
 
259
        XmlRole {
 
260
            name: "transport"
 
261
            query: "itdMeansOfTransport/@productName/string()"
 
262
        }
 
263
        XmlRole {
 
264
            name: "line"
 
265
            query: "itdMeansOfTransport/@shortname/string()"
 
266
        }
 
267
    }
 
268
 
 
269
    Component {
 
270
        id: journeyDelegate
 
271
 
 
272
        ListItem.Empty {
 
273
            height: journeyLegData.height + tabRoot.margins
 
274
            Row {
 
275
                spacing: units.gu(2)
 
276
 
 
277
                Grid {
 
278
                    spacing: units.gu(1)
 
279
                    columns: 1
 
280
 
 
281
                    Label {
 
282
                        text: Datetime.displayTimeString(departHour, departMinute)
 
283
                        fontSize: "large"
 
284
                    }
 
285
                    Label {
 
286
                        text: Datetime.displayTimeString(arriveHour, arriveMinute)
 
287
                        fontSize: "large"
 
288
                    }
 
289
                }
 
290
 
 
291
                Grid {
 
292
                    id: journeyLegData
 
293
                    spacing: units.gu(1)
 
294
                    columns: 2
 
295
 
 
296
                    Label {
 
297
                        text: i18n.tr("From")
 
298
                    }
 
299
                    Label {
 
300
                        text: departLocation
 
301
                    }
 
302
                    Label {
 
303
                        text: i18n.tr("To")
 
304
                    }
 
305
                    Label {
 
306
                        text: arriveLocation
 
307
                    }
 
308
                    Label {
 
309
                        text: i18n.tr("By")
 
310
                    }
 
311
                    Label {
 
312
                        text: transport + " (" + line + ")"
 
313
                    }
 
314
                }
 
315
            }
 
316
        }
 
317
    }
 
318
 
 
319
    XmlListModel {
 
320
        id: originModel
 
321
        xml: tripModel.xml
 
322
        query: "/itdRequest/itdTripRequest/itdOdv[@usage='origin']/itdOdvName/odvNameElem"
 
323
 
 
324
        XmlRole {
 
325
            name: "stopId"
 
326
            query: "@stopID/string()"
 
327
        }
 
328
 
 
329
        XmlRole {
 
330
            name: "stopName"
 
331
            query: "string()"
 
332
        }
 
333
 
 
334
        onStatusChanged: {
 
335
            if (status === XmlListModel.Ready) {
 
336
                if (count == 1) {
 
337
                    originInput.text = get(0).stopName
 
338
                }
 
339
            }
 
340
        }
 
341
    }
 
342
 
 
343
    Component {
 
344
        id: originPopover
 
345
 
 
346
        Popover {
 
347
            id: popover
 
348
 
 
349
            ListView {
 
350
                clip: true
 
351
                anchors {
 
352
                    left: parent.left
 
353
                    right: parent.right
 
354
                    top: parent.top
 
355
                }
 
356
                model: originModel
 
357
                height: units.gu(50)
 
358
                delegate: ListItem.Standard {
 
359
                    text: stopName
 
360
                    onClicked: {
 
361
                        originInput.text = stopName
 
362
                        PopupUtils.close(popover)
 
363
                    }
 
364
                }
 
365
            }
 
366
 
 
367
        }
 
368
    }
 
369
 
 
370
    XmlListModel {
 
371
        id: destinationModel
 
372
        xml: tripModel.xml
 
373
        query: "/itdRequest/itdTripRequest/itdOdv[@usage='destination']/itdOdvName/odvNameElem"
 
374
 
 
375
        XmlRole {
 
376
            name: "stopId"
 
377
            query: "@stopID/string()"
 
378
        }
 
379
 
 
380
        XmlRole {
 
381
            name: "stopName"
 
382
            query: "string()"
 
383
        }
 
384
 
 
385
        onStatusChanged: {
 
386
            if (status === XmlListModel.Ready) {
 
387
                if (count == 1) {
 
388
                    destinationInput.text = get(0).stopName
 
389
                }
 
390
            }
 
391
        }
 
392
    }
 
393
 
 
394
    Component {
 
395
        id: destinationPopover
 
396
 
 
397
        Popover {
 
398
            id: popover
 
399
 
 
400
            ListView {
 
401
                clip: true
 
402
                anchors {
 
403
                    left: parent.left
 
404
                    right: parent.right
 
405
                    top: parent.top
 
406
                }
 
407
                model: destinationModel
 
408
                height: units.gu(50)
 
409
                delegate: ListItem.Standard {
 
410
                    text: stopName
 
411
                    onClicked: {
 
412
                        destinationInput.text = stopName
 
413
                        PopupUtils.close(popover)
 
414
                    }
 
415
                }
 
416
            }
 
417
 
 
418
        }
 
419
    }
 
420
}