source: src/mlx/gui/faultexplain.py@ 919:2ce8ca39525b

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

Ran 2to3

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