source: src/mlx/gui/common.py@ 357:95a02e0c97d3

Last change on this file since 357:95a02e0c97d3 was 345:a62373a28d90, checked in by István Váradi <ivaradi@…>, 12 years ago

Implemented the new, more flexible way of storing the log during flight (#143)

File size: 10.6 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_BOLD = pango.WEIGHT_BOLD
77
78 WINDOW_STATE_ICONIFIED = gdk.WINDOW_STATE_ICONIFIED
79 WINDOW_STATE_WITHDRAWN = gdk.WINDOW_STATE_WITHDRAWN
80
81 SORT_ASCENDING = gtk.SORT_ASCENDING
82 SORT_DESCENDING = gtk.SORT_DESCENDING
83
84 EVENT_BUTTON_PRESS = gdk.BUTTON_PRESS
85
86 pixbuf_new_from_file = gdk.pixbuf_new_from_file
87
88 def text2unicode(text):
89 """Convert the given text, returned by a Gtk widget, to Unicode."""
90 return unicode(text)
91
92 def text2str(text):
93 """Convert the given text, returned by xstr to a string."""
94 return str(text)
95else:
96 print "Using PyGObject"
97 pygobject = True
98 from gi.repository import Gdk as gdk
99 from gi.repository import GdkPixbuf as gdkPixbuf
100 from gi.repository import Gtk as gtk
101 from gi.repository import GObject as gobject
102 from gi.repository import AppIndicator3 as appindicator
103 from gi.repository import Pango as pango
104 appIndicator = True
105
106 MESSAGETYPE_ERROR = gtk.MessageType.ERROR
107 MESSAGETYPE_QUESTION = gtk.MessageType.QUESTION
108 MESSAGETYPE_INFO = gtk.MessageType.INFO
109 RESPONSETYPE_OK = gtk.ResponseType.OK
110 RESPONSETYPE_YES = gtk.ResponseType.YES
111 RESPONSETYPE_NO = gtk.ResponseType.NO
112 RESPONSETYPE_ACCEPT = gtk.ResponseType.ACCEPT
113 RESPONSETYPE_REJECT = gtk.ResponseType.REJECT
114 RESPONSETYPE_CANCEL = gtk.ResponseType.CANCEL
115 ACCEL_VISIBLE = gtk.AccelFlags.VISIBLE
116 CONTROL_MASK = gdk.ModifierType.CONTROL_MASK
117 DIALOG_MODAL = gtk.DialogFlags.MODAL
118 WRAP_WORD = gtk.WrapMode.WORD
119 JUSTIFY_CENTER = gtk.Justification.CENTER
120
121 CONTROL_MASK = gdk.ModifierType.CONTROL_MASK
122 SHIFT_MASK = gdk.ModifierType.SHIFT_MASK
123 BUTTON1_MASK = gdk.ModifierType.BUTTON1_MASK
124
125 SCROLL_UP = gdk.ScrollDirection.UP
126 SCROLL_DOWN = gdk.ScrollDirection.DOWN
127
128 SPIN_USER_DEFINED = gtk.SpinType.USER_DEFINED
129
130 FILE_CHOOSER_ACTION_SELECT_FOLDER = gtk.FileChooserAction.SELECT_FOLDER
131 FILE_CHOOSER_ACTION_OPEN = gtk.FileChooserAction.OPEN
132 FILE_CHOOSER_ACTION_SAVE = gtk.FileChooserAction.SAVE
133
134 SELECTION_MULTIPLE = gtk.SelectionMode.MULTIPLE
135
136 SHADOW_IN = gtk.ShadowType.IN
137
138 POLICY_AUTOMATIC = gtk.PolicyType.AUTOMATIC
139
140 WEIGHT_BOLD = pango.Weight.BOLD
141
142 WINDOW_STATE_ICONIFIED = gdk.WindowState.ICONIFIED
143 WINDOW_STATE_WITHDRAWN = gdk.WindowState.WITHDRAWN
144
145 SORT_ASCENDING = gtk.SortType.ASCENDING
146 SORT_DESCENDING = gtk.SortType.DESCENDING
147
148 EVENT_BUTTON_PRESS = gdk.EventType.BUTTON_PRESS
149
150 pixbuf_new_from_file = gdkPixbuf.Pixbuf.new_from_file
151
152 import codecs
153 _utf8Decoder = codecs.getdecoder("utf-8")
154
155 def text2unicode(str):
156 """Convert the given text, returned by a Gtk widget, to Unicode."""
157 return _utf8Decoder(str)[0]
158
159 def text2str(text):
160 """Convert the given text, returned by xstr to a string."""
161 return _utf8Decoder(text)[0]
162
163import cairo
164
165#------------------------------------------------------------------------------
166
167class FlightStatusHandler(object):
168 """Base class for objects that handle the flight status in some way."""
169 def __init__(self):
170 self._stage = None
171 self._rating = 100
172 self._noGoReason = None
173
174 def resetFlightStatus(self):
175 """Reset the flight status."""
176 self._stage = None
177 self._rating = 100
178 self._noGoReason = None
179 self._updateFlightStatus()
180
181 def setStage(self, stage):
182 """Set the stage of the flight."""
183 if stage!=self._stage:
184 self._stage = stage
185 self._updateFlightStatus()
186
187 def setRating(self, rating):
188 """Set the rating to the given value."""
189 if rating!=self._rating:
190 self._rating = rating
191 if self._noGoReason is None:
192 self._updateFlightStatus()
193
194 def setNoGo(self, reason):
195 """Set a No-Go condition with the given reason."""
196 if self._noGoReason is None:
197 self._noGoReason = reason
198 self._updateFlightStatus()
199
200#------------------------------------------------------------------------------
201
202class IntegerEntry(gtk.Entry):
203 """An entry that allows only either an empty value, or an integer."""
204 def __init__(self, defaultValue = None):
205 """Construct the entry."""
206 gtk.Entry.__init__(self)
207
208 self.set_alignment(1.0)
209
210 self._defaultValue = defaultValue
211 self._currentInteger = defaultValue
212 self._selfSetting = False
213 self._set_text()
214
215 self.connect("changed", self._handle_changed)
216
217 def get_int(self):
218 """Get the integer."""
219 return self._currentInteger
220
221 def reset(self):
222 """Reset the integer."""
223 self.set_int(None)
224
225 def set_int(self, value):
226 """Set the integer."""
227 if value!=self._currentInteger:
228 self._currentInteger = value
229 self.emit("integer-changed", self._currentInteger)
230 self._set_text()
231
232 def _handle_changed(self, widget):
233 """Handle the changed signal."""
234 if self._selfSetting:
235 return
236 text = self.get_text()
237 if text=="":
238 self.set_int(self._defaultValue)
239 else:
240 try:
241 self.set_int(int(text))
242 except:
243 self._set_text()
244
245 def _set_text(self):
246 """Set the text value from the current integer."""
247 self._selfSetting = True
248 self.set_text("" if self._currentInteger is None
249 else str(self._currentInteger))
250 self._selfSetting = False
251
252#------------------------------------------------------------------------------
253
254gobject.signal_new("integer-changed", IntegerEntry, gobject.SIGNAL_RUN_FIRST,
255 None, (object,))
256
257#------------------------------------------------------------------------------
258
259PROGRAM_NAME = "MAVA Logger X"
260
261WINDOW_TITLE_BASE = PROGRAM_NAME + " " + _const.VERSION
262
263#------------------------------------------------------------------------------
264
265# A mapping of aircraft types to their screen names
266aircraftNames = { _const.AIRCRAFT_B736 : xstr("aircraft_b736"),
267 _const.AIRCRAFT_B737 : xstr("aircraft_b737"),
268 _const.AIRCRAFT_B738 : xstr("aircraft_b738"),
269 _const.AIRCRAFT_B738C : xstr("aircraft_b738c"),
270 _const.AIRCRAFT_B733 : xstr("aircraft_b733"),
271 _const.AIRCRAFT_B734 : xstr("aircraft_b734"),
272 _const.AIRCRAFT_B735 : xstr("aircraft_b735"),
273 _const.AIRCRAFT_DH8D : xstr("aircraft_dh8d"),
274 _const.AIRCRAFT_B762 : xstr("aircraft_b762"),
275 _const.AIRCRAFT_B763 : xstr("aircraft_b763"),
276 _const.AIRCRAFT_CRJ2 : xstr("aircraft_crj2"),
277 _const.AIRCRAFT_F70 : xstr("aircraft_f70"),
278 _const.AIRCRAFT_DC3 : xstr("aircraft_dc3"),
279 _const.AIRCRAFT_T134 : xstr("aircraft_t134"),
280 _const.AIRCRAFT_T154 : xstr("aircraft_t154"),
281 _const.AIRCRAFT_YK40 : xstr("aircraft_yk40") }
282
283#------------------------------------------------------------------------------
284
285def formatFlightLogLine(timeStr, line):
286 """Format the given flight log line."""
287 """Format the given line for flight logging."""
288 if timeStr is not None:
289 line = timeStr + ": " + line
290 return line + "\n"
291
292#------------------------------------------------------------------------------
293
294def addFaultTag(buffer):
295 """Add a tag named 'fault' to the given buffer."""
296 faultTag = gtk.TextTag(name = "fault")
297 faultTag.set_property("foreground", "red")
298 faultTag.set_property("weight", WEIGHT_BOLD)
299 buffer.get_tag_table().add(faultTag)
300
301#------------------------------------------------------------------------------
302
303def appendTextBuffer(buffer, text, isFault = False):
304 """Append the given line at the end of the given text buffer.
305
306 If isFault is set, use the tag named 'fault'."""
307 insertTextBuffer(buffer, buffer.get_end_iter(), text, isFault)
308
309#------------------------------------------------------------------------------
310
311def insertTextBuffer(buffer, iter, text, isFault = False):
312 """Insert the given line into the given text buffer at the given iterator.
313
314 If isFault is set, use the tag named 'fault'."""
315 if isFault:
316 buffer.insert_with_tags_by_name(iter, text, "fault")
317 else:
318 buffer.insert(iter, text)
319
320#------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.