source: src/mlx/gui/weighthelp.py@ 173:e8aaabbd86c6

Last change on this file since 173:e8aaabbd86c6 was 117:af3d52b9adc4, checked in by István Váradi <ivaradi@…>, 13 years ago

Implemented the weight help tab

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