source: src/mlx/gui/delaycodes.py@ 994:e71fbf2ee978

python3
Last change on this file since 994:e71fbf2ee978 was 994:e71fbf2ee978, checked in by István Váradi <ivaradi@…>, 5 years ago

Removed conditions on pygobject (re #347)

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