Add support for searching/displaying available lines
[pywienerlinien] / gotovienna-qml
1 #!/usr/env/python
2
3 """Public transport information for Vienna"""
4
5 __author__ = 'kelvan <kelvan@logic.at>'
6 __version__ = '1.0'
7 __website__ = 'https://github.com/kelvan/gotoVienna/'
8 __license__ = 'GNU General Public License v3 or later'
9
10 from PySide.QtCore import *
11 from PySide.QtGui import *
12 from PySide.QtDeclarative import *
13
14 from gotovienna.utils import *
15 from gotovienna.realtime import *
16
17 import urllib2
18 import os
19 import sys
20
21 class GotoViennaListModel(QAbstractListModel):
22     def __init__(self, objects=None):
23         QAbstractListModel.__init__(self)
24         if objects is None:
25             objects = []
26         self._objects = objects
27         self.setRoleNames({0: 'modelData'})
28
29     def set_objects(self, objects):
30         self._objects = objects
31
32     def get_objects(self):
33         return self._objects
34
35     def get_object(self, index):
36         return self._objects[index.row()]
37
38     def rowCount(self, parent=QModelIndex()):
39         return len(self._objects)
40
41     def data(self, index, role):
42         if index.isValid():
43             if role == 0:
44                 return self.get_object(index)
45         return None
46
47
48 class Gui(QObject):
49     def __init__(self):
50         QObject.__init__(self)
51         self.itip = ITipParser()
52         self.lines = []
53
54         # Read line names in categorized/sorted order
55         for _, lines in categorize_lines(self.itip.lines):
56             self.lines.extend(lines)
57
58     @Slot(result='QStringList')
59     def get_lines(self):
60         return self.lines
61
62     @Slot(str, str)
63     def search(self, line, station):
64         line = line.upper()
65         station = station.decode('utf-8')
66         print line, station
67
68         if line not in self.lines:
69             return "Invalid line"
70
71         try:
72             stations = sorted(self.itip.get_stations(line).items())
73             print stations
74             headers, stations = zip(*stations)
75             print headers
76             print stations
77             details = [(direction, name, url) for direction, stops in stations
78                         for name, url in stops if match_station(station, name)]
79             print details
80         except urllib2.URLError as e:
81             print e.message
82             return e.message
83
84 if __name__ == '__main__':
85     app = QApplication(sys.argv)
86
87     view = QDeclarativeView()
88
89     # instantiate the Python object
90     itip = Gui()
91
92     # expose the object to QML
93     context = view.rootContext()
94     context.setContextProperty('itip', itip)
95
96     if os.path.abspath(__file__).startswith('/usr/bin/'):
97         # Assume system-wide installation, QML from /usr/share/
98         view.setSource('/usr/share/gotovienna/qml/main.qml')
99     else:
100         # Assume test from source directory, use relative path
101         view.setSource(os.path.join(os.path.dirname(__file__), 'qml/main.qml'))
102
103     view.showFullScreen()
104
105     sys.exit(app.exec_())
106