source: src/mlx/gui/faultexplain.py@ 988:0ba32f747163

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

The fault display in the explanation widget is of the correct size initially as well (re #347).

File size: 10.9 KB
Line 
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
6from mlx.gui.common import *
7
8#-------------------------------------------------------------------------------
9
10## @package mlx.gui.faultexplain
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
21class FaultFrame(gtk.Frame):
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."""
28 gtk.Frame.__init__(self)
29
30 self._faultText = faultText
31
32 vbox = gtk.VBox()
33
34 self._fault = fault = gtk.Label()
35 fault.set_xalign(0.0)
36 fault.set_justify(JUSTIFY_LEFT)
37 fault.set_line_wrap(True)
38
39 self.faultText = faultText
40
41 faultAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
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
48 self._explanation = explanation = gtk.TextView()
49 explanation.set_wrap_mode(WRAP_WORD)
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
61 if pygobject:
62 styleContext = self.get_style_context()
63 color = styleContext.get_background_color(gtk.StateFlags.NORMAL)
64 fault.override_background_color(0, color)
65 else:
66 style = self.rc_get_style()
67 fault.modify_base(0, style.bg[0])
68
69 self._hasExplanation = False
70
71 @property
72 def faultText(self):
73 """Get the text of the fault."""
74 return self._faultText
75
76 @faultText.setter
77 def faultText(self, faultText):
78 """Update the text of the fault."""
79 self._faultText = faultText
80 self._fault.set_markup("<b>" + faultText + "</b>")
81
82 @property
83 def explanation(self):
84 """Get the text of the explanation."""
85 buffer = self._explanation.get_buffer()
86 return buffer.get_text(buffer.get_start_iter(),
87 buffer.get_end_iter(), True)
88
89 @explanation.setter
90 def explanation(self, explanation):
91 """Set the explanation."""
92 self._explanation.get_buffer().set_text(explanation)
93
94 @property
95 def hasExplanation(self):
96 """Determine if there is a valid explanation."""
97 return self._hasExplanation
98
99 @property
100 def html(self):
101 """Convert the contents of the widget into HTML."""
102 return self._faultText + "<br/></b>" + self.explanation + "<b>"
103
104 def setMnemonicFor(self, widget):
105 """Set the explanation text view as the mnemonic widget for the given
106 one."""
107 widget.set_mnemonic_widget(self._explanation)
108
109 def _explanationChanged(self, textBuffer):
110 """Called when the explanation's text has changed."""
111 hasExplanation = len(textBuffer.get_text(textBuffer.get_start_iter(),
112 textBuffer.get_end_iter(),
113 True))>3
114 if self._hasExplanation != hasExplanation:
115 self._hasExplanation = hasExplanation
116 self.emit("explanation-changed", hasExplanation)
117
118#-------------------------------------------------------------------------------
119
120gobject.signal_new("explanation-changed", FaultFrame, gobject.SIGNAL_RUN_FIRST,
121 None, (bool,))
122
123#-------------------------------------------------------------------------------
124
125class FaultExplainWidget(gtk.Frame):
126 """The widget for the faults and their explanations."""
127 @staticmethod
128 def getFaultFrame(alignment):
129 """Get the fault frame from the given alignment."""
130 return alignment.get_children()[0]
131
132 def __init__(self, gui):
133 gtk.Frame.__init__(self)
134
135 self._gui = gui
136 self.set_label(xstr("info_faults"))
137 label = self.get_label_widget()
138 label.set_use_underline(True)
139
140 alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
141 xscale = 1.0, yscale = 1.0)
142 alignment.set_padding(padding_top = 4, padding_bottom = 4,
143 padding_left = 4, padding_right = 4)
144
145 self._outerBox = outerBox = gtk.EventBox()
146 outerBox.add(alignment)
147
148 self._innerBox = innerBox = gtk.EventBox()
149 alignment.add(self._innerBox)
150
151 alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
152 xscale = 1.0, yscale = 1.0)
153 alignment.set_padding(padding_top = 0, padding_bottom = 0,
154 padding_left = 0, padding_right = 0)
155
156 innerBox.add(alignment)
157
158 scroller = gtk.ScrolledWindow()
159 scroller.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
160 scroller.set_shadow_type(SHADOW_NONE)
161
162 self._faults = gtk.VBox()
163 self._faults.set_homogeneous(False)
164 scroller.add(self._faults)
165
166 alignment.add(scroller)
167
168 self._faultWidgets = {}
169
170 self.add(outerBox)
171 self.show_all()
172
173 self._numFaults = 0
174 self._numExplanations = 0
175
176 @property
177 def fullyExplained(self):
178 """Indicate if the faults have been fully explained."""
179 return self._numExplanations>=self._numFaults
180
181 @property
182 def html(self):
183 """Convert the contents of the widget into HTML."""
184 html = ""
185 for alignment in self._faults.get_children():
186 faultFrame = FaultExplainWidget.getFaultFrame(alignment)
187 if html:
188 html += "<br><br>"
189 html += faultFrame.html
190 return html
191
192 def addFault(self, id, faultText):
193 """Add a fault with the given ID and text."""
194
195 alignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
196 xscale = 1.0, yscale = 0.0)
197 alignment.set_padding(padding_top = 2, padding_bottom = 2,
198 padding_left = 4, padding_right = 4)
199
200 faultFrame = FaultFrame(faultText)
201 if self._numFaults==0:
202 faultFrame.setMnemonicFor(self.get_label_widget())
203 faultFrame.connect("explanation-changed", self._explanationChanged)
204
205 alignment.add(faultFrame)
206 self._faults.pack_start(alignment, False, False, 4)
207 self._faults.show_all()
208
209 self._faultWidgets[id] = (alignment, faultFrame)
210
211 self._updateStats(numFaults = self._numFaults + 1)
212
213 def updateFault(self, id, faultText):
214 """Update the text of the fault with the given ID."""
215 self._faultWidgets[id][1].faultText = faultText
216
217 def clearFault(self, id):
218 """Clear (remove) the fault with the given ID."""
219 (alignment, faultFrame) = self._faultWidgets[id]
220 hasExplanation = faultFrame.hasExplanation
221
222 children = self._faults.get_children()
223 if alignment is children[0] and len(children)>1:
224 faultFrame = FaultExplainWidget.getFaultFrame(children[1])
225 faultFrame.setMnemonicFor(self.get_label_widget())
226
227 self._faults.remove(alignment)
228 self._faults.show_all()
229
230 del self._faultWidgets[id]
231
232 self._updateStats(numFaults = self._numFaults - 1,
233 numExplanations = self._numExplanations -
234 (1 if hasExplanation else 0))
235
236 def setExplanation(self, id, explanation):
237 """Set the explanation for the fault with the given ID"""
238 self._faultWidgets[id][1].explanation = explanation
239
240 def reset(self):
241 """Reset the widget by removing all faults."""
242 for (alignment, faultFrame) in self._faultWidgets.values():
243 self._faults.remove(alignment)
244 self._faults.show_all()
245
246 self._faultWidgets = {}
247 self._numFaults = self._numExplanations = 0
248 self._setColor()
249
250 def set_sensitive(self, sensitive):
251 """Set the sensitiviy of the widget.
252
253 The outer event box's sensitivity is changed only."""
254 self._outerBox.set_sensitive(sensitive)
255
256 def _updateStats(self, numFaults = None, numExplanations = None):
257 """Update the statistics.
258
259 If the explanation status has changed, emit the corresponding
260 signal."""
261 before = self.fullyExplained
262
263 if numFaults is None:
264 numFaults = self._numFaults
265
266 if numExplanations is None:
267 numExplanations = self._numExplanations
268
269 after = numExplanations >= numFaults
270
271 self._numFaults = numFaults
272 self._numExplanations = numExplanations
273
274 if before!=after:
275 self._setColor()
276 self.emit("explanations-changed", after)
277
278 def _explanationChanged(self, faultFrame, hasExplanation):
279 """Called when the status of an explanation has changed."""
280 self._updateStats(numExplanations = (self._numExplanations +
281 (1 if hasExplanation else -1)))
282
283 def _setColor(self):
284 """Set the color to indicate if an unexplained fault is present or
285 not."""
286 allExplained = self._numExplanations >= self._numFaults
287 if pygobject:
288 styleContext = self.get_style_context()
289 if allExplained:
290 outerColour = innerColour = gdk.RGBA(red = 0.0, green=0.0,
291 blue=0.0, alpha=0.0)
292 else:
293 outerColour = \
294 styleContext.get_border_color(gtk.StateFlags.DROP_ACTIVE)
295 innerColour = self._gui.backgroundColour
296
297 self._outerBox.override_background_color(gtk.StateFlags.NORMAL,
298 outerColour)
299 self._innerBox.override_background_color(gtk.StateFlags.NORMAL,
300 innerColour)
301 else:
302 style = self.rc_get_style()
303 self._outerBox.modify_bg(0, style.bg[0 if allExplained else 3])
304
305#-------------------------------------------------------------------------------
306
307gobject.signal_new("explanations-changed", FaultExplainWidget,
308 gobject.SIGNAL_RUN_FIRST, None, (bool,))
Note: See TracBrowser for help on using the repository browser.