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 PySide.QtCore import QAbstractListModel, QModelIndex, QObject, Slot, Signal
11 from PySide.QtGui import QApplication
12 from PySide.QtDeclarative import QDeclarativeView
14 from gotovienna.utils import *
15 from gotovienna.realtime import *
22 class GotoViennaListModel(QAbstractListModel):
23 def __init__(self, objects=None):
24 QAbstractListModel.__init__(self)
27 self._objects = objects
28 self.setRoleNames({0: 'modelData'})
30 def set_objects(self, objects):
31 self._objects = objects
33 def get_objects(self):
36 def get_object(self, index):
37 return self._objects[index.row()]
39 def rowCount(self, parent=QModelIndex()):
40 return len(self._objects)
42 def data(self, index, role):
45 return self.get_object(index)
51 QObject.__init__(self)
52 self.itip = ITipParser()
55 # Read line names in categorized/sorted order
56 for _, lines in categorize_lines(self.itip.lines):
57 self.lines.extend(lines)
59 self.current_line = ''
60 self.current_stations = []
61 self.current_departures = []
63 @Slot(int, result=str)
64 def get_direction(self, idx):
65 return self.current_stations[idx][0]
67 @Slot(str, str, result='QStringList')
68 def get_stations(self, line, direction):
69 print 'line:', line, 'current line:', self.current_line
70 for dx, stations in self.current_stations:
71 print 'dx:', dx, 'direction:', direction
73 return [stationname for stationname, url in stations]
75 return ['no stations found']
77 directionsLoaded = Signal()
80 def load_directions(self, line):
82 stations = sorted(self.itip.get_stations(line).items())
84 self.current_line = line
85 self.current_stations = stations
87 self.directionsLoaded.emit()
89 threading.Thread(target=load_async).start()
91 departuresLoaded = Signal()
94 def load_departures(self, url):
96 self.current_departures = [str(x) for x in
97 self.itip.get_departures(url)]
98 print self.current_departures
99 self.departuresLoaded.emit()
101 threading.Thread(target=load_async).start()
103 @Slot(str, str, str, result=str)
104 def get_directions_url(self, line, direction, station):
105 return self.itip.get_url_from_direction(line, direction, station)
107 @Slot(result='QStringList')
111 @Slot(result='QStringList')
112 def get_departures(self):
113 return self.current_departures
116 def search(self, line, station):
118 station = station.decode('utf-8')
121 if line not in self.lines:
122 return "Invalid line"
125 stations = sorted(self.itip.get_stations(line).items())
127 headers, stations = zip(*stations)
130 details = [(direction, name, url) for direction, stops in stations
131 for name, url in stops if match_station(station, name)]
133 except urllib2.URLError as e:
137 if __name__ == '__main__':
138 app = QApplication(sys.argv)
140 view = QDeclarativeView()
142 # instantiate the Python object
145 # expose the object to QML
146 context = view.rootContext()
147 context.setContextProperty('itip', itip)
149 if os.path.abspath(__file__).startswith('/usr/bin/'):
150 # Assume system-wide installation, QML from /usr/share/
151 view.setSource('/usr/share/gotovienna/qml/main.qml')
153 # Assume test from source directory, use relative path
154 view.setSource(os.path.join(os.path.dirname(__file__), 'qml/main.qml'))
156 view.showFullScreen()
158 sys.exit(app.exec_())