source: src/mlx/gui/common.py@ 271:fa4c688d0fb4

Last change on this file since 271:fa4c688d0fb4 was 264:6fbadb22b2c3, checked in by István Váradi <ivaradi@…>, 12 years ago

Started implementing the approach callout editor

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