3 """Public transport information for Vienna"""
5 __author__ = 'kelvan <kelvan@logic.at>'
7 __website__ = 'https://github.com/kelvan/gotoVienna/'
8 __license__ = 'GNU General Public License v3 or later'
10 from datetime import datetime
12 from PySide.QtCore import QAbstractListModel, QModelIndex, QObject, Slot, Signal
13 from PySide.QtGui import QApplication
14 from PySide.QtDeclarative import QDeclarativeView
16 from gotovienna.utils import *
17 from gotovienna.realtime import *
18 from gotovienna.gps import *
25 class GotoViennaListModel(QAbstractListModel):
26 def __init__(self, objects=None):
27 QAbstractListModel.__init__(self)
30 self._objects = objects
31 self.setRoleNames({0: 'modelData'})
33 def set_objects(self, objects):
34 self._objects = objects
36 def get_objects(self):
39 def get_object(self, index):
40 return self._objects[index.row()]
42 def rowCount(self, parent=QModelIndex()):
43 return len(self._objects)
45 def data(self, index, role):
48 return self.get_object(index)
54 QObject.__init__(self)
55 self.itip = ITipParser()
58 # Read line names in categorized/sorted order
59 for _, lines in categorize_lines(self.itip.lines):
60 self.lines.extend(lines)
62 self.current_line = ''
63 self.current_stations = []
64 self.current_departures = []
66 @Slot(int, result=str)
67 def get_direction(self, idx):
68 return self.current_stations[idx][0]
70 @Slot(str, str, result='QStringList')
71 def get_stations(self, line, direction):
72 print 'line:', line, 'current line:', self.current_line
73 for dx, stations in self.current_stations:
74 print 'dx:', dx, 'direction:', direction
76 return [stationname for stationname, url in stations]
78 return ['no stations found']
80 directionsLoaded = Signal()
83 def load_directions(self, line):
85 stations = sorted(self.itip.get_stations(line).items())
87 self.current_line = line
88 self.current_stations = stations
90 self.directionsLoaded.emit()
92 threading.Thread(target=load_async).start()
94 departuresLoaded = Signal()
97 def load_departures(self, url):
99 self.current_departures = [x['ftime'] for x in
100 self.itip.get_departures(url)]
101 print self.current_departures
102 self.departuresLoaded.emit()
104 threading.Thread(target=load_async).start()
106 @Slot(str, str, str, result=str)
107 def get_directions_url(self, line, direction, station):
108 return self.itip.get_url_from_direction(line, direction, station)
110 @Slot(result='QStringList')
114 @Slot(result='QStringList')
115 def get_departures(self):
116 return self.current_departures
118 @Slot(float, float, result=str)
119 def get_nearby_stations(self, lat, lon):
121 return ', '.join(get_nearby_stations(lat, lon))
122 except Exception as e:
127 def search(self, line, station):
129 station = station.decode('utf-8')
132 if line not in self.lines:
133 return "Invalid line"
136 stations = sorted(self.itip.get_stations(line).items())
138 headers, stations = zip(*stations)
141 details = [(direction, name, url) for direction, stops in stations
142 for name, url in stops if match_station(station, name)]
144 except urllib2.URLError as e:
148 if __name__ == '__main__':
149 app = QApplication(sys.argv)
151 view = QDeclarativeView()
153 # instantiate the Python object
156 # expose the object to QML
157 context = view.rootContext()
158 context.setContextProperty('itip', itip)
160 if os.path.abspath(__file__).startswith('/usr/bin/'):
161 # Assume system-wide installation, QML from /usr/share/
162 view.setSource('/usr/share/gotovienna/qml/main.qml')
164 # Assume test from source directory, use relative path
165 view.setSource(os.path.join(os.path.dirname(__file__), 'qml/main.qml'))
167 view.showFullScreen()
169 sys.exit(app.exec_())