source: src/mlx/gui/statusicon.py@ 999:e096a5638b87

python3
Last change on this file since 999:e096a5638b87 was 998:6fda08fc895b, checked in by István Váradi <ivaradi@…>, 5 years ago

Using 'GdkPixbuf' instead of 'gdkPixbuf', 'AppIndicator3' instead of 'appindicator' and 'Pango' instead of 'pango' (re #347)

File size: 6.3 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 indicator = AppIndicator3.Indicator.new ("mava-logger-x", iconFile,
77 AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
78 indicator.set_status (AppIndicator3.IndicatorStatus.ACTIVE)
79
80 indicator.set_menu(menu)
81 self._indicator = indicator
82 else:
83 def popup_menu(status, button, time):
84 menu.popup(None, None, Gtk.status_icon_position_menu,
85 button, time, status)
86
87 statusIcon = Gtk.StatusIcon()
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
94
95 self._updateFlightStatus()
96
97 def mainWindowHidden(self):
98 """Called when the main window is hidden."""
99 if self._showHideMenuItem.get_active():
100 self._selfToggling = True
101 self._showHideMenuItem.set_active(False)
102
103 def mainWindowShown(self):
104 """Called when the main window is shown."""
105 if not self._showHideMenuItem.get_active():
106 self._selfToggling = True
107 self._showHideMenuItem.set_active(True)
108
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
121 def destroy(self):
122 """Hide and destroy the status icon."""
123 if appIndicator:
124 self._indicator.set_status(AppIndicator3.IndicatorStatus.PASSIVE)
125 else:
126 self._statusIcon.set_visible(False)
127
128 def _showHideToggled(self, menuitem):
129 """Called when the show/hide menu item is toggled."""
130 if self._selfToggling:
131 self._selfToggling = False
132 elif self._showHideMenuItem.get_active():
133 self._gui.showMainWindow()
134 else:
135 self._gui.hideMainWindow()
136
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
146 def _updateFlightStatus(self):
147 """Update the flight status."""
148 stage = "-" if self._stage is None \
149 else xstr("flight_stage_" + const.stage2string(self._stage))
150
151 if self._noGoReason is None:
152 rating = "%.1f%%" % (self._rating,)
153 else:
154 rating = self._noGoReason
155
156 if appIndicator:
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))
163 else:
164 if self._noGoReason is not None:
165 rating = '<span foreground="red">' + rating + '</span>'
166 markup = "MAVA Logger X %s\n\n%s: %s\n%s: %s" %\
167 (const.VERSION, xstr("statusicon_stage"), stage,
168 xstr("statusicon_rating"), rating)
169 self._statusIcon.set_tooltip_markup(markup)
Note: See TracBrowser for help on using the repository browser.