Merge branch 'development'
[quandoparte] / application / resources / harmattan / qml / StationPage.qml
1 import QtQuick 1.1
2 import com.nokia.meego 1.1
3 import net.cirulla.quandoparte 1.0
4 import "uiconstants.js" as UiConstants
5
6 Page {
7     property alias name: schedule.name
8     property alias code: schedule.code
9
10     tools: ToolBarLayout {
11         id: toolBar
12         ToolIcon {
13             iconId: "icon-m-toolbar-back" + (theme.inverted ? "-white": "")
14             onClicked: pageStack.pop()
15         }
16         ToolIcon {
17             iconId: "icon-m-toolbar-refresh" + (theme.inverted ? "-white": "")
18             onClicked: updateStation() }
19         ToolIcon {
20             iconId: "icon-m-toolbar-view-menu" + (theme.inverted ? "-white": "")
21             onClicked: menu.open()
22         }
23     }
24     PageHeader {
25         id: header
26         anchors.top: parent.top
27         selectedIndex: schedule.type
28         options: [
29             qsTr("Departures"),
30             qsTr("Arrivals")
31         ]
32     }
33     InfoBar {
34         id: infoBar
35         anchors.top: header.bottom
36         text: parent.name
37     }
38     Binding {
39         target: schedule
40         property: "type"
41         value: header.selectedIndex
42     }
43     LabelStyle {
44         id: labelStyle
45     }
46     Item {
47         id: view
48         anchors {
49             top: infoBar.bottom
50             bottom: parent.bottom
51             left: parent.left
52             right: parent.right
53         }
54         DroppedShadow {
55             id: shadow
56             anchors.top: view.top
57         }
58         ListView {
59             id: stationScheduleView
60             clip: true
61             visible: false
62             width: parent.width
63             cacheBuffer: 40
64             anchors {
65                 top: shadow.top
66                 bottom: parent.bottom
67             }
68             model: schedule
69             delegate: StationScheduleDelegate {
70                 width: stationScheduleView.width
71                 type: schedule.type
72                 arrivalTime: model.arrivalTime
73                 departureTime: model.departureTime
74                 train: model.train
75                 arrivalStation: model.arrivalStation
76                 departureStation: model.departureStation
77                 delay: model.delay
78                 actualPlatform: model.actualPlatform
79                 expectedPlatfrom: model.expectedPlatform
80             }
81         }
82         ScrollDecorator {
83             id: decorator
84             flickableItem: stationScheduleView
85         }
86         BusyIndicator {
87             id: busyIndicator
88             platformStyle: BusyIndicatorStyle {
89                 size: "large"
90             }
91             anchors.centerIn: parent
92             running: visible
93         }
94         Item {
95             id: errorDisplay
96             anchors {
97                 top: shadow.top
98                 bottom: parent.bottom
99             }
100             width: parent.width
101             opacity: 0.6
102             Label {
103                 textFormat: Text.RichText
104                 visible: parent.visible
105                 wrapMode: Text.WordWrap
106                 text: "<h2>" + qsTr("Error!") + "</h2><p>" + schedule.error + "</p>"
107                 width: parent.width
108                 // font.pixelSize: UiConstants.HeaderFontPixelSize
109                 horizontalAlignment: Text.AlignHCenter
110                 anchors.centerIn: parent
111             }
112             onVisibleChanged: if (visible) console.log("showing error: " + schedule.error)
113         }
114         states: [
115             State {
116                 name: "loading"
117                 when: !completed
118                 PropertyChanges {
119                     target: stationScheduleView
120                     visible: false
121                 }
122                 PropertyChanges {
123                     target: errorDisplay
124                     visible: false
125                 }
126                 PropertyChanges {
127                     target: busyIndicator
128                     visible: true
129                 }
130             },
131             State {
132                 name: "ready"
133                 when: completed && schedule.error === ""
134                 PropertyChanges {
135                     target: stationScheduleView
136                     visible: true
137                 }
138                 PropertyChanges {
139                     target: errorDisplay
140                     visible: false
141                 }
142                 PropertyChanges {
143                     target: busyIndicator
144                     visible: false
145                 }
146             },
147             State {
148                 name: "error"
149                 when: schedule.error !== ""
150                 PropertyChanges {
151                     target: stationScheduleView
152                     visible: false
153                 }
154                 PropertyChanges {
155                     target: errorDisplay
156                     visible: true
157                 }
158                 PropertyChanges {
159                     target: busyIndicator
160                     visible: false
161                 }
162             }
163         ]
164     }
165     StationScheduleModel {
166         id: schedule
167         onNameChanged: updateStation()
168         onLayoutChanged: if (error) view.state = "error"
169                          else view.state = "ready"
170     }
171     Component.onCompleted: {
172         updateTimer.timeout.connect(updateStation)
173         view.state = "loading"
174     }
175     function updateStation() {
176         view.state = "loading"
177         console.log("Updating station with " + schedule.name + ", " + schedule.code)
178         schedule.fetch(schedule.name, schedule.code)
179     }
180 }