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 *
11 from PySide.QtGui import *
12 from PySide.QtDeclarative import *
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 = []
62 @Slot(int, result=str)
63 def get_direction(self, idx):
64 return self.current_stations[idx][0]
66 @Slot(str, str, result='QStringList')
67 def get_stations(self, line, direction):
68 print 'line:', line, 'current line:', self.current_line
69 for dx, stations in self.current_stations:
70 print 'dx:', dx, 'direction:', direction
72 return [stationname for stationname, url in stations]
74 return ['no stations found']
76 directionsLoaded = Signal()
79 def load_directions(self, line):
81 stations = sorted(self.itip.get_stations(line).items())
83 self.current_line = line
84 self.current_stations = stations
86 self.directionsLoaded.emit()
88 threading.Thread(target=load_async).start()
90 @Slot(result='QStringList')
95 def search(self, line, station):
97 station = station.decode('utf-8')
100 if line not in self.lines:
101 return "Invalid line"
104 stations = sorted(self.itip.get_stations(line).items())
106 headers, stations = zip(*stations)
109 details = [(direction, name, url) for direction, stops in stations
110 for name, url in stops if match_station(station, name)]
112 except urllib2.URLError as e:
116 if __name__ == '__main__':
117 app = QApplication(sys.argv)
119 view = QDeclarativeView()
121 # instantiate the Python object
124 # expose the object to QML
125 context = view.rootContext()
126 context.setContextProperty('itip', itip)
128 if os.path.abspath(__file__).startswith('/usr/bin/'):
129 # Assume system-wide installation, QML from /usr/share/
130 view.setSource('/usr/share/gotovienna/qml/main.qml')
132 # Assume test from source directory, use relative path
133 view.setSource(os.path.join(os.path.dirname(__file__), 'qml/main.qml'))
135 view.showFullScreen()
137 sys.exit(app.exec_())