[858] | 1 | # Module for handling the time table and booking flights
|
---|
| 2 |
|
---|
| 3 | #-----------------------------------------------------------------------------
|
---|
| 4 |
|
---|
| 5 | from mlx.gui.common import *
|
---|
[919] | 6 | from .flightlist import ColumnDescriptor
|
---|
[859] | 7 | from mlx.rpc import ScheduledFlight
|
---|
[858] | 8 |
|
---|
| 9 | import mlx.const as const
|
---|
| 10 |
|
---|
| 11 | import datetime
|
---|
[859] | 12 | import random
|
---|
[858] | 13 |
|
---|
| 14 | #-----------------------------------------------------------------------------
|
---|
| 15 |
|
---|
[996] | 16 | class Timetable(Gtk.Alignment):
|
---|
[858] | 17 | """The widget for the time table."""
|
---|
| 18 | def _getVIPRenderer():
|
---|
| 19 | """Get the renderer for the VIP column."""
|
---|
[996] | 20 | renderer = Gtk.CellRendererToggle()
|
---|
[858] | 21 | renderer.set_activatable(True)
|
---|
| 22 | return renderer
|
---|
| 23 |
|
---|
| 24 | defaultColumnDescriptors = [
|
---|
| 25 | ColumnDescriptor("callsign", xstr("timetable_no"),
|
---|
| 26 | sortable = True, defaultSortable = True),
|
---|
| 27 | ColumnDescriptor("aircraftType", xstr("timetable_type"),
|
---|
| 28 | sortable = True,
|
---|
| 29 | convertFn = lambda aircraftType, flight:
|
---|
| 30 | aircraftNames[aircraftType]),
|
---|
| 31 | ColumnDescriptor("departureICAO", xstr("timetable_from"),
|
---|
| 32 | sortable = True),
|
---|
| 33 | ColumnDescriptor("arrivalICAO", xstr("timetable_to"), sortable = True),
|
---|
| 34 | ColumnDescriptor("departureTime", xstr("timetable_dep"),
|
---|
| 35 | sortable = True),
|
---|
| 36 | ColumnDescriptor("arrivalTime", xstr("timetable_arr"), sortable = True),
|
---|
| 37 | ColumnDescriptor("duration", xstr("timetable_duration"),
|
---|
| 38 | sortable = True,
|
---|
| 39 | convertFn = lambda duration, flight:
|
---|
| 40 | "%02d:%02d" % (duration/3600,
|
---|
| 41 | (duration%3600)/60)),
|
---|
[859] | 42 | ColumnDescriptor("type", xstr("timetable_vip"), type = bool,
|
---|
[858] | 43 | renderer = _getVIPRenderer(),
|
---|
| 44 | sortable = True,
|
---|
[859] | 45 | convertFn = lambda type, flight:
|
---|
| 46 | type==ScheduledFlight.TYPE_VIP)
|
---|
[858] | 47 | ]
|
---|
| 48 |
|
---|
[859] | 49 | columnOrdering = ["callsign", "aircraftType",
|
---|
| 50 | "date", "departureTime", "arrivalTime",
|
---|
| 51 | "departureICAO", "arrivalICAO", "duration", "type"]
|
---|
| 52 |
|
---|
[858] | 53 | @staticmethod
|
---|
| 54 | def isFlightSelected(flight, regularEnabled, vipEnabled, aircraftTypes):
|
---|
| 55 | """Determine if the given flight is selected by the given
|
---|
| 56 | filtering conditions."""
|
---|
[859] | 57 | return ((regularEnabled and flight.type==ScheduledFlight.TYPE_NORMAL) or \
|
---|
| 58 | (vipEnabled and flight.type==ScheduledFlight.TYPE_VIP)) and \
|
---|
[858] | 59 | flight.aircraftType in aircraftTypes
|
---|
| 60 |
|
---|
| 61 | def __init__(self, columnDescriptors = defaultColumnDescriptors,
|
---|
| 62 | popupMenuProducer = None):
|
---|
| 63 | """Construct the time table."""
|
---|
| 64 | # FIXME: this is very similar to flightlist.FlightList
|
---|
| 65 | self._columnDescriptors = columnDescriptors
|
---|
| 66 | self._popupMenuProducer = popupMenuProducer
|
---|
| 67 | self._popupMenu = None
|
---|
| 68 |
|
---|
| 69 | types = [int]
|
---|
| 70 | defaultSortableIndex = None
|
---|
| 71 | for columnDescriptor in self._columnDescriptors:
|
---|
| 72 | if columnDescriptor.defaultSortable:
|
---|
| 73 | defaultSortableIndex = len(types)
|
---|
| 74 | columnDescriptor.appendType(types)
|
---|
| 75 |
|
---|
[996] | 76 | self._model = Gtk.ListStore(*types)
|
---|
[858] | 77 | if defaultSortableIndex is not None:
|
---|
[999] | 78 | sortOrder = Gtk.SortType.DESCENDING \
|
---|
[858] | 79 | if self._columnDescriptors[defaultSortableIndex-1]._defaultDescending \
|
---|
[999] | 80 | else Gtk.SortType.ASCENDING
|
---|
[858] | 81 | self._model.set_sort_column_id(defaultSortableIndex, sortOrder)
|
---|
[996] | 82 | self._view = Gtk.TreeView(self._model)
|
---|
[858] | 83 |
|
---|
[859] | 84 | self._view.connect("motion-notify-event", self._updateTooltip)
|
---|
| 85 |
|
---|
[996] | 86 | flightPairIndexColumn = Gtk.TreeViewColumn()
|
---|
[858] | 87 | flightPairIndexColumn.set_visible(False)
|
---|
| 88 | self._view.append_column(flightPairIndexColumn)
|
---|
| 89 |
|
---|
| 90 | index = 1
|
---|
| 91 | for columnDescriptor in self._columnDescriptors:
|
---|
| 92 | column = columnDescriptor.getViewColumn(index)
|
---|
| 93 | self._view.append_column(column)
|
---|
[859] | 94 | self._model.set_sort_func(index, self._compareFlights,
|
---|
| 95 | columnDescriptor.attribute)
|
---|
[858] | 96 | index += 1
|
---|
| 97 |
|
---|
| 98 | self._view.connect("row-activated", self._rowActivated)
|
---|
| 99 | self._view.connect("button-press-event", self._buttonPressEvent)
|
---|
| 100 |
|
---|
| 101 | selection = self._view.get_selection()
|
---|
| 102 | selection.connect("changed", self._selectionChanged)
|
---|
| 103 |
|
---|
[996] | 104 | scrolledWindow = Gtk.ScrolledWindow()
|
---|
[858] | 105 | scrolledWindow.add(self._view)
|
---|
| 106 | scrolledWindow.set_size_request(800, -1)
|
---|
| 107 |
|
---|
| 108 | # FIXME: these should be constants in common.py
|
---|
[996] | 109 | scrolledWindow.set_policy(Gtk.PolicyType.AUTOMATIC,
|
---|
| 110 | Gtk.PolicyType.AUTOMATIC)
|
---|
| 111 | scrolledWindow.set_shadow_type(Gtk.ShadowType.IN)
|
---|
[858] | 112 |
|
---|
| 113 | super(Timetable, self).__init__(xalign = 0.5, yalign = 0.0,
|
---|
| 114 | xscale = 0.0, yscale = 1.0)
|
---|
| 115 | self.add(scrolledWindow)
|
---|
| 116 |
|
---|
| 117 | self._flightPairs = []
|
---|
| 118 |
|
---|
| 119 | @property
|
---|
[859] | 120 | def selectedIndexes(self):
|
---|
| 121 | """Get the indexes of the selected entries, if any.
|
---|
| 122 |
|
---|
| 123 | The indexes are sorted."""
|
---|
| 124 | selection = self._view.get_selection()
|
---|
| 125 | (model, rows) = selection.get_selected_rows()
|
---|
| 126 |
|
---|
| 127 | indexes = [self._getIndexForPath(path) for path in rows]
|
---|
| 128 | indexes.sort()
|
---|
| 129 | return indexes
|
---|
| 130 |
|
---|
| 131 | @property
|
---|
[858] | 132 | def hasFlightPairs(self):
|
---|
| 133 | """Determine if the timetable contains any flights."""
|
---|
| 134 | return len(self._flightPairs)>0
|
---|
| 135 |
|
---|
| 136 | def clear(self):
|
---|
| 137 | """Clear the flight pairs."""
|
---|
| 138 | self._model.clear()
|
---|
| 139 | self._flightPairs = []
|
---|
| 140 |
|
---|
| 141 | def setFlightPairs(self, flightPairs):
|
---|
| 142 | """Setup the table contents from the given list of
|
---|
| 143 | rpc.ScheduledFlightPair objects."""
|
---|
| 144 | self.clear()
|
---|
| 145 |
|
---|
| 146 | self._flightPairs = flightPairs
|
---|
| 147 |
|
---|
[859] | 148 | def getFlightPair(self, index):
|
---|
| 149 | """Get the flight pair with the given index."""
|
---|
| 150 | return self._flightPairs[index]
|
---|
| 151 |
|
---|
[858] | 152 | def updateList(self, regularEnabled, vipEnabled, types):
|
---|
| 153 | """Update the actual list according to the given filter values."""
|
---|
| 154 | index = 0
|
---|
| 155 | self._model.clear()
|
---|
| 156 | for flightPair in self._flightPairs:
|
---|
| 157 | flight = flightPair.flight0
|
---|
| 158 | if Timetable.isFlightSelected(flight, regularEnabled, vipEnabled,
|
---|
| 159 | types):
|
---|
| 160 | values = [index]
|
---|
| 161 | for columnDescriptor in self._columnDescriptors:
|
---|
| 162 | values.append(columnDescriptor.getValueFrom(flight))
|
---|
| 163 | self._model.append(values)
|
---|
| 164 | index += 1
|
---|
| 165 |
|
---|
[859] | 166 | def _getIndexForPath(self, path):
|
---|
| 167 | """Get the index for the given path."""
|
---|
| 168 | iter = self._model.get_iter(path)
|
---|
| 169 | return self._model.get_value(iter, 0)
|
---|
| 170 |
|
---|
[858] | 171 | def _rowActivated(self, flightList, path, column):
|
---|
| 172 | """Called when a row is selected."""
|
---|
[859] | 173 | self.emit("row-activated", self._getIndexForPath(path))
|
---|
[858] | 174 |
|
---|
| 175 | def _buttonPressEvent(self, widget, event):
|
---|
| 176 | """Called when a mouse button is pressed or released."""
|
---|
[999] | 177 | if event.type!=Gdk.EventType.BUTTON_PRESS or event.button!=3 or \
|
---|
[859] | 178 | self._popupMenuProducer is None:
|
---|
| 179 | return
|
---|
| 180 |
|
---|
| 181 | (path, _, _, _) = self._view.get_path_at_pos(int(event.x),
|
---|
| 182 | int(event.y))
|
---|
| 183 | selection = self._view.get_selection()
|
---|
| 184 | selection.unselect_all()
|
---|
| 185 | selection.select_path(path)
|
---|
| 186 |
|
---|
| 187 | if self._popupMenu is None:
|
---|
| 188 | self._popupMenu = self._popupMenuProducer()
|
---|
| 189 | menu = self._popupMenu
|
---|
[994] | 190 | menu.popup(None, None, None, None, event.button, event.time)
|
---|
[858] | 191 |
|
---|
| 192 | def _selectionChanged(self, selection):
|
---|
| 193 | """Called when the selection has changed."""
|
---|
[859] | 194 | self.emit("selection-changed", self.selectedIndexes)
|
---|
| 195 |
|
---|
| 196 | def _compareFlights(self, model, iter1, iter2, mainColumn):
|
---|
| 197 | """Compare the flights at the given iterators according to the given
|
---|
| 198 | main column."""
|
---|
| 199 | index1 = self._model.get_value(iter1, 0)
|
---|
| 200 | index2 = self._model.get_value(iter2, 0)
|
---|
| 201 |
|
---|
| 202 | flightPair1 = self._flightPairs[index1]
|
---|
| 203 | flightPair2 = self._flightPairs[index2]
|
---|
| 204 |
|
---|
| 205 | result = flightPair1.compareBy(flightPair2, mainColumn)
|
---|
| 206 | if result==0:
|
---|
| 207 | for column in Timetable.columnOrdering:
|
---|
| 208 | if column!=mainColumn:
|
---|
| 209 | result = flightPair1.compareBy(flightPair2, column)
|
---|
| 210 | if result!=0:
|
---|
| 211 | break
|
---|
| 212 | return result
|
---|
| 213 |
|
---|
| 214 | def _updateTooltip(self, widget, event):
|
---|
| 215 | """Update the tooltip for the position of the given event."""
|
---|
| 216 | try:
|
---|
[879] | 217 | result = widget.get_path_at_pos( int(event.x), int(event.y))
|
---|
| 218 | if result is None:
|
---|
[927] | 219 | widget.set_tooltip_text("")
|
---|
[879] | 220 | else:
|
---|
| 221 | (path, col, x, y) = result
|
---|
| 222 | index = self._getIndexForPath(path)
|
---|
| 223 |
|
---|
| 224 | flight = self._flightPairs[index].flight0
|
---|
| 225 | comment = flight.comment
|
---|
| 226 | date = flight.date
|
---|
| 227 |
|
---|
| 228 | if comment or date!=const.defaultDate:
|
---|
| 229 | text = ""
|
---|
| 230 | if comment:
|
---|
| 231 | text = comment
|
---|
| 232 | if date!=const.defaultDate:
|
---|
| 233 | if text:
|
---|
| 234 | text += "; "
|
---|
| 235 | text += date.strftime("%Y-%m-%d")
|
---|
| 236 |
|
---|
[927] | 237 | widget.set_tooltip_text(text)
|
---|
[879] | 238 | else:
|
---|
[927] | 239 | widget.set_tooltip_text("")
|
---|
[919] | 240 | except Exception as e:
|
---|
| 241 | print(e)
|
---|
[927] | 242 | widget.set_tooltip_text("")
|
---|
| 243 |
|
---|
| 244 | #-----------------------------------------------------------------------------
|
---|
| 245 |
|
---|
[995] | 246 | GObject.signal_new("row-activated", Timetable, GObject.SIGNAL_RUN_FIRST,
|
---|
[927] | 247 | None, (int,))
|
---|
| 248 |
|
---|
[995] | 249 | GObject.signal_new("selection-changed", Timetable, GObject.SIGNAL_RUN_FIRST,
|
---|
[927] | 250 | None, (object,))
|
---|
[858] | 251 |
|
---|
| 252 | #-----------------------------------------------------------------------------
|
---|
| 253 |
|
---|
[996] | 254 | class CalendarWindow(Gtk.Window):
|
---|
[858] | 255 | """A window for a calendar."""
|
---|
| 256 | def __init__(self):
|
---|
| 257 | """Construct the window."""
|
---|
| 258 | super(CalendarWindow, self).__init__()
|
---|
| 259 |
|
---|
| 260 | self.set_decorated(False)
|
---|
| 261 | self.set_modal(True)
|
---|
| 262 | self.connect("key-press-event", self._keyPressed)
|
---|
| 263 |
|
---|
[996] | 264 | mainAlignment = Gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
[858] | 265 | xscale = 1.0, yscale = 1.0)
|
---|
| 266 | #mainAlignment.set_padding(padding_top = 0, padding_bottom = 12,
|
---|
| 267 | # padding_left = 8, padding_right = 8)
|
---|
| 268 |
|
---|
[996] | 269 | self._calendar = Gtk.Calendar()
|
---|
[858] | 270 | self._calendar.connect("day-selected-double-click", self._daySelectedDoubleClick)
|
---|
| 271 | mainAlignment.add(self._calendar)
|
---|
| 272 |
|
---|
| 273 | self.add(mainAlignment)
|
---|
| 274 |
|
---|
| 275 | def setDate(self, date):
|
---|
| 276 | """Set the current date to the given one."""
|
---|
| 277 | self._calendar.select_month(date.month-1, date.year)
|
---|
| 278 | self._calendar.select_day(date.day)
|
---|
| 279 |
|
---|
| 280 | def getDate(self):
|
---|
| 281 | """Get the currently selected date."""
|
---|
| 282 | (year, monthM1, day) = self._calendar.get_date()
|
---|
| 283 | return datetime.date(year, monthM1+1, day)
|
---|
| 284 |
|
---|
| 285 | def _daySelectedDoubleClick(self, calendar):
|
---|
| 286 | """Called when a date is double clicked."""
|
---|
| 287 | self.emit("date-selected")
|
---|
| 288 |
|
---|
| 289 | def _keyPressed(self, window, event):
|
---|
| 290 | """Called when a key is pressed in the window.
|
---|
| 291 |
|
---|
| 292 | If the Escape key is pressed, 'delete-event' is emitted to close the
|
---|
| 293 | window."""
|
---|
[997] | 294 | keyName = Gdk.keyval_name(event.keyval)
|
---|
[858] | 295 | if keyName =="Escape":
|
---|
| 296 | self.emit("delete-event", None)
|
---|
| 297 | return True
|
---|
| 298 | elif keyName =="Return":
|
---|
| 299 | self.emit("date-selected")
|
---|
| 300 | return True
|
---|
| 301 |
|
---|
[995] | 302 | GObject.signal_new("date-selected", CalendarWindow, GObject.SIGNAL_RUN_FIRST,
|
---|
[858] | 303 | None, ())
|
---|
| 304 |
|
---|
| 305 | #-----------------------------------------------------------------------------
|
---|
| 306 |
|
---|
[996] | 307 | class BookDialog(Gtk.Dialog):
|
---|
[859] | 308 | """The dialog box to select additional data for a booking."""
|
---|
| 309 | def __init__(self, timetableWindow, flightPair, planes):
|
---|
| 310 | """Construct the dialog box."""
|
---|
| 311 | super(BookDialog, self).__init__(title = WINDOW_TITLE_BASE +
|
---|
| 312 | " - " +
|
---|
| 313 | xstr("timetable_book_title"),
|
---|
| 314 | parent = timetableWindow)
|
---|
| 315 | contentArea = self.get_content_area()
|
---|
| 316 |
|
---|
[1001] | 317 | frame = Gtk.Frame(label = xstr("timetable_book_frame_title"))
|
---|
[859] | 318 | frame.set_size_request(600, -1)
|
---|
| 319 |
|
---|
[996] | 320 | mainAlignment = Gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
[859] | 321 | xscale = 0.0, yscale = 0.0)
|
---|
| 322 | mainAlignment.set_padding(padding_top = 16, padding_bottom = 12,
|
---|
| 323 | padding_left = 8, padding_right = 8)
|
---|
| 324 |
|
---|
[996] | 325 | table = Gtk.Table(6, 2)
|
---|
[859] | 326 | table.set_row_spacings(8)
|
---|
| 327 | table.set_col_spacings(16)
|
---|
| 328 |
|
---|
| 329 | row = 0
|
---|
[996] | 330 | label = Gtk.Label()
|
---|
[859] | 331 | label.set_markup(xstr("timetable_book_callsign"))
|
---|
| 332 | label.set_alignment(0.0, 0.5)
|
---|
| 333 | table.attach(label, 0, 1, row, row + 1)
|
---|
| 334 |
|
---|
| 335 | text = flightPair.flight0.callsign
|
---|
| 336 | if flightPair.flight1 is not None:
|
---|
| 337 | text += " / " + flightPair.flight1.callsign
|
---|
[996] | 338 | label = Gtk.Label(text)
|
---|
[859] | 339 | label.set_alignment(0.0, 0.5)
|
---|
| 340 | table.attach(label, 1, 2, row, row + 1)
|
---|
| 341 |
|
---|
| 342 | row += 1
|
---|
| 343 |
|
---|
[996] | 344 | label = Gtk.Label()
|
---|
[859] | 345 | label.set_markup(xstr("timetable_book_from_to"))
|
---|
| 346 | label.set_alignment(0.0, 0.5)
|
---|
| 347 | table.attach(label, 0, 1, row, row + 1)
|
---|
| 348 |
|
---|
| 349 | text = flightPair.flight0.departureICAO + " - " + \
|
---|
| 350 | flightPair.flight0.arrivalICAO
|
---|
| 351 | if flightPair.flight1 is not None:
|
---|
| 352 | text += " - " + flightPair.flight1.arrivalICAO
|
---|
[996] | 353 | label = Gtk.Label(text)
|
---|
[859] | 354 | label.set_alignment(0.0, 0.5)
|
---|
| 355 | table.attach(label, 1, 2, row, row + 1)
|
---|
| 356 |
|
---|
| 357 | row += 1
|
---|
| 358 |
|
---|
| 359 | if flightPair.flight0.type==ScheduledFlight.TYPE_VIP and \
|
---|
| 360 | flightPair.flight0.date!=const.defaultDate:
|
---|
[996] | 361 | label = Gtk.Label()
|
---|
[859] | 362 | label.set_markup(xstr("timetable_book_flightDate"))
|
---|
| 363 | label.set_use_underline(True)
|
---|
| 364 | label.set_alignment(0.0, 0.5)
|
---|
| 365 | table.attach(label, 0, 1, row, row + 1)
|
---|
| 366 |
|
---|
[996] | 367 | self._flightDate = Gtk.Button()
|
---|
[859] | 368 | self._flightDate.connect("clicked", self._flightDateClicked)
|
---|
| 369 | self._flightDate.set_tooltip_text(xstr("timetable_book_flightDate_tooltip"))
|
---|
| 370 | label.set_mnemonic_widget(self._flightDate)
|
---|
| 371 |
|
---|
| 372 | table.attach(self._flightDate, 1, 2, row, row + 1)
|
---|
| 373 |
|
---|
| 374 | self._calendarWindow = calendarWindow = CalendarWindow()
|
---|
| 375 | calendarWindow.set_transient_for(self)
|
---|
| 376 | calendarWindow.connect("delete-event", self._calendarWindowDeleted)
|
---|
| 377 | calendarWindow.connect("date-selected", self._calendarWindowDateSelected)
|
---|
| 378 |
|
---|
| 379 | self._setDate(flightPair.flight0.date)
|
---|
| 380 |
|
---|
| 381 | row += 1
|
---|
| 382 | else:
|
---|
| 383 | self._flightDate = None
|
---|
| 384 | self._calendarWindow = None
|
---|
| 385 |
|
---|
[996] | 386 | label = Gtk.Label()
|
---|
[859] | 387 | label.set_markup(xstr("timetable_book_dep_arr"))
|
---|
| 388 | label.set_alignment(0.0, 0.5)
|
---|
| 389 | table.attach(label, 0, 1, row, row + 1)
|
---|
| 390 |
|
---|
| 391 | text = str(flightPair.flight0.departureTime) + " - " + \
|
---|
| 392 | str(flightPair.flight0.arrivalTime)
|
---|
| 393 | if flightPair.flight1 is not None:
|
---|
| 394 | text += " / " + str(flightPair.flight1.departureTime) + " - " + \
|
---|
| 395 | str(flightPair.flight1.arrivalTime)
|
---|
[996] | 396 | label = Gtk.Label(text)
|
---|
[859] | 397 | label.set_alignment(0.0, 0.5)
|
---|
| 398 | table.attach(label, 1, 2, row, row + 1)
|
---|
| 399 |
|
---|
| 400 | row += 1
|
---|
| 401 |
|
---|
[996] | 402 | label = Gtk.Label()
|
---|
[859] | 403 | label.set_markup(xstr("timetable_book_duration"))
|
---|
| 404 | label.set_alignment(0.0, 0.5)
|
---|
| 405 | table.attach(label, 0, 1, row, row + 1)
|
---|
| 406 |
|
---|
| 407 |
|
---|
| 408 | duration = flightPair.flight0.duration
|
---|
| 409 | text = "%02d:%02d" % (duration/3600, (duration%3600)/60)
|
---|
| 410 | if flightPair.flight1 is not None:
|
---|
| 411 | duration = flightPair.flight0.duration
|
---|
| 412 | text += " / %02d:%02d" % (duration/3600, (duration%3600)/60)
|
---|
[996] | 413 | label = Gtk.Label(text)
|
---|
[859] | 414 | label.set_alignment(0.0, 0.5)
|
---|
| 415 | table.attach(label, 1, 2, row, row + 1)
|
---|
| 416 |
|
---|
| 417 | row += 2
|
---|
| 418 |
|
---|
[996] | 419 | label = Gtk.Label()
|
---|
[859] | 420 | label.set_markup(xstr("timetable_book_tailNumber"))
|
---|
| 421 | label.set_alignment(0.0, 0.5)
|
---|
| 422 | table.attach(label, 0, 1, row, row + 1)
|
---|
| 423 |
|
---|
| 424 | self._planes = planes
|
---|
[996] | 425 | tailNumbersModel = Gtk.ListStore(str)
|
---|
[859] | 426 | for plane in planes:
|
---|
| 427 | tailNumbersModel.append((plane.tailNumber,))
|
---|
| 428 |
|
---|
[996] | 429 | self._tailNumber = Gtk.ComboBox(model = tailNumbersModel)
|
---|
| 430 | renderer = Gtk.CellRendererText()
|
---|
[859] | 431 | self._tailNumber.pack_start(renderer, True)
|
---|
| 432 | self._tailNumber.add_attribute(renderer, "text", 0)
|
---|
| 433 | self._tailNumber.set_tooltip_text(xstr("timetable_book_tailNumber_tooltip"))
|
---|
| 434 | self._tailNumber.set_active(random.randint(0, len(planes)-1))
|
---|
| 435 |
|
---|
| 436 | table.attach(self._tailNumber, 1, 2, row, row + 1)
|
---|
| 437 |
|
---|
| 438 | mainAlignment.add(table)
|
---|
| 439 |
|
---|
| 440 | frame.add(mainAlignment)
|
---|
| 441 | contentArea.pack_start(frame, True, True, 4)
|
---|
| 442 |
|
---|
[999] | 443 | self.add_button(xstr("button_cancel"), Gtk.ResponseType.CANCEL)
|
---|
[859] | 444 |
|
---|
[999] | 445 | self._okButton = self.add_button(xstr("button_book"), Gtk.ResponseType.OK)
|
---|
[859] | 446 | self._okButton.set_use_underline(True)
|
---|
| 447 | self._okButton.set_can_default(True)
|
---|
| 448 |
|
---|
| 449 | @property
|
---|
| 450 | def plane(self):
|
---|
| 451 | """Get the currently selected plane."""
|
---|
| 452 | return self._planes[self._tailNumber.get_active()]
|
---|
| 453 |
|
---|
| 454 | @property
|
---|
| 455 | def date(self):
|
---|
| 456 | """Get the flight date, if selected."""
|
---|
| 457 | return None if self._calendarWindow is None \
|
---|
| 458 | else self._calendarWindow.getDate()
|
---|
| 459 |
|
---|
| 460 | def _setDate(self, date):
|
---|
| 461 | """Set the date to the given one."""
|
---|
| 462 | self._flightDate.set_label(date.strftime("%Y-%m-%d"))
|
---|
| 463 | self._calendarWindow.setDate(date)
|
---|
| 464 |
|
---|
| 465 | def _flightDateClicked(self, button):
|
---|
| 466 | """Called when the flight date button is clicked."""
|
---|
[1012] | 467 | self._calendarWindow.set_position(Gtk.WindowPosition.MOUSE)
|
---|
[859] | 468 | self.set_focus(self._calendarWindow)
|
---|
| 469 | self._calendarWindow.show_all()
|
---|
| 470 |
|
---|
| 471 | def _calendarWindowDeleted(self, window, event):
|
---|
| 472 | """Called when the flight date window is deleted."""
|
---|
| 473 | self._calendarWindow.hide()
|
---|
| 474 |
|
---|
| 475 | def _calendarWindowDateSelected(self, window):
|
---|
| 476 | """Called when the flight date window is deleted."""
|
---|
| 477 | self._calendarWindow.hide()
|
---|
| 478 | date = window.getDate()
|
---|
| 479 | self._flightDate.set_label(date.strftime("%Y-%m-%d"))
|
---|
| 480 |
|
---|
| 481 | #-----------------------------------------------------------------------------
|
---|
| 482 |
|
---|
[996] | 483 | class TimetableWindow(Gtk.Window):
|
---|
[858] | 484 | """The window to display the timetable."""
|
---|
| 485 | typeFamilies = [
|
---|
| 486 | const.AIRCRAFT_FAMILY_B737NG,
|
---|
| 487 | const.AIRCRAFT_FAMILY_DH8D,
|
---|
| 488 | const.AIRCRAFT_FAMILY_B767,
|
---|
| 489 |
|
---|
| 490 | const.AIRCRAFT_FAMILY_B737CL,
|
---|
| 491 | const.AIRCRAFT_FAMILY_CRJ2,
|
---|
| 492 | const.AIRCRAFT_FAMILY_F70,
|
---|
| 493 |
|
---|
| 494 | const.AIRCRAFT_FAMILY_T134,
|
---|
| 495 | const.AIRCRAFT_FAMILY_T154
|
---|
| 496 | ]
|
---|
| 497 |
|
---|
| 498 | def __init__(self, gui):
|
---|
| 499 | super(TimetableWindow, self).__init__()
|
---|
| 500 |
|
---|
| 501 | self._gui = gui
|
---|
| 502 | self.set_title(WINDOW_TITLE_BASE + " - " + xstr("timetable_title"))
|
---|
| 503 | self.set_size_request(-1, 600)
|
---|
| 504 | self.set_transient_for(gui.mainWindow)
|
---|
| 505 | self.set_modal(True)
|
---|
| 506 | self.connect("key-press-event", self._keyPressed)
|
---|
| 507 |
|
---|
[996] | 508 | mainAlignment = Gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
[858] | 509 | xscale = 1.0, yscale = 1.0)
|
---|
| 510 | mainAlignment.set_padding(padding_top = 0, padding_bottom = 12,
|
---|
| 511 | padding_left = 8, padding_right = 8)
|
---|
| 512 |
|
---|
[996] | 513 | vbox = Gtk.VBox()
|
---|
[858] | 514 |
|
---|
[996] | 515 | filterAlignment = Gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
[858] | 516 | xscale = 1.0, yscale = 1.0)
|
---|
| 517 |
|
---|
[996] | 518 | filterFrame = Gtk.Frame()
|
---|
[927] | 519 | filterFrame.set_label(xstr("timetable_filter"))
|
---|
[858] | 520 |
|
---|
[996] | 521 | filterVBox = Gtk.VBox()
|
---|
[858] | 522 |
|
---|
[996] | 523 | topAlignment = Gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
[858] | 524 | xscale = 0.0, yscale = 0.0)
|
---|
[996] | 525 | topHBox = Gtk.HBox()
|
---|
[858] | 526 |
|
---|
[996] | 527 | label = Gtk.Label(xstr("timetable_flightdate"))
|
---|
[858] | 528 | label.set_use_underline(True)
|
---|
| 529 | topHBox.pack_start(label, False, False, 4)
|
---|
| 530 |
|
---|
[996] | 531 | self._flightDate = Gtk.Button()
|
---|
[858] | 532 | self._flightDate.connect("clicked", self._flightDateClicked)
|
---|
[859] | 533 | self._flightDate.connect("clicked", self._flightDateClicked)
|
---|
[858] | 534 | self._flightDate.set_tooltip_text(xstr("timetable_flightdate_tooltip"))
|
---|
| 535 | label.set_mnemonic_widget(self._flightDate)
|
---|
| 536 | topHBox.pack_start(self._flightDate, False, False, 4)
|
---|
| 537 |
|
---|
[996] | 538 | filler = Gtk.Alignment()
|
---|
[858] | 539 | filler.set_size_request(48, 2)
|
---|
| 540 | topHBox.pack_start(filler, False, True, 0)
|
---|
| 541 |
|
---|
[996] | 542 | self._regularFlights = Gtk.CheckButton(xstr("timetable_show_regular"))
|
---|
[858] | 543 | self._regularFlights.set_use_underline(True)
|
---|
| 544 | self._regularFlights.set_tooltip_text(xstr("timetable_show_regular_tooltip"))
|
---|
| 545 | self._regularFlights.set_active(True)
|
---|
| 546 | self._regularFlights.connect("toggled", self._filterChanged)
|
---|
| 547 | topHBox.pack_start(self._regularFlights, False, False, 8)
|
---|
| 548 |
|
---|
[996] | 549 | self._vipFlights = Gtk.CheckButton(xstr("timetable_show_vip"))
|
---|
[858] | 550 | self._vipFlights.set_use_underline(True)
|
---|
| 551 | self._vipFlights.set_tooltip_text(xstr("timetable_show_vip_tooltip"))
|
---|
| 552 | self._vipFlights.set_active(True)
|
---|
| 553 | self._vipFlights.connect("toggled", self._filterChanged)
|
---|
| 554 | topHBox.pack_start(self._vipFlights, False, False, 8)
|
---|
| 555 |
|
---|
| 556 | topAlignment.add(topHBox)
|
---|
| 557 |
|
---|
| 558 | filterVBox.pack_start(topAlignment, False, False, 4)
|
---|
| 559 |
|
---|
[996] | 560 | separator = Gtk.HSeparator()
|
---|
[858] | 561 | filterVBox.pack_start(separator, False, False, 4)
|
---|
| 562 |
|
---|
[996] | 563 | typeAlignment = Gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
[858] | 564 | xscale = 0.0, yscale = 0.0)
|
---|
| 565 |
|
---|
| 566 | numColumns = 4
|
---|
| 567 | numRows = (len(TimetableWindow.typeFamilies)+numColumns-1)/numColumns
|
---|
| 568 |
|
---|
[996] | 569 | typeTable = Gtk.Table(numRows, numColumns)
|
---|
[858] | 570 | typeTable.set_col_spacings(8)
|
---|
| 571 | row = 0
|
---|
| 572 | column = 0
|
---|
| 573 | self._typeFamilyButtons = {}
|
---|
| 574 | for typeFamily in TimetableWindow.typeFamilies:
|
---|
[996] | 575 | checkButton = Gtk.CheckButton(aircraftFamilyNames[typeFamily])
|
---|
[858] | 576 | checkButton.set_active(True)
|
---|
| 577 | checkButton.connect("toggled", self._filterChanged)
|
---|
| 578 | self._typeFamilyButtons[typeFamily] = checkButton
|
---|
| 579 |
|
---|
| 580 | typeTable.attach(checkButton, column, column + 1, row, row+1)
|
---|
| 581 |
|
---|
| 582 | column += 1
|
---|
| 583 | if column>=numColumns:
|
---|
| 584 | row += 1
|
---|
| 585 | column = 0
|
---|
| 586 |
|
---|
| 587 | typeAlignment.add(typeTable)
|
---|
| 588 | filterVBox.pack_start(typeAlignment, False, False, 4)
|
---|
| 589 |
|
---|
| 590 | filterFrame.add(filterVBox)
|
---|
| 591 |
|
---|
| 592 | filterAlignment.add(filterFrame)
|
---|
| 593 | vbox.pack_start(filterAlignment, False, False, 2)
|
---|
| 594 |
|
---|
[859] | 595 | self._timetable = Timetable(popupMenuProducer =
|
---|
| 596 | self._createTimetablePopupMenu)
|
---|
| 597 | self._timetable.connect("row-activated", self._rowActivated)
|
---|
| 598 | self._timetable.connect("selection-changed", self._selectionChanged)
|
---|
[858] | 599 | vbox.pack_start(self._timetable, True, True, 2)
|
---|
| 600 |
|
---|
[996] | 601 | alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
|
---|
[858] | 602 | xscale = 0.0, yscale = 0.0)
|
---|
[996] | 603 | buttonBox = Gtk.HBox()
|
---|
[859] | 604 |
|
---|
[996] | 605 | self._bookButton = Gtk.Button(xstr("button_book"))
|
---|
[859] | 606 | self._bookButton.set_use_underline(True)
|
---|
| 607 | self._bookButton.set_can_default(True)
|
---|
| 608 | self._bookButton.connect("clicked", self._bookClicked)
|
---|
| 609 | self._bookButton.set_sensitive(False)
|
---|
| 610 | buttonBox.pack_start(self._bookButton, False, False, 4);
|
---|
| 611 |
|
---|
[996] | 612 | self._closeButton = Gtk.Button(xstr("button_close"))
|
---|
[859] | 613 | self._closeButton.set_use_underline(True)
|
---|
[858] | 614 | self._closeButton.connect("clicked", self._closeClicked)
|
---|
[859] | 615 | buttonBox.pack_start(self._closeButton, False, False, 4);
|
---|
| 616 |
|
---|
| 617 | alignment.add(buttonBox)
|
---|
[858] | 618 | vbox.pack_start(alignment, False, False, 2)
|
---|
| 619 |
|
---|
| 620 | mainAlignment.add(vbox)
|
---|
| 621 |
|
---|
| 622 | self._calendarWindow = calendarWindow = CalendarWindow()
|
---|
| 623 | calendarWindow.set_transient_for(self)
|
---|
| 624 | calendarWindow.connect("delete-event", self._calendarWindowDeleted)
|
---|
| 625 | calendarWindow.connect("date-selected", self._calendarWindowDateSelected)
|
---|
| 626 |
|
---|
| 627 | self.add(mainAlignment)
|
---|
| 628 |
|
---|
| 629 | self.setDate(datetime.date.today())
|
---|
| 630 |
|
---|
[859] | 631 | self._flightPairToBook = None
|
---|
| 632 |
|
---|
[858] | 633 | @property
|
---|
| 634 | def hasFlightPairs(self):
|
---|
| 635 | """Determine if there any flight pairs displayed in the window."""
|
---|
| 636 | return self._timetable.hasFlightPairs
|
---|
| 637 |
|
---|
| 638 | @property
|
---|
| 639 | def isRegularEnabled(self):
|
---|
| 640 | """Determine if regular flights are enabled."""
|
---|
| 641 | return self._regularFlights.get_active()!=0
|
---|
| 642 |
|
---|
| 643 | @property
|
---|
| 644 | def isVIPEnabled(self):
|
---|
| 645 | """Determine if VIP flights are enabled."""
|
---|
| 646 | return self._vipFlights.get_active()!=0
|
---|
| 647 |
|
---|
| 648 | def setTypes(self, aircraftTypes):
|
---|
| 649 | """Enable/disable the type family checkboxes according to the given
|
---|
| 650 | list of types."""
|
---|
| 651 | typeFamilies = set()
|
---|
| 652 | for aircraftType in aircraftTypes:
|
---|
| 653 | typeFamilies.add(const.aircraftType2Family(aircraftType))
|
---|
| 654 |
|
---|
[919] | 655 | for (typeFamily, checkButton) in self._typeFamilyButtons.items():
|
---|
[858] | 656 | checkButton.set_sensitive(typeFamily in typeFamilies)
|
---|
[1013] | 657 | checkButton.set_active(typeFamily in typeFamilies)
|
---|
[858] | 658 |
|
---|
| 659 | def clear(self):
|
---|
| 660 | """Clear all flight pairs."""
|
---|
| 661 | self._timetable.clear()
|
---|
| 662 |
|
---|
| 663 | def setFlightPairs(self, flightPairs):
|
---|
| 664 | """Set the flight pairs."""
|
---|
| 665 | self._timetable.setFlightPairs(flightPairs)
|
---|
| 666 | self._updateList()
|
---|
| 667 |
|
---|
| 668 | def setDate(self, date):
|
---|
| 669 | """Set the date to the given one."""
|
---|
| 670 | self._flightDate.set_label(date.strftime("%Y-%m-%d"))
|
---|
| 671 | self._calendarWindow.setDate(date)
|
---|
| 672 |
|
---|
| 673 | def _closeClicked(self, button):
|
---|
| 674 | """Called when the Close button is clicked.
|
---|
| 675 |
|
---|
| 676 | A 'delete-event' is emitted to close the window."""
|
---|
| 677 | self.emit("delete-event", None)
|
---|
| 678 |
|
---|
| 679 | def _flightDateClicked(self, button):
|
---|
| 680 | """Called when the flight date button is clicked."""
|
---|
[1012] | 681 | self._calendarWindow.set_position(Gtk.WindowPosition.MOUSE)
|
---|
[858] | 682 | self.set_focus(self._calendarWindow)
|
---|
| 683 | self._calendarWindow.show_all()
|
---|
| 684 |
|
---|
| 685 | def _calendarWindowDeleted(self, window, event):
|
---|
| 686 | """Called when the flight date window is deleted."""
|
---|
| 687 | self._calendarWindow.hide()
|
---|
| 688 |
|
---|
| 689 | def _calendarWindowDateSelected(self, window):
|
---|
| 690 | """Called when the flight date window is deleted."""
|
---|
| 691 | self._calendarWindow.hide()
|
---|
| 692 | date = window.getDate()
|
---|
| 693 | self._flightDate.set_label(date.strftime("%Y-%m-%d"))
|
---|
| 694 | self._gui.updateTimeTable(date)
|
---|
| 695 |
|
---|
| 696 | def _filterChanged(self, checkButton):
|
---|
| 697 | """Called when the filter conditions have changed."""
|
---|
| 698 | self._updateList()
|
---|
| 699 |
|
---|
| 700 | def _keyPressed(self, window, event):
|
---|
| 701 | """Called when a key is pressed in the window.
|
---|
| 702 |
|
---|
| 703 | If the Escape key is pressed, 'delete-event' is emitted to close the
|
---|
| 704 | window."""
|
---|
[997] | 705 | if Gdk.keyval_name(event.keyval) == "Escape":
|
---|
[858] | 706 | self.emit("delete-event", None)
|
---|
| 707 | return True
|
---|
| 708 |
|
---|
| 709 | def _updateList(self):
|
---|
| 710 | """Update the timetable list."""
|
---|
| 711 | aircraftTypes = []
|
---|
[919] | 712 | for (aircraftFamily, button) in self._typeFamilyButtons.items():
|
---|
[858] | 713 | if button.get_active():
|
---|
| 714 | aircraftTypes += const.aircraftFamily2Types[aircraftFamily]
|
---|
| 715 |
|
---|
| 716 | self._timetable.updateList(self.isRegularEnabled,
|
---|
| 717 | self.isVIPEnabled,
|
---|
| 718 | aircraftTypes)
|
---|
[859] | 719 |
|
---|
| 720 | def _bookClicked(self, button):
|
---|
| 721 | """Called when the book button has been clicked."""
|
---|
| 722 | self._book(self._timetable.getFlightPair(self._timetable.selectedIndexes[0]))
|
---|
| 723 |
|
---|
| 724 | def _rowActivated(self, timetable, index):
|
---|
| 725 | """Called when a row has been activated (e.g. double-clicked) in the
|
---|
| 726 | timetable."""
|
---|
| 727 | self._book(self._timetable.getFlightPair(index))
|
---|
| 728 |
|
---|
| 729 | def _selectionChanged(self, timetable, indexes):
|
---|
| 730 | """Called when the selection has changed.
|
---|
| 731 |
|
---|
| 732 | It sets the sensitivity of the book button based on whether a row is
|
---|
| 733 | selected or not."""
|
---|
| 734 | self._bookButton.set_sensitive(len(indexes)>0)
|
---|
| 735 |
|
---|
| 736 | def _book(self, flightPair):
|
---|
| 737 | """Try to book the given flight pair."""
|
---|
| 738 | self._flightPairToBook = flightPair
|
---|
| 739 | self._gui.getFleet(callback = self._continueBook,
|
---|
| 740 | busyCallback = self._busyCallback)
|
---|
| 741 |
|
---|
| 742 | def _busyCallback(self, busy):
|
---|
| 743 | """Called when the busy state has changed."""
|
---|
| 744 | self.set_sensitive(not busy)
|
---|
| 745 |
|
---|
| 746 | def _continueBook(self, fleet):
|
---|
| 747 | """Continue booking, once the fleet is available."""
|
---|
| 748 | flightPair = self._flightPairToBook
|
---|
| 749 | aircraftType = flightPair.flight0.aircraftType
|
---|
| 750 | planes = [plane for plane in fleet
|
---|
| 751 | if plane.aircraftType == aircraftType]
|
---|
[923] | 752 | planes.sort(key = lambda p: p.tailNumber)
|
---|
[859] | 753 |
|
---|
| 754 | dialog = BookDialog(self, flightPair, planes)
|
---|
| 755 | dialog.show_all()
|
---|
| 756 | result = dialog.run()
|
---|
| 757 | dialog.hide()
|
---|
[999] | 758 | if result==Gtk.ResponseType.OK:
|
---|
[859] | 759 | flightIDs = [flightPair.flight0.id]
|
---|
| 760 | if flightPair.flight1 is not None:
|
---|
| 761 | flightIDs.append(flightPair.flight1.id)
|
---|
| 762 |
|
---|
| 763 | date = dialog.date
|
---|
| 764 | if date is None:
|
---|
| 765 | date = self._calendarWindow.getDate()
|
---|
| 766 |
|
---|
| 767 | self._gui.bookFlights(self._bookFlightsCallback,
|
---|
| 768 | flightIDs, date, dialog.plane.tailNumber,
|
---|
| 769 | busyCallback = self._busyCallback)
|
---|
| 770 |
|
---|
| 771 | def _bookFlightsCallback(self, returned, result):
|
---|
| 772 | """Called when the booking has finished."""
|
---|
| 773 | if returned:
|
---|
[996] | 774 | dialog = Gtk.MessageDialog(parent = self,
|
---|
[999] | 775 | type = Gtk.MessageType.INFO,
|
---|
[859] | 776 | message_format = xstr("bookflights_successful"))
|
---|
| 777 | dialog.format_secondary_markup(xstr("bookflights_successful_secondary"))
|
---|
| 778 | else:
|
---|
[996] | 779 | dialog = Gtk.MessageDialog(parent = self,
|
---|
[999] | 780 | type = Gtk.MessageType.ERROR,
|
---|
[859] | 781 | message_format = xstr("bookflights_failed"))
|
---|
| 782 | dialog.format_secondary_markup(xstr("bookflights_failed_secondary"))
|
---|
| 783 |
|
---|
[999] | 784 | dialog.add_button(xstr("button_ok"), Gtk.ResponseType.OK)
|
---|
[859] | 785 | dialog.set_title(WINDOW_TITLE_BASE)
|
---|
| 786 |
|
---|
| 787 | dialog.run()
|
---|
| 788 | dialog.hide()
|
---|
| 789 |
|
---|
| 790 | def _createTimetablePopupMenu(self):
|
---|
| 791 | """Get the popuop menu for the timetable."""
|
---|
[996] | 792 | menu = Gtk.Menu()
|
---|
[859] | 793 |
|
---|
[996] | 794 | menuItem = Gtk.MenuItem()
|
---|
[859] | 795 | menuItem.set_label(xstr("timetable_popup_book"))
|
---|
| 796 | menuItem.set_use_underline(True)
|
---|
| 797 | menuItem.connect("activate", self._popupBook)
|
---|
| 798 | menuItem.show()
|
---|
| 799 |
|
---|
| 800 | menu.append(menuItem)
|
---|
| 801 |
|
---|
| 802 | return menu
|
---|
| 803 |
|
---|
| 804 | def _popupBook(self, menuItem):
|
---|
| 805 | """Try to book the given flight pair."""
|
---|
| 806 | index = self._timetable.selectedIndexes[0]
|
---|
| 807 | self._book(self._timetable.getFlightPair(index))
|
---|