source: src/mlx/gui/common.py@ 234:9e75e8bff310

Last change on this file since 234:9e75e8bff310 was 232:e8102d6ba023, checked in by István Váradi <ivaradi@…>, 12 years ago

Fixed the window titles

File size: 8.8 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 set_int(self, value):
182 """Set the integer."""
183 if value!=self._currentInteger:
184 self._currentInteger = value
185 self.emit("integer-changed", self._currentInteger)
186 self._set_text()
187
188 def _handle_changed(self, widget):
189 """Handle the changed signal."""
190 if self._selfSetting:
191 return
192 text = self.get_text()
193 if text=="":
194 self.set_int(self._defaultValue)
195 else:
196 try:
197 self.set_int(int(text))
198 except:
199 self._set_text()
200
201 def _set_text(self):
202 """Set the text value from the current integer."""
203 self._selfSetting = True
204 self.set_text("" if self._currentInteger is None
205 else str(self._currentInteger))
206 self._selfSetting = False
207
208#------------------------------------------------------------------------------
209
210gobject.signal_new("integer-changed", IntegerEntry, gobject.SIGNAL_RUN_FIRST,
211 None, (object,))
212
213#------------------------------------------------------------------------------
214
215PROGRAM_NAME = "MAVA Logger X"
216
217WINDOW_TITLE_BASE = PROGRAM_NAME + " " + _const.VERSION
218
219#------------------------------------------------------------------------------
220
221# A mapping of aircraft types to their screen names
222aircraftNames = { _const.AIRCRAFT_B736 : xstr("aircraft_b736"),
223 _const.AIRCRAFT_B737 : xstr("aircraft_b737"),
224 _const.AIRCRAFT_B738 : xstr("aircraft_b738"),
225 _const.AIRCRAFT_B738C : xstr("aircraft_b738c"),
226 _const.AIRCRAFT_B733 : xstr("aircraft_b733"),
227 _const.AIRCRAFT_B734 : xstr("aircraft_b734"),
228 _const.AIRCRAFT_B735 : xstr("aircraft_b735"),
229 _const.AIRCRAFT_DH8D : xstr("aircraft_dh8d"),
230 _const.AIRCRAFT_B762 : xstr("aircraft_b762"),
231 _const.AIRCRAFT_B763 : xstr("aircraft_b763"),
232 _const.AIRCRAFT_CRJ2 : xstr("aircraft_crj2"),
233 _const.AIRCRAFT_F70 : xstr("aircraft_f70"),
234 _const.AIRCRAFT_DC3 : xstr("aircraft_dc3"),
235 _const.AIRCRAFT_T134 : xstr("aircraft_t134"),
236 _const.AIRCRAFT_T154 : xstr("aircraft_t154"),
237 _const.AIRCRAFT_YK40 : xstr("aircraft_yk40") }
238
239#------------------------------------------------------------------------------
240
241def formatFlightLogLine(timeStr, line):
242 """Format the given flight log line."""
243 """Format the given line for flight logging."""
244 if timeStr is not None:
245 line = timeStr + ": " + line
246 return line + "\n"
247
248#------------------------------------------------------------------------------
249
250def addFaultTag(buffer):
251 """Add a tag named 'fault' to the given buffer."""
252 faultTag = gtk.TextTag(name = "fault")
253 faultTag.set_property("foreground", "red")
254 faultTag.set_property("weight", WEIGHT_BOLD)
255 buffer.get_tag_table().add(faultTag)
256
257#------------------------------------------------------------------------------
258
259def appendTextBuffer(buffer, text, isFault = False):
260 """Append the given line at the end of the given text buffer.
261
262 If isFault is set, use the tag named 'fault'."""
263 if isFault:
264 buffer.insert_with_tags_by_name(buffer.get_end_iter(), text,
265 "fault")
266 else:
267 buffer.insert(buffer.get_end_iter(), text)
268
269#------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.