Add support for displaying realtime results + refreshing
[pywienerlinien] / gotovienna-qml
index 3dab53a..6a94bcf 100755 (executable)
@@ -17,6 +17,7 @@ from gotovienna.realtime import *
 import urllib2
 import os
 import sys
+import threading
 
 class GotoViennaListModel(QAbstractListModel):
     def __init__(self, objects=None):
@@ -55,10 +56,62 @@ class Gui(QObject):
         for _, lines in categorize_lines(self.itip.lines):
             self.lines.extend(lines)
 
+        self.current_line = ''
+        self.current_stations = []
+        self.current_departures = []
+
+    @Slot(int, result=str)
+    def get_direction(self, idx):
+        return self.current_stations[idx][0]
+
+    @Slot(str, str, result='QStringList')
+    def get_stations(self, line, direction):
+        print 'line:', line, 'current line:', self.current_line
+        for dx, stations in self.current_stations:
+            print 'dx:', dx, 'direction:', direction
+            if dx == direction:
+                return [stationname for stationname, url in stations]
+
+        return ['no stations found']
+
+    directionsLoaded = Signal()
+
+    @Slot(str)
+    def load_directions(self, line):
+        def load_async():
+            stations = sorted(self.itip.get_stations(line).items())
+
+            self.current_line = line
+            self.current_stations = stations
+
+            self.directionsLoaded.emit()
+
+        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()