source: src/mlx/gui/faultexplain.py@ 1074:638d4c261307

python3
Last change on this file since 1074:638d4c261307 was 999:e096a5638b87, checked in by István Váradi <ivaradi@…>, 5 years ago

Removed Gtk 2/3 constant definitions (re #347)

File size: 10.6 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(Gtk.Justification.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(Gtk.WrapMode.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 styleContext = self.get_style_context()
62 color = styleContext.get_background_color(Gtk.StateFlags.NORMAL)
63 fault.override_background_color(0, color)
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
76 self._fault.set_markup("<b>" + faultText + "</b>")
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
85 @explanation.setter
86 def explanation(self, explanation):
87 """Set the explanation."""
88 self._explanation.get_buffer().set_text(explanation)
89
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
116GObject.signal_new("explanation-changed", FaultFrame, GObject.SIGNAL_RUN_FIRST,
117 None, (bool,))
118
119#-------------------------------------------------------------------------------
120
121class FaultExplainWidget(Gtk.Frame):
122 """The widget for the faults and their explanations."""
123 @staticmethod
124 def getFaultFrame(alignment):
125 """Get the fault frame from the given alignment."""
126 return alignment.get_children()[0]
127
128 def __init__(self, gui):
129 Gtk.Frame.__init__(self)
130
131 self._gui = gui
132 self.set_label(xstr("info_faults"))
133 label = self.get_label_widget()
134 label.set_use_underline(True)
135
136 alignment = Gtk.Alignment(xalign = 0.5, yalign = 0.5,
137 xscale = 1.0, yscale = 1.0)
138 alignment.set_padding(padding_top = 4, padding_bottom = 4,
139 padding_left = 4, padding_right = 4)
140
141 self._outerBox = outerBox = Gtk.EventBox()
142 outerBox.add(alignment)
143
144 self._innerBox = innerBox = Gtk.EventBox()
145 alignment.add(self._innerBox)
146
147 alignment = Gtk.Alignment(xalign = 0.5, yalign = 0.5,
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)
153
154 scroller = Gtk.ScrolledWindow()
155 scroller.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
156 scroller.set_shadow_type(Gtk.ShadowType.NONE)
157
158 self._faults = Gtk.VBox()
159 self._faults.set_homogeneous(False)
160 scroller.add(self._faults)
161
162 alignment.add(scroller)
163
164 self._faultWidgets = {}
165
166 self.add(outerBox)
167 self.show_all()
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
191 alignment = Gtk.Alignment(xalign = 0.0, yalign = 0.0,
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
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
236 def reset(self):
237 """Reset the widget by removing all faults."""
238 for (alignment, faultFrame) in self._faultWidgets.values():
239 self._faults.remove(alignment)
240 self._faults.show_all()
241
242 self._faultWidgets = {}
243 self._numFaults = self._numExplanations = 0
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)
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:
271 self._setColor()
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
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
283 styleContext = self.get_style_context()
284 if allExplained:
285 outerColour = innerColour = Gdk.RGBA(red = 0.0, green=0.0,
286 blue=0.0, alpha=0.0)
287 else:
288 outerColour = \
289 styleContext.get_border_color(Gtk.StateFlags.DROP_ACTIVE)
290 innerColour = self._gui.backgroundColour
291
292 self._outerBox.override_background_color(Gtk.StateFlags.NORMAL,
293 outerColour)
294 self._innerBox.override_background_color(Gtk.StateFlags.NORMAL,
295 innerColour)
296
297#-------------------------------------------------------------------------------
298
299GObject.signal_new("explanations-changed", FaultExplainWidget,
300 GObject.SIGNAL_RUN_FIRST, None, (bool,))
Note: See TracBrowser for help on using the repository browser.