source: src/mlx/gui/faultexplain.py@ 619:7763179ff6b0

Last change on this file since 619:7763179ff6b0 was 617:7b17915dbbaf, checked in by István Váradi <ivaradi@…>, 9 years ago

A better fix (re #254)

File size: 9.0 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):
135 gtk.Frame.__init__(self)
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 = 8, padding_right = 8)
144
145 scroller = gtk.ScrolledWindow()
146 scroller.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
147
148 self._faults = gtk.VBox()
149 self._faults.set_homogeneous(False)
150 scroller.add_with_viewport(self._faults)
151
152 self._faultWidgets = {}
153
154 alignment.add(scroller)
155 self.add(alignment)
156
157 self._numFaults = 0
158 self._numExplanations = 0
159
160 @property
161 def fullyExplained(self):
162 """Indicate if the faults have been fully explained."""
163 return self._numExplanations>=self._numFaults
164
165 @property
166 def html(self):
167 """Convert the contents of the widget into HTML."""
168 html = ""
169 for alignment in self._faults.get_children():
170 faultFrame = FaultExplainWidget.getFaultFrame(alignment)
171 if html:
172 html += "<br><br>"
173 html += faultFrame.html
174 return html
175
176 def addFault(self, id, faultText):
177 """Add a fault with the given ID and text."""
178
179 alignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
180 xscale = 1.0, yscale = 0.0)
181 alignment.set_padding(padding_top = 2, padding_bottom = 2,
182 padding_left = 4, padding_right = 4)
183
184 faultFrame = FaultFrame(faultText)
185 if self._numFaults==0:
186 faultFrame.setMnemonicFor(self.get_label_widget())
187 faultFrame.connect("explanation-changed", self._explanationChanged)
188
189 alignment.add(faultFrame)
190 self._faults.pack_start(alignment, False, False, 4)
191 self._faults.show_all()
192
193 self._faultWidgets[id] = (alignment, faultFrame)
194
195 self._updateStats(numFaults = self._numFaults + 1)
196
197 def updateFault(self, id, faultText):
198 """Update the text of the fault with the given ID."""
199 self._faultWidgets[id][1].faultText = faultText
200
201 def clearFault(self, id):
202 """Clear (remove) the fault with the given ID."""
203 (alignment, faultFrame) = self._faultWidgets[id]
204 hasExplanation = faultFrame.hasExplanation
205
206 children = self._faults.get_children()
207 if alignment is children[0] and len(children)>1:
208 faultFrame = FaultExplainWidget.getFaultFrame(children[1])
209 faultFrame.setMnemonicFor(self.get_label_widget())
210
211 self._faults.remove(alignment)
212 self._faults.show_all()
213
214 del self._faultWidgets[id]
215
216 self._updateStats(numFaults = self._numFaults - 1,
217 numExplanations = self._numExplanations -
218 (1 if hasExplanation else 0))
219
220 def reset(self):
221 """Reset the widget by removing all faults."""
222 for (alignment, faultFrame) in self._faultWidgets.itervalues():
223 self._faults.remove(alignment)
224 self._faults.show_all()
225
226 self._faultWidgets = {}
227 self._numFaults = self._numExplanations = 0
228
229 def _updateStats(self, numFaults = None, numExplanations = None):
230 """Update the statistics.
231
232 If the explanation status has changed, emit the corresponding
233 signal."""
234 before = self.fullyExplained
235
236 if numFaults is None:
237 numFaults = self._numFaults
238
239 if numExplanations is None:
240 numExplanations = self._numExplanations
241
242 after = numExplanations >= numFaults
243
244 self._numFaults = numFaults
245 self._numExplanations = numExplanations
246
247 if before!=after:
248 self.emit("explanations-changed", after)
249
250 def _explanationChanged(self, faultFrame, hasExplanation):
251 """Called when the status of an explanation has changed."""
252 self._updateStats(numExplanations = (self._numExplanations +
253 (1 if hasExplanation else -1)))
254
255#-------------------------------------------------------------------------------
256
257gobject.signal_new("explanations-changed", FaultExplainWidget,
258 gobject.SIGNAL_RUN_FIRST, None, (bool,))
Note: See TracBrowser for help on using the repository browser.