~brunogirin/+junk/journey-planner

« back to all changes in this revision

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