Add support for displaying realtime results + refreshing
authorThomas Perl <m@thp.io>
Fri, 14 Oct 2011 14:17:01 +0000 (16:17 +0200)
committerThomas Perl <m@thp.io>
Fri, 14 Oct 2011 14:17:01 +0000 (16:17 +0200)
gotovienna-qml
gotovienna/realtime.py
qml/MainPage.qml
qml/ResultRealtime.qml
qml/main.qml

index d0ba500..6a94bcf 100755 (executable)
@@ -58,6 +58,7 @@ class Gui(QObject):
 
         self.current_line = ''
         self.current_stations = []
+        self.current_departures = []
 
     @Slot(int, result=str)
     def get_direction(self, idx):
@@ -87,10 +88,30 @@ class Gui(QObject):
 
         threading.Thread(target=load_async).start()
 
+    departuresLoaded = Signal()
+
+    @Slot(str)
+    def load_departures(self, url):
+        def load_async():
+            self.current_departures = [str(x) for x in
+                    self.itip.get_departures(url)]
+            print self.current_departures
+            self.departuresLoaded.emit()
+
+        threading.Thread(target=load_async).start()
+
+    @Slot(str, str, str, result=str)
+    def get_directions_url(self, line, direction, station):
+        return self.itip.get_url_from_direction(line, direction, station)
+
     @Slot(result='QStringList')
     def get_lines(self):
         return self.lines
 
+    @Slot(result='QStringList')
+    def get_departures(self):
+        return self.current_departures
+
     @Slot(str, str)
     def search(self, line, station):
         line = line.upper()
index 3f8cb53..be36453 100644 (file)
@@ -59,6 +59,15 @@ class ITipParser:
 
         return self._lines
 
+    def get_url_from_direction(self, line, direction, station):
+        stations = self.get_stations(line)
+
+        for stationname, url in stations.get(direction, []):
+            if stationname == station:
+                return url
+
+        return None
+
     def get_departures(self, url):
         """ Get list of next departures
         integer if time until next departure
index d1ccffc..0d5e2c8 100644 (file)
@@ -6,6 +6,12 @@ import "ExtrasConstants.js" as ExtrasConstants
 Page {
     tools: commonTools
 
+    property bool canRefresh: realtimeResult.sourceUrl != ''
+
+    function refresh() {
+        realtimeResult.refresh()
+    }
+
     Image {
         id: logo
         source: 'logo.png'
@@ -59,6 +65,7 @@ Page {
         onTextChanged: {
             // TODO: Check if text matches an item in lineSelectorModel and
             // set selectedIndex in lineSelector to the right item
+            gstation.text = ''
 
             if (lineSelector.selectedIndex == -1) {
                 return
@@ -98,6 +105,7 @@ Page {
     TextField {
         placeholderText: 'Station'
         id: gstation
+
         anchors {
             top: gline.bottom
             left: parent.left
@@ -106,9 +114,6 @@ Page {
             leftMargin: 10
             rightMargin: 10*stationPickerButton.opacity
         }
-        onTextChanged: {
-            directionLabel.currentDirection = ''
-        }
     }
 
     Sheet {
@@ -203,6 +208,11 @@ Page {
                 property int selectedIndex: -1
                 onSelectedIndexChanged: {
                     console.log('current index: ' + selectedIndex)
+                    if (selectedIndex != -1) {
+                        stationSheet.currentStation = directionChooserModel.get(selectedIndex).station
+                    } else {
+                        stationSheet.currentStation = ''
+                    }
                 }
 
                 anchors {
@@ -235,13 +245,15 @@ Page {
         }
 
         onAccepted: {
-            if (stationSelectorListView.selectedIndex == -1) {
-                gstation.text = ''
-            } else {
-                gstation.text = directionChooserModel.get(stationSelectorListView.selectedIndex).station
-            }
+            gstation.text = stationSheet.currentStation
+
+            realtimeResult.gline = stationSheet.currentLine
+            realtimeResult.gstation = stationSheet.currentStation
+            realtimeResult.gdirection = stationSheet.currentDirection
+
+            realtimeResult.sourceUrl = itip.get_directions_url(stationSheet.currentLine, stationSheet.currentDirection, stationSheet.currentStation)
+            console.log('url to get: ' + realtimeResult.sourceUrl)
 
-            directionLabel.currentDirection = stationSheet.currentDirection
         }
     }
 
@@ -269,43 +281,24 @@ Page {
         }
     }
 
-    ResultRealtime { id: resu }
-
-    Label {
-        id: directionLabel
-        property string currentDirection: ''
-        text: 'Richtung ' + currentDirection
+    ResultRealtime {
+        id: realtimeResult
 
         anchors {
+            margins: 10
+            top: gstation.bottom
             left: parent.left
+            bottom: parent.bottom
             right: parent.right
-            top: gstation.bottom
-            margins: 20*directionLabel.opacity
         }
 
-        font.pixelSize: UIConstants.FONT_SMALL
-
-        opacity: currentDirection != ''
-        scale: opacity
+        clip: true
 
-        Behavior on opacity { PropertyAnimation { } }
-    }
+        gline: stationSheet.currentLine
+        gstation: stationSheet.currentStation
+        gdirection: stationSheet.currentDirection
 
-    Button {
-        id: btnSearch
-        text: 'Search'
-        anchors {
-            top: directionLabel.bottom
-            topMargin: 10
-            horizontalCenter: parent.horizontalCenter
-        }
-        onClicked: {
-            resu.gline = gline.text
-            resu.gstation = gstation.text
-            pageStack.push(resu)
-            itip.search(gline.text, gstation.text)
-            resu.busy = false
-        }
+        sourceUrl: stationSheet.currentUrl
     }
 }
 
index e15fa93..13a6ec2 100644 (file)
@@ -4,12 +4,45 @@ import com.nokia.extras 1.0
 import "UIConstants.js" as UIConstants
 import "ExtrasConstants.js" as ExtrasConstants
 
-Page {
-    tools: commonTools
-
-    property string gline : ""
-    property string gstation : ""
-    property bool busy : true
+Item {
+    id: resultRealtime
+
+    property string gline: ''
+    property string gstation: ''
+    property string gdirection: ''
+
+    property string sourceUrl: ''
+    property bool busy: true
+
+    function refresh() {
+        busy = true
+        itip.load_departures(sourceUrl)
+        console.log('refreshing')
+    }
+
+    onSourceUrlChanged: {
+        refresh()
+        console.log('source url changed: ' + sourceUrl)
+    }
+
+    Connections {
+        target: itip
+
+        onDeparturesLoaded: {
+            busy = false
+            departuresModel.clear()
+
+            var departures = itip.get_departures()
+
+            for (var d in departures) {
+                console.log('departure: ' + departures[d])
+                // XXX: destination might be wrong?!
+                var row = {'line': resultRealtime.gline, 'station': resultRealtime.gstation, 'destination': gdirection, 'departure': departures[d]}
+                console.log('inserting: ' + row)
+                departuresModel.append(row)
+            }
+        }
+    }
 
     Component {
          id: departureDelegate
@@ -89,35 +122,34 @@ Page {
          }
      }
 
-    Component {
-             id: heading
-             Rectangle {
-                 width: parent.width
-                 height: childrenRect.height + 2*UIConstants.DEFAULT_MARGIN
-                 color: "lightsteelblue"
-
-                 Text {
-                     anchors {
-                         top: parent.top
-                         left: parent.left
-                         margins: UIConstants.DEFAULT_MARGIN
-                     }
-
-                     text: gstation + " [" + gline + "]"
-                     font.bold: true
-                     font.family: ExtrasConstants.FONT_FAMILY_LIGHT
-                     font.pixelSize: UIConstants.FONT_LSMALL
-                 }
-             }
-         }
-
      ListView {
          id: list
          width: parent.width; height: parent.height
 
-         header: heading
+         header: Rectangle {
+             width: parent.width
+             height: childrenRect.height + 2*UIConstants.DEFAULT_MARGIN
+             color: "lightsteelblue"
+
+             Text {
+                 anchors {
+                     top: parent.top
+                     left: parent.left
+                     right: parent.right
+                     margins: UIConstants.DEFAULT_MARGIN
+                 }
+
+                 text: 'Abfahrten Richtung ' + gdirection
+                 elide: Text.ElideRight
+                 font.bold: true
+                 font.family: ExtrasConstants.FONT_FAMILY_LIGHT
+                 font.pixelSize: UIConstants.FONT_LSMALL
+             }
+         }
 
          model: ListModel {
+             id: departuresModel
+/*
             ListElement {
                 line: "N60"
                 station: "Schottentor"
@@ -129,81 +161,11 @@ Page {
                 station: "Schottentor"
                 destination: "Grinzing"
                 departure: 7
-            }
-            ListElement {
-                line: "N25"
-                station: "Schottentor"
-                destination: "Großfeldsiedlung"
-                departure: 8
-            }
-            ListElement {
-                line: "N41"
-                station: "Schottentor"
-                destination: "Pötzleinsdorf"
-                departure: 12
-            }
-            ListElement {
-                line: "N43"
-                station: "Schottentor"
-                destination: "Neuwaldegg"
-                departure: 12
-            }
-            ListElement {
-                line: "N66"
-                station: "Schottentor"
-                destination: "Liesing S"
-                departure: 20
-            }
-            ListElement {
-                line: "N38"
-                station: "Schottentor"
-                destination: "Grinzing"
-                departure: 22
-            }
-            ListElement {
-                line: "N25"
-                station: "Schottentor"
-                destination: "Großfeldsiedlung"
-                departure: 35
-            }
-            ListElement {
-                line: "N60"
-                station: "Schottentor"
-                destination: "Maurer Hauptplatz"
-                departure: 35
-            }
-            ListElement {
-                line: "N38"
-                station: "Schottentor"
-                destination: "Grinzing"
-                departure: 37
-            }
-            ListElement {
-                line: "N41"
-                station: "Schottentor"
-                destination: "Pötzleinsdorf"
-                departure: "03:12"
-            }
-            ListElement {
-                line: "N43"
-                station: "Schottentor"
-                destination: "Neuwaldegg"
-                departure: 42
-            }
-            ListElement {
-                line: "N66"
-                station: "Schottentor"
-                destination: "Liesing S"
-                departure: 50
-            }
-            ListElement {
-                line: "N38"
-                station: "Schottentor"
-                destination: "Grinzing"
-                departure: 52
-            }
+            }*/
          }
          delegate: departureDelegate
+
+         visible: !resultRealtime.busy && resultRealtime.sourceUrl != ''
      }
 
      ScrollDecorator {
@@ -214,8 +176,8 @@ Page {
 
      BusyIndicator {
          id: busyIndicator
-         visible: busy
-         running: true
+         visible: resultRealtime.busy && resultRealtime.sourceUrl != ''
+         running: visible
          platformStyle: BusyIndicatorStyle { size: 'large' }
          anchors.centerIn: parent
      }
index 281fd7c..40a2234 100644 (file)
@@ -13,17 +13,17 @@ PageStackWindow {
     ToolBarLayout {
         id: commonTools
         visible: true
-        ToolIcon {
+        /*ToolIcon {
             platformIconId: "toolbar-view-menu"
             anchors.right: (parent === undefined) ? undefined : parent.right
             onClicked: (menu.status == DialogStatus.Closed) ? menu.open() : menu.close()
-        }
+        }*/
         ToolIcon {
-              enabled: appWindow.pageStack.depth > 1
-              platformIconId: enabled ? "icon-m-toolbar-back" : "icon-m-toolbar-back-dimmed"
-              anchors.left: parent.left
-              onClicked: pageStack.pop()
-             }
+              enabled: mainPage.canRefresh
+              platformIconId: enabled ? 'icon-m-toolbar-refresh' : 'icon-m-toolbar-refresh-dimmed'
+              anchors.right: parent.right
+              onClicked: mainPage.refresh()
+        }
     }
 
     Menu {