source: src/mlx/gui/common.py@ 262:16f4855e111a

Last change on this file since 262:16f4855e111a was 246:bf4298ec3d46, checked in by István Váradi <ivaradi@…>, 12 years ago

Fixed the problem with the main window not being responsive to keystrokes after showing after being hidden by minimization

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