strip long stations in resultlist
[pywienerlinien] / qml / MainPage.qml
1 import QtQuick 1.1
2 import com.nokia.meego 1.0
3 import "UIConstants.js" as UIConstants
4 import "ExtrasConstants.js" as ExtrasConstants
5
6 Page {
7     tools: commonTools
8
9     property bool canRefresh: realtimeResult.sourceUrl != ''
10
11     function refresh() {
12         realtimeResult.refresh()
13     }
14
15     /*Image {
16         id: logo
17         source: 'logo.png'
18
19         anchors {
20             topMargin: 25
21             top: parent.top
22             horizontalCenter: parent.horizontalCenter
23         }
24     }*/
25
26     SelectionDialog {
27         id: lineSelector
28         titleText: 'Select line'
29
30         model: ListModel {
31             id: lineSelectorModel
32
33             Component.onCompleted: {
34                 var lines = itip.get_lines()
35
36                 for (var idx in lines) {
37                     lineSelectorModel.append({'name': lines[idx]})
38                 }
39             }
40         }
41
42         // XXX It would be nice if we could make a delegate with
43         // icons (i.e. U1, U2, ... in the right colors), but we
44         // would have to "copy" the default delegate style
45
46         onAccepted: {
47             console.log('accepted: ' + selectedIndex)
48             gline.text = lineSelectorModel.get(selectedIndex).name
49         }
50     }
51
52     TextField {
53         placeholderText: 'Line'
54
55         id: gline
56         anchors {
57             top: parent.top
58             left: parent.left
59             topMargin: 20
60             leftMargin: 10
61             rightMargin: 10
62             right: lineSearchButton.left
63         }
64
65         onTextChanged: {
66             // TODO: Check if text matches an item in lineSelectorModel and
67             // set selectedIndex in lineSelector to the right item
68             gstation.text = ''
69
70             if (lineSelector.selectedIndex == -1) {
71                 return
72             }
73
74             // Disable selection in line selector if user changes the text
75             if (lineSelectorModel.get(lineSelector.selectedIndex).name != text) {
76                 lineSelector.selectedIndex = -1
77             }
78         }
79
80          MouseArea {
81              anchors.fill: parent
82              drag.target: gline
83              drag.axis: Drag.YAxis
84              drag.minimumY: 0
85              drag.maximumY: parent.height
86          }
87     }
88
89     Button {
90         id: lineSearchButton
91
92         anchors {
93             top: gline.top
94             bottom: gline.bottom
95             right: parent.right
96             rightMargin: 10
97         }
98
99         width: 60
100         iconSource: 'image://theme/icon-m-common-search'
101
102         onClicked: lineSelector.open()
103     }
104
105     TextField {
106         placeholderText: 'Station'
107         id: gstation
108
109         anchors {
110             top: gline.bottom
111             left: parent.left
112             right: stationPickerButton.left
113             topMargin: 10
114             leftMargin: 10
115             rightMargin: 10*stationPickerButton.opacity
116         }
117     }
118
119     Sheet {
120         id: stationSheet
121         property string currentLine: ''
122         property string currentDirection: ''
123         property string currentStation: ''
124
125         acceptButtonText: 'Select'
126         rejectButtonText: 'Cancel'
127
128         function loadData(lineName) {
129             stationSheet.currentLine = lineName
130
131             directionChooser.direction1 = ''
132             directionChooser.direction2 = ''
133
134             directionChooserBusyIndicator.running = true
135             itip.load_directions(stationSheet.currentLine)
136
137             firstDirection.clicked()
138             directionChooser.checkedButton = firstDirection
139         }
140
141         Connections {
142             target: itip
143
144             onDirectionsLoaded: {
145                 directionChooserBusyIndicator.running = false
146
147                 directionChooser.direction1 = itip.get_direction(0)
148                 directionChooser.direction2 = itip.get_direction(1)
149
150                 firstDirection.clicked()
151                 directionChooser.checkedButton = firstDirection
152             }
153         }
154
155         content: Item {
156             anchors.fill: parent
157
158             ButtonColumn {
159                 id: directionChooser
160                 property string direction1
161                 property string direction2
162
163                 visible: !directionChooserBusyIndicator.running
164
165                 function chosen(idx) {
166                     console.log('direction chosen: '+ idx)
167
168                     stationSelectorListView.selectedIndex = -1
169
170                     if (idx == 1) {
171                         stationSheet.currentDirection = directionChooser.direction1
172                     } else {
173                         stationSheet.currentDirection = directionChooser.direction2
174                     }
175
176                     directionChooserModel.clear()
177                     var stations = itip.get_stations(stationSheet.currentLine, stationSheet.currentDirection)
178
179                     for (var s in stations) {
180                         directionChooserModel.append({'station': stations[s]})
181                     }
182                 }
183
184                 anchors {
185                     margins: 10
186                     top: parent.top
187                     left: parent.left
188                     right: parent.right
189                 }
190
191                 Button {
192                     id: firstDirection
193                     text: 'Richtung ' + directionChooser.direction1
194                     onClicked: directionChooser.chosen(1)
195                 }
196
197                 Button {
198                     id: secondDirection
199                     text: 'Richtung ' + directionChooser.direction2
200                     onClicked: directionChooser.chosen(2)
201                 }
202             }
203
204             ListView {
205                 id: stationSelectorListView
206                 visible: !directionChooserBusyIndicator.running
207
208                 property int selectedIndex: -1
209                 onSelectedIndexChanged: {
210                     console.log('current index: ' + selectedIndex)
211                     if (selectedIndex != -1) {
212                         stationSheet.currentStation = directionChooserModel.get(selectedIndex).station
213                     } else {
214                         stationSheet.currentStation = ''
215                     }
216                 }
217
218                 anchors {
219                     margins: 10
220                     top: directionChooser.bottom
221                     left: parent.left
222                     right: parent.right
223                     bottom: parent.bottom
224                 }
225
226                 clip: true
227
228                 model: ListModel {
229                     id: directionChooserModel
230                 }
231
232                 delegate: StationListItem { selector: stationSelectorListView }
233             }
234
235             ScrollDecorator {
236                 flickableItem: stationSelectorListView
237             }
238
239             BusyIndicator {
240                 id: directionChooserBusyIndicator
241                 anchors.centerIn: parent
242                 visible: running
243                 platformStyle: BusyIndicatorStyle { size: 'large' }
244             }
245         }
246
247         onAccepted: {
248             gstation.text = stationSheet.currentStation
249
250             realtimeResult.gline = stationSheet.currentLine
251             realtimeResult.gstation = stationSheet.currentStation
252             realtimeResult.gdirection = stationSheet.currentDirection
253
254             realtimeResult.sourceUrl = itip.get_directions_url(stationSheet.currentLine, stationSheet.currentDirection, stationSheet.currentStation)
255             console.log('url to get: ' + realtimeResult.sourceUrl)
256
257         }
258     }
259
260     Button {
261         id: stationPickerButton
262
263         anchors {
264             top: gstation.top
265             bottom: gstation.bottom
266             right: parent.right
267             rightMargin: 10
268         }
269
270         Behavior on opacity { PropertyAnimation { } }
271
272         opacity: gline.text != '' // XXX: Check if the line is valid
273
274         width: lineSearchButton.width * opacity
275         //iconSource: 'image://theme/icon-m-common-location-picker'
276         iconSource: 'image://theme/icon-m-toolbar-list'
277
278         onClicked: {
279             stationSheet.open()
280             stationSheet.loadData(gline.text)
281         }
282     }
283
284     ResultRealtime {
285         id: realtimeResult
286
287         anchors {
288             margins: 10
289             top: gstation.bottom
290             left: parent.left
291             bottom: parent.bottom
292             right: parent.right
293         }
294
295         clip: true
296
297         gline: stationSheet.currentLine
298         gstation: stationSheet.currentStation
299         gdirection: stationSheet.currentDirection
300
301         sourceUrl: stationSheet.currentUrl
302     }
303 }
304