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(),
|
---|
13 | extraColumnAttributes = None, sortable = False):
|
---|
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
|
---|
21 | self._sortable = sortable
|
---|
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)
|
---|
42 | if self._sortable:
|
---|
43 | column.set_sort_column_id(index)
|
---|
44 | column.set_sort_indicator(True)
|
---|
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 |
|
---|
69 | types = [int]
|
---|
70 | for columnDescriptor in self._columnDescriptors:
|
---|
71 | columnDescriptor.appendType(types)
|
---|
72 |
|
---|
73 | self._model = gtk.ListStore(*types)
|
---|
74 | self._model.set_sort_column_id(2, SORT_ASCENDING)
|
---|
75 | self._view = gtk.TreeView(self._model)
|
---|
76 |
|
---|
77 | flightIndexColumn = gtk.TreeViewColumn()
|
---|
78 | flightIndexColumn.set_visible(False)
|
---|
79 | self._view.append_column(flightIndexColumn)
|
---|
80 |
|
---|
81 | index = 1
|
---|
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:
|
---|
117 | index = model.get_value(iter, 0)
|
---|
118 | return index
|
---|
119 |
|
---|
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 |
|
---|
125 | def clear(self):
|
---|
126 | """Clear the model."""
|
---|
127 | self._model.clear()
|
---|
128 |
|
---|
129 | def addFlight(self, flight):
|
---|
130 | """Add the given booked flight."""
|
---|
131 | values = [self._model.iter_n_children(None)]
|
---|
132 | for columnDescriptor in self._columnDescriptors:
|
---|
133 | values.append(columnDescriptor.getValueFrom(flight))
|
---|
134 | self._model.append(values)
|
---|
135 |
|
---|
136 | def _rowActivated(self, flightList, path, column):
|
---|
137 | """Called when a row is selected."""
|
---|
138 | self.emit("row-activated", self.selectedIndex)
|
---|
139 |
|
---|
140 | def _buttonPressEvent(self, widget, event):
|
---|
141 | """Called when a mouse button is pressed or released."""
|
---|
142 | if event.type!=EVENT_BUTTON_PRESS or event.button!=3:
|
---|
143 | return
|
---|
144 |
|
---|
145 | (path, _, _, _) = self._view.get_path_at_pos(int(event.x),
|
---|
146 | int(event.y))
|
---|
147 | selection = self._view.get_selection()
|
---|
148 | selection.unselect_all()
|
---|
149 | selection.select_path(path)
|
---|
150 |
|
---|
151 | if self._popupMenu is None:
|
---|
152 | self._popupMenu = self._popupMenuProducer()
|
---|
153 | menu = self._popupMenu
|
---|
154 | if pygobject:
|
---|
155 | menu.popup(None, None, None, None, event.button, event.time)
|
---|
156 | else:
|
---|
157 | menu.popup(None, None, None, event.button, event.time)
|
---|
158 |
|
---|
159 | def _selectionChanged(self, selection):
|
---|
160 | """Called when the selection has changed."""
|
---|
161 | self.emit("selection-changed", self.selectedIndex)
|
---|
162 |
|
---|
163 | #-------------------------------------------------------------------------------
|
---|
164 |
|
---|
165 | gobject.signal_new("row-activated", FlightList, gobject.SIGNAL_RUN_FIRST,
|
---|
166 | None, (int,))
|
---|
167 |
|
---|
168 | gobject.signal_new("selection-changed", FlightList, gobject.SIGNAL_RUN_FIRST,
|
---|
169 | None, (object,))
|
---|
170 |
|
---|
171 | #-----------------------------------------------------------------------------
|
---|