source: src/mlx/gui/flightlist.py@ 985:f5569c17e934

python3
Last change on this file since 985:f5569c17e934 was 983:c8bdc51e9f79, checked in by István Váradi <ivaradi@…>, 5 years ago

The cell data function of the flight percentage column in the accepted flights window expects the correct number of arguments (re #347).

File size: 28.7 KB
Line 
1# A widget which is a generic list of flights
2
3#-----------------------------------------------------------------------------
4
5from mlx.gui.common import *
6
7import mlx.const as const
8
9#-----------------------------------------------------------------------------
10
11class ColumnDescriptor(object):
12 """A descriptor for a column in the list."""
13 def __init__(self, attribute, heading, type = str,
14 convertFn = None, renderer = None,
15 extraColumnAttributes = None, sortable = False,
16 defaultSortable = False, defaultDescending = False,
17 cellDataFn = None):
18 """Construct the descriptor."""
19 self._attribute = attribute
20 self._heading = heading
21 self._type = type
22 self._convertFn = convertFn
23 self._renderer = \
24 gtk.CellRendererText() if renderer is None else renderer
25 self._extraColumnAttributes = extraColumnAttributes
26 self._sortable = sortable
27 self._defaultSortable = defaultSortable
28 self._defaultDescending = defaultDescending
29 self._cellDataFn = cellDataFn
30
31 @property
32 def attribute(self):
33 """Get the attribute the column belongs to."""
34 return self._attribute
35
36 @property
37 def defaultSortable(self):
38 """Determine if this column is the default sortable one."""
39 return self._defaultSortable
40
41 def appendType(self, types):
42 """Append the type of this column to the given list of types."""
43 types.append(self._type)
44
45 def getViewColumn(self, index):
46 """Get a new column object for a tree view.
47
48 @param index is the 0-based index of the column."""
49 if isinstance(self._renderer, gtk.CellRendererText):
50 column = gtk.TreeViewColumn(self._heading, self._renderer,
51 text = index)
52 elif isinstance(self._renderer, gtk.CellRendererToggle):
53 column = gtk.TreeViewColumn(self._heading, self._renderer,
54 active = index)
55 else:
56 column = gtk.TreeViewColumn(self._heading, self._renderer)
57 column.set_expand(True)
58 if self._sortable:
59 column.set_sort_column_id(index)
60 column.set_sort_indicator(True)
61
62 if self._extraColumnAttributes is not None:
63 for (key, value) in self._extraColumnAttributes.items():
64 if key=="alignment":
65 self._renderer.set_alignment(value, 0.5)
66 else:
67 raise Exception("unhandled extra column attribute '" +
68 key + "'")
69 if self._cellDataFn is not None:
70 column.set_cell_data_func(self._renderer, self._cellDataFn)
71
72 return column
73
74 def getValueFrom(self, flight):
75 """Get the value from the given flight."""
76 attributes = self._attribute.split(".")
77 value = getattr(flight, attributes[0])
78 for attr in attributes[1:]:
79 value = getattr(value, attr)
80 return self._type(value) if self._convertFn is None \
81 else self._convertFn(value, flight)
82
83#-----------------------------------------------------------------------------
84
85class FlightList(gtk.Alignment):
86 """Construct the flight list.
87
88 This is a complete widget with a scroll window. It is alignment centered
89 horizontally and expandable vertically."""
90
91 defaultColumnDescriptors = [
92 ColumnDescriptor("callsign", xstr("flightsel_no")),
93 ColumnDescriptor("departureTime", xstr("flightsel_deptime"),
94 sortable = True, defaultSortable = True),
95 ColumnDescriptor("departureICAO", xstr("flightsel_from"),
96 sortable = True),
97 ColumnDescriptor("arrivalICAO", xstr("flightsel_to"), sortable = True)
98 ]
99
100 def __init__(self, columnDescriptors = defaultColumnDescriptors,
101 popupMenuProducer = None, widthRequest = None,
102 multiSelection = False):
103 """Construct the flight list with the given column descriptors."""
104
105 self._columnDescriptors = columnDescriptors
106 self._popupMenuProducer = popupMenuProducer
107 self._popupMenu = None
108
109 types = [int]
110 defaultSortableIndex = None
111 for columnDescriptor in self._columnDescriptors:
112 if columnDescriptor.defaultSortable:
113 defaultSortableIndex = len(types)
114 columnDescriptor.appendType(types)
115
116 self._model = gtk.ListStore(*types)
117 if defaultSortableIndex is not None:
118 sortOrder = SORT_DESCENDING \
119 if self._columnDescriptors[defaultSortableIndex-1]._defaultDescending \
120 else SORT_ASCENDING
121 self._model.set_sort_column_id(defaultSortableIndex, sortOrder)
122 self._view = gtk.TreeView(self._model)
123
124 flightIndexColumn = gtk.TreeViewColumn()
125 flightIndexColumn.set_visible(False)
126 self._view.append_column(flightIndexColumn)
127
128 index = 1
129 for columnDescriptor in self._columnDescriptors:
130 column = columnDescriptor.getViewColumn(index)
131 self._view.append_column(column)
132 index += 1
133
134 self._view.connect("row-activated", self._rowActivated)
135 self._view.connect("button-press-event", self._buttonPressEvent)
136
137 selection = self._view.get_selection()
138 selection.connect("changed", self._selectionChanged)
139 if multiSelection:
140 selection.set_mode(SELECTION_MULTIPLE)
141
142 scrolledWindow = gtk.ScrolledWindow()
143 scrolledWindow.add(self._view)
144 if widthRequest is not None:
145 scrolledWindow.set_size_request(widthRequest, -1)
146 # FIXME: these should be constants in common.py
147 scrolledWindow.set_policy(gtk.PolicyType.AUTOMATIC if pygobject
148 else gtk.POLICY_AUTOMATIC,
149 gtk.PolicyType.AUTOMATIC if pygobject
150 else gtk.POLICY_AUTOMATIC)
151 scrolledWindow.set_shadow_type(gtk.ShadowType.IN if pygobject
152 else gtk.SHADOW_IN)
153
154 super(FlightList, self).__init__(xalign = 0.5, yalign = 0.0,
155 xscale = 0.0, yscale = 1.0)
156 self.add(scrolledWindow)
157
158 @property
159 def selectedIndexes(self):
160 """Get the indexes of the selected entries, if any.
161
162 The indexes are sorted."""
163 selection = self._view.get_selection()
164 (model, rows) = selection.get_selected_rows()
165
166 indexes = [self._getIndexForPath(path) for path in rows]
167 indexes.sort()
168 return indexes
169
170 @property
171 def hasFlights(self):
172 """Determine if there are any flights in the list."""
173 return self._model.get_iter_first() is not None
174
175 def clear(self):
176 """Clear the model."""
177 self._model.clear()
178
179 def addFlight(self, flight):
180 """Add the given booked flight."""
181 values = [self._model.iter_n_children(None)]
182 for columnDescriptor in self._columnDescriptors:
183 values.append(columnDescriptor.getValueFrom(flight))
184 self._model.append(values)
185
186 def removeFlights(self, indexes):
187 """Remove the flights with the given indexes."""
188 model = self._model
189 idx = 0
190 iter = model.get_iter_first()
191 while iter is not None:
192 nextIter = model.iter_next(iter)
193 if model.get_value(iter, 0) in indexes:
194 model.remove(iter)
195 else:
196 model.set_value(iter, 0, idx)
197 idx += 1
198 iter = nextIter
199
200 def _getIndexForPath(self, path):
201 """Get the index for the given path."""
202 iter = self._model.get_iter(path)
203 return self._model.get_value(iter, 0)
204
205 def _rowActivated(self, flightList, path, column):
206 """Called when a row is selected."""
207 self.emit("row-activated", self._getIndexForPath(path))
208
209 def _buttonPressEvent(self, widget, event):
210 """Called when a mouse button is pressed or released."""
211 if event.type!=EVENT_BUTTON_PRESS or event.button!=3 or \
212 self._popupMenuProducer is None:
213 return
214
215 (path, _, _, _) = self._view.get_path_at_pos(int(event.x),
216 int(event.y))
217 selection = self._view.get_selection()
218 selection.unselect_all()
219 selection.select_path(path)
220
221 if self._popupMenu is None:
222 self._popupMenu = self._popupMenuProducer()
223 menu = self._popupMenu
224 if pygobject:
225 menu.popup(None, None, None, None, event.button, event.time)
226 else:
227 menu.popup(None, None, None, event.button, event.time)
228
229 def _selectionChanged(self, selection):
230 """Called when the selection has changed."""
231 self.emit("selection-changed", self.selectedIndexes)
232
233#-------------------------------------------------------------------------------
234
235gobject.signal_new("row-activated", FlightList, gobject.SIGNAL_RUN_FIRST,
236 None, (int,))
237
238gobject.signal_new("selection-changed", FlightList, gobject.SIGNAL_RUN_FIRST,
239 None, (object,))
240
241#-----------------------------------------------------------------------------
242
243class PendingFlightsFrame(gtk.Frame):
244 """A frame for a list of pending (reported or rejected) flights.
245
246 It contains the list and the buttons available."""
247 @staticmethod
248 def getAircraft(tailNumber, bookedFlight):
249 """Get the aircraft from the given booked flight.
250
251 This is the tail number followed by the ICAO code of the aircraft's
252 type."""
253 return tailNumber + \
254 " (" + const.icaoCodes[bookedFlight.aircraftType] + ")"
255
256 def _getAcft(tailNumber, bookedFlight):
257 return PendingFlightsFrame.getAircraft(tailNumber, bookedFlight)
258
259 columnDescriptors = [
260 ColumnDescriptor("callsign", xstr("flightsel_no")),
261 ColumnDescriptor("departureTime", xstr("flightsel_deptime"),
262 sortable = True, defaultSortable = True),
263 ColumnDescriptor("departureICAO", xstr("flightsel_from"),
264 sortable = True),
265 ColumnDescriptor("arrivalICAO", xstr("flightsel_to"),
266 sortable = True),
267 ColumnDescriptor("tailNumber", xstr("pendflt_acft"),
268 convertFn = _getAcft)
269 ]
270
271 def __init__(self, which, wizard, window, pirepEditable = False):
272 """Construct the frame with the given title."""
273 super(PendingFlightsFrame, self).__init__()
274 self.set_label(xstr("pendflt_title_" + which))
275
276 self._which = which
277 self._wizard = wizard
278 self._window = window
279 self._pirepEditable = pirepEditable
280
281 alignment = gtk.Alignment(xscale = 1.0, yscale = 1.0)
282 alignment.set_padding(padding_top = 2, padding_bottom = 8,
283 padding_left = 4, padding_right = 4)
284
285 hbox = gtk.HBox()
286
287 self._flights = []
288 self._flightList = FlightList(columnDescriptors =
289 PendingFlightsFrame.columnDescriptors,
290 widthRequest = 500, multiSelection = True,
291 popupMenuProducer =
292 self._producePopupMenu)
293 self._flightList.connect("selection-changed", self._selectionChanged)
294
295 hbox.pack_start(self._flightList, True, True, 4)
296
297 buttonBox = gtk.VBox()
298
299 self._editButton = gtk.Button(xstr("pendflt_" +
300 ("edit" if pirepEditable else
301 "view") + "_" + which))
302 self._editButton.set_sensitive(False)
303 self._editButton.set_use_underline(True)
304 self._editButton.connect("clicked", self._editClicked)
305 buttonBox.pack_start(self._editButton, False, False, 2)
306
307 self._reflyButton = gtk.Button(xstr("pendflt_refly_" + which))
308 self._reflyButton.set_sensitive(False)
309 self._reflyButton.set_use_underline(True)
310 self._reflyButton.connect("clicked", self._reflyClicked)
311 buttonBox.pack_start(self._reflyButton, False, False, 2)
312
313 self._deleteButton = gtk.Button(xstr("pendflt_delete_" + which))
314 self._deleteButton.set_sensitive(False)
315 self._deleteButton.set_use_underline(True)
316 self._deleteButton.connect("clicked", self._deleteClicked)
317 buttonBox.pack_start(self._deleteButton, False, False, 2)
318
319 hbox.pack_start(buttonBox, False, False, 4)
320
321 alignment.add(hbox)
322 self.add(alignment)
323
324 @property
325 def hasFlights(self):
326 """Determine if there are any flights in the list."""
327 return self._flightList.hasFlights
328
329 def clear(self):
330 """Clear the lists."""
331 self._flights = []
332 self._flightList.clear()
333
334 def addFlight(self, flight):
335 """Add a flight to the list."""
336 self._flights.append(flight)
337 self._flightList.addFlight(flight)
338
339 def _selectionChanged(self, flightList, selectedIndexes):
340 """Called when the selection in the list has changed."""
341 self._editButton.set_sensitive(len(selectedIndexes)==1)
342 self._reflyButton.set_sensitive(len(selectedIndexes)>0)
343 self._deleteButton.set_sensitive(len(selectedIndexes)>0)
344
345 def _editClicked(self, button):
346 """Called when the Edit button is clicked."""
347 self._editSelected()
348
349 def _editSelected(self):
350 """Edit or view the selected flight."""
351 gui = self._wizard.gui
352 gui.beginBusy(xstr("pendflt_pirep_busy"))
353 self.set_sensitive(False)
354
355 indexes = self._flightList.selectedIndexes
356 assert(len(indexes)==1)
357
358 flightID = self._flights[indexes[0]].id
359 gui.webHandler.getPIREP(self._pirepResultCallback, flightID)
360
361 def _pirepResultCallback(self, returned, result):
362 """Called when the PIREP query result is available."""
363 gobject.idle_add(self._handlePIREPResult, returned, result)
364
365 def _handlePIREPResult(self, returned, result):
366 """Handle the refly result."""
367
368 self.set_sensitive(True)
369 gui = self._wizard.gui
370 gui.endBusy()
371
372 if returned:
373 if self._pirepEditable:
374 gui.editPIREP(result.pirep)
375 else:
376 gui.viewMessagedPIREP(result.pirep)
377
378 def _reflyClicked(self, button):
379 """Called when the Refly button is clicked."""
380 self._reflySelected()
381
382 def _reflySelected(self):
383 """Mark the selected flight(s) to refly."""
384 if askYesNo(xstr("pendflt_refly_question"), parent = self._window):
385 gui = self._wizard.gui
386 gui.beginBusy(xstr("pendflt_refly_busy"))
387 self.set_sensitive(False)
388
389 flightIDs = [self._flights[i].id
390 for i in self._flightList.selectedIndexes]
391 gui.webHandler.reflyFlights(self._reflyResultCallback, flightIDs)
392
393 def _reflyResultCallback(self, returned, result):
394 """Called when the refly result is available."""
395 gobject.idle_add(self._handleReflyResult, returned, result)
396
397 def _handleReflyResult(self, returned, result):
398 """Handle the refly result."""
399
400 self.set_sensitive(True)
401 gui = self._wizard.gui
402 gui.endBusy()
403
404 print("PendingFlightsFrame._handleReflyResult", returned, result)
405
406 if returned:
407 indexes = self._flightList.selectedIndexes
408
409 flights = [self._flights[index] for index in indexes]
410
411 self._flightList.removeFlights(indexes)
412 for index in indexes[::-1]:
413 del self._flights[index]
414
415 for flight in flights:
416 self._wizard.reflyFlight(flight)
417 self._window.checkFlights()
418 else:
419 communicationErrorDialog()
420
421 def _deleteClicked(self, button):
422 """Called when the Delete button is clicked."""
423 self._deleteSelected()
424
425 def _deleteSelected(self):
426 """Delete the selected flight."""
427 if askYesNo(xstr("flight_delete_question"), parent = self._window):
428 gui = self._wizard.gui
429 gui.beginBusy(xstr("pendflt_refly_busy"))
430 self.set_sensitive(False)
431
432 flightIDs = [self._flights[i].id
433 for i in self._flightList.selectedIndexes]
434 gui.webHandler.deleteFlights(self._deleteResultCallback, flightIDs)
435
436 def _deleteResultCallback(self, returned, result):
437 """Called when the deletion result is available."""
438 gobject.idle_add(self._handleDeleteResult, returned, result)
439
440 def _handleDeleteResult(self, returned, result):
441 """Handle the delete result."""
442
443 self.set_sensitive(True)
444 gui = self._wizard.gui
445 gui.endBusy()
446
447 print("PendingFlightsFrame._handleDeleteResult", returned, result)
448
449 if returned:
450 indexes = self._flightList.selectedIndexes
451
452 flights = [self._flights[index] for index in indexes]
453
454 self._flightList.removeFlights(indexes)
455 for index in indexes[::-1]:
456 del self._flights[index]
457
458 for flight in flights:
459 self._wizard.deleteFlight(flight)
460 self._window.checkFlights()
461 else:
462 communicationErrorDialog()
463
464 def _producePopupMenu(self):
465 """Create the popup menu for the flights."""
466 menu = gtk.Menu()
467
468 menuItem = gtk.MenuItem()
469 menuItem.set_label(xstr("pendflt_" +
470 ("edit" if self._pirepEditable else "view") +
471 "_" + self._which))
472 menuItem.set_use_underline(True)
473 menuItem.connect("activate", self._popupEdit)
474 menuItem.show()
475
476 menu.append(menuItem)
477
478 menuItem = gtk.MenuItem()
479 menuItem.set_label(xstr("pendflt_refly_" + self._which))
480 menuItem.set_use_underline(True)
481 menuItem.connect("activate", self._popupRefly)
482 menuItem.show()
483
484 menu.append(menuItem)
485
486 menuItem = gtk.MenuItem()
487 menuItem.set_label(xstr("pendflt_delete_" + self._which))
488 menuItem.set_use_underline(True)
489 menuItem.connect("activate", self._popupDelete)
490 menuItem.show()
491
492 menu.append(menuItem)
493
494 return menu
495
496 def _popupEdit(self, menuitem):
497 """Called when the Edit or View menu item is selected from the popup
498 menu."""
499 self._editSelected()
500
501 def _popupRefly(self, menuitem):
502 """Called when the Refly menu item is selected from the popup menu."""
503 self._reflySelected()
504
505 def _popupDelete(self, menuitem):
506 """Called when the Delete menu item is selected from the popup menu."""
507 self._deleteSelected()
508
509#-----------------------------------------------------------------------------
510
511class PendingFlightsWindow(gtk.Window):
512 """The window to display the lists of the pending (reported or rejected)
513 flights."""
514 def __init__(self, wizard):
515 """Construct the window"""
516 super(PendingFlightsWindow, self).__init__()
517
518 gui = wizard.gui
519
520 self.set_title(WINDOW_TITLE_BASE + " - " + xstr("pendflt_title"))
521 self.set_size_request(-1, 450)
522 self.set_transient_for(gui.mainWindow)
523 self.set_modal(True)
524
525 mainAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
526 xscale = 1.0, yscale = 1.0)
527 mainAlignment.set_padding(padding_top = 0, padding_bottom = 12,
528 padding_left = 8, padding_right = 8)
529
530 vbox = gtk.VBox()
531
532 self._reportedFrame = PendingFlightsFrame("reported", wizard, self,
533 True)
534 vbox.pack_start(self._reportedFrame, True, True, 2)
535
536 self._rejectedFrame = PendingFlightsFrame("rejected", wizard, self)
537 vbox.pack_start(self._rejectedFrame, True, True, 2)
538
539 alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
540 xscale = 0.0, yscale = 0.0)
541 self._closeButton = gtk.Button(xstr("button_ok"))
542 self._closeButton.connect("clicked", self._closeClicked)
543 self._closeButton.set_use_underline(True)
544 alignment.add(self._closeButton)
545 vbox.pack_start(alignment, False, False, 2)
546
547 mainAlignment.add(vbox)
548
549 self.add(mainAlignment)
550
551 self.connect("key-press-event", self._keyPressed)
552
553 @property
554 def hasFlights(self):
555 """Determine if the window has any flights."""
556 return self._reportedFrame.hasFlights or self._rejectedFrame.hasFlights
557
558 def clear(self):
559 """Clear the lists."""
560 self._reportedFrame.clear()
561 self._rejectedFrame.clear()
562
563 def addReportedFlight(self, flight):
564 """Add a reported flight."""
565 self._reportedFrame.addFlight(flight)
566
567 def addRejectedFlight(self, flight):
568 """Add a rejected flight."""
569 self._rejectedFrame.addFlight(flight)
570
571 def checkFlights(self):
572 """Check if there are any flights in any of the lists, and close the
573 window if not."""
574 if not self.hasFlights:
575 self.emit("delete-event", None)
576
577 def _closeClicked(self, button):
578 """Called when the Close button is clicked.
579
580 A 'delete-event' is emitted to close the window."""
581 self.emit("delete-event", None)
582
583 def _keyPressed(self, window, event):
584 """Called when a key is pressed in the window.
585
586 If the Escape key is pressed, 'delete-event' is emitted to close the
587 window."""
588 if gdk.keyval_name(event.keyval) == "Escape":
589 self.emit("delete-event", None)
590 return True
591
592#-----------------------------------------------------------------------------
593
594class AcceptedFlightsWindow(gtk.Window):
595 """A window for a list of accepted flights."""
596 def getFlightDuration(flightTimeStart, flight):
597 """Get the flight duration for the given flight."""
598 minutes = int(round((flight.flightTimeEnd - flightTimeStart)/60.0))
599 return "%02d:%02d" % (minutes/60, minutes%60)
600
601 columnDescriptors = [
602 ColumnDescriptor("bookedFlight.callsign", xstr("flightsel_no")),
603 ColumnDescriptor("bookedFlight.departureTime", xstr("flightsel_deptime"),
604 sortable = True, defaultSortable = True,
605 defaultDescending = True),
606 ColumnDescriptor("bookedFlight.departureICAO", xstr("flightsel_from"),
607 sortable = True),
608 ColumnDescriptor("bookedFlight.arrivalICAO", xstr("flightsel_to"),
609 sortable = True),
610 ColumnDescriptor("bookedFlight.tailNumber", xstr("pendflt_acft"),
611 convertFn = lambda value, flight:
612 PendingFlightsFrame.getAircraft(value,
613 flight.bookedFlight)),
614 ColumnDescriptor("flightTimeStart", xstr("acceptedflt_flight_duration"),
615 convertFn = getFlightDuration, sortable = True,
616 extraColumnAttributes =
617 { "alignment": 0.5 } ),
618 ColumnDescriptor("numPassengers", xstr("acceptedflt_num_pax"),
619 type = int, sortable = True,
620 extraColumnAttributes =
621 { "alignment": 1.0 } ),
622 ColumnDescriptor("fuelUsed", xstr("acceptedflt_fuel"),
623 type = int, sortable = True,
624 extraColumnAttributes =
625 { "alignment": 1.0 } ),
626 ColumnDescriptor("rating", xstr("acceptedflt_rating"),
627 type = float, sortable = True,
628 extraColumnAttributes =
629 { "alignment": 1.0 },
630 cellDataFn = lambda col, cell, model, iter, data:
631 cell.set_property("text",
632 "%.0f" %
633 (model.get(iter, 9)[0],)))
634 ]
635
636 def __init__(self, gui):
637 """Construct the window."""
638 super(AcceptedFlightsWindow, self).__init__()
639
640 self._gui = gui
641
642 self.set_title(WINDOW_TITLE_BASE + " - " + xstr("acceptedflt_title"))
643 self.set_size_request(-1, 700)
644 self.set_transient_for(gui.mainWindow)
645
646 alignment = gtk.Alignment(xscale = 1.0, yscale = 1.0)
647 alignment.set_padding(padding_top = 2, padding_bottom = 8,
648 padding_left = 4, padding_right = 4)
649
650 vbox = gtk.VBox()
651
652 hbox = gtk.HBox()
653 vbox.pack_start(hbox, True, True, 4)
654
655 self._flights = []
656 self._flightList = FlightList(columnDescriptors =
657 AcceptedFlightsWindow.columnDescriptors,
658 widthRequest = 750,
659 multiSelection = False)
660 self._flightList.connect("selection-changed", self._selectionChanged)
661 self._flightList.connect("row-activated", self._rowActivated)
662
663 hbox.pack_start(self._flightList, True, True, 4)
664
665 buttonBox = gtk.VBox()
666
667 self._refreshButton = gtk.Button(xstr("acceptedflt_refresh"))
668 self._refreshButton.set_sensitive(True)
669 self._refreshButton.set_use_underline(True)
670 self._refreshButton.connect("clicked", self._refreshClicked)
671 buttonBox.pack_start(self._refreshButton, False, False, 2)
672
673 filler = gtk.Alignment(xalign = 0.0, yalign = 0.0,
674 xscale = 1.0, yscale = 1.0)
675 filler.set_size_request(-1, 4)
676 buttonBox.pack_start(filler, False, False, 0)
677
678 self._viewButton = gtk.Button(xstr("acceptedflt_view"))
679 self._viewButton.set_sensitive(False)
680 self._viewButton.set_use_underline(True)
681 self._viewButton.connect("clicked", self._viewClicked)
682 buttonBox.pack_start(self._viewButton, False, False, 2)
683
684 hbox.pack_start(buttonBox, False, False, 4)
685
686 buttonAlignment = gtk.Alignment(xscale = 0.0, yscale = 0.0,
687 xalign = 0.5, yalign = 0.5)
688
689 self._closeButton = gtk.Button(xstr("button_ok"))
690 self._closeButton.connect("clicked", self._closeClicked)
691 self._closeButton.set_use_underline(True)
692
693 buttonAlignment.add(self._closeButton)
694 vbox.pack_start(buttonAlignment, False, False, 2)
695
696 alignment.add(vbox)
697
698 self.add(alignment)
699
700 self.connect("key-press-event", self._keyPressed)
701
702 @property
703 def hasFlights(self):
704 """Determine if there are any flights that we know of."""
705 return len(self._flights)>0
706
707 def clear(self):
708 """Clear the flight list."""
709 self._flights = []
710 self._flightList.clear()
711
712 def addFlight(self, flight):
713 """Add the given flight."""
714 self._flights.append(flight)
715 self._flightList.addFlight(flight)
716
717 def _selectionChanged(self, flightList, selectedIndexes):
718 """Called when the selection has changed."""
719 self._viewButton.set_sensitive(len(selectedIndexes)==1)
720
721 def _rowActivated(self, timetable, index):
722 """Called when a row has been activated (e.g. double-clicked) in the
723 flight list."""
724 self._viewSelected()
725
726 def _refreshClicked(self, button):
727 """Called when the refresh button has been clicked."""
728 self.clear()
729 self._gui.showFlights(None)
730
731 def _viewClicked(self, button):
732 """Called when the view button has been clicked."""
733 self._viewSelected()
734
735 def _viewSelected(self):
736 """View the selected flight."""
737 gui = self._gui
738 gui.beginBusy(xstr("pendflt_pirep_busy"))
739 self.set_sensitive(False)
740
741 indexes = self._flightList.selectedIndexes
742 assert(len(indexes)==1)
743
744 flightID = self._flights[indexes[0]].bookedFlight.id
745 gui.webHandler.getPIREP(self._pirepResultCallback, flightID)
746
747 def _pirepResultCallback(self, returned, result):
748 """Called when the PIREP query result is available."""
749 gobject.idle_add(self._handlePIREPResult, returned, result)
750
751 def _handlePIREPResult(self, returned, result):
752 """Handle the refly result."""
753 self.set_sensitive(True)
754 gui = self._gui
755 gui.endBusy()
756
757 if returned:
758 gui.viewMessagedPIREP(result.pirep)
759
760 def _closeClicked(self, button):
761 """Called when the Close button is clicked.
762
763 A 'delete-event' is emitted to close the window."""
764 self.emit("delete-event", None)
765
766 def _keyPressed(self, window, event):
767 """Called when a key is pressed in the window.
768
769 If the Escape key is pressed, 'delete-event' is emitted to close the
770 window."""
771 if gdk.keyval_name(event.keyval) == "Escape":
772 self.emit("delete-event", None)
773 return True
774
775#-----------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.