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