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 *
24 class GotoViennaListModel(QAbstractListModel):
25 def __init__(self, objects=None):
26 QAbstractListModel.__init__(self)
29 self._objects = objects
30 self.setRoleNames({0: 'modelData'})
32 def set_objects(self, objects):
33 self._objects = objects
35 def get_objects(self):
38 def get_object(self, index):
39 return self._objects[index.row()]
41 def rowCount(self, parent=QModelIndex()):
42 return len(self._objects)
44 def data(self, index, role):
47 return self.get_object(index)
53 QObject.__init__(self)
54 self.itip = ITipParser()
57 # Read line names in categorized/sorted order
58 for _, lines in categorize_lines(self.itip.lines):
59 self.lines.extend(lines)
61 self.current_line = ''
62 self.current_stations = []
63 self.current_departures = []
65 @Slot(int, result=str)
66 def get_direction(self, idx):
67 return self.current_stations[idx][0]
69 @Slot(str, str, result='QStringList')
70 def get_stations(self, line, direction):
71 print 'line:', line, 'current line:', self.current_line
72 for dx, stations in self.current_stations:
73 print 'dx:', dx, 'direction:', direction
75 return [stationname for stationname, url in stations]
77 return ['no stations found']
79 directionsLoaded = Signal()
82 def load_directions(self, line):
84 stations = sorted(self.itip.get_stations(line).items())
86 self.current_line = line
87 self.current_stations = stations
89 self.directionsLoaded.emit()
91 threading.Thread(target=load_async).start()
93 departuresLoaded = Signal()
96 def load_departures(self, url):
98 self.current_departures = [x['ftime'] for x in
99 self.itip.get_departures(url)]
100 print self.current_departures
101 self.departuresLoaded.emit()
103 threading.Thread(target=load_async).start()
105 @Slot(str, str, str, result=str)
106 def get_directions_url(self, line, direction, station):
107 return self.itip.get_url_from_direction(line, direction, station)
109 @Slot(result='QStringList')
113 @Slot(result='QStringList')
114 def get_departures(self):
115 return self.current_departures
118 def search(self, line, station):
120 station = station.decode('utf-8')
123 if line not in self.lines:
124 return "Invalid line"
127 stations = sorted(self.itip.get_stations(line).items())
129 headers, stations = zip(*stations)
132 details = [(direction, name, url) for direction, stops in stations
133 for name, url in stops if match_station(station, name)]
135 except urllib2.URLError as e:
139 if __name__ == '__main__':
140 app = QApplication(sys.argv)
142 view = QDeclarativeView()
144 # instantiate the Python object
147 # expose the object to QML
148 context = view.rootContext()
149 context.setContextProperty('itip', itip)
151 if os.path.abspath(__file__).startswith('/usr/bin/'):
152 # Assume system-wide installation, QML from /usr/share/
153 view.setSource('/usr/share/gotovienna/qml/main.qml')
155 # Assume test from source directory, use relative path
156 view.setSource(os.path.join(os.path.dirname(__file__), 'qml/main.qml'))
158 view.showFullScreen()
160 sys.exit(app.exec_())