[28] | 1 |
|
---|
[105] | 2 | import mlx.const as _const
|
---|
[175] | 3 | from mlx.i18n import xstr
|
---|
[105] | 4 |
|
---|
[373] | 5 | from mlx.util import secondaryInstallation
|
---|
| 6 |
|
---|
[28] | 7 | import os
|
---|
| 8 |
|
---|
[300] | 9 | #-----------------------------------------------------------------------------
|
---|
| 10 |
|
---|
| 11 | ## @package mlx.gui.common
|
---|
| 12 | #
|
---|
| 13 | # Common definitions and utilities for the GUI
|
---|
| 14 | #
|
---|
| 15 | # The main purpose of this module is to provide common definitions for things
|
---|
| 16 | # that are named differently in Gtk+ 2 and 3. This way the other parts of the
|
---|
[474] | 17 | # GUI have to check the version in use very rarely. The variable \c pygobject
|
---|
[300] | 18 | # tells which version is being used. If it is \c True, Gtk+ 3 is used via the
|
---|
| 19 | # PyGObject interface. Otherwise Gtk+ 2 is used, which is the default on
|
---|
| 20 | # Windows or when the \c FORCE_PYGTK environment variable is set.
|
---|
| 21 | #
|
---|
[302] | 22 | # Besides this there are some common utility classes and functions.
|
---|
[300] | 23 |
|
---|
| 24 | #-----------------------------------------------------------------------------
|
---|
| 25 |
|
---|
[28] | 26 | appIndicator = False
|
---|
| 27 |
|
---|
| 28 | if os.name=="nt" or "FORCE_PYGTK" in os.environ:
|
---|
| 29 | print "Using PyGTK"
|
---|
| 30 | pygobject = False
|
---|
| 31 | import pygtk
|
---|
[29] | 32 | import gtk.gdk as gdk
|
---|
[28] | 33 | import gtk
|
---|
| 34 | import gobject
|
---|
[44] | 35 | import pango
|
---|
[28] | 36 | try:
|
---|
| 37 | import appindicator
|
---|
| 38 | appIndicator = True
|
---|
| 39 | except Exception, e:
|
---|
| 40 | pass
|
---|
[42] | 41 |
|
---|
| 42 | MESSAGETYPE_ERROR = gtk.MESSAGE_ERROR
|
---|
[76] | 43 | MESSAGETYPE_QUESTION = gtk.MESSAGE_QUESTION
|
---|
[97] | 44 | MESSAGETYPE_INFO = gtk.MESSAGE_INFO
|
---|
[175] | 45 |
|
---|
[124] | 46 | RESPONSETYPE_OK = gtk.RESPONSE_OK
|
---|
[76] | 47 | RESPONSETYPE_YES = gtk.RESPONSE_YES
|
---|
[124] | 48 | RESPONSETYPE_NO = gtk.RESPONSE_NO
|
---|
[123] | 49 | RESPONSETYPE_ACCEPT = gtk.RESPONSE_ACCEPT
|
---|
| 50 | RESPONSETYPE_REJECT = gtk.RESPONSE_REJECT
|
---|
[149] | 51 | RESPONSETYPE_CANCEL = gtk.RESPONSE_CANCEL
|
---|
[175] | 52 |
|
---|
[93] | 53 | ACCEL_VISIBLE = gtk.ACCEL_VISIBLE
|
---|
| 54 | CONTROL_MASK = gdk.CONTROL_MASK
|
---|
[123] | 55 | DIALOG_MODAL = gtk.DIALOG_MODAL
|
---|
[127] | 56 | WRAP_WORD = gtk.WRAP_WORD
|
---|
[132] | 57 | JUSTIFY_CENTER = gtk.JUSTIFY_CENTER
|
---|
[97] | 58 |
|
---|
[144] | 59 | CONTROL_MASK = gdk.CONTROL_MASK
|
---|
| 60 | SHIFT_MASK = gdk.SHIFT_MASK
|
---|
| 61 | BUTTON1_MASK = gdk.BUTTON1_MASK
|
---|
| 62 |
|
---|
| 63 | SCROLL_UP = gdk.SCROLL_UP
|
---|
| 64 | SCROLL_DOWN = gdk.SCROLL_DOWN
|
---|
| 65 |
|
---|
| 66 | SPIN_USER_DEFINED = gtk.SPIN_USER_DEFINED
|
---|
| 67 |
|
---|
[149] | 68 | FILE_CHOOSER_ACTION_SELECT_FOLDER = gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER
|
---|
[151] | 69 | FILE_CHOOSER_ACTION_OPEN = gtk.FILE_CHOOSER_ACTION_OPEN
|
---|
| 70 | FILE_CHOOSER_ACTION_SAVE = gtk.FILE_CHOOSER_ACTION_SAVE
|
---|
[149] | 71 |
|
---|
[175] | 72 | SELECTION_MULTIPLE = gtk.SELECTION_MULTIPLE
|
---|
| 73 |
|
---|
[220] | 74 | SHADOW_IN = gtk.SHADOW_IN
|
---|
[433] | 75 | SHADOW_NONE = gtk.SHADOW_NONE
|
---|
[220] | 76 |
|
---|
| 77 | POLICY_AUTOMATIC = gtk.POLICY_AUTOMATIC
|
---|
| 78 |
|
---|
[362] | 79 | WEIGHT_NORMAL = pango.WEIGHT_NORMAL
|
---|
[226] | 80 | WEIGHT_BOLD = pango.WEIGHT_BOLD
|
---|
| 81 |
|
---|
[246] | 82 | WINDOW_STATE_ICONIFIED = gdk.WINDOW_STATE_ICONIFIED
|
---|
| 83 | WINDOW_STATE_WITHDRAWN = gdk.WINDOW_STATE_WITHDRAWN
|
---|
| 84 |
|
---|
[264] | 85 | SORT_ASCENDING = gtk.SORT_ASCENDING
|
---|
| 86 | SORT_DESCENDING = gtk.SORT_DESCENDING
|
---|
| 87 |
|
---|
[277] | 88 | EVENT_BUTTON_PRESS = gdk.BUTTON_PRESS
|
---|
| 89 |
|
---|
[433] | 90 | TREE_VIEW_COLUMN_FIXED = gtk.TREE_VIEW_COLUMN_FIXED
|
---|
| 91 |
|
---|
[438] | 92 | FILL = gtk.FILL
|
---|
| 93 | EXPAND = gtk.EXPAND
|
---|
| 94 |
|
---|
[227] | 95 | pixbuf_new_from_file = gdk.pixbuf_new_from_file
|
---|
| 96 |
|
---|
[97] | 97 | def text2unicode(text):
|
---|
| 98 | """Convert the given text, returned by a Gtk widget, to Unicode."""
|
---|
| 99 | return unicode(text)
|
---|
[276] | 100 |
|
---|
| 101 | def text2str(text):
|
---|
| 102 | """Convert the given text, returned by xstr to a string."""
|
---|
| 103 | return str(text)
|
---|
[401] | 104 |
|
---|
[28] | 105 | else:
|
---|
| 106 | print "Using PyGObject"
|
---|
| 107 | pygobject = True
|
---|
[29] | 108 | from gi.repository import Gdk as gdk
|
---|
[227] | 109 | from gi.repository import GdkPixbuf as gdkPixbuf
|
---|
[28] | 110 | from gi.repository import Gtk as gtk
|
---|
| 111 | from gi.repository import GObject as gobject
|
---|
| 112 | from gi.repository import AppIndicator3 as appindicator
|
---|
[44] | 113 | from gi.repository import Pango as pango
|
---|
[28] | 114 | appIndicator = True
|
---|
[401] | 115 |
|
---|
[42] | 116 | MESSAGETYPE_ERROR = gtk.MessageType.ERROR
|
---|
[76] | 117 | MESSAGETYPE_QUESTION = gtk.MessageType.QUESTION
|
---|
[97] | 118 | MESSAGETYPE_INFO = gtk.MessageType.INFO
|
---|
[124] | 119 | RESPONSETYPE_OK = gtk.ResponseType.OK
|
---|
[76] | 120 | RESPONSETYPE_YES = gtk.ResponseType.YES
|
---|
[124] | 121 | RESPONSETYPE_NO = gtk.ResponseType.NO
|
---|
[123] | 122 | RESPONSETYPE_ACCEPT = gtk.ResponseType.ACCEPT
|
---|
| 123 | RESPONSETYPE_REJECT = gtk.ResponseType.REJECT
|
---|
[149] | 124 | RESPONSETYPE_CANCEL = gtk.ResponseType.CANCEL
|
---|
[93] | 125 | ACCEL_VISIBLE = gtk.AccelFlags.VISIBLE
|
---|
| 126 | CONTROL_MASK = gdk.ModifierType.CONTROL_MASK
|
---|
[123] | 127 | DIALOG_MODAL = gtk.DialogFlags.MODAL
|
---|
[127] | 128 | WRAP_WORD = gtk.WrapMode.WORD
|
---|
[132] | 129 | JUSTIFY_CENTER = gtk.Justification.CENTER
|
---|
[42] | 130 |
|
---|
[144] | 131 | CONTROL_MASK = gdk.ModifierType.CONTROL_MASK
|
---|
| 132 | SHIFT_MASK = gdk.ModifierType.SHIFT_MASK
|
---|
| 133 | BUTTON1_MASK = gdk.ModifierType.BUTTON1_MASK
|
---|
| 134 |
|
---|
| 135 | SCROLL_UP = gdk.ScrollDirection.UP
|
---|
| 136 | SCROLL_DOWN = gdk.ScrollDirection.DOWN
|
---|
| 137 |
|
---|
| 138 | SPIN_USER_DEFINED = gtk.SpinType.USER_DEFINED
|
---|
| 139 |
|
---|
[149] | 140 | FILE_CHOOSER_ACTION_SELECT_FOLDER = gtk.FileChooserAction.SELECT_FOLDER
|
---|
[151] | 141 | FILE_CHOOSER_ACTION_OPEN = gtk.FileChooserAction.OPEN
|
---|
| 142 | FILE_CHOOSER_ACTION_SAVE = gtk.FileChooserAction.SAVE
|
---|
[149] | 143 |
|
---|
[175] | 144 | SELECTION_MULTIPLE = gtk.SelectionMode.MULTIPLE
|
---|
| 145 |
|
---|
[220] | 146 | SHADOW_IN = gtk.ShadowType.IN
|
---|
[433] | 147 | SHADOW_NONE = gtk.ShadowType.NONE
|
---|
[220] | 148 |
|
---|
| 149 | POLICY_AUTOMATIC = gtk.PolicyType.AUTOMATIC
|
---|
| 150 |
|
---|
[362] | 151 | WEIGHT_NORMAL = pango.Weight.NORMAL
|
---|
[226] | 152 | WEIGHT_BOLD = pango.Weight.BOLD
|
---|
| 153 |
|
---|
[246] | 154 | WINDOW_STATE_ICONIFIED = gdk.WindowState.ICONIFIED
|
---|
| 155 | WINDOW_STATE_WITHDRAWN = gdk.WindowState.WITHDRAWN
|
---|
| 156 |
|
---|
[264] | 157 | SORT_ASCENDING = gtk.SortType.ASCENDING
|
---|
| 158 | SORT_DESCENDING = gtk.SortType.DESCENDING
|
---|
| 159 |
|
---|
[277] | 160 | EVENT_BUTTON_PRESS = gdk.EventType.BUTTON_PRESS
|
---|
| 161 |
|
---|
[433] | 162 | TREE_VIEW_COLUMN_FIXED = gtk.TreeViewColumnSizing.FIXED
|
---|
| 163 |
|
---|
[438] | 164 | FILL = gtk.AttachOptions.FILL
|
---|
| 165 | EXPAND = gtk.AttachOptions.EXPAND
|
---|
| 166 |
|
---|
[227] | 167 | pixbuf_new_from_file = gdkPixbuf.Pixbuf.new_from_file
|
---|
| 168 |
|
---|
[97] | 169 | import codecs
|
---|
| 170 | _utf8Decoder = codecs.getdecoder("utf-8")
|
---|
[401] | 171 |
|
---|
[97] | 172 | def text2unicode(str):
|
---|
| 173 | """Convert the given text, returned by a Gtk widget, to Unicode."""
|
---|
| 174 | return _utf8Decoder(str)[0]
|
---|
| 175 |
|
---|
[276] | 176 | def text2str(text):
|
---|
| 177 | """Convert the given text, returned by xstr to a string."""
|
---|
| 178 | return _utf8Decoder(text)[0]
|
---|
| 179 |
|
---|
[28] | 180 | import cairo
|
---|
| 181 |
|
---|
[32] | 182 | #------------------------------------------------------------------------------
|
---|
| 183 |
|
---|
| 184 | class FlightStatusHandler(object):
|
---|
| 185 | """Base class for objects that handle the flight status in some way."""
|
---|
| 186 | def __init__(self):
|
---|
| 187 | self._stage = None
|
---|
| 188 | self._rating = 100
|
---|
| 189 | self._noGoReason = None
|
---|
| 190 |
|
---|
| 191 | def resetFlightStatus(self):
|
---|
| 192 | """Reset the flight status."""
|
---|
| 193 | self._stage = None
|
---|
| 194 | self._rating = 100
|
---|
| 195 | self._noGoReason = None
|
---|
| 196 | self._updateFlightStatus()
|
---|
[401] | 197 |
|
---|
[32] | 198 | def setStage(self, stage):
|
---|
| 199 | """Set the stage of the flight."""
|
---|
| 200 | if stage!=self._stage:
|
---|
| 201 | self._stage = stage
|
---|
| 202 | self._updateFlightStatus()
|
---|
| 203 |
|
---|
| 204 | def setRating(self, rating):
|
---|
| 205 | """Set the rating to the given value."""
|
---|
| 206 | if rating!=self._rating:
|
---|
| 207 | self._rating = rating
|
---|
| 208 | if self._noGoReason is None:
|
---|
| 209 | self._updateFlightStatus()
|
---|
| 210 |
|
---|
| 211 | def setNoGo(self, reason):
|
---|
| 212 | """Set a No-Go condition with the given reason."""
|
---|
| 213 | if self._noGoReason is None:
|
---|
| 214 | self._noGoReason = reason
|
---|
| 215 | self._updateFlightStatus()
|
---|
| 216 |
|
---|
| 217 | #------------------------------------------------------------------------------
|
---|
[84] | 218 |
|
---|
| 219 | class IntegerEntry(gtk.Entry):
|
---|
| 220 | """An entry that allows only either an empty value, or an integer."""
|
---|
| 221 | def __init__(self, defaultValue = None):
|
---|
| 222 | """Construct the entry."""
|
---|
| 223 | gtk.Entry.__init__(self)
|
---|
| 224 |
|
---|
[86] | 225 | self.set_alignment(1.0)
|
---|
| 226 |
|
---|
[84] | 227 | self._defaultValue = defaultValue
|
---|
| 228 | self._currentInteger = defaultValue
|
---|
| 229 | self._selfSetting = False
|
---|
| 230 | self._set_text()
|
---|
| 231 |
|
---|
| 232 | self.connect("changed", self._handle_changed)
|
---|
| 233 |
|
---|
| 234 | def get_int(self):
|
---|
| 235 | """Get the integer."""
|
---|
| 236 | return self._currentInteger
|
---|
| 237 |
|
---|
[241] | 238 | def reset(self):
|
---|
| 239 | """Reset the integer."""
|
---|
| 240 | self.set_int(None)
|
---|
| 241 |
|
---|
[84] | 242 | def set_int(self, value):
|
---|
| 243 | """Set the integer."""
|
---|
| 244 | if value!=self._currentInteger:
|
---|
| 245 | self._currentInteger = value
|
---|
| 246 | self.emit("integer-changed", self._currentInteger)
|
---|
| 247 | self._set_text()
|
---|
[401] | 248 |
|
---|
[84] | 249 | def _handle_changed(self, widget):
|
---|
| 250 | """Handle the changed signal."""
|
---|
| 251 | if self._selfSetting:
|
---|
| 252 | return
|
---|
| 253 | text = self.get_text()
|
---|
| 254 | if text=="":
|
---|
| 255 | self.set_int(self._defaultValue)
|
---|
| 256 | else:
|
---|
| 257 | try:
|
---|
| 258 | self.set_int(int(text))
|
---|
| 259 | except:
|
---|
| 260 | self._set_text()
|
---|
| 261 |
|
---|
| 262 | def _set_text(self):
|
---|
| 263 | """Set the text value from the current integer."""
|
---|
| 264 | self._selfSetting = True
|
---|
| 265 | self.set_text("" if self._currentInteger is None
|
---|
| 266 | else str(self._currentInteger))
|
---|
| 267 | self._selfSetting = False
|
---|
[401] | 268 |
|
---|
[84] | 269 | #------------------------------------------------------------------------------
|
---|
| 270 |
|
---|
| 271 | gobject.signal_new("integer-changed", IntegerEntry, gobject.SIGNAL_RUN_FIRST,
|
---|
| 272 | None, (object,))
|
---|
| 273 |
|
---|
| 274 | #------------------------------------------------------------------------------
|
---|
[105] | 275 |
|
---|
[227] | 276 | PROGRAM_NAME = "MAVA Logger X"
|
---|
| 277 |
|
---|
[232] | 278 | WINDOW_TITLE_BASE = PROGRAM_NAME + " " + _const.VERSION
|
---|
[373] | 279 | if secondaryInstallation:
|
---|
| 280 | WINDOW_TITLE_BASE += " (" + xstr("secondary") + ")"
|
---|
[105] | 281 |
|
---|
| 282 | #------------------------------------------------------------------------------
|
---|
[175] | 283 |
|
---|
| 284 | # A mapping of aircraft types to their screen names
|
---|
[191] | 285 | aircraftNames = { _const.AIRCRAFT_B736 : xstr("aircraft_b736"),
|
---|
| 286 | _const.AIRCRAFT_B737 : xstr("aircraft_b737"),
|
---|
| 287 | _const.AIRCRAFT_B738 : xstr("aircraft_b738"),
|
---|
| 288 | _const.AIRCRAFT_B738C : xstr("aircraft_b738c"),
|
---|
| 289 | _const.AIRCRAFT_B733 : xstr("aircraft_b733"),
|
---|
| 290 | _const.AIRCRAFT_B734 : xstr("aircraft_b734"),
|
---|
| 291 | _const.AIRCRAFT_B735 : xstr("aircraft_b735"),
|
---|
| 292 | _const.AIRCRAFT_DH8D : xstr("aircraft_dh8d"),
|
---|
| 293 | _const.AIRCRAFT_B762 : xstr("aircraft_b762"),
|
---|
| 294 | _const.AIRCRAFT_B763 : xstr("aircraft_b763"),
|
---|
| 295 | _const.AIRCRAFT_CRJ2 : xstr("aircraft_crj2"),
|
---|
| 296 | _const.AIRCRAFT_F70 : xstr("aircraft_f70"),
|
---|
| 297 | _const.AIRCRAFT_DC3 : xstr("aircraft_dc3"),
|
---|
| 298 | _const.AIRCRAFT_T134 : xstr("aircraft_t134"),
|
---|
| 299 | _const.AIRCRAFT_T154 : xstr("aircraft_t154"),
|
---|
[443] | 300 | _const.AIRCRAFT_YK40 : xstr("aircraft_yk40"),
|
---|
| 301 | _const.AIRCRAFT_B462 : xstr("aircraft_b462") }
|
---|
[175] | 302 |
|
---|
| 303 | #------------------------------------------------------------------------------
|
---|
[221] | 304 |
|
---|
[226] | 305 | def formatFlightLogLine(timeStr, line):
|
---|
[221] | 306 | """Format the given flight log line."""
|
---|
| 307 | """Format the given line for flight logging."""
|
---|
| 308 | if timeStr is not None:
|
---|
| 309 | line = timeStr + ": " + line
|
---|
| 310 | return line + "\n"
|
---|
| 311 |
|
---|
| 312 | #------------------------------------------------------------------------------
|
---|
[226] | 313 |
|
---|
| 314 | def addFaultTag(buffer):
|
---|
| 315 | """Add a tag named 'fault' to the given buffer."""
|
---|
[362] | 316 | buffer.create_tag("fault", foreground="red", weight=WEIGHT_BOLD)
|
---|
[226] | 317 |
|
---|
| 318 | #------------------------------------------------------------------------------
|
---|
| 319 |
|
---|
| 320 | def appendTextBuffer(buffer, text, isFault = False):
|
---|
| 321 | """Append the given line at the end of the given text buffer.
|
---|
| 322 |
|
---|
| 323 | If isFault is set, use the tag named 'fault'."""
|
---|
[345] | 324 | insertTextBuffer(buffer, buffer.get_end_iter(), text, isFault)
|
---|
[226] | 325 |
|
---|
| 326 | #------------------------------------------------------------------------------
|
---|
[345] | 327 |
|
---|
| 328 | def insertTextBuffer(buffer, iter, text, isFault = False):
|
---|
| 329 | """Insert the given line into the given text buffer at the given iterator.
|
---|
| 330 |
|
---|
[362] | 331 | If isFault is set, use the tag named 'fault' else use the tag named
|
---|
| 332 | 'normal'."""
|
---|
| 333 | line = iter.get_line()
|
---|
| 334 |
|
---|
| 335 | buffer.insert(iter, text)
|
---|
| 336 |
|
---|
| 337 | iter0 = buffer.get_iter_at_line(line)
|
---|
| 338 | iter1 = buffer.get_iter_at_line(line+1)
|
---|
[345] | 339 | if isFault:
|
---|
[362] | 340 | buffer.apply_tag_by_name("fault", iter0, iter1)
|
---|
[345] | 341 | else:
|
---|
[362] | 342 | buffer.remove_all_tags(iter0, iter1)
|
---|
[345] | 343 |
|
---|
| 344 | #------------------------------------------------------------------------------
|
---|