1 |
|
---|
2 | import mlx.const as _const
|
---|
3 | from mlx.i18n import xstr
|
---|
4 |
|
---|
5 | from mlx.util import secondaryInstallation
|
---|
6 |
|
---|
7 | import os
|
---|
8 |
|
---|
9 | #-----------------------------------------------------------------------------
|
---|
10 |
|
---|
11 | ## @package mlx.gui.common
|
---|
12 | #
|
---|
13 | # Common definitions and utilities for the GUI
|
---|
14 | #
|
---|
15 | # The main purpose of this module is to provide common definitions for things
|
---|
16 | # that are named differently in Gtk+ 2 and 3. This way the other parts of the
|
---|
17 | # GUI have to check the version in use very rarely. The variable \ref pygobject
|
---|
18 | # tells which version is being used. If it is \c True, Gtk+ 3 is used via the
|
---|
19 | # PyGObject interface. Otherwise Gtk+ 2 is used, which is the default on
|
---|
20 | # Windows or when the \c FORCE_PYGTK environment variable is set.
|
---|
21 | #
|
---|
22 | # Besides this there are some common utility classes and functions.
|
---|
23 |
|
---|
24 | #-----------------------------------------------------------------------------
|
---|
25 |
|
---|
26 | appIndicator = False
|
---|
27 |
|
---|
28 | if os.name=="nt" or "FORCE_PYGTK" in os.environ:
|
---|
29 | print "Using PyGTK"
|
---|
30 | pygobject = False
|
---|
31 | import pygtk
|
---|
32 | import gtk.gdk as gdk
|
---|
33 | import gtk
|
---|
34 | import gobject
|
---|
35 | import pango
|
---|
36 | try:
|
---|
37 | import appindicator
|
---|
38 | appIndicator = True
|
---|
39 | except Exception, e:
|
---|
40 | pass
|
---|
41 |
|
---|
42 | MESSAGETYPE_ERROR = gtk.MESSAGE_ERROR
|
---|
43 | MESSAGETYPE_QUESTION = gtk.MESSAGE_QUESTION
|
---|
44 | MESSAGETYPE_INFO = gtk.MESSAGE_INFO
|
---|
45 |
|
---|
46 | RESPONSETYPE_OK = gtk.RESPONSE_OK
|
---|
47 | RESPONSETYPE_YES = gtk.RESPONSE_YES
|
---|
48 | RESPONSETYPE_NO = gtk.RESPONSE_NO
|
---|
49 | RESPONSETYPE_ACCEPT = gtk.RESPONSE_ACCEPT
|
---|
50 | RESPONSETYPE_REJECT = gtk.RESPONSE_REJECT
|
---|
51 | RESPONSETYPE_CANCEL = gtk.RESPONSE_CANCEL
|
---|
52 |
|
---|
53 | ACCEL_VISIBLE = gtk.ACCEL_VISIBLE
|
---|
54 | CONTROL_MASK = gdk.CONTROL_MASK
|
---|
55 | DIALOG_MODAL = gtk.DIALOG_MODAL
|
---|
56 | WRAP_WORD = gtk.WRAP_WORD
|
---|
57 | JUSTIFY_CENTER = gtk.JUSTIFY_CENTER
|
---|
58 |
|
---|
59 | CONTROL_MASK = gdk.CONTROL_MASK
|
---|
60 | SHIFT_MASK = gdk.SHIFT_MASK
|
---|
61 | BUTTON1_MASK = gdk.BUTTON1_MASK
|
---|
62 |
|
---|
63 | SCROLL_UP = gdk.SCROLL_UP
|
---|
64 | SCROLL_DOWN = gdk.SCROLL_DOWN
|
---|
65 |
|
---|
66 | SPIN_USER_DEFINED = gtk.SPIN_USER_DEFINED
|
---|
67 |
|
---|
68 | FILE_CHOOSER_ACTION_SELECT_FOLDER = gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER
|
---|
69 | FILE_CHOOSER_ACTION_OPEN = gtk.FILE_CHOOSER_ACTION_OPEN
|
---|
70 | FILE_CHOOSER_ACTION_SAVE = gtk.FILE_CHOOSER_ACTION_SAVE
|
---|
71 |
|
---|
72 | SELECTION_MULTIPLE = gtk.SELECTION_MULTIPLE
|
---|
73 |
|
---|
74 | SHADOW_IN = gtk.SHADOW_IN
|
---|
75 | SHADOW_NONE = gtk.SHADOW_NONE
|
---|
76 |
|
---|
77 | POLICY_AUTOMATIC = gtk.POLICY_AUTOMATIC
|
---|
78 |
|
---|
79 | WEIGHT_NORMAL = pango.WEIGHT_NORMAL
|
---|
80 | WEIGHT_BOLD = pango.WEIGHT_BOLD
|
---|
81 |
|
---|
82 | WINDOW_STATE_ICONIFIED = gdk.WINDOW_STATE_ICONIFIED
|
---|
83 | WINDOW_STATE_WITHDRAWN = gdk.WINDOW_STATE_WITHDRAWN
|
---|
84 |
|
---|
85 | SORT_ASCENDING = gtk.SORT_ASCENDING
|
---|
86 | SORT_DESCENDING = gtk.SORT_DESCENDING
|
---|
87 |
|
---|
88 | EVENT_BUTTON_PRESS = gdk.BUTTON_PRESS
|
---|
89 |
|
---|
90 | TREE_VIEW_COLUMN_FIXED = gtk.TREE_VIEW_COLUMN_FIXED
|
---|
91 |
|
---|
92 | pixbuf_new_from_file = gdk.pixbuf_new_from_file
|
---|
93 |
|
---|
94 | def text2unicode(text):
|
---|
95 | """Convert the given text, returned by a Gtk widget, to Unicode."""
|
---|
96 | return unicode(text)
|
---|
97 |
|
---|
98 | def text2str(text):
|
---|
99 | """Convert the given text, returned by xstr to a string."""
|
---|
100 | return str(text)
|
---|
101 |
|
---|
102 | else:
|
---|
103 | print "Using PyGObject"
|
---|
104 | pygobject = True
|
---|
105 | from gi.repository import Gdk as gdk
|
---|
106 | from gi.repository import GdkPixbuf as gdkPixbuf
|
---|
107 | from gi.repository import Gtk as gtk
|
---|
108 | from gi.repository import GObject as gobject
|
---|
109 | from gi.repository import AppIndicator3 as appindicator
|
---|
110 | from gi.repository import Pango as pango
|
---|
111 | appIndicator = True
|
---|
112 |
|
---|
113 | MESSAGETYPE_ERROR = gtk.MessageType.ERROR
|
---|
114 | MESSAGETYPE_QUESTION = gtk.MessageType.QUESTION
|
---|
115 | MESSAGETYPE_INFO = gtk.MessageType.INFO
|
---|
116 | RESPONSETYPE_OK = gtk.ResponseType.OK
|
---|
117 | RESPONSETYPE_YES = gtk.ResponseType.YES
|
---|
118 | RESPONSETYPE_NO = gtk.ResponseType.NO
|
---|
119 | RESPONSETYPE_ACCEPT = gtk.ResponseType.ACCEPT
|
---|
120 | RESPONSETYPE_REJECT = gtk.ResponseType.REJECT
|
---|
121 | RESPONSETYPE_CANCEL = gtk.ResponseType.CANCEL
|
---|
122 | ACCEL_VISIBLE = gtk.AccelFlags.VISIBLE
|
---|
123 | CONTROL_MASK = gdk.ModifierType.CONTROL_MASK
|
---|
124 | DIALOG_MODAL = gtk.DialogFlags.MODAL
|
---|
125 | WRAP_WORD = gtk.WrapMode.WORD
|
---|
126 | JUSTIFY_CENTER = gtk.Justification.CENTER
|
---|
127 |
|
---|
128 | CONTROL_MASK = gdk.ModifierType.CONTROL_MASK
|
---|
129 | SHIFT_MASK = gdk.ModifierType.SHIFT_MASK
|
---|
130 | BUTTON1_MASK = gdk.ModifierType.BUTTON1_MASK
|
---|
131 |
|
---|
132 | SCROLL_UP = gdk.ScrollDirection.UP
|
---|
133 | SCROLL_DOWN = gdk.ScrollDirection.DOWN
|
---|
134 |
|
---|
135 | SPIN_USER_DEFINED = gtk.SpinType.USER_DEFINED
|
---|
136 |
|
---|
137 | FILE_CHOOSER_ACTION_SELECT_FOLDER = gtk.FileChooserAction.SELECT_FOLDER
|
---|
138 | FILE_CHOOSER_ACTION_OPEN = gtk.FileChooserAction.OPEN
|
---|
139 | FILE_CHOOSER_ACTION_SAVE = gtk.FileChooserAction.SAVE
|
---|
140 |
|
---|
141 | SELECTION_MULTIPLE = gtk.SelectionMode.MULTIPLE
|
---|
142 |
|
---|
143 | SHADOW_IN = gtk.ShadowType.IN
|
---|
144 | SHADOW_NONE = gtk.ShadowType.NONE
|
---|
145 |
|
---|
146 | POLICY_AUTOMATIC = gtk.PolicyType.AUTOMATIC
|
---|
147 |
|
---|
148 | WEIGHT_NORMAL = pango.Weight.NORMAL
|
---|
149 | WEIGHT_BOLD = pango.Weight.BOLD
|
---|
150 |
|
---|
151 | WINDOW_STATE_ICONIFIED = gdk.WindowState.ICONIFIED
|
---|
152 | WINDOW_STATE_WITHDRAWN = gdk.WindowState.WITHDRAWN
|
---|
153 |
|
---|
154 | SORT_ASCENDING = gtk.SortType.ASCENDING
|
---|
155 | SORT_DESCENDING = gtk.SortType.DESCENDING
|
---|
156 |
|
---|
157 | EVENT_BUTTON_PRESS = gdk.EventType.BUTTON_PRESS
|
---|
158 |
|
---|
159 | TREE_VIEW_COLUMN_FIXED = gtk.TreeViewColumnSizing.FIXED
|
---|
160 |
|
---|
161 | pixbuf_new_from_file = gdkPixbuf.Pixbuf.new_from_file
|
---|
162 |
|
---|
163 | import codecs
|
---|
164 | _utf8Decoder = codecs.getdecoder("utf-8")
|
---|
165 |
|
---|
166 | def text2unicode(str):
|
---|
167 | """Convert the given text, returned by a Gtk widget, to Unicode."""
|
---|
168 | return _utf8Decoder(str)[0]
|
---|
169 |
|
---|
170 | def text2str(text):
|
---|
171 | """Convert the given text, returned by xstr to a string."""
|
---|
172 | return _utf8Decoder(text)[0]
|
---|
173 |
|
---|
174 | import cairo
|
---|
175 |
|
---|
176 | #------------------------------------------------------------------------------
|
---|
177 |
|
---|
178 | class FlightStatusHandler(object):
|
---|
179 | """Base class for objects that handle the flight status in some way."""
|
---|
180 | def __init__(self):
|
---|
181 | self._stage = None
|
---|
182 | self._rating = 100
|
---|
183 | self._noGoReason = None
|
---|
184 |
|
---|
185 | def resetFlightStatus(self):
|
---|
186 | """Reset the flight status."""
|
---|
187 | self._stage = None
|
---|
188 | self._rating = 100
|
---|
189 | self._noGoReason = None
|
---|
190 | self._updateFlightStatus()
|
---|
191 |
|
---|
192 | def setStage(self, stage):
|
---|
193 | """Set the stage of the flight."""
|
---|
194 | if stage!=self._stage:
|
---|
195 | self._stage = stage
|
---|
196 | self._updateFlightStatus()
|
---|
197 |
|
---|
198 | def setRating(self, rating):
|
---|
199 | """Set the rating to the given value."""
|
---|
200 | if rating!=self._rating:
|
---|
201 | self._rating = rating
|
---|
202 | if self._noGoReason is None:
|
---|
203 | self._updateFlightStatus()
|
---|
204 |
|
---|
205 | def setNoGo(self, reason):
|
---|
206 | """Set a No-Go condition with the given reason."""
|
---|
207 | if self._noGoReason is None:
|
---|
208 | self._noGoReason = reason
|
---|
209 | self._updateFlightStatus()
|
---|
210 |
|
---|
211 | #------------------------------------------------------------------------------
|
---|
212 |
|
---|
213 | class IntegerEntry(gtk.Entry):
|
---|
214 | """An entry that allows only either an empty value, or an integer."""
|
---|
215 | def __init__(self, defaultValue = None):
|
---|
216 | """Construct the entry."""
|
---|
217 | gtk.Entry.__init__(self)
|
---|
218 |
|
---|
219 | self.set_alignment(1.0)
|
---|
220 |
|
---|
221 | self._defaultValue = defaultValue
|
---|
222 | self._currentInteger = defaultValue
|
---|
223 | self._selfSetting = False
|
---|
224 | self._set_text()
|
---|
225 |
|
---|
226 | self.connect("changed", self._handle_changed)
|
---|
227 |
|
---|
228 | def get_int(self):
|
---|
229 | """Get the integer."""
|
---|
230 | return self._currentInteger
|
---|
231 |
|
---|
232 | def reset(self):
|
---|
233 | """Reset the integer."""
|
---|
234 | self.set_int(None)
|
---|
235 |
|
---|
236 | def set_int(self, value):
|
---|
237 | """Set the integer."""
|
---|
238 | if value!=self._currentInteger:
|
---|
239 | self._currentInteger = value
|
---|
240 | self.emit("integer-changed", self._currentInteger)
|
---|
241 | self._set_text()
|
---|
242 |
|
---|
243 | def _handle_changed(self, widget):
|
---|
244 | """Handle the changed signal."""
|
---|
245 | if self._selfSetting:
|
---|
246 | return
|
---|
247 | text = self.get_text()
|
---|
248 | if text=="":
|
---|
249 | self.set_int(self._defaultValue)
|
---|
250 | else:
|
---|
251 | try:
|
---|
252 | self.set_int(int(text))
|
---|
253 | except:
|
---|
254 | self._set_text()
|
---|
255 |
|
---|
256 | def _set_text(self):
|
---|
257 | """Set the text value from the current integer."""
|
---|
258 | self._selfSetting = True
|
---|
259 | self.set_text("" if self._currentInteger is None
|
---|
260 | else str(self._currentInteger))
|
---|
261 | self._selfSetting = False
|
---|
262 |
|
---|
263 | #------------------------------------------------------------------------------
|
---|
264 |
|
---|
265 | gobject.signal_new("integer-changed", IntegerEntry, gobject.SIGNAL_RUN_FIRST,
|
---|
266 | None, (object,))
|
---|
267 |
|
---|
268 | #------------------------------------------------------------------------------
|
---|
269 |
|
---|
270 | PROGRAM_NAME = "MAVA Logger X"
|
---|
271 |
|
---|
272 | WINDOW_TITLE_BASE = PROGRAM_NAME + " " + _const.VERSION
|
---|
273 | if secondaryInstallation:
|
---|
274 | WINDOW_TITLE_BASE += " (" + xstr("secondary") + ")"
|
---|
275 |
|
---|
276 | #------------------------------------------------------------------------------
|
---|
277 |
|
---|
278 | # A mapping of aircraft types to their screen names
|
---|
279 | aircraftNames = { _const.AIRCRAFT_B736 : xstr("aircraft_b736"),
|
---|
280 | _const.AIRCRAFT_B737 : xstr("aircraft_b737"),
|
---|
281 | _const.AIRCRAFT_B738 : xstr("aircraft_b738"),
|
---|
282 | _const.AIRCRAFT_B738C : xstr("aircraft_b738c"),
|
---|
283 | _const.AIRCRAFT_B733 : xstr("aircraft_b733"),
|
---|
284 | _const.AIRCRAFT_B734 : xstr("aircraft_b734"),
|
---|
285 | _const.AIRCRAFT_B735 : xstr("aircraft_b735"),
|
---|
286 | _const.AIRCRAFT_DH8D : xstr("aircraft_dh8d"),
|
---|
287 | _const.AIRCRAFT_B762 : xstr("aircraft_b762"),
|
---|
288 | _const.AIRCRAFT_B763 : xstr("aircraft_b763"),
|
---|
289 | _const.AIRCRAFT_CRJ2 : xstr("aircraft_crj2"),
|
---|
290 | _const.AIRCRAFT_F70 : xstr("aircraft_f70"),
|
---|
291 | _const.AIRCRAFT_DC3 : xstr("aircraft_dc3"),
|
---|
292 | _const.AIRCRAFT_T134 : xstr("aircraft_t134"),
|
---|
293 | _const.AIRCRAFT_T154 : xstr("aircraft_t154"),
|
---|
294 | _const.AIRCRAFT_YK40 : xstr("aircraft_yk40") }
|
---|
295 |
|
---|
296 | #------------------------------------------------------------------------------
|
---|
297 |
|
---|
298 | def formatFlightLogLine(timeStr, line):
|
---|
299 | """Format the given flight log line."""
|
---|
300 | """Format the given line for flight logging."""
|
---|
301 | if timeStr is not None:
|
---|
302 | line = timeStr + ": " + line
|
---|
303 | return line + "\n"
|
---|
304 |
|
---|
305 | #------------------------------------------------------------------------------
|
---|
306 |
|
---|
307 | def addFaultTag(buffer):
|
---|
308 | """Add a tag named 'fault' to the given buffer."""
|
---|
309 | buffer.create_tag("fault", foreground="red", weight=WEIGHT_BOLD)
|
---|
310 |
|
---|
311 | #------------------------------------------------------------------------------
|
---|
312 |
|
---|
313 | def appendTextBuffer(buffer, text, isFault = False):
|
---|
314 | """Append the given line at the end of the given text buffer.
|
---|
315 |
|
---|
316 | If isFault is set, use the tag named 'fault'."""
|
---|
317 | insertTextBuffer(buffer, buffer.get_end_iter(), text, isFault)
|
---|
318 |
|
---|
319 | #------------------------------------------------------------------------------
|
---|
320 |
|
---|
321 | def insertTextBuffer(buffer, iter, text, isFault = False):
|
---|
322 | """Insert the given line into the given text buffer at the given iterator.
|
---|
323 |
|
---|
324 | If isFault is set, use the tag named 'fault' else use the tag named
|
---|
325 | 'normal'."""
|
---|
326 | line = iter.get_line()
|
---|
327 |
|
---|
328 | buffer.insert(iter, text)
|
---|
329 |
|
---|
330 | iter0 = buffer.get_iter_at_line(line)
|
---|
331 | iter1 = buffer.get_iter_at_line(line+1)
|
---|
332 | if isFault:
|
---|
333 | buffer.apply_tag_by_name("fault", iter0, iter1)
|
---|
334 | else:
|
---|
335 | buffer.remove_all_tags(iter0, iter1)
|
---|
336 |
|
---|
337 | #------------------------------------------------------------------------------
|
---|