source: src/mlx/gui/flightlist.py@ 828:b5013b5bffb1

Last change on this file since 828:b5013b5bffb1 was 828:b5013b5bffb1, checked in by István Váradi <ivaradi@…>, 8 years ago

The pending flights window gets closed automatically if no pending flights remain (re #307).

File size: 17.1 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 = 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
65class 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
213gobject.signal_new("row-activated", FlightList, gobject.SIGNAL_RUN_FIRST,
214 None, (int,))
215
216gobject.signal_new("selection-changed", FlightList, gobject.SIGNAL_RUN_FIRST,
217 None, (object,))
218
219#-----------------------------------------------------------------------------
220
221class 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 self._window.checkFlights()
345 else:
346 communicationErrorDialog()
347
348 def _deleteClicked(self, button):
349 """Called when the Delete button is clicked."""
350 if askYesNo(xstr("flight_delete_question"), parent = self._window):
351 gui = self._wizard.gui
352 gui.beginBusy(xstr("pendflt_refly_busy"))
353 self.set_sensitive(False)
354
355 flightIDs = [self._flights[i].id
356 for i in self._flightList.selectedIndexes]
357 gui.webHandler.deleteFlights(self._deleteResultCallback, flightIDs)
358
359 def _deleteResultCallback(self, returned, result):
360 """Called when the deletion result is available."""
361 gobject.idle_add(self._handleDeleteResult, returned, result)
362
363 def _handleDeleteResult(self, returned, result):
364 """Handle the delete result."""
365
366 self.set_sensitive(True)
367 gui = self._wizard.gui
368 gui.endBusy()
369
370 print "PendingFlightsFrame._handleDeleteResult", returned, result
371
372 if returned:
373 indexes = self._flightList.selectedIndexes
374
375 flights = [self._flights[index] for index in indexes]
376
377 self._flightList.removeFlights(indexes)
378 for index in indexes[::-1]:
379 del self._flights[index]
380
381 for flight in flights:
382 self._wizard.deleteFlight(flight)
383 self._window.checkFlights()
384 else:
385 communicationErrorDialog()
386
387#-----------------------------------------------------------------------------
388
389class PendingFlightsWindow(gtk.Window):
390 """The window to display the lists of the pending (reported or rejected)
391 flights."""
392 def __init__(self, wizard):
393 """Construct the window"""
394 super(PendingFlightsWindow, self).__init__()
395
396 gui = wizard.gui
397
398 self.set_title(WINDOW_TITLE_BASE + " - " + xstr("pendflt_title"))
399 self.set_size_request(-1, 450)
400 self.set_transient_for(gui.mainWindow)
401 self.set_modal(True)
402
403 mainAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
404 xscale = 1.0, yscale = 1.0)
405 mainAlignment.set_padding(padding_top = 0, padding_bottom = 12,
406 padding_left = 8, padding_right = 8)
407
408 vbox = gtk.VBox()
409
410 self._reportedFrame = PendingFlightsFrame("reported", wizard, self)
411 vbox.pack_start(self._reportedFrame, True, True, 2)
412
413 self._rejectedFrame = PendingFlightsFrame("rejected", wizard, self)
414 vbox.pack_start(self._rejectedFrame, True, True, 2)
415
416 alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
417 xscale = 0.0, yscale = 0.0)
418 self._closeButton = gtk.Button(xstr("button_ok"))
419 self._closeButton.connect("clicked", self._closeClicked)
420 alignment.add(self._closeButton)
421 vbox.pack_start(alignment, False, False, 2)
422
423 mainAlignment.add(vbox)
424
425 self.add(mainAlignment)
426
427 self.connect("key-press-event", self._keyPressed)
428
429 @property
430 def hasFlights(self):
431 """Determine if the window has any flights."""
432 return self._reportedFrame.hasFlights or self._rejectedFrame.hasFlights
433
434 def clear(self):
435 """Clear the lists."""
436 self._reportedFrame.clear()
437 self._rejectedFrame.clear()
438
439 def addReportedFlight(self, flight):
440 """Add a reported flight."""
441 self._reportedFrame.addFlight(flight)
442
443 def addRejectedFlight(self, flight):
444 """Add a rejected flight."""
445 self._rejectedFrame.addFlight(flight)
446
447 def checkFlights(self):
448 """Check if there are any flights in any of the lists, and close the
449 window if not."""
450 if not self.hasFlights:
451 self.emit("delete-event", None)
452
453 def _closeClicked(self, button):
454 """Called when the Close button is clicked.
455
456 A 'delete-event' is emitted to close the window."""
457 self.emit("delete-event", None)
458
459 def _keyPressed(self, window, event):
460 """Called when a key is pressed in the window.
461
462 If the Escape key is pressed, 'delete-event' is emitted to close the
463 window."""
464 if gdk.keyval_name(event.keyval) == "Escape":
465 self.emit("delete-event", None)
466 return True
467
468#-----------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.