source: src/mlx/gui/flightlist.py@ 814:6886acd6bcd5

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

Columns can be sorted in the flight list (re #307).

File size: 6.2 KB
Line 
1# A widget which is a generic list of flights
2
3#-----------------------------------------------------------------------------
4
5from mlx.gui.common import *
6
7#-----------------------------------------------------------------------------
8
9class 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
56class 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 def clear(self):
121 """Clear the model."""
122 self._model.clear()
123
124 def addFlight(self, flight):
125 """Add the given booked flight."""
126 values = [self._model.iter_n_children(None)]
127 for columnDescriptor in self._columnDescriptors:
128 values.append(columnDescriptor.getValueFrom(flight))
129 self._model.append(values)
130
131 def _rowActivated(self, flightList, path, column):
132 """Called when a row is selected."""
133 self.emit("row-activated", self.selectedIndex)
134
135 def _buttonPressEvent(self, widget, event):
136 """Called when a mouse button is pressed or released."""
137 if event.type!=EVENT_BUTTON_PRESS or event.button!=3:
138 return
139
140 (path, _, _, _) = self._view.get_path_at_pos(int(event.x),
141 int(event.y))
142 selection = self._view.get_selection()
143 selection.unselect_all()
144 selection.select_path(path)
145
146 if self._popupMenu is None:
147 self._popupMenu = self._popupMenuProducer()
148 menu = self._popupMenu
149 if pygobject:
150 menu.popup(None, None, None, None, event.button, event.time)
151 else:
152 menu.popup(None, None, None, event.button, event.time)
153
154 def _selectionChanged(self, selection):
155 """Called when the selection has changed."""
156 self.emit("selection-changed", self.selectedIndex)
157
158#-------------------------------------------------------------------------------
159
160gobject.signal_new("row-activated", FlightList, gobject.SIGNAL_RUN_FIRST,
161 None, (int,))
162
163gobject.signal_new("selection-changed", FlightList, gobject.SIGNAL_RUN_FIRST,
164 None, (object,))
165
166#-----------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.