[29] | 1 | # Implementation of the status icon
|
---|
| 2 |
|
---|
| 3 | #-------------------------------------------------------------------------------
|
---|
| 4 |
|
---|
| 5 | from common import *
|
---|
| 6 |
|
---|
[31] | 7 | import mlx.const as const
|
---|
| 8 |
|
---|
[29] | 9 | #-------------------------------------------------------------------------------
|
---|
| 10 |
|
---|
| 11 | class StatusIcon(object):
|
---|
| 12 | """The class handling the status icon."""
|
---|
| 13 | def __init__(self, iconDirectory, gui):
|
---|
| 14 | """Construct the status icon."""
|
---|
| 15 | self._gui = gui
|
---|
[31] | 16 |
|
---|
| 17 | self._stage = None
|
---|
| 18 | self._rating = 100
|
---|
| 19 | self._noGoReason = None
|
---|
[29] | 20 |
|
---|
| 21 | menu = gtk.Menu()
|
---|
| 22 |
|
---|
[31] | 23 | if appIndicator:
|
---|
| 24 | self._stageMenuItem = gtk.MenuItem()
|
---|
| 25 | self._stageMenuItem.set_label("Stage: -")
|
---|
| 26 | self._stageMenuItem.show()
|
---|
| 27 | menu.append(self._stageMenuItem)
|
---|
| 28 |
|
---|
| 29 | self._ratingMenuItem = gtk.MenuItem()
|
---|
| 30 | self._ratingMenuItem.set_label("Rating: 100%")
|
---|
| 31 | self._ratingMenuItem.show()
|
---|
| 32 | menu.append(self._ratingMenuItem)
|
---|
| 33 |
|
---|
| 34 | separator = gtk.SeparatorMenuItem()
|
---|
| 35 | separator.show()
|
---|
| 36 | menu.append(separator)
|
---|
| 37 |
|
---|
[29] | 38 | self._showHideMenuItem = gtk.CheckMenuItem()
|
---|
| 39 | self._showHideMenuItem.set_label("Show main window")
|
---|
| 40 | self._showHideMenuItem.set_active(True)
|
---|
| 41 | self._showHideMenuItem.connect("toggled", self._showHideToggled)
|
---|
| 42 | self._showHideMenuItem.show()
|
---|
| 43 | menu.append(self._showHideMenuItem)
|
---|
| 44 |
|
---|
| 45 | menu.show()
|
---|
| 46 |
|
---|
| 47 | iconFile = os.path.join(iconDirectory, "logo.ico")
|
---|
| 48 |
|
---|
| 49 | if appIndicator:
|
---|
| 50 | if pygobject:
|
---|
| 51 | indicator = appindicator.Indicator.new ("mava-logger-x", iconFile,
|
---|
| 52 | appindicator.IndicatorCategory.APPLICATION_STATUS)
|
---|
| 53 | indicator.set_status (appindicator.IndicatorStatus.ACTIVE)
|
---|
| 54 | else:
|
---|
| 55 | indicator = appindicator.Indicator ("mava-logger-x", iconFile,
|
---|
| 56 | appindicator.CATEGORY_APPLICATION_STATUS)
|
---|
| 57 | indicator.set_status (appindicator.STATUS_ACTIVE)
|
---|
| 58 |
|
---|
| 59 | indicator.set_menu(menu)
|
---|
| 60 | self._indicator = indicator
|
---|
| 61 | else:
|
---|
| 62 | def popup_menu(status, button, time):
|
---|
| 63 | menu.popup(None, None, gtk.status_icon_position_menu,
|
---|
| 64 | button, time, status)
|
---|
| 65 |
|
---|
| 66 | statusIcon = gtk.StatusIcon()
|
---|
| 67 | statusIcon.set_from_file(iconFile)
|
---|
| 68 | statusIcon.set_visible(True)
|
---|
| 69 | statusIcon.connect('popup-menu', popup_menu)
|
---|
| 70 | statusIcon.connect('activate',
|
---|
| 71 | lambda status: self._gui.toggleMainWindow())
|
---|
| 72 | self._statusIcon = statusIcon
|
---|
[31] | 73 | self._setTooltip()
|
---|
[29] | 74 |
|
---|
| 75 | def mainWindowHidden(self):
|
---|
| 76 | """Called when the main window is hidden."""
|
---|
| 77 | self._showHideMenuItem.set_active(False)
|
---|
| 78 |
|
---|
| 79 | def mainWindowShown(self):
|
---|
| 80 | """Called when the main window is shown."""
|
---|
| 81 | self._showHideMenuItem.set_active(True)
|
---|
[31] | 82 |
|
---|
| 83 | def resetFlightStatus(self):
|
---|
| 84 | """Reset the status of the flight."""
|
---|
| 85 | if not appIndicator:
|
---|
| 86 | self._statusIcon.set_blinking(False)
|
---|
| 87 | self._noGoReason = None
|
---|
| 88 | self.setStage(None)
|
---|
| 89 | self.setRating(100)
|
---|
[29] | 90 |
|
---|
[31] | 91 | def setStage(self, stage):
|
---|
| 92 | """Set the stage of the flight."""
|
---|
| 93 | self._stage = stage
|
---|
| 94 | if appIndicator:
|
---|
| 95 | label = "Stage: %s" % ("-" if self._stage is None \
|
---|
| 96 | else (const.stage2string(stage),))
|
---|
| 97 | self._stageMenuItem.set_label(label)
|
---|
| 98 | else:
|
---|
| 99 | self._setTooltip()
|
---|
| 100 |
|
---|
| 101 | def setRating(self, rating):
|
---|
| 102 | """Set the rating to the given value."""
|
---|
| 103 | if rating==self._rating:
|
---|
| 104 | return
|
---|
| 105 | self._rating = rating
|
---|
| 106 |
|
---|
| 107 | if appIndicator:
|
---|
| 108 | if self._noGoReason is None:
|
---|
| 109 | self._ratingMenuItem.set_label("Rating: %.0f%%" % (rating,))
|
---|
| 110 | else:
|
---|
| 111 | self._setTooltip()
|
---|
| 112 |
|
---|
| 113 | def setNoGo(self, reason):
|
---|
| 114 | """Set a No-Go condition with the given reason."""
|
---|
| 115 | if self._noGoReason is not None:
|
---|
| 116 | return
|
---|
| 117 |
|
---|
| 118 | self._noGoReason = reason
|
---|
| 119 | if appIndicator:
|
---|
| 120 | self._ratingMenuItem.set_label("Rating: %s" % (reason,))
|
---|
| 121 | else:
|
---|
| 122 | self._setTooltip()
|
---|
| 123 | self._statusIcon.set_blinking(True)
|
---|
| 124 |
|
---|
[29] | 125 | def _showHideToggled(self, menuitem):
|
---|
| 126 | """Called when the show/hide menu item is toggled."""
|
---|
| 127 | if self._showHideMenuItem.get_active():
|
---|
| 128 | self._gui.showMainWindow()
|
---|
| 129 | else:
|
---|
| 130 | self._gui.hideMainWindow()
|
---|
[31] | 131 |
|
---|
| 132 | def _setTooltip(self):
|
---|
| 133 | """Set the tooltip of the status icon."""
|
---|
| 134 | if self._noGoReason is None:
|
---|
| 135 | rating = "%.0f%%" % (self._rating,)
|
---|
| 136 | else:
|
---|
| 137 | rating = '<span foreground="red">' + self._noGoReason + '</span>'
|
---|
| 138 |
|
---|
| 139 | markup = "MAVA Logger X %s\n\nStage: %s\nRating: %s" %\
|
---|
| 140 | (const.VERSION, ("-" if self._stage is None else
|
---|
| 141 | const.stage2string(self._stage)),
|
---|
| 142 | rating)
|
---|
| 143 |
|
---|
| 144 | self._statusIcon.set_tooltip_markup(markup)
|
---|
| 145 |
|
---|