base version
[pystan] / src / pystan / lib / timetable.py
1 # -*- coding: UTF-8 -*-
2
3 import gtk
4 import sys
5
6 from stan import STAN
7
8 class StanTimetable(gtk.VBox):
9
10     def create(self):
11         self.current_timetable = False
12         self.navigation = False
13         self.navigation_pages = []
14
15     def set_activated_callback(self, new_callback):
16         self.cell_activated = new_callback
17
18     def cell_activated(self, data):
19         print data
20
21     def __cell_activated(self, timetable_view=None, cell_position=None, column_view=None):
22         if len(cell_position) == 1:
23             station = self.timetable_stations[ cell_position[0] ]
24             station['name'] = self.timetable_struct[ cell_position[0] ][0]
25             self.cell_activated(station)
26
27     def navigation_buttons(self):
28         box = gtk.HBox()
29
30         current = self.navigation_pages[len(self.navigation_pages)-1]
31
32         #previous_page = None
33         #if len(self.navigation_pages) > 0:
34         #    previous_page = gtk.Button(u"<")
35         #    previous_page.connect('clicked', self.load_by_params, self.navigation_pages[len(self.navigation_pages)-1])
36
37         if self.timetable_navigation.has_key('next'):
38             image = gtk.Image()
39             image.set_from_stock(gtk.STOCK_GO_FORWARD, 48)
40             next = gtk.Button()
41             next.add(image)
42             index_next = self.timetable_navigation['next']
43             next_nav = current.copy(); next_nav.update({ 'hour':int(index_next) })
44             next.connect('clicked',  self.load_by_params, next_nav)
45             box.pack_end(next,False,False)
46
47         if self.timetable_navigation.has_key('prev'):
48             image = gtk.Image()
49             image.set_from_stock(gtk.STOCK_GO_BACK, 48)
50             index_prev = self.timetable_navigation['prev']
51             prev = gtk.Button()
52             prev.add(image)
53             prev_nav = current.copy(); prev_nav.update({'hour':int(index_prev) })
54             prev.connect('clicked',  self.load_by_params, prev_nav)
55             box.pack_start(prev,False,False)
56
57         invert = gtk.Button()
58         invert_nav = current.copy(); invert_nav.update({'direction':int(not invert_nav['direction'])})
59         invert.connect('clicked',  self.load_by_params, invert_nav)
60         image = gtk.Image()
61         if current['direction']:
62             image.set_from_stock(gtk.STOCK_SORT_DESCENDING, 48)
63         else:
64             image.set_from_stock(gtk.STOCK_SORT_ASCENDING, 48)
65         invert.add(image)
66         box.add(invert)
67
68         alignment_box_navigation = gtk.Alignment(xscale=1.0,yscale=0.1)
69         alignment_box_navigation.add(box)
70         return alignment_box_navigation
71
72     def load_by_params(self, widget, params):
73         if params.has_key('pop') and params['pop'] and len(self.navigation_pages) > 0:
74             self.navigation_pages.pop(len(self.navigation_pages)-1)
75
76         self.load(params['lineobj'], params['date'], params['hour'], params['direction'], params['bypass'])
77
78     def load(self, lineobj, date, hour, direction, bypass=False):
79
80         params = {'lineobj':lineobj, 'date':date, 'hour':hour, 'direction':direction, 'bypass':True }
81
82         if (self.current_timetable):
83             self.remove(self.current_timetable)
84
85         if (self.navigation):
86             self.remove(self.navigation)
87
88         self.timetable_struct = STAN().load_timetable(lineobj['id'], params, bypass)
89
90         if self.timetable_struct is None or (self.timetable_struct.has_key('timetable') and len(self.timetable_struct['timetable']) == 0):
91             self.current_timetable = gtk.VBox()
92             self.current_timetable.add(gtk.Label(u"No timetable available for line \n%s" % lineobj['name']))
93
94             if len(self.navigation_pages) > 0:
95                 go_previous_page = gtk.Button(u"Get back")
96                 back_params = self.navigation_pages[len(self.navigation_pages)-1]
97                 back_params['pop'] = True
98                 go_previous_page.connect('clicked', self.load_by_params, back_params)
99                 self.current_timetable.add(go_previous_page)
100
101         elif self.timetable_struct == False:
102             self.current_timetable = gtk.VBox()
103             self.current_timetable.add(gtk.Label(u"No recent timetable available for line \n%s" % lineobj['name']))
104             bypass_button = gtk.Button(u"See old local timetable")
105             bypass_button.connect("clicked", self.load_by_params, params)
106             self.current_timetable.add(bypass_button)
107
108         elif len(self.timetable_struct['timetable']) > 0:
109             self.navigation_pages.append(params)
110
111             self.timetable_navigation = self.timetable_struct['navigation']
112             self.timetable_stations = self.timetable_struct['stations']
113             self.timetable_struct = self.timetable_struct['timetable']
114
115             #self.timetable_store = gtk.ListStore(([str] * len(self.timetable_struct[0])))
116             # beurk
117
118             if len(self.timetable_struct[0]) < 7:
119                 for k,v in enumerate(self.timetable_struct):
120                     self.timetable_struct[k].append('-')
121
122             self.timetable_store = gtk.ListStore(str, str, str, str, str, str, str)
123             for k,line in enumerate(self.timetable_struct):
124                 self.timetable_store.append( line )
125
126             self.timetable_view = gtk.TreeView(self.timetable_store)
127
128             self.timetable_view.connect("row-activated", self.__cell_activated)
129
130             column_name = gtk.TreeViewColumn(u"Name")
131             self.timetable_view.append_column(column_name)
132             cell = gtk.CellRendererText()
133             cell.set_property('scale', 0.8)
134             column_name.pack_start(cell, True)
135             column_name.add_attribute(cell, 'text', 0)
136
137             nb_curses_columns = len(self.timetable_struct[0])-1
138
139             for i in range(nb_curses_columns):
140                 column = gtk.TreeViewColumn(u"Schedules")
141                 self.timetable_view.append_column(column)
142                 cell = gtk.CellRendererText()
143                 cell.set_property('xalign', 0.5)
144                 cell.set_property('scale', 0.8)
145                 cell.set_property('xpad', 9)
146                 column.pack_end(cell, True)
147                 column.add_attribute(cell, 'text', i+1)
148
149             #self.timetable_view.set_property('search_column', 0)
150             #self.timetable_view.set_property('enable-search', True)
151             #self.timetable_view.set_property('headers-clickable', True)
152
153             self.current_timetable = gtk.ScrolledWindow()
154             self.current_timetable.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
155
156             self.current_timetable.add_with_viewport(self.timetable_view)
157
158             if self.navigation:
159                 self.remove(self.navigation)
160             self.navigation = self.navigation_buttons()
161             self.add(self.navigation)
162
163         self.add(self.current_timetable)
164         self.show_all()