source: src/mlx/gui/statusicon.py@ 861:5fa8e92b19b2

Last change on this file since 861:5fa8e92b19b2 was 544:781b62c20c0d, checked in by István Váradi <ivaradi@…>, 11 years ago

The empty menu items are created with a non-empty default label so that they appear

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