Debian Codegraph @

Nagstamon/qui/widgets/menu.py

raw
1 # Nagstamon - Nagios status monitor for your desktop
2 # Copyright (C) 2008-2026 Henri Wahl <[email protected]> et al.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17
18 from Nagstamon.config import (conf,
19 OS,
20 OS_MACOS)
21 from Nagstamon.qui.qt import (QAction,
22 QMenu, \
23 QCursor,
24 QPoint,
25 Signal,
26 Slot)
27 from Nagstamon.servers import servers
28
29
30 class MenuAtCursor(QMenu):
31 """
32 Displays the menu at the current mouse pointer position.
33
34 Signals:
35 is_shown (bool): Emitted with True when the menu is shown, and with False when it is closed.
36
37 Args:
38 parent (QWidget, optional): The parent widget of the menu.
39
40 Methods:
41 show_at_cursor(): Shows the menu at the current mouse pointer position and emits is_shown signals.
42 """
43 # flag to avoid too fast popping up menus
44 # available = True
45
46 is_shown = Signal(bool)
47
48 def __init__(self, parent=None):
49 QMenu.__init__(self, parent=parent)
50
51 @Slot()
52 def show_at_cursor(self):
53 """
54 Pop up at mouse pointer position, lock itself to avoid constantly popping menus on Windows
55 """
56 # get cursor coordinates and decrease them to show menu under mouse pointer
57 x = QCursor.pos().x() - 10
58 y = QCursor.pos().y() - 10
59 # tell the world that the menu will be shown
60 self.is_shown.emit(True)
61 # show menu
62 self.exec(QPoint(x, y))
63 # tell world that menu will be closed
64 self.is_shown.emit(False)
65 del x, y
66
67 class MenuContext(MenuAtCursor):
68 """
69 class for universal context menu, used at systray icon and hamburger menu
70 """
71
72 menu_ready = Signal(QMenu)
73
74 def __init__(self, parent=None):
75 MenuAtCursor.__init__(self, parent=parent)
76
77 self.parent_statuswindow = self.parentWidget()
78
79 # connect all relevant widgets which should show the context menu
80 for widget in [self.parent_statuswindow.toparea.button_hamburger_menu,
81 self.parent_statuswindow.toparea.label_version,
82 self.parent_statuswindow.toparea.label_empty_space,
83 self.parent_statuswindow.toparea.logo,
84 self.parent_statuswindow.statusbar.logo,
85 self.parent_statuswindow.statusbar.label_message]:
86 self.menu_ready.connect(widget.set_menu)
87
88 for color_label in self.parent_statuswindow.statusbar.color_labels.values():
89 self.menu_ready.connect(color_label.set_menu)
90
91 self.initialize()
92
93 @Slot()
94 def initialize(self):
95 """
96 add actions and servers to menu
97 """
98
99 # first clear to get rid of old servers
100 self.clear()
101
102 self.action_refresh = QAction('Refresh', self)
103 self.action_refresh.triggered.connect(self.parent_statuswindow.refresh)
104 self.addAction(self.action_refresh)
105
106 self.action_recheck = QAction('Recheck all', self)
107 self.action_recheck.triggered.connect(self.parent_statuswindow.recheck_all)
108 self.addAction(self.action_recheck)
109
110 self.addSeparator()
111
112 # dict to hold all servers - more flexible this way
113 self.action_servers = dict()
114
115 # connect every server to its monitoring webpage
116 for server in sorted([x.name for x in conf.servers.values() if x.enabled], key=str.lower):
117 self.action_servers[server] = QAction(server, self)
118 self.action_servers[server].triggered.connect(servers[server].open_monitor_webpage)
119 self.addAction(self.action_servers[server])
120
121 self.addSeparator()
122
123 self.action_settings = QAction('Settings...', self)
124 self.action_settings.triggered.connect(self.parent_statuswindow.hide_window)
125 self.action_settings.triggered.connect(self.parent_statuswindow.injected_dialogs.settings.show)
126 self.addAction(self.action_settings)
127
128 self.action_save_position = QAction('Save position', self)
129 # TODO: remove action from menu if not needed aka not floating
130 if conf.statusbar_floating:
131 self.addAction(self.action_save_position)
132 self.action_save_position.triggered.connect(self.parent_statuswindow.save_position_to_conf)
133
134 self.addSeparator()
135
136 self.action_about = QAction('About...', self)
137 self.action_about.triggered.connect(self.parent_statuswindow.hide_window)
138 self.action_about.triggered.connect(self.parent_statuswindow.injected_dialogs.about.show)
139 self.addAction(self.action_about)
140
141 self.action_exit = QAction('Exit', self)
142 self.action_exit.triggered.connect(self.parent_statuswindow.exit)
143 self.addAction(self.action_exit)
144
145 # tell all widgets to use the new menu
146 self.menu_ready.emit(self)
147
148
149 class MenuContextSystrayicon(MenuContext):
150 """
151 Necessary for Ubuntu 16.04 new Qt5-Systray-AppIndicator meltdown
152 Maybe in general a good idea to offer status window popup here
153 """
154
155 action_status = None
156
157 def __init__(self, parent=None):
158 """
159 clone of normal MenuContext which serves well in all other places
160 but no need of signal/slots initialization
161 """
162 QMenu.__init__(self, parent=parent)
163
164 self.parent_statuswindow = self.parentWidget()
165
166 # initialize as default + extra
167 self.initialize()
168
169
170 def initialize(self):
171 """
172 initialize as inherited + a popup menu entry mostly useful in Ubuntu Unity
173 """
174 MenuContext.initialize(self)
175 # makes even less sense on OSX
176 if OS != OS_MACOS:
177 self.action_status = QAction('Show status window', self)
178 self.action_status.triggered.connect(self.parent_statuswindow.show_window_systrayicon)
179 self.insertAction(self.action_refresh, self.action_status)
180 self.insertSeparator(self.action_refresh)