source: src/mlx/gui/common.py@ 367:ecebdd53757b

Last change on this file since 367:ecebdd53757b was 362:f57c0009eddc, checked in by István Váradi <ivaradi@…>, 12 years ago

Fixed the handling of the tags in the log buffers

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