Debian Codegraph @

Nagstamon/qui/widgets/buttons.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.helpers import webbrowser_open
22 from Nagstamon.qui.qt import (QIcon,
23 QMenu,
24 QProxyStyle,
25 QPushButton,
26 QStyle,
27 QToolButton,
28 Signal,
29 Slot)
30
31
32 class FlatButton(QToolButton):
33 """
34 A QToolButton that visually and functionally acts as a push button.
35
36 Args:
37 text (str, optional): The button label text. Defaults to an empty string.
38 parent (QWidget, optional): The parent widget. Defaults to None.
39 server (optional): Optional server reference for context. Defaults to None.
40 url_type (str, optional): Optional URL type for button context. Defaults to an empty string.
41
42 Attributes:
43 Inherits all attributes from QToolButton.
44 """
45
46 def __init__(self, text='', parent=None, server=None, url_type=''):
47 QToolButton.__init__(self, parent=parent)
48 self.setAutoRaise(True)
49 self.setStyleSheet('''padding: 3px;''')
50 self.setText(text)
51
52
53 # OSX does not support flat QToolButtons so keep the neat default ones
54 if OS == OS_MACOS:
55 Button = QPushButton
56 CSS_CLOSE_BUTTON = '''QPushButton {border-width: 0px;
57 border-style: none;
58 margin-right: 5px;}
59 QPushButton:hover {background-color: white;
60 border-radius: 4px;}'''
61 CSS_HAMBURGER_MENU = '''QPushButton {border-width: 0px;
62 border-style: none;}
63 QPushButton::menu-indicator{image:url(none.jpg)};
64 QPushButton:hover {background-color: white;
65 border-radius: 4px;}'''
66 else:
67 Button = FlatButton
68 CSS_CLOSE_BUTTON = '''margin-right: 5px;'''
69 CSS_HAMBURGER_MENU = '''FlatButton::menu-indicator{image:url(none.jpg);}'''
70
71
72 class PushButtonHamburger(Button):
73 """
74 A push button styled as a hamburger menu button with an attached menu.
75
76 Attributes:
77 pressed (Signal): Emitted when the button is pressed.
78
79 Methods:
80 mousePressEvent(event): Emits the `pressed` signal and shows the menu.
81 set_menu(menu): Sets the menu to be displayed when the button is pressed.
82
83 Usage:
84 Use this button to provide a compact menu access point, typically represented
85 by a hamburger icon, in toolbars or application headers.
86 """
87
88 pressed = Signal()
89
90 def __init__(self):
91 super().__init__()
92 self.setStyleSheet(CSS_HAMBURGER_MENU)
93
94 def mousePressEvent(self, event):
95 self.pressed.emit()
96 self.showMenu()
97
98 @Slot(QMenu)
99 def set_menu(self, menu):
100 self.setMenu(menu)
101
102
103 class PushButtonBrowserURL(Button):
104 """
105 QPushButton for ServerVBox which opens certain URL if clicked
106 """
107
108 # send when a web browser is opened
109 # this is used to hide the status window in fullscreen mode
110 webbrowser_opened = Signal()
111
112 def __init__(self, text='', parent=None, server=None, url_type=''):
113 Button.__init__(self, text, parent=parent)
114 self.server = server
115 self.url_type = url_type
116 self.parent_statuswindow = self.parentWidget().parentWidget()
117
118 @Slot()
119 def open_url(self):
120 """
121 open URL from BROWSER_URLS in webbrowser
122 """
123 # BROWSER_URLS come with $MONITOR$ instead of real monitor url - heritage from actions
124 url = self.server.BROWSER_URLS[self.url_type]
125 url = url.replace('$MONITOR$', self.server.monitor_url)
126 url = url.replace('$MONITOR-CGI$', self.server.monitor_cgi_url)
127
128 if conf.debug_mode:
129 self.server.debug(server=self.server.get_name(), debug='Open {0} web page {1}'.format(self.url_type, url))
130
131 # use Python method to open browser
132 webbrowser_open(url)
133
134 # hide status window to get screen space for browser
135 if not conf.fullscreen and not conf.windowed:
136 self.webbrowser_opened.emit()
137
138
139 class StandardDialogIconsProxyStyle(QProxyStyle):
140 """
141 proxy style to block standard dialog icons (OK, Cancel) to enforce no-ugly-icons
142 """
143 def standardIcon(self, standard_pixmap, option=None, widget=None):
144 blocked = {
145 QStyle.StandardPixmap.SP_DialogOkButton,
146 QStyle.StandardPixmap.SP_DialogCancelButton,
147 QStyle.StandardPixmap.SP_DialogYesButton,
148 QStyle.StandardPixmap.SP_DialogNoButton,
149 QStyle.StandardPixmap.SP_DialogHelpButton
150 }
151 if standard_pixmap in blocked:
152 return QIcon()
153 return super().standardIcon(standard_pixmap, option, widget)