source: src/mlx/gui/flightlist.py@ 826:5c6ff27a9206

Last change on this file since 826:5c6ff27a9206 was 825:7d3e38a81acc, checked in by István Váradi <ivaradi@…>, 8 years ago

Added a confirmation question before marking flights for reflying (re #307)

File size: 17.4 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 dialog = gtk.MessageDialog(parent = self._window,
312 type = MESSAGETYPE_QUESTION,
313 message_format = xstr("pendflt_refly_question"))
314
315 dialog.add_button(xstr("button_no"), RESPONSETYPE_NO)
316 dialog.add_button(xstr("button_yes"), RESPONSETYPE_YES)
317
318 dialog.set_title(WINDOW_TITLE_BASE)
319 result = dialog.run()
320 dialog.hide()
321
322 if result==RESPONSETYPE_YES:
323 gui = self._wizard.gui
324 gui.beginBusy(xstr("pendflt_refly_busy"))
325 self.set_sensitive(False)
326
327 flightIDs = [self._flights[i].id
328 for i in self._flightList.selectedIndexes]
329 gui.webHandler.reflyFlights(self._reflyResultCallback, flightIDs)
330
331 def _reflyResultCallback(self, returned, result):
332 """Called when the refly result is available."""
333 gobject.idle_add(self._handleReflyResult, returned, result)
334
335 def _handleReflyResult(self, returned, result):
336 """Handle the refly result."""
337
338 self.set_sensitive(True)
339 gui = self._wizard.gui
340 gui.endBusy()
341
342 print "PendingFlightsFrame._handleReflyResult", returned, result
343
344 if returned:
345 indexes = self._flightList.selectedIndexes
346
347 flights = [self._flights[index] for index in indexes]
348
349 self._flightList.removeFlights(indexes)
350 for index in indexes[::-1]:
351 del self._flights[index]
352
353 for flight in flights:
354 self._wizard.reflyFlight(flight)
355
356 def _deleteClicked(self, button):
357 """Called when the Delete button is clicked."""
358 dialog = gtk.MessageDialog(parent = self._window,
359 type = MESSAGETYPE_QUESTION,
360 message_format = xstr("flight_delete_question"))
361
362 dialog.add_button(xstr("button_no"), RESPONSETYPE_NO)
363 dialog.add_button(xstr("button_yes"), RESPONSETYPE_YES)
364
365 dialog.set_title(WINDOW_TITLE_BASE)
366 result = dialog.run()
367 dialog.hide()
368
369 if result==RESPONSETYPE_YES:
370 gui = self._wizard.gui
371 gui.beginBusy(xstr("pendflt_refly_busy"))
372 self.set_sensitive(False)
373
374 flightIDs = [self._flights[i].id
375 for i in self._flightList.selectedIndexes]
376 gui.webHandler.deleteFlights(self._deleteResultCallback, flightIDs)
377
378 def _deleteResultCallback(self, returned, result):
379 """Called when the deletion result is available."""
380 gobject.idle_add(self._handleDeleteResult, returned, result)
381
382 def _handleDeleteResult(self, returned, result):
383 """Handle the delete result."""
384
385 self.set_sensitive(True)
386 gui = self._wizard.gui
387 gui.endBusy()
388
389 print "PendingFlightsFrame._handleDeleteResult", returned, result
390
391 if returned:
392 indexes = self._flightList.selectedIndexes
393
394 flights = [self._flights[index] for index in indexes]
395
396 self._flightList.removeFlights(indexes)
397 for index in indexes[::-1]:
398 del self._flights[index]
399
400 for flight in flights:
401 self._wizard.deleteFlight(flight)
402
403#-----------------------------------------------------------------------------
404
405class PendingFlightsWindow(gtk.Window):
406 """The window to display the lists of the pending (reported or rejected)
407 flights."""
408 def __init__(self, wizard):
409 """Construct the window"""
410 super(PendingFlightsWindow, self).__init__()
411
412 gui = wizard.gui
413
414 self.set_title(WINDOW_TITLE_BASE + " - " + xstr("pendflt_title"))
415 self.set_size_request(-1, 450)
416 self.set_transient_for(gui.mainWindow)
417 self.set_modal(True)
418
419 mainAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
420 xscale = 1.0, yscale = 1.0)
421 mainAlignment.set_padding(padding_top = 0, padding_bottom = 12,
422 padding_left = 8, padding_right = 8)
423
424 vbox = gtk.VBox()
425
426 self._reportedFrame = PendingFlightsFrame("reported", wizard, self)
427 vbox.pack_start(self._reportedFrame, True, True, 2)
428
429 self._rejectedFrame = PendingFlightsFrame("rejected", wizard, self)
430 vbox.pack_start(self._rejectedFrame, True, True, 2)
431
432 alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
433 xscale = 0.0, yscale = 0.0)
434 self._closeButton = gtk.Button(xstr("button_ok"))
435 self._closeButton.connect("clicked", self._closeClicked)
436 alignment.add(self._closeButton)
437 vbox.pack_start(alignment, False, False, 2)
438
439 mainAlignment.add(vbox)
440
441 self.add(mainAlignment)
442
443 self.connect("key-press-event", self._keyPressed)
444
445 @property
446 def hasFlights(self):
447 """Determine if the window has any flights."""
448 return self._reportedFrame.hasFlights or self._rejectedFrame.hasFlights
449
450 def clear(self):
451 """Clear the lists."""
452 self._reportedFrame.clear()
453 self._rejectedFrame.clear()
454
455 def addReportedFlight(self, flight):
456 """Add a reported flight."""
457 self._reportedFrame.addFlight(flight)
458
459 def addRejectedFlight(self, flight):
460 """Add a rejected flight."""
461 self._rejectedFrame.addFlight(flight)
462
463 def _closeClicked(self, button):
464 """Called when the Close button is clicked.
465
466 A 'delete-event' is emitted to close the window."""
467 self.emit("delete-event", None)
468
469 def _keyPressed(self, window, event):
470 """Called when a key is pressed in the window.
471
472 If the Escape key is pressed, 'delete-event' is emitted to close the
473 window."""
474 if gdk.keyval_name(event.keyval) == "Escape":
475 self.emit("delete-event", None)
476 return True
477
478#-----------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.