source: src/mlx/gui/common.py@ 226:473bbc9d355f

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

Faults are logged highlighted

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