source: src/mlx/gui/delaycodes.py@ 437:750a9bfbc6dd

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

The main delay code handling logic is present (re #154)

File size: 14.2 KB
Line 
1# Module to handle the GUI aspects of the table of the delay codes
2
3#------------------------------------------------------------------------------
4
5from mlx.gui.common import *
6
7import mlx.const as const
8
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)
104 self._treeView.set_hadjustment(value)
105 else:
106 raise AttributeError("mlx.gui.delaycodes.DelayCodeTableBase: property %s is not handled in do_set_property" %
107 (prop.name,))
108
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
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)
156 self._treeView.set_hadjustment(hAdjustment)
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
164 def setStyle(self):
165 """Set the style of the event box from the treeview."""
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])
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
187class CheckButton(gtk.CheckButton):
188 """A check button that contains a reference to a row in the delay code
189 data table."""
190 def __init__(self, delayCodeRow):
191 """Construct the check button."""
192 super(CheckButton, self).__init__()
193 self.delayCodeRow = delayCodeRow
194
195#------------------------------------------------------------------------------
196
197CAPTION = 1
198
199DELAYCODE = 2
200
201_data1 = ( lambda row: row[0].strip(),
202 ["Num", "Code", "Title", "Description"],
203 [ (CAPTION, "Others"),
204 (DELAYCODE, (" 6", "OA ", "NO GATES/STAND AVAILABLE",
205 "Due to own airline activity")),
206 (DELAYCODE, ("9", "SG", "SCHEDULED GROUND TIME",
207 "Planned turnaround time less than declared minimum")),
208 (CAPTION, "Passenger and baggage"),
209 (DELAYCODE, ("11", "PD", "LATE CHECK-IN",
210 "Check-in reopened for late passengers")),
211 (DELAYCODE, ("12", "PL", "LATE CHECK-IN",
212 "Check-in not completed by flight closure time")),
213 (DELAYCODE, ("13", "PE", "CHECK-IN ERROR",
214 "Error with passenger or baggage details")) ] )
215
216_data2 = ( lambda row: row[0].strip(),
217 ["MA", "IATA", "Description"],
218 [ (CAPTION, "Passenger and baggage"),
219 (DELAYCODE, (" 012", "01 ",
220 "Late shipping of parts and/or materials")),
221 (DELAYCODE, (" 111", "11",
222 "Check-in reopened for late passengers")),
223 (DELAYCODE, (" 121", "12",
224 "Check-in not completed by flight closure time")),
225 (DELAYCODE, (" 132", "13",
226 "Error with passenger or baggage details"))
227 ])
228
229#------------------------------------------------------------------------------
230
231class DelayCodeTable(DelayCodeTableBase):
232 """The delay code table."""
233 def __init__(self):
234 """Construct the delay code table."""
235 super(DelayCodeTable, self).__init__()
236
237 self._delayCodeData = None
238
239 self._treeView = None
240
241 self._treeView = gtk.TreeView(gtk.ListStore(str, str))
242 self._treeView.set_rules_hint(True)
243
244 self.pack_start(self._treeView, False, False, 0)
245
246 self._alignments = []
247 self._checkButtons = []
248
249 self._eventBox = gtk.EventBox()
250
251 self._table = None
252
253 self._viewport = self._createViewport()
254 self._viewport.add(self._eventBox)
255 self._viewport.set_shadow_type(SHADOW_NONE)
256
257 self.pack_start(self._viewport, True, True, 0)
258
259 self._previousWidth = 0
260
261 @property
262 def delayCodes(self):
263 """Get a list of the delay codes checked by the user."""
264 codes = []
265
266 if self._delayCodeData is not None:
267 codeExtractor = self._delayCodeData[0]
268 for checkButton in self._checkButtons:
269 if checkButton.get_active():
270 codes.append(codeExtractor(checkButton.delayCodeRow))
271
272 return codes
273
274 def allocate_column_sizes(self, allocation):
275 """Allocate the column sizes."""
276 if allocation.width!=self._previousWidth:
277 self._previousWidth = allocation.width
278 index = 0
279 for alignment in self._alignments:
280 column = self._treeView.get_column(index)
281 width = alignment.allocatedWidth + (8 if index==0 else 16)
282 column.set_fixed_width(width)
283 index += 1
284
285 def setType(self, aircraftType):
286 """Setup the delay code table according to the given aircraft type."""
287 if aircraftType==const.AIRCRAFT_B736:
288 data = _data1
289 else:
290 data = _data2
291
292 self._delayCodeData = data
293
294 columns = self._treeView.get_columns()
295 for column in columns:
296 self._treeView.remove_column(column)
297
298 (_extractor, headers, rows) = data
299 numColumns = len(headers) + 1
300 numRows = len(rows)
301
302 column = gtk.TreeViewColumn("", gtk.CellRendererText())
303 column.set_sizing(TREE_VIEW_COLUMN_FIXED)
304 self._treeView.append_column(column)
305
306 for header in headers:
307 column = gtk.TreeViewColumn(header, gtk.CellRendererText())
308 column.set_sizing(TREE_VIEW_COLUMN_FIXED)
309 self._treeView.append_column(column)
310
311 self._table = gtk.Table(numRows, numColumns)
312 self._table.set_homogeneous(False)
313 self._table.set_col_spacings(16)
314 self._table.set_row_spacings(4)
315 self._eventBox.add(self._table)
316
317 self._alignments = []
318 self._checkButtons = []
319
320 firstDelayCodeRow = True
321 for i in range(0, numRows):
322 (type, elements) = rows[i]
323 if type==CAPTION:
324 alignment = gtk.Alignment(xalign = 0.0, yalign = 0.5,
325 xscale = 1.0)
326 label = gtk.Label("<b>" + elements + "</b>")
327 label.set_use_markup(True)
328 label.set_alignment(0.0, 0.5)
329 alignment.add(label)
330 self._table.attach(alignment, 1, numColumns, i, i+1)
331 self._table.set_row_spacing(i, 8)
332 elif type==DELAYCODE:
333 checkButton = CheckButton(elements)
334 self._checkButtons.append(checkButton)
335 alignment = Alignment(xalign = 0.5, yalign = 0.5, xscale = 1.0)
336 alignment.add(checkButton)
337 self._table.attach(alignment, 0, 1, i, i+1)
338 if firstDelayCodeRow:
339 self._alignments.append(alignment)
340
341 for j in range(0, len(elements)):
342 label = gtk.Label(elements[j])
343 label.set_alignment(1.0 if j==0 else 0.0, 0.5)
344 alignment = Alignment(xalign = 0.5, yalign = 0.5,
345 xscale = 1.0)
346 alignment.add(label)
347 self._table.attach(alignment, j+1, j+2, i, i+1)
348 if firstDelayCodeRow:
349 self._alignments.append(alignment)
350 firstDelayCodeRow = False
351
352 self._previousWidth = 0
353 self.show_all()
354
355 def reset(self):
356 """Reset the delay code table."""
357 columns = self._treeView.get_columns()
358 for column in columns:
359 self._treeView.remove_column(column)
360 self._eventBox.remove(self._table)
361 self._table = None
362 self.show_all()
363
364#------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.