source: src/mlx/gui/flightlist.py@ 823:7a9ce7d89ece

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

Multiple flights can be selected in a flight list (re #307).

File size: 15.2 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):
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
252 alignment = gtk.Alignment(xscale = 1.0, yscale = 1.0)
253 alignment.set_padding(padding_top = 2, padding_bottom = 8,
254 padding_left = 4, padding_right = 4)
255
256 hbox = gtk.HBox()
257
258 self._flights = []
259 self._flightList = FlightList(columnDescriptors =
260 PendingFlightsFrame.columnDescriptors,
261 widthRequest = 500, multiSelection = True)
262 self._flightList.connect("selection-changed", self._selectionChanged)
263
264 hbox.pack_start(self._flightList, True, True, 4)
265
266 buttonBox = gtk.VBox()
267
268 self._editButton = gtk.Button(xstr("pendflt_edit_" + which))
269 self._editButton.set_sensitive(False)
270 buttonBox.pack_start(self._editButton, False, False, 2)
271
272 self._reflyButton = gtk.Button(xstr("pendflt_refly_" + which))
273 self._reflyButton.set_sensitive(False)
274 self._reflyButton.connect("clicked", self._reflyClicked)
275 buttonBox.pack_start(self._reflyButton, False, False, 2)
276
277 self._deleteButton = gtk.Button(xstr("pendflt_delete_" + which))
278 self._deleteButton.set_sensitive(False)
279 buttonBox.pack_start(self._deleteButton, False, False, 2)
280
281 hbox.pack_start(buttonBox, False, False, 4)
282
283 alignment.add(hbox)
284 self.add(alignment)
285
286 @property
287 def hasFlights(self):
288 """Determine if there are any flights in the list."""
289 return self._flightList.hasFlights
290
291 def clear(self):
292 """Clear the lists."""
293 self._flights = []
294 self._flightList.clear()
295
296 def addFlight(self, flight):
297 """Add a flight to the list."""
298 self._flights.append(flight)
299 self._flightList.addFlight(flight)
300
301 def _selectionChanged(self, flightList, selectedIndexes):
302 """Called when the selection in the list has changed."""
303 self._editButton.set_sensitive(len(selectedIndexes)==1)
304 self._reflyButton.set_sensitive(len(selectedIndexes)>0)
305 self._deleteButton.set_sensitive(len(selectedIndexes)>0)
306
307 def _reflyClicked(self, button):
308 """Called when the Refly button is clicked."""
309 gui = self._wizard.gui
310 gui.beginBusy(xstr("pendflt_refly_busy"))
311 self.set_sensitive(False)
312
313 flightIDs = [self._flights[i].id
314 for i in self._flightList.selectedIndexes]
315 gui.webHandler.reflyFlights(self._reflyResultCallback, flightIDs)
316
317 def _reflyResultCallback(self, returned, result):
318 """Called when the refly result is available."""
319 gobject.idle_add(self._handleReflyResult, returned, result)
320
321 def _handleReflyResult(self, returned, result):
322 """Handle the refly result."""
323
324 self.set_sensitive(True)
325 gui = self._wizard.gui
326 gui.endBusy()
327
328 print "PendingFlightsFrame._handleReflyResult", returned, result
329
330 if returned:
331 indexes = self._flightList.selectedIndexes
332
333 flights = [self._flights[index] for index in indexes]
334
335 self._flightList.removeFlights(indexes)
336 for index in indexes[::-1]:
337 del self._flights[index]
338
339 for flight in flights:
340 self._wizard.reflyFlight(flight)
341
342
343#-----------------------------------------------------------------------------
344
345class PendingFlightsWindow(gtk.Window):
346 """The window to display the lists of the pending (reported or rejected)
347 flights."""
348 def __init__(self, wizard):
349 """Construct the window"""
350 super(PendingFlightsWindow, self).__init__()
351
352 gui = wizard.gui
353
354 self.set_title(WINDOW_TITLE_BASE + " - " + xstr("pendflt_title"))
355 self.set_size_request(-1, 450)
356 self.set_transient_for(gui.mainWindow)
357 self.set_modal(True)
358
359 mainAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
360 xscale = 1.0, yscale = 1.0)
361 mainAlignment.set_padding(padding_top = 0, padding_bottom = 12,
362 padding_left = 8, padding_right = 8)
363
364 vbox = gtk.VBox()
365
366 self._reportedFrame = PendingFlightsFrame("reported", wizard)
367 vbox.pack_start(self._reportedFrame, True, True, 2)
368
369 self._rejectedFrame = PendingFlightsFrame("rejected", wizard)
370 vbox.pack_start(self._rejectedFrame, True, True, 2)
371
372 alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
373 xscale = 0.0, yscale = 0.0)
374 self._closeButton = gtk.Button(xstr("button_ok"))
375 self._closeButton.connect("clicked", self._closeClicked)
376 alignment.add(self._closeButton)
377 vbox.pack_start(alignment, False, False, 2)
378
379 mainAlignment.add(vbox)
380
381 self.add(mainAlignment)
382
383 self.connect("key-press-event", self._keyPressed)
384
385 @property
386 def hasFlights(self):
387 """Determine if the window has any flights."""
388 return self._reportedFrame.hasFlights or self._rejectedFrame.hasFlights
389
390 def clear(self):
391 """Clear the lists."""
392 self._reportedFrame.clear()
393 self._rejectedFrame.clear()
394
395 def addReportedFlight(self, flight):
396 """Add a reported flight."""
397 self._reportedFrame.addFlight(flight)
398
399 def addRejectedFlight(self, flight):
400 """Add a rejected flight."""
401 self._rejectedFrame.addFlight(flight)
402
403 def _closeClicked(self, button):
404 """Called when the Close button is clicked.
405
406 A 'delete-event' is emitted to close the window."""
407 self.emit("delete-event", None)
408
409 def _keyPressed(self, window, event):
410 """Called when a key is pressed in the window.
411
412 If the Escape key is pressed, 'delete-event' is emitted to close the
413 window."""
414 if gdk.keyval_name(event.keyval) == "Escape":
415 self.emit("delete-event", None)
416 return True
417
418#-----------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.