[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 |
|
---|
[32] | 11 | class StatusIcon(FlightStatusHandler):
|
---|
[29] | 12 | """The class handling the status icon."""
|
---|
| 13 | def __init__(self, iconDirectory, gui):
|
---|
| 14 | """Construct the status icon."""
|
---|
[32] | 15 | super(StatusIcon, self).__init__()
|
---|
| 16 |
|
---|
[29] | 17 | self._gui = gui
|
---|
[35] | 18 | self._selfToggling = False
|
---|
[31] | 19 |
|
---|
[29] | 20 | menu = gtk.Menu()
|
---|
| 21 |
|
---|
[31] | 22 | if appIndicator:
|
---|
| 23 | self._stageMenuItem = gtk.MenuItem()
|
---|
| 24 | self._stageMenuItem.show()
|
---|
| 25 | menu.append(self._stageMenuItem)
|
---|
| 26 |
|
---|
| 27 | self._ratingMenuItem = gtk.MenuItem()
|
---|
| 28 | self._ratingMenuItem.show()
|
---|
| 29 | menu.append(self._ratingMenuItem)
|
---|
| 30 |
|
---|
| 31 | separator = gtk.SeparatorMenuItem()
|
---|
| 32 | separator.show()
|
---|
| 33 | menu.append(separator)
|
---|
| 34 |
|
---|
[29] | 35 | self._showHideMenuItem = gtk.CheckMenuItem()
|
---|
| 36 | self._showHideMenuItem.set_label("Show main window")
|
---|
| 37 | self._showHideMenuItem.set_active(True)
|
---|
| 38 | self._showHideMenuItem.connect("toggled", self._showHideToggled)
|
---|
| 39 | self._showHideMenuItem.show()
|
---|
| 40 | menu.append(self._showHideMenuItem)
|
---|
| 41 |
|
---|
[76] | 42 | self._quitMenuItem = gtk.MenuItem()
|
---|
| 43 | self._quitMenuItem.set_label("Quit")
|
---|
| 44 | self._quitMenuItem.show()
|
---|
| 45 | self._quitMenuItem.connect("activate", self._gui._quit)
|
---|
| 46 | menu.append(self._quitMenuItem)
|
---|
| 47 |
|
---|
[29] | 48 | menu.show()
|
---|
| 49 |
|
---|
| 50 | iconFile = os.path.join(iconDirectory, "logo.ico")
|
---|
| 51 |
|
---|
| 52 | if appIndicator:
|
---|
| 53 | if pygobject:
|
---|
| 54 | indicator = appindicator.Indicator.new ("mava-logger-x", iconFile,
|
---|
| 55 | appindicator.IndicatorCategory.APPLICATION_STATUS)
|
---|
| 56 | indicator.set_status (appindicator.IndicatorStatus.ACTIVE)
|
---|
| 57 | else:
|
---|
| 58 | indicator = appindicator.Indicator ("mava-logger-x", iconFile,
|
---|
| 59 | appindicator.CATEGORY_APPLICATION_STATUS)
|
---|
| 60 | indicator.set_status (appindicator.STATUS_ACTIVE)
|
---|
| 61 |
|
---|
| 62 | indicator.set_menu(menu)
|
---|
| 63 | self._indicator = indicator
|
---|
| 64 | else:
|
---|
| 65 | def popup_menu(status, button, time):
|
---|
| 66 | menu.popup(None, None, gtk.status_icon_position_menu,
|
---|
| 67 | button, time, status)
|
---|
| 68 |
|
---|
| 69 | statusIcon = gtk.StatusIcon()
|
---|
| 70 | statusIcon.set_from_file(iconFile)
|
---|
| 71 | statusIcon.set_visible(True)
|
---|
| 72 | statusIcon.connect('popup-menu', popup_menu)
|
---|
| 73 | statusIcon.connect('activate',
|
---|
| 74 | lambda status: self._gui.toggleMainWindow())
|
---|
| 75 | self._statusIcon = statusIcon
|
---|
[32] | 76 |
|
---|
| 77 | self._updateFlightStatus()
|
---|
[29] | 78 |
|
---|
| 79 | def mainWindowHidden(self):
|
---|
| 80 | """Called when the main window is hidden."""
|
---|
[35] | 81 | if self._showHideMenuItem.get_active():
|
---|
| 82 | self._selfToggling = True
|
---|
| 83 | self._showHideMenuItem.set_active(False)
|
---|
[29] | 84 |
|
---|
| 85 | def mainWindowShown(self):
|
---|
| 86 | """Called when the main window is shown."""
|
---|
[35] | 87 | if not self._showHideMenuItem.get_active():
|
---|
| 88 | self._selfToggling = True
|
---|
| 89 | self._showHideMenuItem.set_active(True)
|
---|
[31] | 90 |
|
---|
[38] | 91 | def destroy(self):
|
---|
| 92 | """Hide and destroy the status icon."""
|
---|
| 93 | if appIndicator:
|
---|
| 94 | if pygobject:
|
---|
| 95 | self._indicator.set_status(appindicator.IndicatorStatus.PASSIVE)
|
---|
| 96 | else:
|
---|
| 97 | self._indicator.set_status(appindicator.STATUS_PASSIVE)
|
---|
| 98 | else:
|
---|
| 99 | self._statusIcon.set_visible(False)
|
---|
| 100 |
|
---|
[29] | 101 | def _showHideToggled(self, menuitem):
|
---|
| 102 | """Called when the show/hide menu item is toggled."""
|
---|
[35] | 103 | if self._selfToggling:
|
---|
| 104 | self._selfToggling = False
|
---|
| 105 | elif self._showHideMenuItem.get_active():
|
---|
[29] | 106 | self._gui.showMainWindow()
|
---|
| 107 | else:
|
---|
| 108 | self._gui.hideMainWindow()
|
---|
[31] | 109 |
|
---|
[32] | 110 | def _updateFlightStatus(self):
|
---|
| 111 | """Update the flight status."""
|
---|
| 112 | stage = "-" if self._stage is None else const.stage2string(self._stage)
|
---|
| 113 |
|
---|
[31] | 114 | if self._noGoReason is None:
|
---|
| 115 | rating = "%.0f%%" % (self._rating,)
|
---|
| 116 | else:
|
---|
[32] | 117 | rating = self._noGoReason
|
---|
[31] | 118 |
|
---|
[32] | 119 | if appIndicator:
|
---|
| 120 | self._stageMenuItem.set_label("Stage: %s" % (stage,))
|
---|
| 121 | self._ratingMenuItem.set_label("Rating: %s" % (rating,))
|
---|
| 122 | else:
|
---|
| 123 | if self._noGoReason is not None:
|
---|
| 124 | rating = '<span foreground="red">' + rating + '</span>'
|
---|
| 125 | markup = "MAVA Logger X %s\n\nStage: %s\nRating: %s" %\
|
---|
| 126 | (const.VERSION, stage, rating)
|
---|
| 127 | self._statusIcon.set_tooltip_markup(markup)
|
---|