1 | # A widget which is a generic list of flights
|
---|
2 |
|
---|
3 | #-----------------------------------------------------------------------------
|
---|
4 |
|
---|
5 | from mlx.gui.common import *
|
---|
6 |
|
---|
7 | import mlx.const as const
|
---|
8 |
|
---|
9 | #-----------------------------------------------------------------------------
|
---|
10 |
|
---|
11 | class 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 |
|
---|
85 | class 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 |
|
---|
235 | gobject.signal_new("row-activated", FlightList, gobject.SIGNAL_RUN_FIRST,
|
---|
236 | None, (int,))
|
---|
237 |
|
---|
238 | gobject.signal_new("selection-changed", FlightList, gobject.SIGNAL_RUN_FIRST,
|
---|
239 | None, (object,))
|
---|
240 |
|
---|
241 | #-----------------------------------------------------------------------------
|
---|
242 |
|
---|
243 | class 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 |
|
---|
511 | class 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 | alignment.add(self._closeButton)
|
---|
544 | vbox.pack_start(alignment, False, False, 2)
|
---|
545 |
|
---|
546 | mainAlignment.add(vbox)
|
---|
547 |
|
---|
548 | self.add(mainAlignment)
|
---|
549 |
|
---|
550 | self.connect("key-press-event", self._keyPressed)
|
---|
551 |
|
---|
552 | @property
|
---|
553 | def hasFlights(self):
|
---|
554 | """Determine if the window has any flights."""
|
---|
555 | return self._reportedFrame.hasFlights or self._rejectedFrame.hasFlights
|
---|
556 |
|
---|
557 | def clear(self):
|
---|
558 | """Clear the lists."""
|
---|
559 | self._reportedFrame.clear()
|
---|
560 | self._rejectedFrame.clear()
|
---|
561 |
|
---|
562 | def addReportedFlight(self, flight):
|
---|
563 | """Add a reported flight."""
|
---|
564 | self._reportedFrame.addFlight(flight)
|
---|
565 |
|
---|
566 | def addRejectedFlight(self, flight):
|
---|
567 | """Add a rejected flight."""
|
---|
568 | self._rejectedFrame.addFlight(flight)
|
---|
569 |
|
---|
570 | def checkFlights(self):
|
---|
571 | """Check if there are any flights in any of the lists, and close the
|
---|
572 | window if not."""
|
---|
573 | if not self.hasFlights:
|
---|
574 | self.emit("delete-event", None)
|
---|
575 |
|
---|
576 | def _closeClicked(self, button):
|
---|
577 | """Called when the Close button is clicked.
|
---|
578 |
|
---|
579 | A 'delete-event' is emitted to close the window."""
|
---|
580 | self.emit("delete-event", None)
|
---|
581 |
|
---|
582 | def _keyPressed(self, window, event):
|
---|
583 | """Called when a key is pressed in the window.
|
---|
584 |
|
---|
585 | If the Escape key is pressed, 'delete-event' is emitted to close the
|
---|
586 | window."""
|
---|
587 | if gdk.keyval_name(event.keyval) == "Escape":
|
---|
588 | self.emit("delete-event", None)
|
---|
589 | return True
|
---|
590 |
|
---|
591 | #-----------------------------------------------------------------------------
|
---|
592 |
|
---|
593 | class AcceptedFlightsWindow(gtk.Window):
|
---|
594 | """A window for a list of accepted flights."""
|
---|
595 | def getFlightDuration(flightTimeStart, flight):
|
---|
596 | """Get the flight duration for the given flight."""
|
---|
597 | minutes = int(round((flight.flightTimeEnd - flightTimeStart)/60.0))
|
---|
598 | return "%02d:%02d" % (minutes/60, minutes%60)
|
---|
599 |
|
---|
600 | columnDescriptors = [
|
---|
601 | ColumnDescriptor("bookedFlight.callsign", xstr("flightsel_no")),
|
---|
602 | ColumnDescriptor("bookedFlight.departureTime", xstr("flightsel_deptime"),
|
---|
603 | sortable = True, defaultSortable = True,
|
---|
604 | defaultDescending = True),
|
---|
605 | ColumnDescriptor("bookedFlight.departureICAO", xstr("flightsel_from"),
|
---|
606 | sortable = True),
|
---|
607 | ColumnDescriptor("bookedFlight.arrivalICAO", xstr("flightsel_to"),
|
---|
608 | sortable = True),
|
---|
609 | ColumnDescriptor("bookedFlight.tailNumber", xstr("pendflt_acft"),
|
---|
610 | convertFn = lambda value, flight:
|
---|
611 | PendingFlightsFrame.getAircraft(value,
|
---|
612 | flight.bookedFlight)),
|
---|
613 | ColumnDescriptor("flightTimeStart", xstr("acceptedflt_flight_duration"),
|
---|
614 | convertFn = getFlightDuration, sortable = True,
|
---|
615 | extraColumnAttributes =
|
---|
616 | { "alignment": 0.5 } ),
|
---|
617 | ColumnDescriptor("numPassengers", xstr("acceptedflt_num_pax"),
|
---|
618 | type = int, sortable = True,
|
---|
619 | extraColumnAttributes =
|
---|
620 | { "alignment": 1.0 } ),
|
---|
621 | ColumnDescriptor("fuelUsed", xstr("acceptedflt_fuel"),
|
---|
622 | type = int, sortable = True,
|
---|
623 | extraColumnAttributes =
|
---|
624 | { "alignment": 1.0 } ),
|
---|
625 | ColumnDescriptor("rating", xstr("acceptedflt_rating"),
|
---|
626 | type = float, sortable = True,
|
---|
627 | extraColumnAttributes =
|
---|
628 | { "alignment": 1.0 },
|
---|
629 | cellDataFn = lambda col, cell, model, iter:
|
---|
630 | cell.set_property("text",
|
---|
631 | "%.0f" %
|
---|
632 | (model.get(iter, 9)[0],)))
|
---|
633 | ]
|
---|
634 |
|
---|
635 | def __init__(self, gui):
|
---|
636 | """Construct the window."""
|
---|
637 | super(AcceptedFlightsWindow, self).__init__()
|
---|
638 |
|
---|
639 | self._gui = gui
|
---|
640 |
|
---|
641 | self.set_title(WINDOW_TITLE_BASE + " - " + xstr("acceptedflt_title"))
|
---|
642 | self.set_size_request(-1, 700)
|
---|
643 | self.set_transient_for(gui.mainWindow)
|
---|
644 |
|
---|
645 | alignment = gtk.Alignment(xscale = 1.0, yscale = 1.0)
|
---|
646 | alignment.set_padding(padding_top = 2, padding_bottom = 8,
|
---|
647 | padding_left = 4, padding_right = 4)
|
---|
648 |
|
---|
649 | vbox = gtk.VBox()
|
---|
650 |
|
---|
651 | hbox = gtk.HBox()
|
---|
652 | vbox.pack_start(hbox, True, True, 4)
|
---|
653 |
|
---|
654 | self._flights = []
|
---|
655 | self._flightList = FlightList(columnDescriptors =
|
---|
656 | AcceptedFlightsWindow.columnDescriptors,
|
---|
657 | widthRequest = 750,
|
---|
658 | multiSelection = False)
|
---|
659 | self._flightList.connect("selection-changed", self._selectionChanged)
|
---|
660 | self._flightList.connect("row-activated", self._rowActivated)
|
---|
661 |
|
---|
662 | hbox.pack_start(self._flightList, True, True, 4)
|
---|
663 |
|
---|
664 | buttonBox = gtk.VBox()
|
---|
665 |
|
---|
666 | self._refreshButton = gtk.Button(xstr("acceptedflt_refresh"))
|
---|
667 | self._refreshButton.set_sensitive(True)
|
---|
668 | self._refreshButton.connect("clicked", self._refreshClicked)
|
---|
669 | buttonBox.pack_start(self._refreshButton, False, False, 2)
|
---|
670 |
|
---|
671 | filler = gtk.Alignment(xalign = 0.0, yalign = 0.0,
|
---|
672 | xscale = 1.0, yscale = 1.0)
|
---|
673 | filler.set_size_request(-1, 4)
|
---|
674 | buttonBox.pack_start(filler, False, False, 0)
|
---|
675 |
|
---|
676 | self._viewButton = gtk.Button(xstr("acceptedflt_view"))
|
---|
677 | self._viewButton.set_sensitive(False)
|
---|
678 | self._viewButton.connect("clicked", self._viewClicked)
|
---|
679 | buttonBox.pack_start(self._viewButton, False, False, 2)
|
---|
680 |
|
---|
681 | hbox.pack_start(buttonBox, False, False, 4)
|
---|
682 |
|
---|
683 | buttonAlignment = gtk.Alignment(xscale = 0.0, yscale = 0.0,
|
---|
684 | xalign = 0.5, yalign = 0.5)
|
---|
685 |
|
---|
686 | self._closeButton = gtk.Button(xstr("button_ok"))
|
---|
687 | self._closeButton.connect("clicked", self._closeClicked)
|
---|
688 |
|
---|
689 | buttonAlignment.add(self._closeButton)
|
---|
690 | vbox.pack_start(buttonAlignment, False, False, 2)
|
---|
691 |
|
---|
692 | alignment.add(vbox)
|
---|
693 |
|
---|
694 | self.add(alignment)
|
---|
695 |
|
---|
696 | self.connect("key-press-event", self._keyPressed)
|
---|
697 |
|
---|
698 | @property
|
---|
699 | def hasFlights(self):
|
---|
700 | """Determine if there are any flights that we know of."""
|
---|
701 | return len(self._flights)>0
|
---|
702 |
|
---|
703 | def clear(self):
|
---|
704 | """Clear the flight list."""
|
---|
705 | self._flights = []
|
---|
706 | self._flightList.clear()
|
---|
707 |
|
---|
708 | def addFlight(self, flight):
|
---|
709 | """Add the given flight."""
|
---|
710 | self._flights.append(flight)
|
---|
711 | self._flightList.addFlight(flight)
|
---|
712 |
|
---|
713 | def _selectionChanged(self, flightList, selectedIndexes):
|
---|
714 | """Called when the selection has changed."""
|
---|
715 | self._viewButton.set_sensitive(len(selectedIndexes)==1)
|
---|
716 |
|
---|
717 | def _rowActivated(self, timetable, index):
|
---|
718 | """Called when a row has been activated (e.g. double-clicked) in the
|
---|
719 | flight list."""
|
---|
720 | self._viewSelected()
|
---|
721 |
|
---|
722 | def _refreshClicked(self, button):
|
---|
723 | """Called when the refresh button has been clicked."""
|
---|
724 | self.clear()
|
---|
725 | self._gui.showFlights(None)
|
---|
726 |
|
---|
727 | def _viewClicked(self, button):
|
---|
728 | """Called when the view button has been clicked."""
|
---|
729 | self._viewSelected()
|
---|
730 |
|
---|
731 | def _viewSelected(self):
|
---|
732 | """View the selected flight."""
|
---|
733 | gui = self._gui
|
---|
734 | gui.beginBusy(xstr("pendflt_pirep_busy"))
|
---|
735 | self.set_sensitive(False)
|
---|
736 |
|
---|
737 | indexes = self._flightList.selectedIndexes
|
---|
738 | assert(len(indexes)==1)
|
---|
739 |
|
---|
740 | flightID = self._flights[indexes[0]].bookedFlight.id
|
---|
741 | gui.webHandler.getPIREP(self._pirepResultCallback, flightID)
|
---|
742 |
|
---|
743 | def _pirepResultCallback(self, returned, result):
|
---|
744 | """Called when the PIREP query result is available."""
|
---|
745 | gobject.idle_add(self._handlePIREPResult, returned, result)
|
---|
746 |
|
---|
747 | def _handlePIREPResult(self, returned, result):
|
---|
748 | """Handle the refly result."""
|
---|
749 | self.set_sensitive(True)
|
---|
750 | gui = self._gui
|
---|
751 | gui.endBusy()
|
---|
752 |
|
---|
753 | if returned:
|
---|
754 | gui.viewMessagedPIREP(result.pirep)
|
---|
755 |
|
---|
756 | def _closeClicked(self, button):
|
---|
757 | """Called when the Close button is clicked.
|
---|
758 |
|
---|
759 | A 'delete-event' is emitted to close the window."""
|
---|
760 | self.emit("delete-event", None)
|
---|
761 |
|
---|
762 | def _keyPressed(self, window, event):
|
---|
763 | """Called when a key is pressed in the window.
|
---|
764 |
|
---|
765 | If the Escape key is pressed, 'delete-event' is emitted to close the
|
---|
766 | window."""
|
---|
767 | if gdk.keyval_name(event.keyval) == "Escape":
|
---|
768 | self.emit("delete-event", None)
|
---|
769 | return True
|
---|
770 |
|
---|
771 | #-----------------------------------------------------------------------------
|
---|