source: src/mlx/gui/common.py@ 277:33fbad0ca891

Last change on this file since 277:33fbad0ca891 was 277:33fbad0ca891, checked in by István Váradi <ivaradi@…>, 12 years ago

Added popup menu to the list of flights

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