source: src/mlx/gui/faultexplain.py@ 770:7b14dd1f67ec

Last change on this file since 770:7b14dd1f67ec was 620:bcbc2bc37909, checked in by István Váradi <ivaradi@…>, 10 years ago

The border of the fault explanation widget is highlighted if there are any unexplained faults (re #257)

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