source: src/mlx/gui/weighthelp.py@ 1010:e2b76a41d9ca

python3
Last change on this file since 1010:e2b76a41d9ca was 996:8035d80d5feb, checked in by István Váradi <ivaradi@…>, 5 years ago

Using 'Gtk' instead of 'gtk' (re #347)

File size: 18.1 KB
Line 
1
2from mlx.gui.common import *
3
4from mlx.i18n import xstr
5from mlx.checks import PayloadChecker
6
7#-------------------------------------------------------------------------------
8
9## @package mlx.gui.weighthelp
10#
11# The weight calculation help tab.
12#
13# This module implements the tab containing the weight calculation help.
14
15#-------------------------------------------------------------------------------
16
17class WeightHelp(Gtk.VBox):
18 """The weight calculation help tab."""
19 @staticmethod
20 def _getMarkup(value, expectedValue = None, tolerance = None):
21 """Get the markup for the given value.
22
23 If it is too much different from the expected value, it will be
24 colored yellow (if within the tolerance), or red (if out of the tolerance)."""
25 markup = "%.0f" % (value,)
26 if expectedValue is not None and tolerance is not None:
27 colour = None
28 diff = abs(value - expectedValue)
29 if diff>tolerance: colour = "red"
30 elif (diff*10)>=tolerance: colour = "orange"
31 else: colour = "darkgreen"
32 if colour is not None:
33 markup = '<span foreground="' + colour + '">' + markup + '</span>'
34 return markup
35
36 def __init__(self, gui):
37 """Construct the tab."""
38 super(WeightHelp, self).__init__()
39
40 self._gui = gui
41
42 mainAlignment = Gtk.Alignment(xalign = 0.5, yalign = 0.5,
43 xscale = 1.0, yscale = 1.0)
44 mainAlignment.set_padding(padding_top = 4, padding_bottom = 4,
45 padding_left = 12, padding_right = 12)
46 self.add(mainAlignment)
47
48 self._mainBox = mainBox = Gtk.VBox()
49 mainAlignment.add(mainBox)
50
51 self._usingHelp = Gtk.CheckButton(xstr("weighthelp_usinghelp"))
52 self._usingHelp.set_use_underline(True)
53 self._usingHelp.set_tooltip_text(xstr("weighthelp_usinghelp_tooltip"))
54 self._usingHelp.connect("toggled", self._usingHelpToggled)
55 mainBox.pack_start(self._usingHelp, False, False, 4)
56
57
58 self._weightsTable = table = Gtk.Table(16, 5)
59 table.set_homogeneous(False)
60 table.set_row_spacings(4)
61 table.set_col_spacings(16)
62 alignment = Gtk.Alignment(xalign = 0.5, yalign = 0.5,
63 xscale = 0.0, yscale = 0.0)
64 alignment.add(table)
65 mainBox.pack_start(alignment, True, True, 4)
66
67 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.0,
68 xscale = 0.0, yscale = 0.0)
69 alignment.set_padding(padding_bottom = 16, padding_top = 0,
70 padding_left = 0, padding_right = 0)
71 label = Gtk.Label(xstr("weighthelp_header_calculated"))
72 label.set_use_markup(True)
73 # FIXME: should be a constant in common
74 label.set_justify(Gtk.Justification.CENTER)
75 alignment.add(label)
76 table.attach(alignment, 1, 2, 0, 1)
77
78 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.0,
79 xscale = 0.0, yscale = 0.0)
80 alignment.set_padding(padding_bottom = 16, padding_top = 0,
81 padding_left = 0, padding_right = 0)
82 button = Gtk.Button(xstr("weighthelp_header_simulator"))
83 button.set_tooltip_markup(xstr("weighthelp_header_simulator_tooltip"))
84 button.connect("clicked", self._fsButtonClicked)
85 label = button.get_child()
86 label.set_justify(Gtk.Justification.CENTER)
87 alignment.add(button)
88 table.attach(alignment, 3, 4, 0, 1)
89
90
91 self._crewLabel = Gtk.Label(xstr("weighthelp_crew") % ("99",))
92 alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.5,
93 xscale = 0.0, yscale = 0.0)
94 alignment.add(self._crewLabel)
95 table.attach(alignment, 0, 1, 1, 2)
96
97 self._crewWeight = Gtk.Label("0")
98 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
99 xscale = 0.0, yscale = 0.0)
100 alignment.add(self._crewWeight)
101 table.attach(alignment, 1, 2, 1, 2)
102
103 table.attach(Gtk.Label("kg"), 2, 3, 1, 2)
104
105 text = xstr("weighthelp_pax") % ("999",)
106 self._paxLabel = Gtk.Label(text)
107 self._paxLabel.set_width_chars(len(text))
108 self._paxLabel.set_alignment(0.0, 0.5)
109 alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.5,
110 xscale = 0.0, yscale = 0.0)
111 alignment.add(self._paxLabel)
112 table.attach(alignment, 0, 1, 2, 3)
113
114 self._paxWeight = Gtk.Label("20000")
115 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
116 xscale = 0.0, yscale = 0.0)
117 alignment.add(self._paxWeight)
118 table.attach(alignment, 1, 2, 2, 3)
119
120 table.attach(Gtk.Label("kg"), 2, 3, 2, 3)
121
122 label = Gtk.Label(xstr("weighthelp_baggage"))
123 alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.5,
124 xscale = 0.0, yscale = 0.0)
125 alignment.add(label)
126 table.attach(alignment, 0, 1, 3, 4)
127
128 self._bagWeight = Gtk.Label("2000")
129 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
130 xscale = 0.0, yscale = 0.0)
131 alignment.add(self._bagWeight)
132 table.attach(alignment, 1, 2, 3, 4)
133
134 table.attach(Gtk.Label("kg"), 2, 3, 3, 4)
135
136 label = Gtk.Label(xstr("weighthelp_cargo"))
137 alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.5,
138 xscale = 0.0, yscale = 0.0)
139 alignment.add(label)
140 table.attach(alignment, 0, 1, 4, 5)
141
142 self._cargoWeight = Gtk.Label("2000")
143 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
144 xscale = 0.0, yscale = 0.0)
145 alignment.add(self._cargoWeight)
146 table.attach(alignment, 1, 2, 4, 5)
147
148 table.attach(Gtk.Label("kg"), 2, 3, 4, 5)
149
150 label = Gtk.Label(xstr("weighthelp_mail"))
151 alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.5,
152 xscale = 0.0, yscale = 0.0)
153 alignment.add(label)
154 table.attach(alignment, 0, 1, 5, 6)
155
156 self._mailWeight = Gtk.Label("2000")
157 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
158 xscale = 0.0, yscale = 0.0)
159 alignment.add(self._mailWeight)
160 table.attach(alignment, 1, 2, 5, 6)
161
162 table.attach(Gtk.Label("kg"), 2, 3, 5, 6)
163
164 table.attach(Gtk.HSeparator(), 1, 2, 6, 7)
165
166 label = Gtk.Label("<b>" + xstr("weighthelp_payload") + "</b>")
167 label.set_use_markup(True)
168 alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.5,
169 xscale = 0.0, yscale = 0.0)
170 alignment.add(label)
171 table.attach(alignment, 0, 1, 7, 8)
172
173 self._payload = Gtk.Label("<b>32000</b>")
174 self._payload.set_use_markup(True)
175 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
176 xscale = 0.0, yscale = 0.0)
177 alignment.add(self._payload)
178 table.attach(alignment, 1, 2, 7, 8)
179
180 table.attach(Gtk.Label("kg"), 2, 3, 7, 8)
181
182 self._fsPayload = Gtk.Label("<b>32001</b>")
183 self._fsPayload.set_use_markup(True)
184 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
185 xscale = 0.0, yscale = 0.0)
186 alignment.add(self._fsPayload)
187 table.attach(alignment, 3, 4, 7, 8)
188
189 table.attach(Gtk.Label("kg"), 4, 5, 7, 8)
190
191 label = Gtk.Label(xstr("weighthelp_dow"))
192 label.set_use_markup(True)
193 alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.5,
194 xscale = 0.0, yscale = 0.0)
195 alignment.add(label)
196 table.attach(alignment, 0, 1, 8, 9)
197
198 self._dow = Gtk.Label("35000")
199 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
200 xscale = 0.0, yscale = 0.0)
201 alignment.add(self._dow)
202 table.attach(alignment, 1, 2, 8, 9)
203
204 table.attach(Gtk.Label("kg"), 2, 3, 8, 9)
205
206 self._fsDOW = Gtk.Label("33012")
207 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
208 xscale = 0.0, yscale = 0.0)
209 alignment.add(self._fsDOW)
210 table.attach(alignment, 3, 4, 8, 9)
211
212 table.attach(Gtk.Label("kg"), 4, 5, 8, 9)
213
214 table.attach(Gtk.HSeparator(), 1, 2, 9, 10)
215
216 table.attach(Gtk.HSeparator(), 3, 4, 9, 10)
217
218 label = Gtk.Label("<b>" + xstr("weighthelp_zfw") + "</b>")
219 label.set_use_markup(True)
220 alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.5,
221 xscale = 0.0, yscale = 0.0)
222 alignment.add(label)
223 table.attach(alignment, 0, 1, 10, 11)
224
225 self._zfw = Gtk.Label("<b>122000</b>")
226 self._zfw.set_use_markup(True)
227 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
228 xscale = 0.0, yscale = 0.0)
229 alignment.add(self._zfw)
230 table.attach(alignment, 1, 2, 10, 11)
231
232 table.attach(Gtk.Label("kg"), 2, 3, 10, 11)
233
234 self._fsZFW = Gtk.Label("<b>124000</b>")
235 self._fsZFW.set_use_markup(True)
236 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
237 xscale = 0.0, yscale = 0.0)
238 alignment.add(self._fsZFW)
239 table.attach(alignment, 3, 4, 10, 11)
240
241 table.attach(Gtk.Label("kg"), 4, 5, 10, 11)
242
243 table.attach(Gtk.HSeparator(), 0, 5, 11, 12)
244
245 label = Gtk.Label(xstr("weighthelp_gross"))
246 label.set_use_markup(True)
247 alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.5,
248 xscale = 0.0, yscale = 0.0)
249 alignment.add(label)
250 table.attach(alignment, 0, 1, 12, 13)
251
252 self._fsGross = Gtk.Label("124000")
253 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
254 xscale = 0.0, yscale = 0.0)
255 alignment.add(self._fsGross)
256 table.attach(alignment, 3, 4, 12, 13)
257
258 table.attach(Gtk.Label("kg"), 4, 5, 12, 13)
259
260 label = Gtk.Label(xstr("weighthelp_mzfw"))
261 label.set_use_markup(True)
262 alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.5,
263 xscale = 0.0, yscale = 0.0)
264 alignment.add(label)
265 table.attach(alignment, 0, 1, 13, 14)
266
267 self._mzfw = Gtk.Label("35000")
268 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
269 xscale = 0.0, yscale = 0.0)
270 alignment.add(self._mzfw)
271 table.attach(alignment, 1, 2, 13, 14)
272
273 table.attach(Gtk.Label("kg"), 2, 3, 13, 14)
274
275 label = Gtk.Label(xstr("weighthelp_mtow"))
276 label.set_use_markup(True)
277 alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.5,
278 xscale = 0.0, yscale = 0.0)
279 alignment.add(label)
280 table.attach(alignment, 0, 1, 14, 15)
281
282 self._mtow = Gtk.Label("35000")
283 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
284 xscale = 0.0, yscale = 0.0)
285 alignment.add(self._mtow)
286 table.attach(alignment, 1, 2, 14, 15)
287
288 table.attach(Gtk.Label("kg"), 2, 3, 14, 15)
289
290 label = Gtk.Label(xstr("weighthelp_mlw"))
291 label.set_use_markup(True)
292 alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.5,
293 xscale = 0.0, yscale = 0.0)
294 alignment.add(label)
295 table.attach(alignment, 0, 1, 15, 16)
296
297 self._mlw = Gtk.Label("35000")
298 alignment = Gtk.Alignment(xalign = 1.0, yalign = 0.5,
299 xscale = 0.0, yscale = 0.0)
300 alignment.add(self._mlw)
301 table.attach(alignment, 1, 2, 15, 16)
302
303 table.attach(Gtk.Label("kg"), 2, 3, 15, 16)
304
305 self.show_all()
306
307 def disable(self):
308 """Disable the widget."""
309 self._mainBox.set_sensitive(False)
310
311 def enable(self):
312 """Enable the widget."""
313 self._mainBox.set_sensitive(True)
314
315 def reset(self):
316 """Reset all calculated and FS data."""
317
318 self._usingHelp.set_active(False)
319 self._usingHelp.set_sensitive(True)
320 self._weightsTable.set_sensitive(False)
321
322 self._crew = -1
323 self._pax = -1
324 self._humanWeight = 82.0
325 self._bag = -1
326 self._cargo = -1
327 self._mail = -1
328 self._dowValue = -1
329 self._mzfwValue = -1
330 self._mtowValue = -1
331 self._mlwValue = -1
332
333 self._fsPayloadValue = -1
334 self._fsDOWValue = -1
335 self._fsZFWValue = -1
336 self._fsGrossValue = -1
337
338 self._setupCalculated()
339 self._setupFS()
340
341 def _setupCalculated(self):
342 """Setup the labels for the calculated values."""
343 if self._crew<0:
344 self._crewLabel.set_text(xstr("weighthelp_crew") % ("-",))
345 self._crewWeight.set_text("-")
346 else:
347 self._crewLabel.set_text(xstr("weighthelp_crew") % (str(self._crew),))
348 crewWeight = self._crew * self._humanWeight
349 self._crewWeight.set_text("%.0f" % (crewWeight,))
350
351 if self._pax<0:
352 self._paxLabel.set_text(xstr("weighthelp_pax") % ("-",))
353 self._paxWeight.set_text("-")
354 else:
355 self._paxLabel.set_text(xstr("weighthelp_pax") % (str(self._pax),))
356 paxWeight = self._pax * self._humanWeight
357 self._paxWeight.set_text("%.0f" % (paxWeight,))
358
359 self._setWeightLabel(self._bagWeight, self._bag)
360
361 self._setWeightLabel(self._cargoWeight, self._cargo)
362
363 self._setWeightLabel(self._mailWeight, self._mail)
364
365 (payload, zfw) = self._calculateWeights()
366
367 self._setWeightLabel(self._payload, payload, bold = True)
368
369 self._setWeightLabel(self._dow, self._dowValue)
370
371 if zfw<0:
372 self._zfw.set_text("-")
373 else:
374 markup = "%.0f" % (zfw,)
375 if self._mzfwValue>0 and zfw>self._mzfwValue:
376 markup = '<span foreground="red">' + markup + '</span>'
377 markup = '<b>' + markup + '</b>'
378 self._zfw.set_markup(markup)
379
380 self._setWeightLabel(self._mzfw, self._mzfwValue)
381
382 self._setWeightLabel(self._mtow, self._mtowValue)
383
384 self._setWeightLabel(self._mlw, self._mlwValue)
385
386 def _setupFS(self):
387 if self._dowValue<0:
388 self._dow.set_text("-")
389 else:
390 self._dow.set_text("%.0f" % (self._dowValue,))
391
392 """Setup the labels for the FS values."""
393 (payload, zfw) = self._calculateWeights()
394
395 if self._fsPayloadValue<0:
396 self._fsPayload.set_text("-")
397 else:
398 markup = WeightHelp._getMarkup(self._fsPayloadValue, payload,
399 PayloadChecker.TOLERANCE)
400 self._fsPayload.set_markup("<b>" + markup + "</b>")
401
402 if self._fsDOWValue<0:
403 self._fsDOW.set_text("-")
404 else:
405 markup = WeightHelp._getMarkup(self._fsDOWValue, self._dowValue,
406 PayloadChecker.TOLERANCE)
407 self._fsDOW.set_markup(markup)
408
409 if self._fsZFWValue<0:
410 self._fsZFW.set_text("-")
411 else:
412 markup = WeightHelp._getMarkup(self._fsZFWValue, zfw,
413 PayloadChecker.TOLERANCE)
414 self._fsZFW.set_markup("<b>" + markup + "</b>")
415
416 self._setWeightLabel(self._fsGross, self._fsGrossValue)
417
418 def _calculateWeights(self):
419 """Calculate the payload and the zero-fuel weight.
420
421 It returns a tuple with these two items. If any of the items cannot be
422 calculated, that is -1."""
423 if self._crew<0 or self._pax<0 or \
424 self._bag<0 or self._cargo<0 or self._mail<0:
425 payload = -1
426 else:
427 payload = (self._crew + self._pax) * self._humanWeight + \
428 self._bag + self._cargo + self._mail
429
430 if payload<0 or self._dowValue<0:
431 zfw = -1
432 else:
433 zfw = payload + self._dowValue
434
435 return (payload, zfw)
436
437 def _usingHelpToggled(self, button):
438 """Called when the Using help button is toggled."""
439 assert self._usingHelp.get_active()
440 self._usingHelp.set_sensitive(False)
441
442 self._gui.logger.untimedMessage("The weight calculation help function was used by the pilot")
443
444 self._crew = self._gui.numCrew
445 self._pax = self._gui.numPassengers
446 self._bag = self._gui.bagWeight
447 self._cargo = self._gui.cargoWeight
448 self._mail = self._gui.mailWeight
449
450 aircraft = self._gui.flight.aircraft
451 self._humanWeight = aircraft.humanWeight
452 self._dowValue = aircraft.dow
453 self._mzfwValue = aircraft.mzfw
454 self._mtowValue = aircraft.mtow
455 self._mlwValue = aircraft.mlw
456
457 self._setupCalculated()
458 self._weightsTable.set_sensitive(True)
459
460 def _fsButtonClicked(self, button):
461 """Callback for the FS button being clicked."""
462 gui = self._gui
463 gui.beginBusy(xstr("weighthelp_busy"))
464 gui.simulator.requestWeights(self._handleWeights)
465
466 def _handleWeights(self, dow, payload, zfw, grossWeight):
467 """Handle the given weights."""
468 GObject.idle_add(self._processWeights, dow, payload, zfw, grossWeight)
469
470 def _processWeights(self, dow, payload, zfw, grossWeight):
471 """Process the given weights."""
472 self._gui.endBusy()
473 if self._usingHelp.get_active():
474 self._fsPayloadValue = payload
475 self._fsDOWValue = dow
476 self._fsZFWValue = zfw
477 self._fsGrossValue = grossWeight
478 self._setupFS()
479
480 def _setWeightLabel(self, label, weight, bold = False):
481 """Set the given weight label to the given weight."""
482 if weight<0:
483 label.set_text("-")
484 else:
485 markup = "%.0f" % (weight,)
486 if bold: markup = "<b>" + markup + "</b>"
487 label.set_markup(markup)
488
489
Note: See TracBrowser for help on using the repository browser.