source: src/mlx/gui/delaycodes.py@ 436:4c3e7b66ca89

Last change on this file since 436:4c3e7b66ca89 was 436:4c3e7b66ca89, checked in by István Váradi <ivaradi@…>, 11 years ago

Added support for setting up the delay code table according to the aircraft's type (re #154)

File size: 13.2 KB
RevLine 
[433]1# Module to handle the GUI aspects of the table of the delay codes
2
3#------------------------------------------------------------------------------
4
5from mlx.gui.common import *
6
[436]7import mlx.const as const
8
[433]9#------------------------------------------------------------------------------
10
11if pygobject:
12
13#------------------------------------------------------------------------------
14
15 class Viewport(gtk.Viewport):
16 """Viewport implementation that alleviates the problem with improper
17 resizing by the VBox."""
18 def __init__(self):
19 """Construct the viewport."""
20 gtk.Viewport.__init__(self)
21 self._recursive = False
22 self._vboxHeight = None
23
24 def setVBOXHeight(self, vboxHeight):
25 """Set the height of the VBox which will be used to calculate the
26 viewport's height."""
27 self._vboxHeight = vboxHeight
28
29 def do_size_allocate(self, allocation):
30 """Called when the viewport's size is allocated.
31
32 The height in the allocation object is modified so that it is only
33 so high to fit into the VBox."""
34 if self._vboxHeight is not None:
35 allocation.height = self._vboxHeight - allocation.y
36 self._vboxHeight = None
37 gtk.Viewport.do_size_allocate(self, allocation)
38
39 class DelayCodeTableBase(gtk.VBox, gtk.Scrollable):
40 """PyGObject-specific base class for the delay code table."""
41 __gproperties__ = {
42 "vscroll-policy" : ( gtk.ScrollablePolicy,
43 "vscroll-policy",
44 "The vertical scrolling policy",
45 gtk.ScrollablePolicy.MINIMUM,
46 gobject.PARAM_READWRITE ),
47 "vadjustment" : ( gtk.Adjustment,
48 "vadjustment",
49 "The vertical adjustment",
50 gobject.PARAM_READWRITE ),
51 "hscroll-policy" : ( gtk.ScrollablePolicy,
52 "hscroll-policy",
53 "The horizontal scrolling policy",
54 gtk.ScrollablePolicy.MINIMUM,
55 gobject.PARAM_READWRITE ),
56 "hadjustment" : ( gtk.Adjustment,
57 "hadjustment",
58 "The horizontal adjustment",
59 gobject.PARAM_READWRITE ) }
60
61
62 @staticmethod
63 def _createViewport():
64 """Create an instance of the viewport class used by this base class."""
65 return Viewport()
66
67 def __init__(self):
68 """Construct the delay code table."""
69 super(DelayCodeTableBase, self).__init__()
70
71 def do_size_allocate(self, allocation):
72 """Allocate the size for the table and its children.
73
74 This sets up the VBox height in the viewport and then calls the
75 do_size_allocate() function of VBox()."""
76 self._viewport.setVBOXHeight(allocation.height)
77 gtk.VBox.do_size_allocate(self, allocation)
78 self.allocate_column_sizes(allocation)
79
80 def do_get_property(self, prop):
81 """Get the value of one of the properties defined above.
82
83 The request is forwarded to the viewport."""
84 if prop.name=="vscroll-policy":
85 return self._viewport.get_vscroll_policy()
86 elif prop.name=="hscroll-policy":
87 return self._viewport.get_hscroll_policy()
88 elif prop.name=="vadjustment":
89 return self._viewport.get_vadjustment()
90 elif prop.name=="hadjustment":
91 return self._viewport.get_hadjustment()
92 else:
93 raise AttributeError("mlx.gui.delaycodes.DelayCodeTableBase: property %s is not handled in do_get_property" %
94 (prop.name,))
95
96 def do_set_property(self, prop, value):
97 """Set the value of the adjustment properties defined above.
98
99 The adjustments are forwarded to the viewport."""
100 if prop.name=="vadjustment":
101 self._viewport.set_vadjustment(value)
102 elif prop.name=="hadjustment":
103 self._viewport.set_hadjustment(value)
[435]104 self._treeView.set_hadjustment(value)
[433]105 else:
106 raise AttributeError("mlx.gui.delaycodes.DelayCodeTableBase: property %s is not handled in do_set_property" %
107 (prop.name,))
108
[435]109 def setStyle(self):
110 """Set the style of the event box from the treeview."""
111
112 class Alignment(gtk.Alignment):
113 """An alignment that remembers the width it was given."""
114 def __init__(self, xalign = 0.0, yalign=0.0,
115 xscale = 0.0, yscale = 0.0 ):
116 """Construct the alignment."""
117 super(Alignment, self).__init__(xalign = xalign, yalign = yalign,
118 xscale = xscale, yscale = yscale)
119 self.allocatedWidth = 0
120
121 def do_size_allocate(self, allocation):
122 """Called with the new size allocation."""
123 self.allocatedWidth = allocation.width
124 gtk.Alignment.do_size_allocate(self, allocation)
125
[433]126#------------------------------------------------------------------------------
127
128else: # pygobject
129
130#------------------------------------------------------------------------------
131
132 class DelayCodeTableBase (gtk.VBox):
133 """Base class of the delay code table for PyGtk."""
134
135 __gsignals__ = {
136 "set-scroll-adjustments": (
137 gobject.SIGNAL_RUN_LAST,
138 gobject.TYPE_NONE, (gtk.Adjustment, gtk.Adjustment))
139 }
140
141 @staticmethod
142 def _createViewport():
143 """Create an instance of the viewport class used by this base class."""
144 return gtk.Viewport()
145
146 def __init__(self):
147 """Construct the base class."""
148 super(DelayCodeTableBase, self).__init__()
149 self.set_set_scroll_adjustments_signal("set-scroll-adjustments")
150 self.connect("size-allocate", self._do_size_allocate)
151
152 def do_set_scroll_adjustments(self, hAdjustment, vAdjustment):
153 """Set the adjustments on the viewport."""
154 self._viewport.set_hadjustment(hAdjustment)
155 self._viewport.set_vadjustment(vAdjustment)
[435]156 self._treeView.set_hadjustment(hAdjustment)
[433]157
158 def _do_size_allocate(self, widget, allocation):
159 """Handler of the size-allocate signal.
160
161 Calls allocate_column_sizes()."""
162 self.allocate_column_sizes(allocation)
163
[435]164 def setStyle(self):
165 """Set the style of the event box from the treeview."""
[436]166 if self._treeView is not None:
167 style = self._treeView.rc_get_style()
168 self._eventBox.modify_bg(0, style.bg[2])
169 self._eventBox.modify_fg(0, style.fg[2])
[435]170
171 class Alignment(gtk.Alignment):
172 """An alignment that remembers the width it was given."""
173 def __init__(self, xalign = 0.0, yalign=0.0,
174 xscale = 0.0, yscale = 0.0 ):
175 """Construct the alignment."""
176 super(Alignment, self).__init__(xalign = xalign, yalign = yalign,
177 xscale = xscale, yscale = yscale)
178 self.allocatedWidth = 0
179 self.connect("size-allocate", self._do_size_allocate)
180
181 def _do_size_allocate(self, widget, allocation):
182 """Called with the new size allocation."""
183 self.allocatedWidth = allocation.width
184
185#------------------------------------------------------------------------------
186
187CAPTION = 1
188
189DELAYCODE = 2
190
[436]191_data1 = ( ["Num", "Code", "Title", "Description"],
192 [ (CAPTION, "Others"),
193 (DELAYCODE, (" 6", "OA ", "NO GATES/STAND AVAILABLE",
194 "Due to own airline activity")),
195 (DELAYCODE, ("9", "SG", "SCHEDULED GROUND TIME",
196 "Planned turnaround time less than declared minimum")),
197 (CAPTION, "Passenger and baggage"),
198 (DELAYCODE, ("11", "PD", "LATE CHECK-IN",
199 "Check-in reopened for late passengers")),
200 (DELAYCODE, ("12", "PL", "LATE CHECK-IN",
201 "Check-in not completed by flight closure time")),
202 (DELAYCODE, ("13", "PE", "CHECK-IN ERROR",
203 "Error with passenger or baggage details")) ] )
204
205_data2 = ( ["MA", "IATA", "Description"],
206 [ (CAPTION, "Passenger and baggage"),
207 (DELAYCODE, (" 012", "01",
208 "Late shipping of parts and/or materials")),
209 (DELAYCODE, (" 111", "11",
210 "Check-in reopened for late passengers")),
211 (DELAYCODE, (" 121", "12",
212 "Check-in not completed by flight closure time")),
213 (DELAYCODE, (" 132", "13",
214 "Error with passenger or baggage details"))
215 ])
[435]216
[433]217#------------------------------------------------------------------------------
218
219class DelayCodeTable(DelayCodeTableBase):
220 """The delay code table."""
221 def __init__(self):
222 """Construct the delay code table."""
223 super(DelayCodeTable, self).__init__()
224
[436]225 self._treeView = None
[435]226
227 self._listStore = gtk.ListStore(str, str)
[433]228 self._treeView = gtk.TreeView(self._listStore)
[435]229 self._treeView.set_rules_hint(True)
230
[436]231 self.pack_start(self._treeView, False, False, 0)
232
233 self._alignments = []
234
235 self._eventBox = gtk.EventBox()
236
237 self._table = None
238
239 self._viewport = self._createViewport()
240 self._viewport.add(self._eventBox)
241 self._viewport.set_shadow_type(SHADOW_NONE)
242
243 self.pack_start(self._viewport, True, True, 0)
244
245 self._previousWidth = 0
246
247 def allocate_column_sizes(self, allocation):
248 """Allocate the column sizes."""
249 if allocation.width!=self._previousWidth:
250 self._previousWidth = allocation.width
251 index = 0
252 for alignment in self._alignments:
253 column = self._treeView.get_column(index)
254 width = alignment.allocatedWidth + (8 if index==0 else 16)
255 column.set_fixed_width(width)
256 index += 1
257
258 def setType(self, aircraftType):
259 """Setup the delay code table according to the given aircraft type."""
260 if aircraftType==const.AIRCRAFT_B736:
261 data = _data1
262 else:
263 data = _data2
264
265 columns = self._treeView.get_columns()
266 for column in columns:
267 self._treeView.remove_column(column)
268
269 (headers, rows) = data
270 numColumns = len(headers) + 1
271 numRows = len(rows)
272
[435]273 column = gtk.TreeViewColumn("", gtk.CellRendererText())
[433]274 column.set_sizing(TREE_VIEW_COLUMN_FIXED)
275 self._treeView.append_column(column)
276
[435]277 for header in headers:
278 column = gtk.TreeViewColumn(header, gtk.CellRendererText())
279 column.set_sizing(TREE_VIEW_COLUMN_FIXED)
280 self._treeView.append_column(column)
281
282 self._table = gtk.Table(numRows, numColumns)
283 self._table.set_homogeneous(False)
284 self._table.set_col_spacings(16)
285 self._table.set_row_spacings(4)
[436]286 self._eventBox.add(self._table)
287
288 self._alignments = []
[435]289
290 firstDelayCodeRow = True
291 for i in range(0, numRows):
292 (type, elements) = rows[i]
293 if type==CAPTION:
294 alignment = gtk.Alignment(xalign = 0.0, yalign = 0.5,
295 xscale = 1.0)
296 label = gtk.Label("<b>" + elements + "</b>")
297 label.set_use_markup(True)
298 label.set_alignment(0.0, 0.5)
299 alignment.add(label)
300 self._table.attach(alignment, 1, numColumns, i, i+1)
301 self._table.set_row_spacing(i, 8)
302 elif type==DELAYCODE:
303 alignment = Alignment(xalign = 0.5, yalign = 0.5, xscale = 1.0)
304 alignment.add(gtk.CheckButton())
305 self._table.attach(alignment, 0, 1, i, i+1)
306 if firstDelayCodeRow:
307 self._alignments.append(alignment)
308
309 for j in range(0, len(elements)):
310 label = gtk.Label(elements[j])
311 label.set_alignment(1.0 if j==0 else 0.0, 0.5)
312 alignment = Alignment(xalign = 0.5, yalign = 0.5,
313 xscale = 1.0)
314 alignment.add(label)
315 self._table.attach(alignment, j+1, j+2, i, i+1)
316 if firstDelayCodeRow:
317 self._alignments.append(alignment)
318 firstDelayCodeRow = False
[433]319
320 self._previousWidth = 0
[436]321 self.show_all()
[433]322
[436]323 def reset(self):
324 """Reset the delay code table."""
325 columns = self._treeView.get_columns()
326 for column in columns:
327 self._treeView.remove_column(column)
328 self._eventBox.remove(self._table)
329 self._table = None
330 self.show_all()
[433]331
332#------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.