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