[811] | 1 | # A widget which is a generic list of flights
|
---|
| 2 |
|
---|
| 3 | #-----------------------------------------------------------------------------
|
---|
| 4 |
|
---|
| 5 | from mlx.gui.common import *
|
---|
| 6 |
|
---|
| 7 | #-----------------------------------------------------------------------------
|
---|
| 8 |
|
---|
| 9 | class ColumnDescriptor(object):
|
---|
| 10 | """A descriptor for a column in the list."""
|
---|
| 11 | def __init__(self, attribute, heading, type = str,
|
---|
| 12 | convertFn = None, renderer = gtk.CellRendererText(),
|
---|
[814] | 13 | extraColumnAttributes = None, sortable = False):
|
---|
[811] | 14 | """Construct the descriptor."""
|
---|
| 15 | self._attribute = attribute
|
---|
| 16 | self._heading = heading
|
---|
| 17 | self._type = type
|
---|
| 18 | self._convertFn = convertFn
|
---|
| 19 | self._renderer = renderer
|
---|
| 20 | self._extraColumnAttributes = extraColumnAttributes
|
---|
[814] | 21 | self._sortable = sortable
|
---|
[811] | 22 |
|
---|
| 23 | def appendType(self, types):
|
---|
| 24 | """Append the type of this column to the given list of types."""
|
---|
| 25 | types.append(self._type)
|
---|
| 26 |
|
---|
| 27 | def getViewColumn(self, index):
|
---|
| 28 | """Get a new column object for a tree view.
|
---|
| 29 |
|
---|
| 30 | @param index is the 0-based index of the column."""
|
---|
| 31 | if self._extraColumnAttributes is None:
|
---|
| 32 | if isinstance(self._renderer, gtk.CellRendererText):
|
---|
| 33 | extraColumnAttributes = {"text" : index}
|
---|
| 34 | else:
|
---|
| 35 | extraColumnAttributes = {}
|
---|
| 36 | else:
|
---|
| 37 | extraColumnAttributes = self._extraColumnAttributes
|
---|
| 38 |
|
---|
| 39 | column = gtk.TreeViewColumn(self._heading, self._renderer,
|
---|
| 40 | text = index)
|
---|
| 41 | column.set_expand(True)
|
---|
[814] | 42 | if self._sortable:
|
---|
| 43 | column.set_sort_column_id(index)
|
---|
| 44 | column.set_sort_indicator(True)
|
---|
[811] | 45 |
|
---|
| 46 | return column
|
---|
| 47 |
|
---|
| 48 | def getValueFrom(self, flight):
|
---|
| 49 | """Get the value from the given flight."""
|
---|
| 50 | value = getattr(flight, self._attribute)
|
---|
| 51 | return self._type(value) if self._convertFn is None \
|
---|
| 52 | else self._convertFn(value)
|
---|
| 53 |
|
---|
| 54 | #-----------------------------------------------------------------------------
|
---|
| 55 |
|
---|
| 56 | class FlightList(gtk.Alignment):
|
---|
| 57 | """Construct the flight list.
|
---|
| 58 |
|
---|
| 59 | This is a complete widget with a scroll window. It is alignment centered
|
---|
| 60 | horizontally and expandable vertically."""
|
---|
| 61 | def __init__(self, columnDescriptors, popupMenuProducer = None,
|
---|
| 62 | widthRequest = None):
|
---|
| 63 | """Construct the flight list with the given column descriptors."""
|
---|
| 64 |
|
---|
| 65 | self._columnDescriptors = columnDescriptors
|
---|
| 66 | self._popupMenuProducer = popupMenuProducer
|
---|
| 67 | self._popupMenu = None
|
---|
| 68 |
|
---|
[814] | 69 | types = [int]
|
---|
[811] | 70 | for columnDescriptor in self._columnDescriptors:
|
---|
| 71 | columnDescriptor.appendType(types)
|
---|
| 72 |
|
---|
| 73 | self._model = gtk.ListStore(*types)
|
---|
[814] | 74 | self._model.set_sort_column_id(2, SORT_ASCENDING)
|
---|
[811] | 75 | self._view = gtk.TreeView(self._model)
|
---|
| 76 |
|
---|
[814] | 77 | flightIndexColumn = gtk.TreeViewColumn()
|
---|
| 78 | flightIndexColumn.set_visible(False)
|
---|
| 79 | self._view.append_column(flightIndexColumn)
|
---|
| 80 |
|
---|
| 81 | index = 1
|
---|
[811] | 82 | for columnDescriptor in self._columnDescriptors:
|
---|
| 83 | column = columnDescriptor.getViewColumn(index)
|
---|
| 84 | self._view.append_column(column)
|
---|
| 85 | index += 1
|
---|
| 86 |
|
---|
| 87 | self._view.connect("row-activated", self._rowActivated)
|
---|
| 88 | self._view.connect("button-press-event", self._buttonPressEvent)
|
---|
| 89 |
|
---|
| 90 | selection = self._view.get_selection()
|
---|
| 91 | selection.connect("changed", self._selectionChanged)
|
---|
| 92 |
|
---|
| 93 | scrolledWindow = gtk.ScrolledWindow()
|
---|
| 94 | scrolledWindow.add(self._view)
|
---|
| 95 | if widthRequest is not None:
|
---|
| 96 | scrolledWindow.set_size_request(widthRequest, -1)
|
---|
| 97 | # FIXME: these should be constants in common.py
|
---|
| 98 | scrolledWindow.set_policy(gtk.PolicyType.AUTOMATIC if pygobject
|
---|
| 99 | else gtk.POLICY_AUTOMATIC,
|
---|
| 100 | gtk.PolicyType.AUTOMATIC if pygobject
|
---|
| 101 | else gtk.POLICY_AUTOMATIC)
|
---|
| 102 | scrolledWindow.set_shadow_type(gtk.ShadowType.IN if pygobject
|
---|
| 103 | else gtk.SHADOW_IN)
|
---|
| 104 |
|
---|
| 105 | super(FlightList, self).__init__(xalign = 0.5, yalign = 0.0,
|
---|
| 106 | xscale = 0.0, yscale = 1.0)
|
---|
| 107 | self.add(scrolledWindow)
|
---|
| 108 |
|
---|
| 109 | @property
|
---|
| 110 | def selectedIndex(self):
|
---|
| 111 | """Get the index of the selected entry, if any."""
|
---|
| 112 | selection = self._view.get_selection()
|
---|
| 113 | (model, iter) = selection.get_selected()
|
---|
| 114 | if iter is None:
|
---|
| 115 | return None
|
---|
| 116 | else:
|
---|
[814] | 117 | index = model.get_value(iter, 0)
|
---|
[811] | 118 | return index
|
---|
| 119 |
|
---|
[815] | 120 | @property
|
---|
| 121 | def hasFlights(self):
|
---|
| 122 | """Determine if there are any flights in the list."""
|
---|
| 123 | return self._model.get_iter_root() is not None
|
---|
| 124 |
|
---|
[811] | 125 | def clear(self):
|
---|
| 126 | """Clear the model."""
|
---|
| 127 | self._model.clear()
|
---|
| 128 |
|
---|
| 129 | def addFlight(self, flight):
|
---|
| 130 | """Add the given booked flight."""
|
---|
[814] | 131 | values = [self._model.iter_n_children(None)]
|
---|
[811] | 132 | for columnDescriptor in self._columnDescriptors:
|
---|
| 133 | values.append(columnDescriptor.getValueFrom(flight))
|
---|
| 134 | self._model.append(values)
|
---|
| 135 |
|
---|
[816] | 136 | def removeFlight(self, index):
|
---|
| 137 | """Remove the flight with the given index."""
|
---|
| 138 | model = self._model
|
---|
| 139 | idx = 0
|
---|
| 140 | iter = model.get_iter_first()
|
---|
| 141 | while iter is not None:
|
---|
| 142 | nextIter = model.iter_next(iter)
|
---|
| 143 | if model.get_value(iter, 0)==index:
|
---|
| 144 | model.remove(iter)
|
---|
| 145 | else:
|
---|
| 146 | model.set_value(iter, 0, idx)
|
---|
| 147 | idx += 1
|
---|
| 148 | iter = nextIter
|
---|
| 149 |
|
---|
[811] | 150 | def _rowActivated(self, flightList, path, column):
|
---|
| 151 | """Called when a row is selected."""
|
---|
| 152 | self.emit("row-activated", self.selectedIndex)
|
---|
| 153 |
|
---|
| 154 | def _buttonPressEvent(self, widget, event):
|
---|
| 155 | """Called when a mouse button is pressed or released."""
|
---|
[817] | 156 | if event.type!=EVENT_BUTTON_PRESS or event.button!=3 or \
|
---|
| 157 | self._popupMenuProducer is None:
|
---|
[811] | 158 | return
|
---|
| 159 |
|
---|
| 160 | (path, _, _, _) = self._view.get_path_at_pos(int(event.x),
|
---|
| 161 | int(event.y))
|
---|
| 162 | selection = self._view.get_selection()
|
---|
| 163 | selection.unselect_all()
|
---|
| 164 | selection.select_path(path)
|
---|
| 165 |
|
---|
| 166 | if self._popupMenu is None:
|
---|
| 167 | self._popupMenu = self._popupMenuProducer()
|
---|
| 168 | menu = self._popupMenu
|
---|
| 169 | if pygobject:
|
---|
| 170 | menu.popup(None, None, None, None, event.button, event.time)
|
---|
| 171 | else:
|
---|
| 172 | menu.popup(None, None, None, event.button, event.time)
|
---|
| 173 |
|
---|
| 174 | def _selectionChanged(self, selection):
|
---|
| 175 | """Called when the selection has changed."""
|
---|
| 176 | self.emit("selection-changed", self.selectedIndex)
|
---|
| 177 |
|
---|
| 178 | #-------------------------------------------------------------------------------
|
---|
| 179 |
|
---|
| 180 | gobject.signal_new("row-activated", FlightList, gobject.SIGNAL_RUN_FIRST,
|
---|
| 181 | None, (int,))
|
---|
| 182 |
|
---|
| 183 | gobject.signal_new("selection-changed", FlightList, gobject.SIGNAL_RUN_FIRST,
|
---|
| 184 | None, (object,))
|
---|
| 185 |
|
---|
| 186 | #-----------------------------------------------------------------------------
|
---|