[29] | 1 | # Implementation of the status icon
|
---|
| 2 |
|
---|
| 3 | #-------------------------------------------------------------------------------
|
---|
| 4 |
|
---|
| 5 | from common import *
|
---|
| 6 |
|
---|
[31] | 7 | import mlx.const as const
|
---|
[114] | 8 | from mlx.i18n import xstr
|
---|
[31] | 9 |
|
---|
[29] | 10 | #-------------------------------------------------------------------------------
|
---|
| 11 |
|
---|
[32] | 12 | class StatusIcon(FlightStatusHandler):
|
---|
[29] | 13 | """The class handling the status icon."""
|
---|
| 14 | def __init__(self, iconDirectory, gui):
|
---|
| 15 | """Construct the status icon."""
|
---|
[32] | 16 | super(StatusIcon, self).__init__()
|
---|
| 17 |
|
---|
[29] | 18 | self._gui = gui
|
---|
[35] | 19 | self._selfToggling = False
|
---|
[31] | 20 |
|
---|
[29] | 21 | menu = gtk.Menu()
|
---|
| 22 |
|
---|
[31] | 23 | if appIndicator:
|
---|
| 24 | self._stageMenuItem = gtk.MenuItem()
|
---|
| 25 | self._stageMenuItem.show()
|
---|
| 26 | menu.append(self._stageMenuItem)
|
---|
| 27 |
|
---|
| 28 | self._ratingMenuItem = gtk.MenuItem()
|
---|
| 29 | self._ratingMenuItem.show()
|
---|
| 30 | menu.append(self._ratingMenuItem)
|
---|
| 31 |
|
---|
| 32 | separator = gtk.SeparatorMenuItem()
|
---|
| 33 | separator.show()
|
---|
| 34 | menu.append(separator)
|
---|
| 35 |
|
---|
[29] | 36 | self._showHideMenuItem = gtk.CheckMenuItem()
|
---|
[114] | 37 | self._showHideMenuItem.set_label(xstr("statusicon_showmain"))
|
---|
[29] | 38 | self._showHideMenuItem.set_active(True)
|
---|
| 39 | self._showHideMenuItem.connect("toggled", self._showHideToggled)
|
---|
| 40 | self._showHideMenuItem.show()
|
---|
| 41 | menu.append(self._showHideMenuItem)
|
---|
| 42 |
|
---|
[77] | 43 | self._showMonitorMenuItem = gtk.CheckMenuItem()
|
---|
[114] | 44 | self._showMonitorMenuItem.set_label(xstr("statusicon_showmonitor"))
|
---|
[77] | 45 | self._showMonitorMenuItem.set_active(False)
|
---|
| 46 | self._showMonitorMenuItem.connect("toggled", self._showMonitorToggled)
|
---|
| 47 | self._showMonitorMenuItem.show()
|
---|
| 48 | menu.append(self._showMonitorMenuItem)
|
---|
| 49 |
|
---|
| 50 | separator = gtk.SeparatorMenuItem()
|
---|
| 51 | separator.show()
|
---|
| 52 | menu.append(separator)
|
---|
| 53 |
|
---|
[76] | 54 | self._quitMenuItem = gtk.MenuItem()
|
---|
[114] | 55 | self._quitMenuItem.set_label(xstr("statusicon_quit"))
|
---|
[76] | 56 | self._quitMenuItem.show()
|
---|
| 57 | self._quitMenuItem.connect("activate", self._gui._quit)
|
---|
| 58 | menu.append(self._quitMenuItem)
|
---|
| 59 |
|
---|
[29] | 60 | menu.show()
|
---|
| 61 |
|
---|
| 62 | iconFile = os.path.join(iconDirectory, "logo.ico")
|
---|
| 63 |
|
---|
| 64 | if appIndicator:
|
---|
| 65 | if pygobject:
|
---|
| 66 | indicator = appindicator.Indicator.new ("mava-logger-x", iconFile,
|
---|
| 67 | appindicator.IndicatorCategory.APPLICATION_STATUS)
|
---|
| 68 | indicator.set_status (appindicator.IndicatorStatus.ACTIVE)
|
---|
| 69 | else:
|
---|
| 70 | indicator = appindicator.Indicator ("mava-logger-x", iconFile,
|
---|
| 71 | appindicator.CATEGORY_APPLICATION_STATUS)
|
---|
| 72 | indicator.set_status (appindicator.STATUS_ACTIVE)
|
---|
| 73 |
|
---|
| 74 | indicator.set_menu(menu)
|
---|
| 75 | self._indicator = indicator
|
---|
| 76 | else:
|
---|
| 77 | def popup_menu(status, button, time):
|
---|
| 78 | menu.popup(None, None, gtk.status_icon_position_menu,
|
---|
| 79 | button, time, status)
|
---|
| 80 |
|
---|
| 81 | statusIcon = gtk.StatusIcon()
|
---|
| 82 | statusIcon.set_from_file(iconFile)
|
---|
| 83 | statusIcon.set_visible(True)
|
---|
| 84 | statusIcon.connect('popup-menu', popup_menu)
|
---|
| 85 | statusIcon.connect('activate',
|
---|
| 86 | lambda status: self._gui.toggleMainWindow())
|
---|
| 87 | self._statusIcon = statusIcon
|
---|
[32] | 88 |
|
---|
| 89 | self._updateFlightStatus()
|
---|
[29] | 90 |
|
---|
| 91 | def mainWindowHidden(self):
|
---|
| 92 | """Called when the main window is hidden."""
|
---|
[35] | 93 | if self._showHideMenuItem.get_active():
|
---|
| 94 | self._selfToggling = True
|
---|
| 95 | self._showHideMenuItem.set_active(False)
|
---|
[29] | 96 |
|
---|
| 97 | def mainWindowShown(self):
|
---|
| 98 | """Called when the main window is shown."""
|
---|
[35] | 99 | if not self._showHideMenuItem.get_active():
|
---|
| 100 | self._selfToggling = True
|
---|
| 101 | self._showHideMenuItem.set_active(True)
|
---|
[31] | 102 |
|
---|
[77] | 103 | def monitorWindowHidden(self):
|
---|
| 104 | """Called when the monitor window is hidden."""
|
---|
| 105 | if self._showMonitorMenuItem.get_active():
|
---|
| 106 | self._selfToggling = True
|
---|
| 107 | self._showMonitorMenuItem.set_active(False)
|
---|
| 108 |
|
---|
| 109 | def monitorWindowShown(self):
|
---|
| 110 | """Called when the monitor window is shown."""
|
---|
| 111 | if not self._showMonitorMenuItem.get_active():
|
---|
| 112 | self._selfToggling = True
|
---|
| 113 | self._showMonitorMenuItem.set_active(True)
|
---|
| 114 |
|
---|
[38] | 115 | def destroy(self):
|
---|
| 116 | """Hide and destroy the status icon."""
|
---|
| 117 | if appIndicator:
|
---|
| 118 | if pygobject:
|
---|
| 119 | self._indicator.set_status(appindicator.IndicatorStatus.PASSIVE)
|
---|
| 120 | else:
|
---|
| 121 | self._indicator.set_status(appindicator.STATUS_PASSIVE)
|
---|
| 122 | else:
|
---|
| 123 | self._statusIcon.set_visible(False)
|
---|
| 124 |
|
---|
[29] | 125 | def _showHideToggled(self, menuitem):
|
---|
| 126 | """Called when the show/hide menu item is toggled."""
|
---|
[35] | 127 | if self._selfToggling:
|
---|
| 128 | self._selfToggling = False
|
---|
| 129 | elif self._showHideMenuItem.get_active():
|
---|
[29] | 130 | self._gui.showMainWindow()
|
---|
| 131 | else:
|
---|
| 132 | self._gui.hideMainWindow()
|
---|
[31] | 133 |
|
---|
[77] | 134 | def _showMonitorToggled(self, menuitem):
|
---|
| 135 | """Called when the show/hide monitor window menu item is toggled."""
|
---|
| 136 | if self._selfToggling:
|
---|
| 137 | self._selfToggling = False
|
---|
| 138 | elif self._showMonitorMenuItem.get_active():
|
---|
| 139 | self._gui.showMonitorWindow()
|
---|
| 140 | else:
|
---|
| 141 | self._gui.hideMonitorWindow()
|
---|
| 142 |
|
---|
[32] | 143 | def _updateFlightStatus(self):
|
---|
| 144 | """Update the flight status."""
|
---|
[128] | 145 | stage = u"-" if self._stage is None \
|
---|
[114] | 146 | else xstr("flight_stage_" + const.stage2string(self._stage))
|
---|
[32] | 147 |
|
---|
[31] | 148 | if self._noGoReason is None:
|
---|
| 149 | rating = "%.0f%%" % (self._rating,)
|
---|
| 150 | else:
|
---|
[32] | 151 | rating = self._noGoReason
|
---|
[31] | 152 |
|
---|
[32] | 153 | if appIndicator:
|
---|
[128] | 154 | self._stageMenuItem.set_label("%s: %s" % \
|
---|
| 155 | (xstr("statusicon_stage"),
|
---|
| 156 | stage))
|
---|
| 157 | self._ratingMenuItem.set_label("%s: %s" % \
|
---|
| 158 | (xstr("statusicon_rating"),
|
---|
| 159 | rating))
|
---|
[32] | 160 | else:
|
---|
| 161 | if self._noGoReason is not None:
|
---|
| 162 | rating = '<span foreground="red">' + rating + '</span>'
|
---|
[128] | 163 | markup = u"MAVA Logger X %s\n\n%s: %s\n%s: %s" %\
|
---|
[114] | 164 | (const.VERSION, xstr("statusicon_stage"), stage,
|
---|
| 165 | xstr("statusicon_rating"), rating)
|
---|
[32] | 166 | self._statusIcon.set_tooltip_markup(markup)
|
---|