base version
[pystan] / src / pystan / lib / gui.py
1 # -*- coding: UTF-8 -*-
2
3 import gtk
4 import hildon
5 import webbrowser
6
7 import sys, time
8
9 from stan import STAN
10 from timetable import StanTimetable
11
12 class StanGUI(hildon.Program):
13     #
14     def __init__(self):
15         hildon.Program.__init__(self)
16         self.stan = STAN()
17         self.window = hildon.Window()
18         self.window.set_title('pySTAN')
19         self.window.connect("destroy", gtk.main_quit)
20         self.add_window(self.window)
21
22         self.tabs = self.main_tabs()
23         self.window.add(self.tabs)
24         #
25         self.last_station = False
26
27     def main_tabs(self):
28         tabs = gtk.Notebook()
29         tabs.set_tab_pos(gtk.POS_TOP)
30         tabs.append_page(self.tab_lines(), gtk.Label('Par lignes'))
31         #tabs.append_page(self.tab_stations(), gtk.Label('Par arrets'))
32         #tabs.append_page(self.tab_search(), gtk.Label('Recherche'))
33         return tabs
34         #
35     def tab_search(self):
36         tab_search = gtk.Frame(None) #'Recherche de trajet')
37
38         from_location = gtk.Entry()
39         from_location_label = gtk.Label(u"Départ")
40         from_city = gtk.Entry()
41         from_city_label = gtk.Label(u"à")
42
43         to_location = gtk.Entry()
44         to_location_label = gtk.Label(u"Arrivée")
45         to_city = gtk.Entry()
46         to_city_label = gtk.Label(u"à")
47
48         datetime_select = gtk.Calendar()
49         datetime_select.display_options(gtk.CALENDAR_SHOW_DAY_NAMES | gtk.CALENDAR_WEEK_START_MONDAY)
50
51         restriction = gtk.Combo()
52         restriction.set_popdown_strings([u"partir après", u"arriver avant"])
53
54         vbox = gtk.VBox()
55         vbox.add(from_location_label)
56         vbox.add(from_location)
57         vbox.add(from_city)
58         vbox.add(to_location_label)
59         vbox.add(to_location)
60         vbox.add(to_city)
61
62         vbox.add(datetime_select)
63         vbox.add(restriction)
64
65         tab_search.add(vbox)
66
67         return tab_search
68
69     # special callback for drawing line numbers in combo box
70     def line_number_cell_cb(self, treeviewcolumn, cellrenderer, modele, iter):
71         for line in self.stan.lines():
72             cell_line_name = modele.get_value(iter, 1)
73             cell_line_id = modele.get_value(iter, 2)
74
75             if line['id'] == cell_line_id:
76                 text = '-'.join(line['numbers'])
77                 cellrenderer.set_property('text', text)
78
79                 if len(text) <= 10:
80                     cellrenderer.set_property('scale', 0.8)
81                 else:
82                     cellrenderer.set_property('scale', 0.6)
83
84                 if not line.has_key('fg-color'):
85                     #print "ERROR:\tline %s do not have foreground color" % line['id']
86                     pass
87                 else:
88                     cellrenderer.set_property('foreground-gdk', gtk.gdk.color_parse(line['fg-color']))
89
90                 if not line.has_key('bg-color'):
91                     #print "ERROR:\tline %s do not have background color" % line['id']
92                     pass
93                 else:
94                     cellrenderer.set_property('background-gdk', gtk.gdk.color_parse(line['bg-color']))
95         return
96
97     def set_previous_button_cb(self, callback):
98         self._previous_button_cb = callback
99
100     def _previous_button_cb(self, data):
101         pass
102
103     def set_active_lineid(self, lineid):
104         pass
105
106     def tab_lines(self):
107         tab_lines = gtk.Frame(None) #'Recherche par lignes')
108
109         #lines_navigation = gtk.Table(rows=2, columns=2)
110         lines_navigation = gtk.HBox()
111
112         #previous_button = gtk.Button(u"<")
113         #previous_button.connect('clicked', self._previous_button_cb)
114         #alignment_previous_button = gtk.Alignment(xalign=0, xscale=0.02)
115         #alignment_previous_button.add(previous_button)
116         #lines_navigation.attach(previous_button, 0, 1, 0, 1, xoptions=gtk.SHRINK, yoptions=gtk.SHRINK, xpadding=1, ypadding=0)
117         #lines_navigation.pack_start(previous_button)
118
119         lines_list = gtk.ComboBox()
120         lines_model = gtk.ListStore(str, str, int)
121         lines_item_number = gtk.CellRendererText()
122         lines_item_number.set_property('scale', 0.8)
123         lines_item_name = gtk.CellRendererText()
124         lines_item_name.set_property('scale', 0.8)
125
126         lines_list.pack_start(lines_item_number, True)
127         lines_list.pack_start(lines_item_name, True)
128
129         lines_list.add_attribute(lines_item_number, 'text', 0)
130         lines_list.add_attribute(lines_item_name, 'text', 1)
131
132         lines_list.set_cell_data_func(lines_item_number, self.line_number_cell_cb)
133
134         for line in self.stan.lines():
135             lines_model.append([ '' , line['name'], line['id'] ])
136
137         lines_list.connect('changed', self.show_line_timetable)
138         lines_list.set_model(lines_model)
139
140         alignment_lines_list = gtk.Alignment(yscale=0.1)
141         alignment_lines_list.add(lines_list)
142         lines_navigation.pack_end(alignment_lines_list)
143         #lines_navigation.attach(lines_list, 1, 2, 0, 1, xoptions=gtk.SHRINK, yoptions=gtk.FILL)
144
145         vbox = gtk.VBox()
146
147         alignment_lines_navigation = gtk.Alignment(xscale=1.0, yscale=0.1)
148         alignment_lines_navigation.add(lines_navigation)
149
150         vbox.pack_start(alignment_lines_navigation, False)
151
152         self.timetable = StanTimetable()
153         self.timetable.create()
154         self.timetable.set_activated_callback(self.timetable_row_clicked)
155
156         #alignment_tt = gtk.Alignment(yscale=0.9)
157         #alignment_tt.add(self.timetable)
158         vbox.pack_start(self.timetable, True, True)
159
160         #lines_navigation.attach(self.timetable, 0, 2, 1, 2, xoptions=gtk.EXPAND, yoptions=gtk.EXPAND, xpadding=1, ypadding=0)
161         #lines_navigation.pack_start(self.timetable)
162
163         tab_lines.add(vbox)
164         return tab_lines
165
166     def show_line_timetable(self, selection):
167         self.timetable.load( self.stan.lines()[selection.get_active()], time.strftime("%d/%m/%Y"), time.strftime("%H"), 0 )
168
169     def timetable_row_clicked(self, station):
170         if self.last_station != station['name']:
171             if self.last_station != False:
172                 self.tabs.remove(self.tab_map)
173             self.last_station = station['name']
174             self.tab_map = self.tab_geomap(station)
175             self.tabs.append_page(self.tab_map, gtk.Label(station['name']))
176             self.tabs.show_all()
177             self.tabs.set_current_page(1)
178
179     def tab_stations(self):
180         tab_stations = gtk.Frame(None) #'Recherche par arrets')
181
182         list_stations = gtk.Combo()
183         list_stations.set_popdown_strings([])
184
185         tab_stations.add(list_stations)
186
187         return tab_stations
188
189     def tab_geomap(self, station):
190         tab_map = gtk.Frame(None)
191         map_button = gtk.Button()
192         #map_label = gtk.Label(u"Link to map")
193         map_button.connect('clicked', self.open_map, station['href'])
194
195         tab_map.add(map_button)
196         return tab_map
197
198     def open_map(self, data, url):
199         webbrowser.open(url)
200
201     def run(self):
202         self.window.show_all()
203         gtk.main()
204