add first maemo5 deb config
[pywienerlinien] / gotovienna-qml
1 #!/usr/env/python
2
3 """Public transport information for Vienna"""
4
5 __author__ = 'kelvan <kelvan@logic.at>'
6 __version__ = '0.8.1'
7 __website__ = 'https://github.com/kelvan/gotoVienna/'
8 __license__ = 'GNU General Public License v3 or later'
9
10 from datetime import datetime
11
12 from PySide.QtCore import QAbstractListModel, QModelIndex, QObject, Slot, Signal
13 from PySide.QtGui import QApplication
14 from PySide.QtDeclarative import QDeclarativeView
15
16 from gotovienna.utils import *
17 from gotovienna.realtime import *
18
19 import urllib2
20 import os
21 import sys
22 import threading
23
24 class GotoViennaListModel(QAbstractListModel):
25     def __init__(self, objects=None):
26         QAbstractListModel.__init__(self)
27         if objects is None:
28             objects = []
29         self._objects = objects
30         self.setRoleNames({0: 'modelData'})
31
32     def set_objects(self, objects):
33         self._objects = objects
34
35     def get_objects(self):
36         return self._objects
37
38     def get_object(self, index):
39         return self._objects[index.row()]
40
41     def rowCount(self, parent=QModelIndex()):
42         return len(self._objects)
43
44     def data(self, index, role):
45         if index.isValid():
46             if role == 0:
47                 return self.get_object(index)
48         return None
49
50
51 class Gui(QObject):
52     def __init__(self):
53         QObject.__init__(self)
54         self.itip = ITipParser()
55         self.lines = []
56
57         # Read line names in categorized/sorted order
58         for _, lines in categorize_lines(self.itip.lines):
59             self.lines.extend(lines)
60
61         self.current_line = ''
62         self.current_stations = []
63         self.current_departures = []
64
65     @Slot(int, result=str)
66     def get_direction(self, idx):
67         return self.current_stations[idx][0]
68
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
74             if dx == direction:
75                 return [stationname for stationname, url in stations]
76
77         return ['no stations found']
78
79     directionsLoaded = Signal()
80
81     @Slot(str)
82     def load_directions(self, line):
83         def load_async():
84             stations = sorted(self.itip.get_stations(line).items())
85
86             self.current_line = line
87             self.current_stations = stations
88
89             self.directionsLoaded.emit()
90
91         threading.Thread(target=load_async).start()
92
93     departuresLoaded = Signal()
94
95     @Slot(str)
96     def load_departures(self, url):
97         def load_async():
98             self.current_departures = [x['ftime'] for x in
99                     self.itip.get_departures(url)]
100             print self.current_departures
101             self.departuresLoaded.emit()
102
103         threading.Thread(target=load_async).start()
104
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)
108
109     @Slot(result='QStringList')
110     def get_lines(self):
111         return self.lines
112
113     @Slot(result='QStringList')
114     def get_departures(self):
115         return self.current_departures
116
117     @Slot(str, str)
118     def search(self, line, station):
119         line = line.upper()
120         station = station.decode('utf-8')
121         print line, station
122
123         if line not in self.lines:
124             return "Invalid line"
125
126         try:
127             stations = sorted(self.itip.get_stations(line).items())
128             print stations
129             headers, stations = zip(*stations)
130             print headers
131             print stations
132             details = [(direction, name, url) for direction, stops in stations
133                         for name, url in stops if match_station(station, name)]
134             print details
135         except urllib2.URLError as e:
136             print e.message
137             return e.message
138
139 if __name__ == '__main__':
140     app = QApplication(sys.argv)
141
142     view = QDeclarativeView()
143
144     # instantiate the Python object
145     itip = Gui()
146
147     # expose the object to QML
148     context = view.rootContext()
149     context.setContextProperty('itip', itip)
150
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')
154     else:
155         # Assume test from source directory, use relative path
156         view.setSource(os.path.join(os.path.dirname(__file__), 'qml/main.qml'))
157
158     view.showFullScreen()
159
160     sys.exit(app.exec_())
161