source: src/mlx/gui/common.py@ 241:dea155dd3ac0

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

Added support for calculating speeds in km/h for Soviet aircraft

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