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 = gtk.CellRendererText(),
|
---|
15 | extraColumnAttributes = None, sortable = False,
|
---|
16 | defaultSortable = False):
|
---|
17 | """Construct the descriptor."""
|
---|
18 | self._attribute = attribute
|
---|
19 | self._heading = heading
|
---|
20 | self._type = type
|
---|
21 | self._convertFn = convertFn
|
---|
22 | self._renderer = renderer
|
---|
23 | self._extraColumnAttributes = extraColumnAttributes
|
---|
24 | self._sortable = sortable
|
---|
25 | self._defaultSortable = defaultSortable
|
---|
26 |
|
---|
27 | @property
|
---|
28 | def defaultSortable(self):
|
---|
29 | """Determine if this column is the default sortable one."""
|
---|
30 | return self._defaultSortable
|
---|
31 |
|
---|
32 | def appendType(self, types):
|
---|
33 | """Append the type of this column to the given list of types."""
|
---|
34 | types.append(self._type)
|
---|
35 |
|
---|
36 | def getViewColumn(self, index):
|
---|
37 | """Get a new column object for a tree view.
|
---|
38 |
|
---|
39 | @param index is the 0-based index of the column."""
|
---|
40 | if self._extraColumnAttributes is None:
|
---|
41 | if isinstance(self._renderer, gtk.CellRendererText):
|
---|
42 | extraColumnAttributes = {"text" : index}
|
---|
43 | else:
|
---|
44 | extraColumnAttributes = {}
|
---|
45 | else:
|
---|
46 | extraColumnAttributes = self._extraColumnAttributes
|
---|
47 |
|
---|
48 | column = gtk.TreeViewColumn(self._heading, self._renderer,
|
---|
49 | text = index)
|
---|
50 | column.set_expand(True)
|
---|
51 | if self._sortable:
|
---|
52 | column.set_sort_column_id(index)
|
---|
53 | column.set_sort_indicator(True)
|
---|
54 |
|
---|
55 | return column
|
---|
56 |
|
---|
57 | def getValueFrom(self, flight):
|
---|
58 | """Get the value from the given flight."""
|
---|
59 | value = getattr(flight, self._attribute)
|
---|
60 | return self._type(value) if self._convertFn is None \
|
---|
61 | else self._convertFn(value, flight)
|
---|
62 |
|
---|
63 | #-----------------------------------------------------------------------------
|
---|
64 |
|
---|
65 | class FlightList(gtk.Alignment):
|
---|
66 | """Construct the flight list.
|
---|
67 |
|
---|
68 | This is a complete widget with a scroll window. It is alignment centered
|
---|
69 | horizontally and expandable vertically."""
|
---|
70 |
|
---|
71 | defaultColumnDescriptors = [
|
---|
72 | ColumnDescriptor("callsign", xstr("flightsel_no")),
|
---|
73 | ColumnDescriptor("departureTime", xstr("flightsel_deptime"),
|
---|
74 | sortable = True, defaultSortable = True),
|
---|
75 | ColumnDescriptor("departureICAO", xstr("flightsel_from"),
|
---|
76 | sortable = True),
|
---|
77 | ColumnDescriptor("arrivalICAO", xstr("flightsel_to"), sortable = True)
|
---|
78 | ]
|
---|
79 |
|
---|
80 | def __init__(self, columnDescriptors = defaultColumnDescriptors,
|
---|
81 | popupMenuProducer = None, widthRequest = None,
|
---|
82 | multiSelection = False):
|
---|
83 | """Construct the flight list with the given column descriptors."""
|
---|
84 |
|
---|
85 | self._columnDescriptors = columnDescriptors
|
---|
86 | self._popupMenuProducer = popupMenuProducer
|
---|
87 | self._popupMenu = None
|
---|
88 |
|
---|
89 | types = [int]
|
---|
90 | defaultSortableIndex = None
|
---|
91 | for columnDescriptor in self._columnDescriptors:
|
---|
92 | if columnDescriptor.defaultSortable:
|
---|
93 | defaultSortableIndex = len(types)
|
---|
94 | columnDescriptor.appendType(types)
|
---|
95 |
|
---|
96 | self._model = gtk.ListStore(*types)
|
---|
97 | if defaultSortableIndex is not None:
|
---|
98 | self._model.set_sort_column_id(defaultSortableIndex,
|
---|
99 | SORT_ASCENDING)
|
---|
100 | self._view = gtk.TreeView(self._model)
|
---|
101 |
|
---|
102 | flightIndexColumn = gtk.TreeViewColumn()
|
---|
103 | flightIndexColumn.set_visible(False)
|
---|
104 | self._view.append_column(flightIndexColumn)
|
---|
105 |
|
---|
106 | index = 1
|
---|
107 | for columnDescriptor in self._columnDescriptors:
|
---|
108 | column = columnDescriptor.getViewColumn(index)
|
---|
109 | self._view.append_column(column)
|
---|
110 | index += 1
|
---|
111 |
|
---|
112 | self._view.connect("row-activated", self._rowActivated)
|
---|
113 | self._view.connect("button-press-event", self._buttonPressEvent)
|
---|
114 |
|
---|
115 | selection = self._view.get_selection()
|
---|
116 | selection.connect("changed", self._selectionChanged)
|
---|
117 | if multiSelection:
|
---|
118 | selection.set_mode(SELECTION_MULTIPLE)
|
---|
119 |
|
---|
120 | scrolledWindow = gtk.ScrolledWindow()
|
---|
121 | scrolledWindow.add(self._view)
|
---|
122 | if widthRequest is not None:
|
---|
123 | scrolledWindow.set_size_request(widthRequest, -1)
|
---|
124 | # FIXME: these should be constants in common.py
|
---|
125 | scrolledWindow.set_policy(gtk.PolicyType.AUTOMATIC if pygobject
|
---|
126 | else gtk.POLICY_AUTOMATIC,
|
---|
127 | gtk.PolicyType.AUTOMATIC if pygobject
|
---|
128 | else gtk.POLICY_AUTOMATIC)
|
---|
129 | scrolledWindow.set_shadow_type(gtk.ShadowType.IN if pygobject
|
---|
130 | else gtk.SHADOW_IN)
|
---|
131 |
|
---|
132 | super(FlightList, self).__init__(xalign = 0.5, yalign = 0.0,
|
---|
133 | xscale = 0.0, yscale = 1.0)
|
---|
134 | self.add(scrolledWindow)
|
---|
135 |
|
---|
136 | @property
|
---|
137 | def selectedIndexes(self):
|
---|
138 | """Get the indexes of the selected entries, if any.
|
---|
139 |
|
---|
140 | The indexes are sorted."""
|
---|
141 | selection = self._view.get_selection()
|
---|
142 | (model, rows) = selection.get_selected_rows()
|
---|
143 |
|
---|
144 | indexes = [self._getIndexForPath(path) for path in rows]
|
---|
145 | indexes.sort()
|
---|
146 | return indexes
|
---|
147 |
|
---|
148 | @property
|
---|
149 | def hasFlights(self):
|
---|
150 | """Determine if there are any flights in the list."""
|
---|
151 | return self._model.get_iter_root() is not None
|
---|
152 |
|
---|
153 | def clear(self):
|
---|
154 | """Clear the model."""
|
---|
155 | self._model.clear()
|
---|
156 |
|
---|
157 | def addFlight(self, flight):
|
---|
158 | """Add the given booked flight."""
|
---|
159 | values = [self._model.iter_n_children(None)]
|
---|
160 | for columnDescriptor in self._columnDescriptors:
|
---|
161 | values.append(columnDescriptor.getValueFrom(flight))
|
---|
162 | self._model.append(values)
|
---|
163 |
|
---|
164 | def removeFlights(self, indexes):
|
---|
165 | """Remove the flights with the given indexes."""
|
---|
166 | model = self._model
|
---|
167 | idx = 0
|
---|
168 | iter = model.get_iter_first()
|
---|
169 | while iter is not None:
|
---|
170 | nextIter = model.iter_next(iter)
|
---|
171 | if model.get_value(iter, 0) in indexes:
|
---|
172 | model.remove(iter)
|
---|
173 | else:
|
---|
174 | model.set_value(iter, 0, idx)
|
---|
175 | idx += 1
|
---|
176 | iter = nextIter
|
---|
177 |
|
---|
178 | def _getIndexForPath(self, path):
|
---|
179 | """Get the index for the given path."""
|
---|
180 | iter = self._model.get_iter(path)
|
---|
181 | return self._model.get_value(iter, 0)
|
---|
182 |
|
---|
183 | def _rowActivated(self, flightList, path, column):
|
---|
184 | """Called when a row is selected."""
|
---|
185 | self.emit("row-activated", self._getIndexForPath(path))
|
---|
186 |
|
---|
187 | def _buttonPressEvent(self, widget, event):
|
---|
188 | """Called when a mouse button is pressed or released."""
|
---|
189 | if event.type!=EVENT_BUTTON_PRESS or event.button!=3 or \
|
---|
190 | self._popupMenuProducer is None:
|
---|
191 | return
|
---|
192 |
|
---|
193 | (path, _, _, _) = self._view.get_path_at_pos(int(event.x),
|
---|
194 | int(event.y))
|
---|
195 | selection = self._view.get_selection()
|
---|
196 | selection.unselect_all()
|
---|
197 | selection.select_path(path)
|
---|
198 |
|
---|
199 | if self._popupMenu is None:
|
---|
200 | self._popupMenu = self._popupMenuProducer()
|
---|
201 | menu = self._popupMenu
|
---|
202 | if pygobject:
|
---|
203 | menu.popup(None, None, None, None, event.button, event.time)
|
---|
204 | else:
|
---|
205 | menu.popup(None, None, None, event.button, event.time)
|
---|
206 |
|
---|
207 | def _selectionChanged(self, selection):
|
---|
208 | """Called when the selection has changed."""
|
---|
209 | self.emit("selection-changed", self.selectedIndexes)
|
---|
210 |
|
---|
211 | #-------------------------------------------------------------------------------
|
---|
212 |
|
---|
213 | gobject.signal_new("row-activated", FlightList, gobject.SIGNAL_RUN_FIRST,
|
---|
214 | None, (int,))
|
---|
215 |
|
---|
216 | gobject.signal_new("selection-changed", FlightList, gobject.SIGNAL_RUN_FIRST,
|
---|
217 | None, (object,))
|
---|
218 |
|
---|
219 | #-----------------------------------------------------------------------------
|
---|
220 |
|
---|
221 | class PendingFlightsFrame(gtk.Frame):
|
---|
222 | """A frame for a list of pending (reported or rejected) flights.
|
---|
223 |
|
---|
224 | It contains the list and the buttons available."""
|
---|
225 | def getAircraft(tailNumber, bookedFlight):
|
---|
226 | """Get the aircraft from the given booked flight.
|
---|
227 |
|
---|
228 | This is the tail number followed by the ICAO code of the aircraft's
|
---|
229 | type."""
|
---|
230 | return tailNumber + \
|
---|
231 | " (" + const.icaoCodes[bookedFlight.aircraftType] + ")"
|
---|
232 |
|
---|
233 | columnDescriptors = [
|
---|
234 | ColumnDescriptor("callsign", xstr("flightsel_no")),
|
---|
235 | ColumnDescriptor("departureTime", xstr("flightsel_deptime"),
|
---|
236 | sortable = True, defaultSortable = True),
|
---|
237 | ColumnDescriptor("departureICAO", xstr("flightsel_from"),
|
---|
238 | sortable = True),
|
---|
239 | ColumnDescriptor("arrivalICAO", xstr("flightsel_to"),
|
---|
240 | sortable = True),
|
---|
241 | ColumnDescriptor("tailNumber", xstr("pendflt_acft"),
|
---|
242 | convertFn = getAircraft)
|
---|
243 | ]
|
---|
244 |
|
---|
245 | def __init__(self, which, wizard, window):
|
---|
246 | """Construct the frame with the given title."""
|
---|
247 | super(PendingFlightsFrame, self).__init__(xstr("pendflt_title_" + which))
|
---|
248 |
|
---|
249 | self._which = which
|
---|
250 | self._wizard = wizard
|
---|
251 | self._window = window
|
---|
252 |
|
---|
253 | alignment = gtk.Alignment(xscale = 1.0, yscale = 1.0)
|
---|
254 | alignment.set_padding(padding_top = 2, padding_bottom = 8,
|
---|
255 | padding_left = 4, padding_right = 4)
|
---|
256 |
|
---|
257 | hbox = gtk.HBox()
|
---|
258 |
|
---|
259 | self._flights = []
|
---|
260 | self._flightList = FlightList(columnDescriptors =
|
---|
261 | PendingFlightsFrame.columnDescriptors,
|
---|
262 | widthRequest = 500, multiSelection = True)
|
---|
263 | self._flightList.connect("selection-changed", self._selectionChanged)
|
---|
264 |
|
---|
265 | hbox.pack_start(self._flightList, True, True, 4)
|
---|
266 |
|
---|
267 | buttonBox = gtk.VBox()
|
---|
268 |
|
---|
269 | self._editButton = gtk.Button(xstr("pendflt_edit_" + which))
|
---|
270 | self._editButton.set_sensitive(False)
|
---|
271 | buttonBox.pack_start(self._editButton, False, False, 2)
|
---|
272 |
|
---|
273 | self._reflyButton = gtk.Button(xstr("pendflt_refly_" + which))
|
---|
274 | self._reflyButton.set_sensitive(False)
|
---|
275 | self._reflyButton.connect("clicked", self._reflyClicked)
|
---|
276 | buttonBox.pack_start(self._reflyButton, False, False, 2)
|
---|
277 |
|
---|
278 | self._deleteButton = gtk.Button(xstr("pendflt_delete_" + which))
|
---|
279 | self._deleteButton.set_sensitive(False)
|
---|
280 | self._deleteButton.connect("clicked", self._deleteClicked)
|
---|
281 | buttonBox.pack_start(self._deleteButton, False, False, 2)
|
---|
282 |
|
---|
283 | hbox.pack_start(buttonBox, False, False, 4)
|
---|
284 |
|
---|
285 | alignment.add(hbox)
|
---|
286 | self.add(alignment)
|
---|
287 |
|
---|
288 | @property
|
---|
289 | def hasFlights(self):
|
---|
290 | """Determine if there are any flights in the list."""
|
---|
291 | return self._flightList.hasFlights
|
---|
292 |
|
---|
293 | def clear(self):
|
---|
294 | """Clear the lists."""
|
---|
295 | self._flights = []
|
---|
296 | self._flightList.clear()
|
---|
297 |
|
---|
298 | def addFlight(self, flight):
|
---|
299 | """Add a flight to the list."""
|
---|
300 | self._flights.append(flight)
|
---|
301 | self._flightList.addFlight(flight)
|
---|
302 |
|
---|
303 | def _selectionChanged(self, flightList, selectedIndexes):
|
---|
304 | """Called when the selection in the list has changed."""
|
---|
305 | self._editButton.set_sensitive(len(selectedIndexes)==1)
|
---|
306 | self._reflyButton.set_sensitive(len(selectedIndexes)>0)
|
---|
307 | self._deleteButton.set_sensitive(len(selectedIndexes)>0)
|
---|
308 |
|
---|
309 | def _reflyClicked(self, button):
|
---|
310 | """Called when the Refly button is clicked."""
|
---|
311 | if askYesNo(xstr("pendflt_refly_question"), parent = self._window):
|
---|
312 | gui = self._wizard.gui
|
---|
313 | gui.beginBusy(xstr("pendflt_refly_busy"))
|
---|
314 | self.set_sensitive(False)
|
---|
315 |
|
---|
316 | flightIDs = [self._flights[i].id
|
---|
317 | for i in self._flightList.selectedIndexes]
|
---|
318 | gui.webHandler.reflyFlights(self._reflyResultCallback, flightIDs)
|
---|
319 |
|
---|
320 | def _reflyResultCallback(self, returned, result):
|
---|
321 | """Called when the refly result is available."""
|
---|
322 | gobject.idle_add(self._handleReflyResult, returned, result)
|
---|
323 |
|
---|
324 | def _handleReflyResult(self, returned, result):
|
---|
325 | """Handle the refly result."""
|
---|
326 |
|
---|
327 | self.set_sensitive(True)
|
---|
328 | gui = self._wizard.gui
|
---|
329 | gui.endBusy()
|
---|
330 |
|
---|
331 | print "PendingFlightsFrame._handleReflyResult", returned, result
|
---|
332 |
|
---|
333 | if returned:
|
---|
334 | indexes = self._flightList.selectedIndexes
|
---|
335 |
|
---|
336 | flights = [self._flights[index] for index in indexes]
|
---|
337 |
|
---|
338 | self._flightList.removeFlights(indexes)
|
---|
339 | for index in indexes[::-1]:
|
---|
340 | del self._flights[index]
|
---|
341 |
|
---|
342 | for flight in flights:
|
---|
343 | self._wizard.reflyFlight(flight)
|
---|
344 |
|
---|
345 | def _deleteClicked(self, button):
|
---|
346 | """Called when the Delete button is clicked."""
|
---|
347 | if askYesNo(xstr("flight_delete_question"), parent = self._window):
|
---|
348 | gui = self._wizard.gui
|
---|
349 | gui.beginBusy(xstr("pendflt_refly_busy"))
|
---|
350 | self.set_sensitive(False)
|
---|
351 |
|
---|
352 | flightIDs = [self._flights[i].id
|
---|
353 | for i in self._flightList.selectedIndexes]
|
---|
354 | gui.webHandler.deleteFlights(self._deleteResultCallback, flightIDs)
|
---|
355 |
|
---|
356 | def _deleteResultCallback(self, returned, result):
|
---|
357 | """Called when the deletion result is available."""
|
---|
358 | gobject.idle_add(self._handleDeleteResult, returned, result)
|
---|
359 |
|
---|
360 | def _handleDeleteResult(self, returned, result):
|
---|
361 | """Handle the delete result."""
|
---|
362 |
|
---|
363 | self.set_sensitive(True)
|
---|
364 | gui = self._wizard.gui
|
---|
365 | gui.endBusy()
|
---|
366 |
|
---|
367 | print "PendingFlightsFrame._handleDeleteResult", returned, result
|
---|
368 |
|
---|
369 | if returned:
|
---|
370 | indexes = self._flightList.selectedIndexes
|
---|
371 |
|
---|
372 | flights = [self._flights[index] for index in indexes]
|
---|
373 |
|
---|
374 | self._flightList.removeFlights(indexes)
|
---|
375 | for index in indexes[::-1]:
|
---|
376 | del self._flights[index]
|
---|
377 |
|
---|
378 | for flight in flights:
|
---|
379 | self._wizard.deleteFlight(flight)
|
---|
380 |
|
---|
381 | #-----------------------------------------------------------------------------
|
---|
382 |
|
---|
383 | class PendingFlightsWindow(gtk.Window):
|
---|
384 | """The window to display the lists of the pending (reported or rejected)
|
---|
385 | flights."""
|
---|
386 | def __init__(self, wizard):
|
---|
387 | """Construct the window"""
|
---|
388 | super(PendingFlightsWindow, self).__init__()
|
---|
389 |
|
---|
390 | gui = wizard.gui
|
---|
391 |
|
---|
392 | self.set_title(WINDOW_TITLE_BASE + " - " + xstr("pendflt_title"))
|
---|
393 | self.set_size_request(-1, 450)
|
---|
394 | self.set_transient_for(gui.mainWindow)
|
---|
395 | self.set_modal(True)
|
---|
396 |
|
---|
397 | mainAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
398 | xscale = 1.0, yscale = 1.0)
|
---|
399 | mainAlignment.set_padding(padding_top = 0, padding_bottom = 12,
|
---|
400 | padding_left = 8, padding_right = 8)
|
---|
401 |
|
---|
402 | vbox = gtk.VBox()
|
---|
403 |
|
---|
404 | self._reportedFrame = PendingFlightsFrame("reported", wizard, self)
|
---|
405 | vbox.pack_start(self._reportedFrame, True, True, 2)
|
---|
406 |
|
---|
407 | self._rejectedFrame = PendingFlightsFrame("rejected", wizard, self)
|
---|
408 | vbox.pack_start(self._rejectedFrame, True, True, 2)
|
---|
409 |
|
---|
410 | alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
411 | xscale = 0.0, yscale = 0.0)
|
---|
412 | self._closeButton = gtk.Button(xstr("button_ok"))
|
---|
413 | self._closeButton.connect("clicked", self._closeClicked)
|
---|
414 | alignment.add(self._closeButton)
|
---|
415 | vbox.pack_start(alignment, False, False, 2)
|
---|
416 |
|
---|
417 | mainAlignment.add(vbox)
|
---|
418 |
|
---|
419 | self.add(mainAlignment)
|
---|
420 |
|
---|
421 | self.connect("key-press-event", self._keyPressed)
|
---|
422 |
|
---|
423 | @property
|
---|
424 | def hasFlights(self):
|
---|
425 | """Determine if the window has any flights."""
|
---|
426 | return self._reportedFrame.hasFlights or self._rejectedFrame.hasFlights
|
---|
427 |
|
---|
428 | def clear(self):
|
---|
429 | """Clear the lists."""
|
---|
430 | self._reportedFrame.clear()
|
---|
431 | self._rejectedFrame.clear()
|
---|
432 |
|
---|
433 | def addReportedFlight(self, flight):
|
---|
434 | """Add a reported flight."""
|
---|
435 | self._reportedFrame.addFlight(flight)
|
---|
436 |
|
---|
437 | def addRejectedFlight(self, flight):
|
---|
438 | """Add a rejected flight."""
|
---|
439 | self._rejectedFrame.addFlight(flight)
|
---|
440 |
|
---|
441 | def _closeClicked(self, button):
|
---|
442 | """Called when the Close button is clicked.
|
---|
443 |
|
---|
444 | A 'delete-event' is emitted to close the window."""
|
---|
445 | self.emit("delete-event", None)
|
---|
446 |
|
---|
447 | def _keyPressed(self, window, event):
|
---|
448 | """Called when a key is pressed in the window.
|
---|
449 |
|
---|
450 | If the Escape key is pressed, 'delete-event' is emitted to close the
|
---|
451 | window."""
|
---|
452 | if gdk.keyval_name(event.keyval) == "Escape":
|
---|
453 | self.emit("delete-event", None)
|
---|
454 | return True
|
---|
455 |
|
---|
456 | #-----------------------------------------------------------------------------
|
---|