1 | # Module for handling the time table and booking flights
|
---|
2 |
|
---|
3 | #-----------------------------------------------------------------------------
|
---|
4 |
|
---|
5 | from mlx.gui.common import *
|
---|
6 | from .flightlist import ColumnDescriptor
|
---|
7 | from mlx.rpc import ScheduledFlight
|
---|
8 |
|
---|
9 | import mlx.const as const
|
---|
10 |
|
---|
11 | import datetime
|
---|
12 | import random
|
---|
13 |
|
---|
14 | #-----------------------------------------------------------------------------
|
---|
15 |
|
---|
16 | class Timetable(gtk.Alignment):
|
---|
17 | """The widget for the time table."""
|
---|
18 | def _getVIPRenderer():
|
---|
19 | """Get the renderer for the VIP column."""
|
---|
20 | renderer = gtk.CellRendererToggle()
|
---|
21 | renderer.set_activatable(True)
|
---|
22 | return renderer
|
---|
23 |
|
---|
24 | defaultColumnDescriptors = [
|
---|
25 | ColumnDescriptor("callsign", xstr("timetable_no"),
|
---|
26 | sortable = True, defaultSortable = True),
|
---|
27 | ColumnDescriptor("aircraftType", xstr("timetable_type"),
|
---|
28 | sortable = True,
|
---|
29 | convertFn = lambda aircraftType, flight:
|
---|
30 | aircraftNames[aircraftType]),
|
---|
31 | ColumnDescriptor("departureICAO", xstr("timetable_from"),
|
---|
32 | sortable = True),
|
---|
33 | ColumnDescriptor("arrivalICAO", xstr("timetable_to"), sortable = True),
|
---|
34 | ColumnDescriptor("departureTime", xstr("timetable_dep"),
|
---|
35 | sortable = True),
|
---|
36 | ColumnDescriptor("arrivalTime", xstr("timetable_arr"), sortable = True),
|
---|
37 | ColumnDescriptor("duration", xstr("timetable_duration"),
|
---|
38 | sortable = True,
|
---|
39 | convertFn = lambda duration, flight:
|
---|
40 | "%02d:%02d" % (duration/3600,
|
---|
41 | (duration%3600)/60)),
|
---|
42 | ColumnDescriptor("type", xstr("timetable_vip"), type = bool,
|
---|
43 | renderer = _getVIPRenderer(),
|
---|
44 | sortable = True,
|
---|
45 | convertFn = lambda type, flight:
|
---|
46 | type==ScheduledFlight.TYPE_VIP)
|
---|
47 | ]
|
---|
48 |
|
---|
49 | columnOrdering = ["callsign", "aircraftType",
|
---|
50 | "date", "departureTime", "arrivalTime",
|
---|
51 | "departureICAO", "arrivalICAO", "duration", "type"]
|
---|
52 |
|
---|
53 | @staticmethod
|
---|
54 | def isFlightSelected(flight, regularEnabled, vipEnabled, aircraftTypes):
|
---|
55 | """Determine if the given flight is selected by the given
|
---|
56 | filtering conditions."""
|
---|
57 | return ((regularEnabled and flight.type==ScheduledFlight.TYPE_NORMAL) or \
|
---|
58 | (vipEnabled and flight.type==ScheduledFlight.TYPE_VIP)) and \
|
---|
59 | flight.aircraftType in aircraftTypes
|
---|
60 |
|
---|
61 | def __init__(self, columnDescriptors = defaultColumnDescriptors,
|
---|
62 | popupMenuProducer = None):
|
---|
63 | """Construct the time table."""
|
---|
64 | # FIXME: this is very similar to flightlist.FlightList
|
---|
65 | self._columnDescriptors = columnDescriptors
|
---|
66 | self._popupMenuProducer = popupMenuProducer
|
---|
67 | self._popupMenu = None
|
---|
68 |
|
---|
69 | types = [int]
|
---|
70 | defaultSortableIndex = None
|
---|
71 | for columnDescriptor in self._columnDescriptors:
|
---|
72 | if columnDescriptor.defaultSortable:
|
---|
73 | defaultSortableIndex = len(types)
|
---|
74 | columnDescriptor.appendType(types)
|
---|
75 |
|
---|
76 | self._model = gtk.ListStore(*types)
|
---|
77 | if defaultSortableIndex is not None:
|
---|
78 | sortOrder = SORT_DESCENDING \
|
---|
79 | if self._columnDescriptors[defaultSortableIndex-1]._defaultDescending \
|
---|
80 | else SORT_ASCENDING
|
---|
81 | self._model.set_sort_column_id(defaultSortableIndex, sortOrder)
|
---|
82 | self._view = gtk.TreeView(self._model)
|
---|
83 |
|
---|
84 | self._view.connect("motion-notify-event", self._updateTooltip)
|
---|
85 |
|
---|
86 | flightPairIndexColumn = gtk.TreeViewColumn()
|
---|
87 | flightPairIndexColumn.set_visible(False)
|
---|
88 | self._view.append_column(flightPairIndexColumn)
|
---|
89 |
|
---|
90 | index = 1
|
---|
91 | for columnDescriptor in self._columnDescriptors:
|
---|
92 | column = columnDescriptor.getViewColumn(index)
|
---|
93 | self._view.append_column(column)
|
---|
94 | self._model.set_sort_func(index, self._compareFlights,
|
---|
95 | columnDescriptor.attribute)
|
---|
96 | index += 1
|
---|
97 |
|
---|
98 | self._view.connect("row-activated", self._rowActivated)
|
---|
99 | self._view.connect("button-press-event", self._buttonPressEvent)
|
---|
100 |
|
---|
101 | selection = self._view.get_selection()
|
---|
102 | selection.connect("changed", self._selectionChanged)
|
---|
103 |
|
---|
104 | scrolledWindow = gtk.ScrolledWindow()
|
---|
105 | scrolledWindow.add(self._view)
|
---|
106 | scrolledWindow.set_size_request(800, -1)
|
---|
107 |
|
---|
108 | # FIXME: these should be constants in common.py
|
---|
109 | scrolledWindow.set_policy(gtk.PolicyType.AUTOMATIC,
|
---|
110 | gtk.PolicyType.AUTOMATIC)
|
---|
111 | scrolledWindow.set_shadow_type(gtk.ShadowType.IN)
|
---|
112 |
|
---|
113 | super(Timetable, self).__init__(xalign = 0.5, yalign = 0.0,
|
---|
114 | xscale = 0.0, yscale = 1.0)
|
---|
115 | self.add(scrolledWindow)
|
---|
116 |
|
---|
117 | self._flightPairs = []
|
---|
118 |
|
---|
119 | @property
|
---|
120 | def selectedIndexes(self):
|
---|
121 | """Get the indexes of the selected entries, if any.
|
---|
122 |
|
---|
123 | The indexes are sorted."""
|
---|
124 | selection = self._view.get_selection()
|
---|
125 | (model, rows) = selection.get_selected_rows()
|
---|
126 |
|
---|
127 | indexes = [self._getIndexForPath(path) for path in rows]
|
---|
128 | indexes.sort()
|
---|
129 | return indexes
|
---|
130 |
|
---|
131 | @property
|
---|
132 | def hasFlightPairs(self):
|
---|
133 | """Determine if the timetable contains any flights."""
|
---|
134 | return len(self._flightPairs)>0
|
---|
135 |
|
---|
136 | def clear(self):
|
---|
137 | """Clear the flight pairs."""
|
---|
138 | self._model.clear()
|
---|
139 | self._flightPairs = []
|
---|
140 |
|
---|
141 | def setFlightPairs(self, flightPairs):
|
---|
142 | """Setup the table contents from the given list of
|
---|
143 | rpc.ScheduledFlightPair objects."""
|
---|
144 | self.clear()
|
---|
145 |
|
---|
146 | self._flightPairs = flightPairs
|
---|
147 |
|
---|
148 | def getFlightPair(self, index):
|
---|
149 | """Get the flight pair with the given index."""
|
---|
150 | return self._flightPairs[index]
|
---|
151 |
|
---|
152 | def updateList(self, regularEnabled, vipEnabled, types):
|
---|
153 | """Update the actual list according to the given filter values."""
|
---|
154 | index = 0
|
---|
155 | self._model.clear()
|
---|
156 | for flightPair in self._flightPairs:
|
---|
157 | flight = flightPair.flight0
|
---|
158 | if Timetable.isFlightSelected(flight, regularEnabled, vipEnabled,
|
---|
159 | types):
|
---|
160 | values = [index]
|
---|
161 | for columnDescriptor in self._columnDescriptors:
|
---|
162 | values.append(columnDescriptor.getValueFrom(flight))
|
---|
163 | self._model.append(values)
|
---|
164 | index += 1
|
---|
165 |
|
---|
166 | def _getIndexForPath(self, path):
|
---|
167 | """Get the index for the given path."""
|
---|
168 | iter = self._model.get_iter(path)
|
---|
169 | return self._model.get_value(iter, 0)
|
---|
170 |
|
---|
171 | def _rowActivated(self, flightList, path, column):
|
---|
172 | """Called when a row is selected."""
|
---|
173 | self.emit("row-activated", self._getIndexForPath(path))
|
---|
174 |
|
---|
175 | def _buttonPressEvent(self, widget, event):
|
---|
176 | """Called when a mouse button is pressed or released."""
|
---|
177 | if event.type!=EVENT_BUTTON_PRESS or event.button!=3 or \
|
---|
178 | self._popupMenuProducer is None:
|
---|
179 | return
|
---|
180 |
|
---|
181 | (path, _, _, _) = self._view.get_path_at_pos(int(event.x),
|
---|
182 | int(event.y))
|
---|
183 | selection = self._view.get_selection()
|
---|
184 | selection.unselect_all()
|
---|
185 | selection.select_path(path)
|
---|
186 |
|
---|
187 | if self._popupMenu is None:
|
---|
188 | self._popupMenu = self._popupMenuProducer()
|
---|
189 | menu = self._popupMenu
|
---|
190 | menu.popup(None, None, None, None, event.button, event.time)
|
---|
191 |
|
---|
192 | def _selectionChanged(self, selection):
|
---|
193 | """Called when the selection has changed."""
|
---|
194 | self.emit("selection-changed", self.selectedIndexes)
|
---|
195 |
|
---|
196 | def _compareFlights(self, model, iter1, iter2, mainColumn):
|
---|
197 | """Compare the flights at the given iterators according to the given
|
---|
198 | main column."""
|
---|
199 | index1 = self._model.get_value(iter1, 0)
|
---|
200 | index2 = self._model.get_value(iter2, 0)
|
---|
201 |
|
---|
202 | flightPair1 = self._flightPairs[index1]
|
---|
203 | flightPair2 = self._flightPairs[index2]
|
---|
204 |
|
---|
205 | result = flightPair1.compareBy(flightPair2, mainColumn)
|
---|
206 | if result==0:
|
---|
207 | for column in Timetable.columnOrdering:
|
---|
208 | if column!=mainColumn:
|
---|
209 | result = flightPair1.compareBy(flightPair2, column)
|
---|
210 | if result!=0:
|
---|
211 | break
|
---|
212 | return result
|
---|
213 |
|
---|
214 | def _updateTooltip(self, widget, event):
|
---|
215 | """Update the tooltip for the position of the given event."""
|
---|
216 | try:
|
---|
217 | result = widget.get_path_at_pos( int(event.x), int(event.y))
|
---|
218 | if result is None:
|
---|
219 | widget.set_tooltip_text("")
|
---|
220 | else:
|
---|
221 | (path, col, x, y) = result
|
---|
222 | index = self._getIndexForPath(path)
|
---|
223 |
|
---|
224 | flight = self._flightPairs[index].flight0
|
---|
225 | comment = flight.comment
|
---|
226 | date = flight.date
|
---|
227 |
|
---|
228 | if comment or date!=const.defaultDate:
|
---|
229 | text = ""
|
---|
230 | if comment:
|
---|
231 | text = comment
|
---|
232 | if date!=const.defaultDate:
|
---|
233 | if text:
|
---|
234 | text += "; "
|
---|
235 | text += date.strftime("%Y-%m-%d")
|
---|
236 |
|
---|
237 | widget.set_tooltip_text(text)
|
---|
238 | else:
|
---|
239 | widget.set_tooltip_text("")
|
---|
240 | except Exception as e:
|
---|
241 | print(e)
|
---|
242 | widget.set_tooltip_text("")
|
---|
243 |
|
---|
244 | #-----------------------------------------------------------------------------
|
---|
245 |
|
---|
246 | gobject.signal_new("row-activated", Timetable, gobject.SIGNAL_RUN_FIRST,
|
---|
247 | None, (int,))
|
---|
248 |
|
---|
249 | gobject.signal_new("selection-changed", Timetable, gobject.SIGNAL_RUN_FIRST,
|
---|
250 | None, (object,))
|
---|
251 |
|
---|
252 | #-----------------------------------------------------------------------------
|
---|
253 |
|
---|
254 | class CalendarWindow(gtk.Window):
|
---|
255 | """A window for a calendar."""
|
---|
256 | def __init__(self):
|
---|
257 | """Construct the window."""
|
---|
258 | super(CalendarWindow, self).__init__()
|
---|
259 |
|
---|
260 | self.set_decorated(False)
|
---|
261 | self.set_modal(True)
|
---|
262 | self.connect("key-press-event", self._keyPressed)
|
---|
263 |
|
---|
264 | mainAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
265 | xscale = 1.0, yscale = 1.0)
|
---|
266 | #mainAlignment.set_padding(padding_top = 0, padding_bottom = 12,
|
---|
267 | # padding_left = 8, padding_right = 8)
|
---|
268 |
|
---|
269 | self._calendar = gtk.Calendar()
|
---|
270 | self._calendar.connect("day-selected-double-click", self._daySelectedDoubleClick)
|
---|
271 | mainAlignment.add(self._calendar)
|
---|
272 |
|
---|
273 | self.add(mainAlignment)
|
---|
274 |
|
---|
275 | def setDate(self, date):
|
---|
276 | """Set the current date to the given one."""
|
---|
277 | self._calendar.select_month(date.month-1, date.year)
|
---|
278 | self._calendar.select_day(date.day)
|
---|
279 |
|
---|
280 | def getDate(self):
|
---|
281 | """Get the currently selected date."""
|
---|
282 | (year, monthM1, day) = self._calendar.get_date()
|
---|
283 | return datetime.date(year, monthM1+1, day)
|
---|
284 |
|
---|
285 | def _daySelectedDoubleClick(self, calendar):
|
---|
286 | """Called when a date is double clicked."""
|
---|
287 | self.emit("date-selected")
|
---|
288 |
|
---|
289 | def _keyPressed(self, window, event):
|
---|
290 | """Called when a key is pressed in the window.
|
---|
291 |
|
---|
292 | If the Escape key is pressed, 'delete-event' is emitted to close the
|
---|
293 | window."""
|
---|
294 | keyName = gdk.keyval_name(event.keyval)
|
---|
295 | if keyName =="Escape":
|
---|
296 | self.emit("delete-event", None)
|
---|
297 | return True
|
---|
298 | elif keyName =="Return":
|
---|
299 | self.emit("date-selected")
|
---|
300 | return True
|
---|
301 |
|
---|
302 | gobject.signal_new("date-selected", CalendarWindow, gobject.SIGNAL_RUN_FIRST,
|
---|
303 | None, ())
|
---|
304 |
|
---|
305 | #-----------------------------------------------------------------------------
|
---|
306 |
|
---|
307 | class BookDialog(gtk.Dialog):
|
---|
308 | """The dialog box to select additional data for a booking."""
|
---|
309 | def __init__(self, timetableWindow, flightPair, planes):
|
---|
310 | """Construct the dialog box."""
|
---|
311 | super(BookDialog, self).__init__(title = WINDOW_TITLE_BASE +
|
---|
312 | " - " +
|
---|
313 | xstr("timetable_book_title"),
|
---|
314 | parent = timetableWindow)
|
---|
315 | contentArea = self.get_content_area()
|
---|
316 |
|
---|
317 | frame = gtk.Frame(xstr("timetable_book_frame_title"))
|
---|
318 | frame.set_size_request(600, -1)
|
---|
319 |
|
---|
320 | mainAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
321 | xscale = 0.0, yscale = 0.0)
|
---|
322 | mainAlignment.set_padding(padding_top = 16, padding_bottom = 12,
|
---|
323 | padding_left = 8, padding_right = 8)
|
---|
324 |
|
---|
325 | table = gtk.Table(6, 2)
|
---|
326 | table.set_row_spacings(8)
|
---|
327 | table.set_col_spacings(16)
|
---|
328 |
|
---|
329 | row = 0
|
---|
330 | label = gtk.Label()
|
---|
331 | label.set_markup(xstr("timetable_book_callsign"))
|
---|
332 | label.set_alignment(0.0, 0.5)
|
---|
333 | table.attach(label, 0, 1, row, row + 1)
|
---|
334 |
|
---|
335 | text = flightPair.flight0.callsign
|
---|
336 | if flightPair.flight1 is not None:
|
---|
337 | text += " / " + flightPair.flight1.callsign
|
---|
338 | label = gtk.Label(text)
|
---|
339 | label.set_alignment(0.0, 0.5)
|
---|
340 | table.attach(label, 1, 2, row, row + 1)
|
---|
341 |
|
---|
342 | row += 1
|
---|
343 |
|
---|
344 | label = gtk.Label()
|
---|
345 | label.set_markup(xstr("timetable_book_from_to"))
|
---|
346 | label.set_alignment(0.0, 0.5)
|
---|
347 | table.attach(label, 0, 1, row, row + 1)
|
---|
348 |
|
---|
349 | text = flightPair.flight0.departureICAO + " - " + \
|
---|
350 | flightPair.flight0.arrivalICAO
|
---|
351 | if flightPair.flight1 is not None:
|
---|
352 | text += " - " + flightPair.flight1.arrivalICAO
|
---|
353 | label = gtk.Label(text)
|
---|
354 | label.set_alignment(0.0, 0.5)
|
---|
355 | table.attach(label, 1, 2, row, row + 1)
|
---|
356 |
|
---|
357 | row += 1
|
---|
358 |
|
---|
359 | if flightPair.flight0.type==ScheduledFlight.TYPE_VIP and \
|
---|
360 | flightPair.flight0.date!=const.defaultDate:
|
---|
361 | label = gtk.Label()
|
---|
362 | label.set_markup(xstr("timetable_book_flightDate"))
|
---|
363 | label.set_use_underline(True)
|
---|
364 | label.set_alignment(0.0, 0.5)
|
---|
365 | table.attach(label, 0, 1, row, row + 1)
|
---|
366 |
|
---|
367 | self._flightDate = gtk.Button()
|
---|
368 | self._flightDate.connect("clicked", self._flightDateClicked)
|
---|
369 | self._flightDate.set_tooltip_text(xstr("timetable_book_flightDate_tooltip"))
|
---|
370 | label.set_mnemonic_widget(self._flightDate)
|
---|
371 |
|
---|
372 | table.attach(self._flightDate, 1, 2, row, row + 1)
|
---|
373 |
|
---|
374 | self._calendarWindow = calendarWindow = CalendarWindow()
|
---|
375 | calendarWindow.set_transient_for(self)
|
---|
376 | calendarWindow.connect("delete-event", self._calendarWindowDeleted)
|
---|
377 | calendarWindow.connect("date-selected", self._calendarWindowDateSelected)
|
---|
378 |
|
---|
379 | self._setDate(flightPair.flight0.date)
|
---|
380 |
|
---|
381 | row += 1
|
---|
382 | else:
|
---|
383 | self._flightDate = None
|
---|
384 | self._calendarWindow = None
|
---|
385 |
|
---|
386 | label = gtk.Label()
|
---|
387 | label.set_markup(xstr("timetable_book_dep_arr"))
|
---|
388 | label.set_alignment(0.0, 0.5)
|
---|
389 | table.attach(label, 0, 1, row, row + 1)
|
---|
390 |
|
---|
391 | text = str(flightPair.flight0.departureTime) + " - " + \
|
---|
392 | str(flightPair.flight0.arrivalTime)
|
---|
393 | if flightPair.flight1 is not None:
|
---|
394 | text += " / " + str(flightPair.flight1.departureTime) + " - " + \
|
---|
395 | str(flightPair.flight1.arrivalTime)
|
---|
396 | label = gtk.Label(text)
|
---|
397 | label.set_alignment(0.0, 0.5)
|
---|
398 | table.attach(label, 1, 2, row, row + 1)
|
---|
399 |
|
---|
400 | row += 1
|
---|
401 |
|
---|
402 | label = gtk.Label()
|
---|
403 | label.set_markup(xstr("timetable_book_duration"))
|
---|
404 | label.set_alignment(0.0, 0.5)
|
---|
405 | table.attach(label, 0, 1, row, row + 1)
|
---|
406 |
|
---|
407 |
|
---|
408 | duration = flightPair.flight0.duration
|
---|
409 | text = "%02d:%02d" % (duration/3600, (duration%3600)/60)
|
---|
410 | if flightPair.flight1 is not None:
|
---|
411 | duration = flightPair.flight0.duration
|
---|
412 | text += " / %02d:%02d" % (duration/3600, (duration%3600)/60)
|
---|
413 | label = gtk.Label(text)
|
---|
414 | label.set_alignment(0.0, 0.5)
|
---|
415 | table.attach(label, 1, 2, row, row + 1)
|
---|
416 |
|
---|
417 | row += 2
|
---|
418 |
|
---|
419 | label = gtk.Label()
|
---|
420 | label.set_markup(xstr("timetable_book_tailNumber"))
|
---|
421 | label.set_alignment(0.0, 0.5)
|
---|
422 | table.attach(label, 0, 1, row, row + 1)
|
---|
423 |
|
---|
424 | self._planes = planes
|
---|
425 | tailNumbersModel = gtk.ListStore(str)
|
---|
426 | for plane in planes:
|
---|
427 | tailNumbersModel.append((plane.tailNumber,))
|
---|
428 |
|
---|
429 | self._tailNumber = gtk.ComboBox(model = tailNumbersModel)
|
---|
430 | renderer = gtk.CellRendererText()
|
---|
431 | self._tailNumber.pack_start(renderer, True)
|
---|
432 | self._tailNumber.add_attribute(renderer, "text", 0)
|
---|
433 | self._tailNumber.set_tooltip_text(xstr("timetable_book_tailNumber_tooltip"))
|
---|
434 | self._tailNumber.set_active(random.randint(0, len(planes)-1))
|
---|
435 |
|
---|
436 | table.attach(self._tailNumber, 1, 2, row, row + 1)
|
---|
437 |
|
---|
438 | mainAlignment.add(table)
|
---|
439 |
|
---|
440 | frame.add(mainAlignment)
|
---|
441 | contentArea.pack_start(frame, True, True, 4)
|
---|
442 |
|
---|
443 | self.add_button(xstr("button_cancel"), RESPONSETYPE_CANCEL)
|
---|
444 |
|
---|
445 | self._okButton = self.add_button(xstr("button_book"), RESPONSETYPE_OK)
|
---|
446 | self._okButton.set_use_underline(True)
|
---|
447 | self._okButton.set_can_default(True)
|
---|
448 |
|
---|
449 | @property
|
---|
450 | def plane(self):
|
---|
451 | """Get the currently selected plane."""
|
---|
452 | return self._planes[self._tailNumber.get_active()]
|
---|
453 |
|
---|
454 | @property
|
---|
455 | def date(self):
|
---|
456 | """Get the flight date, if selected."""
|
---|
457 | return None if self._calendarWindow is None \
|
---|
458 | else self._calendarWindow.getDate()
|
---|
459 |
|
---|
460 | def _setDate(self, date):
|
---|
461 | """Set the date to the given one."""
|
---|
462 | self._flightDate.set_label(date.strftime("%Y-%m-%d"))
|
---|
463 | self._calendarWindow.setDate(date)
|
---|
464 |
|
---|
465 | def _flightDateClicked(self, button):
|
---|
466 | """Called when the flight date button is clicked."""
|
---|
467 | self._calendarWindow.set_position(gtk.WIN_POS_MOUSE)
|
---|
468 | self.set_focus(self._calendarWindow)
|
---|
469 | self._calendarWindow.show_all()
|
---|
470 |
|
---|
471 | def _calendarWindowDeleted(self, window, event):
|
---|
472 | """Called when the flight date window is deleted."""
|
---|
473 | self._calendarWindow.hide()
|
---|
474 |
|
---|
475 | def _calendarWindowDateSelected(self, window):
|
---|
476 | """Called when the flight date window is deleted."""
|
---|
477 | self._calendarWindow.hide()
|
---|
478 | date = window.getDate()
|
---|
479 | self._flightDate.set_label(date.strftime("%Y-%m-%d"))
|
---|
480 |
|
---|
481 | #-----------------------------------------------------------------------------
|
---|
482 |
|
---|
483 | class TimetableWindow(gtk.Window):
|
---|
484 | """The window to display the timetable."""
|
---|
485 | typeFamilies = [
|
---|
486 | const.AIRCRAFT_FAMILY_B737NG,
|
---|
487 | const.AIRCRAFT_FAMILY_DH8D,
|
---|
488 | const.AIRCRAFT_FAMILY_B767,
|
---|
489 |
|
---|
490 | const.AIRCRAFT_FAMILY_B737CL,
|
---|
491 | const.AIRCRAFT_FAMILY_CRJ2,
|
---|
492 | const.AIRCRAFT_FAMILY_F70,
|
---|
493 |
|
---|
494 | const.AIRCRAFT_FAMILY_T134,
|
---|
495 | const.AIRCRAFT_FAMILY_T154
|
---|
496 | ]
|
---|
497 |
|
---|
498 | def __init__(self, gui):
|
---|
499 | super(TimetableWindow, self).__init__()
|
---|
500 |
|
---|
501 | self._gui = gui
|
---|
502 | self.set_title(WINDOW_TITLE_BASE + " - " + xstr("timetable_title"))
|
---|
503 | self.set_size_request(-1, 600)
|
---|
504 | self.set_transient_for(gui.mainWindow)
|
---|
505 | self.set_modal(True)
|
---|
506 | self.connect("key-press-event", self._keyPressed)
|
---|
507 |
|
---|
508 | mainAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
509 | xscale = 1.0, yscale = 1.0)
|
---|
510 | mainAlignment.set_padding(padding_top = 0, padding_bottom = 12,
|
---|
511 | padding_left = 8, padding_right = 8)
|
---|
512 |
|
---|
513 | vbox = gtk.VBox()
|
---|
514 |
|
---|
515 | filterAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
516 | xscale = 1.0, yscale = 1.0)
|
---|
517 |
|
---|
518 | filterFrame = gtk.Frame()
|
---|
519 | filterFrame.set_label(xstr("timetable_filter"))
|
---|
520 |
|
---|
521 | filterVBox = gtk.VBox()
|
---|
522 |
|
---|
523 | topAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
524 | xscale = 0.0, yscale = 0.0)
|
---|
525 | topHBox = gtk.HBox()
|
---|
526 |
|
---|
527 | label = gtk.Label(xstr("timetable_flightdate"))
|
---|
528 | label.set_use_underline(True)
|
---|
529 | topHBox.pack_start(label, False, False, 4)
|
---|
530 |
|
---|
531 | self._flightDate = gtk.Button()
|
---|
532 | self._flightDate.connect("clicked", self._flightDateClicked)
|
---|
533 | self._flightDate.connect("clicked", self._flightDateClicked)
|
---|
534 | self._flightDate.set_tooltip_text(xstr("timetable_flightdate_tooltip"))
|
---|
535 | label.set_mnemonic_widget(self._flightDate)
|
---|
536 | topHBox.pack_start(self._flightDate, False, False, 4)
|
---|
537 |
|
---|
538 | filler = gtk.Alignment()
|
---|
539 | filler.set_size_request(48, 2)
|
---|
540 | topHBox.pack_start(filler, False, True, 0)
|
---|
541 |
|
---|
542 | self._regularFlights = gtk.CheckButton(xstr("timetable_show_regular"))
|
---|
543 | self._regularFlights.set_use_underline(True)
|
---|
544 | self._regularFlights.set_tooltip_text(xstr("timetable_show_regular_tooltip"))
|
---|
545 | self._regularFlights.set_active(True)
|
---|
546 | self._regularFlights.connect("toggled", self._filterChanged)
|
---|
547 | topHBox.pack_start(self._regularFlights, False, False, 8)
|
---|
548 |
|
---|
549 | self._vipFlights = gtk.CheckButton(xstr("timetable_show_vip"))
|
---|
550 | self._vipFlights.set_use_underline(True)
|
---|
551 | self._vipFlights.set_tooltip_text(xstr("timetable_show_vip_tooltip"))
|
---|
552 | self._vipFlights.set_active(True)
|
---|
553 | self._vipFlights.connect("toggled", self._filterChanged)
|
---|
554 | topHBox.pack_start(self._vipFlights, False, False, 8)
|
---|
555 |
|
---|
556 | topAlignment.add(topHBox)
|
---|
557 |
|
---|
558 | filterVBox.pack_start(topAlignment, False, False, 4)
|
---|
559 |
|
---|
560 | separator = gtk.HSeparator()
|
---|
561 | filterVBox.pack_start(separator, False, False, 4)
|
---|
562 |
|
---|
563 | typeAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
564 | xscale = 0.0, yscale = 0.0)
|
---|
565 |
|
---|
566 | numColumns = 4
|
---|
567 | numRows = (len(TimetableWindow.typeFamilies)+numColumns-1)/numColumns
|
---|
568 |
|
---|
569 | typeTable = gtk.Table(numRows, numColumns)
|
---|
570 | typeTable.set_col_spacings(8)
|
---|
571 | row = 0
|
---|
572 | column = 0
|
---|
573 | self._typeFamilyButtons = {}
|
---|
574 | for typeFamily in TimetableWindow.typeFamilies:
|
---|
575 | checkButton = gtk.CheckButton(aircraftFamilyNames[typeFamily])
|
---|
576 | checkButton.set_active(True)
|
---|
577 | checkButton.connect("toggled", self._filterChanged)
|
---|
578 | self._typeFamilyButtons[typeFamily] = checkButton
|
---|
579 |
|
---|
580 | typeTable.attach(checkButton, column, column + 1, row, row+1)
|
---|
581 |
|
---|
582 | column += 1
|
---|
583 | if column>=numColumns:
|
---|
584 | row += 1
|
---|
585 | column = 0
|
---|
586 |
|
---|
587 | typeAlignment.add(typeTable)
|
---|
588 | filterVBox.pack_start(typeAlignment, False, False, 4)
|
---|
589 |
|
---|
590 | filterFrame.add(filterVBox)
|
---|
591 |
|
---|
592 | filterAlignment.add(filterFrame)
|
---|
593 | vbox.pack_start(filterAlignment, False, False, 2)
|
---|
594 |
|
---|
595 | self._timetable = Timetable(popupMenuProducer =
|
---|
596 | self._createTimetablePopupMenu)
|
---|
597 | self._timetable.connect("row-activated", self._rowActivated)
|
---|
598 | self._timetable.connect("selection-changed", self._selectionChanged)
|
---|
599 | vbox.pack_start(self._timetable, True, True, 2)
|
---|
600 |
|
---|
601 | alignment = gtk.Alignment(xalign = 1.0, yalign = 0.5,
|
---|
602 | xscale = 0.0, yscale = 0.0)
|
---|
603 | buttonBox = gtk.HBox()
|
---|
604 |
|
---|
605 | self._bookButton = gtk.Button(xstr("button_book"))
|
---|
606 | self._bookButton.set_use_underline(True)
|
---|
607 | self._bookButton.set_can_default(True)
|
---|
608 | self._bookButton.connect("clicked", self._bookClicked)
|
---|
609 | self._bookButton.set_sensitive(False)
|
---|
610 | buttonBox.pack_start(self._bookButton, False, False, 4);
|
---|
611 |
|
---|
612 | self._closeButton = gtk.Button(xstr("button_close"))
|
---|
613 | self._closeButton.set_use_underline(True)
|
---|
614 | self._closeButton.connect("clicked", self._closeClicked)
|
---|
615 | buttonBox.pack_start(self._closeButton, False, False, 4);
|
---|
616 |
|
---|
617 | alignment.add(buttonBox)
|
---|
618 | vbox.pack_start(alignment, False, False, 2)
|
---|
619 |
|
---|
620 | mainAlignment.add(vbox)
|
---|
621 |
|
---|
622 | self._calendarWindow = calendarWindow = CalendarWindow()
|
---|
623 | calendarWindow.set_transient_for(self)
|
---|
624 | calendarWindow.connect("delete-event", self._calendarWindowDeleted)
|
---|
625 | calendarWindow.connect("date-selected", self._calendarWindowDateSelected)
|
---|
626 |
|
---|
627 | self.add(mainAlignment)
|
---|
628 |
|
---|
629 | self.setDate(datetime.date.today())
|
---|
630 |
|
---|
631 | self._flightPairToBook = None
|
---|
632 |
|
---|
633 | @property
|
---|
634 | def hasFlightPairs(self):
|
---|
635 | """Determine if there any flight pairs displayed in the window."""
|
---|
636 | return self._timetable.hasFlightPairs
|
---|
637 |
|
---|
638 | @property
|
---|
639 | def isRegularEnabled(self):
|
---|
640 | """Determine if regular flights are enabled."""
|
---|
641 | return self._regularFlights.get_active()!=0
|
---|
642 |
|
---|
643 | @property
|
---|
644 | def isVIPEnabled(self):
|
---|
645 | """Determine if VIP flights are enabled."""
|
---|
646 | return self._vipFlights.get_active()!=0
|
---|
647 |
|
---|
648 | def setTypes(self, aircraftTypes):
|
---|
649 | """Enable/disable the type family checkboxes according to the given
|
---|
650 | list of types."""
|
---|
651 | typeFamilies = set()
|
---|
652 | for aircraftType in aircraftTypes:
|
---|
653 | typeFamilies.add(const.aircraftType2Family(aircraftType))
|
---|
654 |
|
---|
655 | for (typeFamily, checkButton) in self._typeFamilyButtons.items():
|
---|
656 | checkButton.set_sensitive(typeFamily in typeFamilies)
|
---|
657 |
|
---|
658 | def clear(self):
|
---|
659 | """Clear all flight pairs."""
|
---|
660 | self._timetable.clear()
|
---|
661 |
|
---|
662 | def setFlightPairs(self, flightPairs):
|
---|
663 | """Set the flight pairs."""
|
---|
664 | self._timetable.setFlightPairs(flightPairs)
|
---|
665 | self._updateList()
|
---|
666 |
|
---|
667 | def setDate(self, date):
|
---|
668 | """Set the date to the given one."""
|
---|
669 | self._flightDate.set_label(date.strftime("%Y-%m-%d"))
|
---|
670 | self._calendarWindow.setDate(date)
|
---|
671 |
|
---|
672 | def _closeClicked(self, button):
|
---|
673 | """Called when the Close button is clicked.
|
---|
674 |
|
---|
675 | A 'delete-event' is emitted to close the window."""
|
---|
676 | self.emit("delete-event", None)
|
---|
677 |
|
---|
678 | def _flightDateClicked(self, button):
|
---|
679 | """Called when the flight date button is clicked."""
|
---|
680 | self._calendarWindow.set_position(gtk.WIN_POS_MOUSE)
|
---|
681 | self.set_focus(self._calendarWindow)
|
---|
682 | self._calendarWindow.show_all()
|
---|
683 |
|
---|
684 | def _calendarWindowDeleted(self, window, event):
|
---|
685 | """Called when the flight date window is deleted."""
|
---|
686 | self._calendarWindow.hide()
|
---|
687 |
|
---|
688 | def _calendarWindowDateSelected(self, window):
|
---|
689 | """Called when the flight date window is deleted."""
|
---|
690 | self._calendarWindow.hide()
|
---|
691 | date = window.getDate()
|
---|
692 | self._flightDate.set_label(date.strftime("%Y-%m-%d"))
|
---|
693 | self._gui.updateTimeTable(date)
|
---|
694 |
|
---|
695 | def _filterChanged(self, checkButton):
|
---|
696 | """Called when the filter conditions have changed."""
|
---|
697 | self._updateList()
|
---|
698 |
|
---|
699 | def _keyPressed(self, window, event):
|
---|
700 | """Called when a key is pressed in the window.
|
---|
701 |
|
---|
702 | If the Escape key is pressed, 'delete-event' is emitted to close the
|
---|
703 | window."""
|
---|
704 | if gdk.keyval_name(event.keyval) == "Escape":
|
---|
705 | self.emit("delete-event", None)
|
---|
706 | return True
|
---|
707 |
|
---|
708 | def _updateList(self):
|
---|
709 | """Update the timetable list."""
|
---|
710 | aircraftTypes = []
|
---|
711 | for (aircraftFamily, button) in self._typeFamilyButtons.items():
|
---|
712 | if button.get_active():
|
---|
713 | aircraftTypes += const.aircraftFamily2Types[aircraftFamily]
|
---|
714 |
|
---|
715 | self._timetable.updateList(self.isRegularEnabled,
|
---|
716 | self.isVIPEnabled,
|
---|
717 | aircraftTypes)
|
---|
718 |
|
---|
719 | def _bookClicked(self, button):
|
---|
720 | """Called when the book button has been clicked."""
|
---|
721 | self._book(self._timetable.getFlightPair(self._timetable.selectedIndexes[0]))
|
---|
722 |
|
---|
723 | def _rowActivated(self, timetable, index):
|
---|
724 | """Called when a row has been activated (e.g. double-clicked) in the
|
---|
725 | timetable."""
|
---|
726 | self._book(self._timetable.getFlightPair(index))
|
---|
727 |
|
---|
728 | def _selectionChanged(self, timetable, indexes):
|
---|
729 | """Called when the selection has changed.
|
---|
730 |
|
---|
731 | It sets the sensitivity of the book button based on whether a row is
|
---|
732 | selected or not."""
|
---|
733 | self._bookButton.set_sensitive(len(indexes)>0)
|
---|
734 |
|
---|
735 | def _book(self, flightPair):
|
---|
736 | """Try to book the given flight pair."""
|
---|
737 | self._flightPairToBook = flightPair
|
---|
738 | self._gui.getFleet(callback = self._continueBook,
|
---|
739 | busyCallback = self._busyCallback)
|
---|
740 |
|
---|
741 | def _busyCallback(self, busy):
|
---|
742 | """Called when the busy state has changed."""
|
---|
743 | self.set_sensitive(not busy)
|
---|
744 |
|
---|
745 | def _continueBook(self, fleet):
|
---|
746 | """Continue booking, once the fleet is available."""
|
---|
747 | flightPair = self._flightPairToBook
|
---|
748 | aircraftType = flightPair.flight0.aircraftType
|
---|
749 | planes = [plane for plane in fleet
|
---|
750 | if plane.aircraftType == aircraftType]
|
---|
751 | planes.sort(key = lambda p: p.tailNumber)
|
---|
752 |
|
---|
753 | dialog = BookDialog(self, flightPair, planes)
|
---|
754 | dialog.show_all()
|
---|
755 | result = dialog.run()
|
---|
756 | dialog.hide()
|
---|
757 | if result==RESPONSETYPE_OK:
|
---|
758 | flightIDs = [flightPair.flight0.id]
|
---|
759 | if flightPair.flight1 is not None:
|
---|
760 | flightIDs.append(flightPair.flight1.id)
|
---|
761 |
|
---|
762 | date = dialog.date
|
---|
763 | if date is None:
|
---|
764 | date = self._calendarWindow.getDate()
|
---|
765 |
|
---|
766 | self._gui.bookFlights(self._bookFlightsCallback,
|
---|
767 | flightIDs, date, dialog.plane.tailNumber,
|
---|
768 | busyCallback = self._busyCallback)
|
---|
769 |
|
---|
770 | def _bookFlightsCallback(self, returned, result):
|
---|
771 | """Called when the booking has finished."""
|
---|
772 | if returned:
|
---|
773 | dialog = gtk.MessageDialog(parent = self,
|
---|
774 | type = MESSAGETYPE_INFO,
|
---|
775 | message_format = xstr("bookflights_successful"))
|
---|
776 | dialog.format_secondary_markup(xstr("bookflights_successful_secondary"))
|
---|
777 | else:
|
---|
778 | dialog = gtk.MessageDialog(parent = self,
|
---|
779 | type = MESSAGETYPE_ERROR,
|
---|
780 | message_format = xstr("bookflights_failed"))
|
---|
781 | dialog.format_secondary_markup(xstr("bookflights_failed_secondary"))
|
---|
782 |
|
---|
783 | dialog.add_button(xstr("button_ok"), RESPONSETYPE_OK)
|
---|
784 | dialog.set_title(WINDOW_TITLE_BASE)
|
---|
785 |
|
---|
786 | dialog.run()
|
---|
787 | dialog.hide()
|
---|
788 |
|
---|
789 | def _createTimetablePopupMenu(self):
|
---|
790 | """Get the popuop menu for the timetable."""
|
---|
791 | menu = gtk.Menu()
|
---|
792 |
|
---|
793 | menuItem = gtk.MenuItem()
|
---|
794 | menuItem.set_label(xstr("timetable_popup_book"))
|
---|
795 | menuItem.set_use_underline(True)
|
---|
796 | menuItem.connect("activate", self._popupBook)
|
---|
797 | menuItem.show()
|
---|
798 |
|
---|
799 | menu.append(menuItem)
|
---|
800 |
|
---|
801 | return menu
|
---|
802 |
|
---|
803 | def _popupBook(self, menuItem):
|
---|
804 | """Try to book the given flight pair."""
|
---|
805 | index = self._timetable.selectedIndexes[0]
|
---|
806 | self._book(self._timetable.getFlightPair(index))
|
---|