[604] | 1 | # Module to provide a widget displaying faults that occured during the flight
|
---|
| 2 | # and giving the user some space to explain it
|
---|
| 3 |
|
---|
| 4 | #-------------------------------------------------------------------------------
|
---|
| 5 |
|
---|
| 6 | from mlx.gui.common import *
|
---|
| 7 |
|
---|
| 8 | #-------------------------------------------------------------------------------
|
---|
| 9 |
|
---|
[608] | 10 | ## @package mlx.gui.faultexplain
|
---|
[604] | 11 | #
|
---|
| 12 | # The widget and associated logic to display faults and allow the pilot to
|
---|
| 13 | # explain them. Each fault is displayed as the text it is accompanied by in the
|
---|
| 14 | # log and there is a text entry field where the user can enter the
|
---|
| 15 | # corresponding explanation. \ref FaultFrame belongs to one fault, while
|
---|
| 16 | # \ref FaultExplainWidget contains the collection of all frames, which is a
|
---|
| 17 | # VBox in a scrolled window.
|
---|
| 18 |
|
---|
| 19 | #-------------------------------------------------------------------------------
|
---|
| 20 |
|
---|
[996] | 21 | class FaultFrame(Gtk.Frame):
|
---|
[604] | 22 | """A frame containing the information about a single fault.
|
---|
| 23 |
|
---|
| 24 | It consists of a text view with the text of the fault and an editable text
|
---|
| 25 | view for the explanation."""
|
---|
| 26 | def __init__(self, faultText):
|
---|
| 27 | """Construct the frame."""
|
---|
[996] | 28 | Gtk.Frame.__init__(self)
|
---|
[604] | 29 |
|
---|
| 30 | self._faultText = faultText
|
---|
| 31 |
|
---|
[996] | 32 | vbox = Gtk.VBox()
|
---|
[604] | 33 |
|
---|
[996] | 34 | self._fault = fault = Gtk.Label()
|
---|
[988] | 35 | fault.set_xalign(0.0)
|
---|
[999] | 36 | fault.set_justify(Gtk.Justification.LEFT)
|
---|
[988] | 37 | fault.set_line_wrap(True)
|
---|
[604] | 38 |
|
---|
| 39 | self.faultText = faultText
|
---|
| 40 |
|
---|
[996] | 41 | faultAlignment = Gtk.Alignment(xalign = 0.0, yalign = 0.0,
|
---|
[604] | 42 | xscale = 1.0, yscale = 0.0)
|
---|
| 43 | faultAlignment.set_padding(padding_top = 0, padding_bottom = 0,
|
---|
| 44 | padding_left = 2, padding_right = 2)
|
---|
| 45 | faultAlignment.add(fault)
|
---|
| 46 | vbox.pack_start(faultAlignment, True, True, 4)
|
---|
| 47 |
|
---|
[996] | 48 | self._explanation = explanation = Gtk.TextView()
|
---|
[999] | 49 | explanation.set_wrap_mode(Gtk.WrapMode.WORD)
|
---|
[604] | 50 | explanation.set_accepts_tab(False)
|
---|
| 51 | explanation.set_size_request(-1, 100)
|
---|
| 52 |
|
---|
| 53 | buffer = explanation.get_buffer()
|
---|
| 54 | buffer.connect("changed", self._explanationChanged)
|
---|
| 55 |
|
---|
| 56 | vbox.pack_start(explanation, True, True, 4)
|
---|
| 57 |
|
---|
| 58 | self.add(vbox)
|
---|
| 59 | self.show_all()
|
---|
| 60 |
|
---|
[994] | 61 | styleContext = self.get_style_context()
|
---|
[996] | 62 | color = styleContext.get_background_color(Gtk.StateFlags.NORMAL)
|
---|
[994] | 63 | fault.override_background_color(0, color)
|
---|
[604] | 64 |
|
---|
| 65 | self._hasExplanation = False
|
---|
| 66 |
|
---|
| 67 | @property
|
---|
| 68 | def faultText(self):
|
---|
| 69 | """Get the text of the fault."""
|
---|
| 70 | return self._faultText
|
---|
| 71 |
|
---|
| 72 | @faultText.setter
|
---|
| 73 | def faultText(self, faultText):
|
---|
| 74 | """Update the text of the fault."""
|
---|
| 75 | self._faultText = faultText
|
---|
[988] | 76 | self._fault.set_markup("<b>" + faultText + "</b>")
|
---|
[604] | 77 |
|
---|
| 78 | @property
|
---|
| 79 | def explanation(self):
|
---|
| 80 | """Get the text of the explanation."""
|
---|
| 81 | buffer = self._explanation.get_buffer()
|
---|
| 82 | return buffer.get_text(buffer.get_start_iter(),
|
---|
| 83 | buffer.get_end_iter(), True)
|
---|
| 84 |
|
---|
[841] | 85 | @explanation.setter
|
---|
| 86 | def explanation(self, explanation):
|
---|
| 87 | """Set the explanation."""
|
---|
| 88 | self._explanation.get_buffer().set_text(explanation)
|
---|
| 89 |
|
---|
[604] | 90 | @property
|
---|
| 91 | def hasExplanation(self):
|
---|
| 92 | """Determine if there is a valid explanation."""
|
---|
| 93 | return self._hasExplanation
|
---|
| 94 |
|
---|
| 95 | @property
|
---|
| 96 | def html(self):
|
---|
| 97 | """Convert the contents of the widget into HTML."""
|
---|
| 98 | return self._faultText + "<br/></b>" + self.explanation + "<b>"
|
---|
| 99 |
|
---|
| 100 | def setMnemonicFor(self, widget):
|
---|
| 101 | """Set the explanation text view as the mnemonic widget for the given
|
---|
| 102 | one."""
|
---|
| 103 | widget.set_mnemonic_widget(self._explanation)
|
---|
| 104 |
|
---|
| 105 | def _explanationChanged(self, textBuffer):
|
---|
| 106 | """Called when the explanation's text has changed."""
|
---|
| 107 | hasExplanation = len(textBuffer.get_text(textBuffer.get_start_iter(),
|
---|
| 108 | textBuffer.get_end_iter(),
|
---|
| 109 | True))>3
|
---|
| 110 | if self._hasExplanation != hasExplanation:
|
---|
| 111 | self._hasExplanation = hasExplanation
|
---|
| 112 | self.emit("explanation-changed", hasExplanation)
|
---|
| 113 |
|
---|
| 114 | #-------------------------------------------------------------------------------
|
---|
| 115 |
|
---|
[995] | 116 | GObject.signal_new("explanation-changed", FaultFrame, GObject.SIGNAL_RUN_FIRST,
|
---|
[604] | 117 | None, (bool,))
|
---|
| 118 |
|
---|
| 119 | #-------------------------------------------------------------------------------
|
---|
| 120 |
|
---|
[996] | 121 | class FaultExplainWidget(Gtk.Frame):
|
---|
[842] | 122 | """The widget for the faults and their explanations."""
|
---|
[604] | 123 | @staticmethod
|
---|
| 124 | def getFaultFrame(alignment):
|
---|
| 125 | """Get the fault frame from the given alignment."""
|
---|
| 126 | return alignment.get_children()[0]
|
---|
| 127 |
|
---|
[620] | 128 | def __init__(self, gui):
|
---|
[996] | 129 | Gtk.Frame.__init__(self)
|
---|
[620] | 130 |
|
---|
| 131 | self._gui = gui
|
---|
[604] | 132 | self.set_label(xstr("info_faults"))
|
---|
| 133 | label = self.get_label_widget()
|
---|
| 134 | label.set_use_underline(True)
|
---|
| 135 |
|
---|
[996] | 136 | alignment = Gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
[604] | 137 | xscale = 1.0, yscale = 1.0)
|
---|
| 138 | alignment.set_padding(padding_top = 4, padding_bottom = 4,
|
---|
[620] | 139 | padding_left = 4, padding_right = 4)
|
---|
| 140 |
|
---|
[996] | 141 | self._outerBox = outerBox = Gtk.EventBox()
|
---|
[620] | 142 | outerBox.add(alignment)
|
---|
| 143 |
|
---|
[996] | 144 | self._innerBox = innerBox = Gtk.EventBox()
|
---|
[620] | 145 | alignment.add(self._innerBox)
|
---|
| 146 |
|
---|
[996] | 147 | alignment = Gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
[620] | 148 | xscale = 1.0, yscale = 1.0)
|
---|
| 149 | alignment.set_padding(padding_top = 0, padding_bottom = 0,
|
---|
| 150 | padding_left = 0, padding_right = 0)
|
---|
| 151 |
|
---|
| 152 | innerBox.add(alignment)
|
---|
[604] | 153 |
|
---|
[996] | 154 | scroller = Gtk.ScrolledWindow()
|
---|
[999] | 155 | scroller.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
|
---|
| 156 | scroller.set_shadow_type(Gtk.ShadowType.NONE)
|
---|
[604] | 157 |
|
---|
[996] | 158 | self._faults = Gtk.VBox()
|
---|
[604] | 159 | self._faults.set_homogeneous(False)
|
---|
[988] | 160 | scroller.add(self._faults)
|
---|
[604] | 161 |
|
---|
[620] | 162 | alignment.add(scroller)
|
---|
| 163 |
|
---|
[604] | 164 | self._faultWidgets = {}
|
---|
| 165 |
|
---|
[620] | 166 | self.add(outerBox)
|
---|
| 167 | self.show_all()
|
---|
[604] | 168 |
|
---|
| 169 | self._numFaults = 0
|
---|
| 170 | self._numExplanations = 0
|
---|
| 171 |
|
---|
| 172 | @property
|
---|
| 173 | def fullyExplained(self):
|
---|
| 174 | """Indicate if the faults have been fully explained."""
|
---|
| 175 | return self._numExplanations>=self._numFaults
|
---|
| 176 |
|
---|
| 177 | @property
|
---|
| 178 | def html(self):
|
---|
| 179 | """Convert the contents of the widget into HTML."""
|
---|
| 180 | html = ""
|
---|
| 181 | for alignment in self._faults.get_children():
|
---|
| 182 | faultFrame = FaultExplainWidget.getFaultFrame(alignment)
|
---|
| 183 | if html:
|
---|
| 184 | html += "<br><br>"
|
---|
| 185 | html += faultFrame.html
|
---|
| 186 | return html
|
---|
| 187 |
|
---|
| 188 | def addFault(self, id, faultText):
|
---|
| 189 | """Add a fault with the given ID and text."""
|
---|
| 190 |
|
---|
[996] | 191 | alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.0,
|
---|
[604] | 192 | xscale = 1.0, yscale = 0.0)
|
---|
| 193 | alignment.set_padding(padding_top = 2, padding_bottom = 2,
|
---|
| 194 | padding_left = 4, padding_right = 4)
|
---|
| 195 |
|
---|
| 196 | faultFrame = FaultFrame(faultText)
|
---|
| 197 | if self._numFaults==0:
|
---|
| 198 | faultFrame.setMnemonicFor(self.get_label_widget())
|
---|
| 199 | faultFrame.connect("explanation-changed", self._explanationChanged)
|
---|
| 200 |
|
---|
| 201 | alignment.add(faultFrame)
|
---|
| 202 | self._faults.pack_start(alignment, False, False, 4)
|
---|
| 203 | self._faults.show_all()
|
---|
| 204 |
|
---|
| 205 | self._faultWidgets[id] = (alignment, faultFrame)
|
---|
| 206 |
|
---|
| 207 | self._updateStats(numFaults = self._numFaults + 1)
|
---|
| 208 |
|
---|
| 209 | def updateFault(self, id, faultText):
|
---|
| 210 | """Update the text of the fault with the given ID."""
|
---|
| 211 | self._faultWidgets[id][1].faultText = faultText
|
---|
| 212 |
|
---|
| 213 | def clearFault(self, id):
|
---|
| 214 | """Clear (remove) the fault with the given ID."""
|
---|
| 215 | (alignment, faultFrame) = self._faultWidgets[id]
|
---|
| 216 | hasExplanation = faultFrame.hasExplanation
|
---|
| 217 |
|
---|
| 218 | children = self._faults.get_children()
|
---|
| 219 | if alignment is children[0] and len(children)>1:
|
---|
| 220 | faultFrame = FaultExplainWidget.getFaultFrame(children[1])
|
---|
| 221 | faultFrame.setMnemonicFor(self.get_label_widget())
|
---|
| 222 |
|
---|
| 223 | self._faults.remove(alignment)
|
---|
| 224 | self._faults.show_all()
|
---|
| 225 |
|
---|
| 226 | del self._faultWidgets[id]
|
---|
| 227 |
|
---|
| 228 | self._updateStats(numFaults = self._numFaults - 1,
|
---|
| 229 | numExplanations = self._numExplanations -
|
---|
| 230 | (1 if hasExplanation else 0))
|
---|
| 231 |
|
---|
[841] | 232 | def setExplanation(self, id, explanation):
|
---|
| 233 | """Set the explanation for the fault with the given ID"""
|
---|
| 234 | self._faultWidgets[id][1].explanation = explanation
|
---|
| 235 |
|
---|
[604] | 236 | def reset(self):
|
---|
| 237 | """Reset the widget by removing all faults."""
|
---|
[919] | 238 | for (alignment, faultFrame) in self._faultWidgets.values():
|
---|
[604] | 239 | self._faults.remove(alignment)
|
---|
| 240 | self._faults.show_all()
|
---|
| 241 |
|
---|
| 242 | self._faultWidgets = {}
|
---|
[617] | 243 | self._numFaults = self._numExplanations = 0
|
---|
[620] | 244 | self._setColor()
|
---|
| 245 |
|
---|
| 246 | def set_sensitive(self, sensitive):
|
---|
| 247 | """Set the sensitiviy of the widget.
|
---|
| 248 |
|
---|
| 249 | The outer event box's sensitivity is changed only."""
|
---|
| 250 | self._outerBox.set_sensitive(sensitive)
|
---|
[604] | 251 |
|
---|
| 252 | def _updateStats(self, numFaults = None, numExplanations = None):
|
---|
| 253 | """Update the statistics.
|
---|
| 254 |
|
---|
| 255 | If the explanation status has changed, emit the corresponding
|
---|
| 256 | signal."""
|
---|
| 257 | before = self.fullyExplained
|
---|
| 258 |
|
---|
| 259 | if numFaults is None:
|
---|
| 260 | numFaults = self._numFaults
|
---|
| 261 |
|
---|
| 262 | if numExplanations is None:
|
---|
| 263 | numExplanations = self._numExplanations
|
---|
| 264 |
|
---|
| 265 | after = numExplanations >= numFaults
|
---|
| 266 |
|
---|
| 267 | self._numFaults = numFaults
|
---|
| 268 | self._numExplanations = numExplanations
|
---|
| 269 |
|
---|
| 270 | if before!=after:
|
---|
[620] | 271 | self._setColor()
|
---|
[604] | 272 | self.emit("explanations-changed", after)
|
---|
| 273 |
|
---|
| 274 | def _explanationChanged(self, faultFrame, hasExplanation):
|
---|
| 275 | """Called when the status of an explanation has changed."""
|
---|
| 276 | self._updateStats(numExplanations = (self._numExplanations +
|
---|
| 277 | (1 if hasExplanation else -1)))
|
---|
| 278 |
|
---|
[620] | 279 | def _setColor(self):
|
---|
| 280 | """Set the color to indicate if an unexplained fault is present or
|
---|
| 281 | not."""
|
---|
| 282 | allExplained = self._numExplanations >= self._numFaults
|
---|
[994] | 283 | styleContext = self.get_style_context()
|
---|
| 284 | if allExplained:
|
---|
[997] | 285 | outerColour = innerColour = Gdk.RGBA(red = 0.0, green=0.0,
|
---|
[994] | 286 | blue=0.0, alpha=0.0)
|
---|
| 287 | else:
|
---|
| 288 | outerColour = \
|
---|
[996] | 289 | styleContext.get_border_color(Gtk.StateFlags.DROP_ACTIVE)
|
---|
[994] | 290 | innerColour = self._gui.backgroundColour
|
---|
[620] | 291 |
|
---|
[996] | 292 | self._outerBox.override_background_color(Gtk.StateFlags.NORMAL,
|
---|
[994] | 293 | outerColour)
|
---|
[996] | 294 | self._innerBox.override_background_color(Gtk.StateFlags.NORMAL,
|
---|
[994] | 295 | innerColour)
|
---|
[620] | 296 |
|
---|
[604] | 297 | #-------------------------------------------------------------------------------
|
---|
| 298 |
|
---|
[995] | 299 | GObject.signal_new("explanations-changed", FaultExplainWidget,
|
---|
| 300 | GObject.SIGNAL_RUN_FIRST, None, (bool,))
|
---|