Ignore:
Timestamp:
09/08/16 12:10:44 (8 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Extracted the flight list widget into its own class (re #307)

Location:
src/mlx/gui
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/gui/flight.py

    r810 r811  
    22from mlx.gui.common import *
    33import mlx.gui.cef as cef
     4from mlx.gui.flightlist import ColumnDescriptor, FlightList
    45
    56import mlx.const as const
     
    436437class FlightSelectionPage(Page):
    437438    """The page to select the flight."""
     439    columnDescriptors = [
     440        ColumnDescriptor("callsign", xstr("flightsel_no")),
     441        ColumnDescriptor("departureTime", xstr("flightsel_deptime")),
     442        ColumnDescriptor("departureICAO", xstr("flightsel_from")),
     443        ColumnDescriptor("arrivalICAO", xstr("flightsel_to"))
     444    ]
     445
    438446    def __init__(self, wizard):
    439447        """Construct the flight selection page."""
     
    444452                                                  help, completedHelp = completedHelp)
    445453
    446 
    447         self._listStore = gtk.ListStore(str, str, str, str)
    448         self._flightList = gtk.TreeView(self._listStore)
    449         column = gtk.TreeViewColumn(xstr("flightsel_no"), gtk.CellRendererText(),
    450                                     text = 1)
    451         column.set_expand(True)
    452         self._flightList.append_column(column)
    453         column = gtk.TreeViewColumn(xstr("flightsel_deptime"), gtk.CellRendererText(),
    454                                     text = 0)
    455         column.set_expand(True)
    456         self._flightList.append_column(column)
    457         column = gtk.TreeViewColumn(xstr("flightsel_from"), gtk.CellRendererText(),
    458                                     text = 2)
    459         column.set_expand(True)
    460         self._flightList.append_column(column)
    461         column = gtk.TreeViewColumn(xstr("flightsel_to"), gtk.CellRendererText(),
    462                                     text = 3)
    463         column.set_expand(True)
    464         self._flightList.append_column(column)
     454        self._flightList = FlightList(FlightSelectionPage.columnDescriptors,
     455                                      popupMenuProducer =
     456                                      self._createListPopupMenu,
     457                                      widthRequest = 400)
    465458        self._flightList.connect("row-activated", self._rowActivated)
    466         self._flightList.connect("button-press-event", self._listButtonPressed)
    467 
    468         self._flightListPopupMenu = None
    469 
    470         flightSelection = self._flightList.get_selection()
    471         flightSelection.connect("changed", self._selectionChanged)
    472 
    473         scrolledWindow = gtk.ScrolledWindow()
    474         scrolledWindow.add(self._flightList)
    475         scrolledWindow.set_size_request(400, -1)
    476         # FIXME: these should be constants in common.py
    477         scrolledWindow.set_policy(gtk.PolicyType.AUTOMATIC if pygobject
    478                                   else gtk.POLICY_AUTOMATIC,
    479                                   gtk.PolicyType.AUTOMATIC if pygobject
    480                                   else gtk.POLICY_AUTOMATIC)
    481         scrolledWindow.set_shadow_type(gtk.ShadowType.IN if pygobject
    482                                        else gtk.SHADOW_IN)
    483 
    484         alignment = gtk.Alignment(xalign = 0.5, yalign = 0.0, xscale = 0.0, yscale = 1.0)
    485         alignment.add(scrolledWindow)
    486 
    487         self.setMainWidget(alignment)
     459        self._flightList.connect("selection-changed", self._selectionChanged)
     460
     461        self.setMainWidget(self._flightList)
    488462
    489463        self._saveButton = self.addButton(xstr("flightsel_save"),
     
    547521        """Rebuild the flights from the login result."""
    548522        self._flights = []
    549         self._listStore.clear()
     523        self._flightList.clear()
    550524        if self._wizard.loggedIn:
    551525            for flight in self._wizard.loginResult.flights:
     
    555529        """Add the given file to the list of flights."""
    556530        self._flights.append(flight)
    557         self._listStore.append([str(flight.departureTime),
    558                                 flight.callsign,
    559                                 flight.departureICAO,
    560                                 flight.arrivalICAO])
     531        self._flightList.addFlight(flight)
    561532
    562533    def _saveClicked(self, button):
     
    608579                self._buildFlights()
    609580
    610     def _selectionChanged(self, selection):
     581    def _selectionChanged(self, flightList, index):
    611582        """Called when the selection is changed."""
    612         selected = selection.count_selected_rows()==1
     583        selected = index is not None
    613584        self._saveButton.set_sensitive(selected)
    614585        self._button.set_sensitive(selected)
     
    649620            self._flightSelected()
    650621
    651     def _rowActivated(self, flightList, path, column):
     622    def _rowActivated(self, flightList, index):
    652623        """Called when a row is activated."""
    653624        if not self._completed:
     
    664635    def _getSelectedFlight(self):
    665636        """Get the currently selected flight."""
    666         selection = self._flightList.get_selection()
    667         (listStore, iter) = selection.get_selected()
    668         path = listStore.get_path(iter)
    669         [index] = path.get_indices() if pygobject else path
    670 
     637        index = self._flightList.selectedIndex
    671638        return self._flights[index]
    672 
    673     def _listButtonPressed(self, widget, event):
    674         """Called when a mouse button is pressed on the flight list."""
    675         if event.type!=EVENT_BUTTON_PRESS or event.button!=3:
    676             return
    677 
    678         (path, _, _, _) = self._flightList.get_path_at_pos(int(event.x),
    679                                                            int(event.y))
    680         selection = self._flightList.get_selection()
    681         selection.unselect_all()
    682         selection.select_path(path)
    683 
    684         menu = self._getListPopupMenu()
    685         if pygobject:
    686             menu.popup(None, None, None, None, event.button, event.time)
    687         else:
    688             menu.popup(None, None, None, event.button, event.time)
    689639
    690640    def _updateDepartureGate(self):
     
    786736        return dialog
    787737
    788     def _getListPopupMenu(self):
     738    def _createListPopupMenu(self):
    789739        """Get the flight list popup menu."""
    790         if self._flightListPopupMenu is None:
    791             menu = gtk.Menu()
    792 
    793             menuItem = gtk.MenuItem()
    794             menuItem.set_label(xstr("flightsel_popup_select"))
    795             menuItem.set_use_underline(True)
    796             menuItem.connect("activate", self._popupSelect)
    797             menuItem.show()
    798 
    799             menu.append(menuItem)
    800 
    801             menuItem = gtk.MenuItem()
    802             menuItem.set_label(xstr("flightsel_popup_save"))
    803             menuItem.set_use_underline(True)
    804             menuItem.connect("activate", self._popupSave)
    805             menuItem.show()
    806 
    807             menu.append(menuItem)
    808 
    809             self._flightListPopupMenu = menu
    810 
    811         return self._flightListPopupMenu
     740        menu = gtk.Menu()
     741
     742        menuItem = gtk.MenuItem()
     743        menuItem.set_label(xstr("flightsel_popup_select"))
     744        menuItem.set_use_underline(True)
     745        menuItem.connect("activate", self._popupSelect)
     746        menuItem.show()
     747
     748        menu.append(menuItem)
     749
     750        menuItem = gtk.MenuItem()
     751        menuItem.set_label(xstr("flightsel_popup_save"))
     752        menuItem.set_use_underline(True)
     753        menuItem.connect("activate", self._popupSave)
     754        menuItem.show()
     755
     756        menu.append(menuItem)
     757
     758        return menu
    812759
    813760    def _popupSelect(self, menuItem):
Note: See TracChangeset for help on using the changeset viewer.