Debian Codegraph @

Nagstamon/qui/dialogs/dialog.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 io import StringIO
19 from pathlib import Path
20
21 from Nagstamon.config import (conf,
22 RESOURCES,
23 OS,
24 OS_MACOS)
25
26 from Nagstamon.qui.constants import ICON
27 from Nagstamon.qui.globals import statuswindow_properties
28 from Nagstamon.qui.helpers import hide_macos_dock_icon
29 from Nagstamon.qui.qt import (QBrush,
30 QListWidgetItem,
31 QObject,
32 QSignalMapper,
33 QSizePolicy,
34 Qt,
35 Signal,
36 Slot,
37 uic,
38 UI_FILE_QT6_QT5_DOWNGRADES)
39
40 # make icon status in macOS dock accessible via NSApp, used by set_macos_dock_icon_visible()
41 if OS == OS_MACOS:
42 from AppKit import NSApp
43
44
45 class Dialog(QObject):
46 """
47 one single dialog
48 """
49 # send a signal, e.g., to the status window if a dialog pops up
50 show_dialog = Signal()
51
52 # signals for macOS dock icon fix
53 check_macos_dock_icon_fix_show = Signal()
54 check_macos_dock_icon_fix_hide = Signal()
55
56 # dummy toggle dependencies
57 TOGGLE_DEPS = {}
58 # auxiliary list of checkboxes which HIDE some other widgets if triggered - for example proxy OS settings
59 TOGGLE_DEPS_INVERTED = []
60 # widgets that might be enabled/disabled depending on a monitor server type
61 VOLATILE_WIDGETS = {}
62 # names of widgets and their defaults
63 WIDGET_NAMES = {}
64 # style stuff used by settings dialog for servers/actions listwidget
65 GRAY = QBrush(Qt.GlobalColor.gray)
66
67 def __init__(self, dialog):
68 QObject.__init__(self)
69
70 # load UI file for manipulation
71 ui_file_path = Path(f'{RESOURCES}/qui/{dialog}.ui')
72 ui_file_content = ui_file_path.read_text(encoding='utf-8')
73 # replace all problematic Qt6 names by Qt5 names to be able to use one single UI file for both Qt5 and Qt6
74 for qt6_name, qt5_name in UI_FILE_QT6_QT5_DOWNGRADES.items():
75 ui_file_content = ui_file_content.replace(qt6_name, qt5_name)
76 # convert string to file-like object for loadUi()
77 ui_file_content_io = StringIO(ui_file_content)
78 # load UI file from resource
79 self.window = uic.loadUi(ui_file_content_io)
80
81 # explicitly set window flags to avoid '?' button on Windows
82 self.window.setWindowFlags(Qt.WindowType.WindowCloseButtonHint)
83
84 # hoping to avoid overly large dialogs
85 self.window.setSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Minimum)
86
87 # set small titlebar icon
88 self.window.setWindowIcon(ICON)
89
90 # treat dialog content after pressing OK button
91 if 'button_box' in dir(self.window):
92 self.window.button_box.accepted.connect(self.ok)
93 self.window.button_box.rejected.connect(self.cancel)
94
95 # QSignalMapper needed to connect all toggle-needing-checkboxes/radiobuttons to one .toggle()-method which
96 # decides which sender to use as key in self.TOGGLE_DEPS
97 self.signalmapper_toggles = QSignalMapper()
98
99 # try to get and keep focus
100 self.window.setWindowModality(Qt.WindowModality.ApplicationModal)
101
102 def initialize(self):
103 """
104 dummy initialize method
105 """
106 pass
107
108 @Slot()
109 def show(self, tab=0):
110 """
111 simple show method, to be enriched
112 """
113 # if running on macOS with disabled dock icon the dock icon might have to be made visible
114 # to make Nagstamon accept keyboard input
115 self.check_macos_dock_icon_fix_show.emit()
116
117 # in case dock icon is configured invisible in macOS it has to be shown while dialog is shown
118 # to be able to get keyboard focus
119 if OS == OS_MACOS and \
120 conf.icon_in_systray and \
121 conf.hide_macos_dock_icon:
122 hide_macos_dock_icon(False)
123
124 # tell the world that dialog pops up
125 self.show_dialog.emit()
126
127 # reset the window if it only needs smaller screen estate
128 self.window.adjustSize()
129 self.window.show()
130 # make sure the dialog window will be the topmost
131 self.window.raise_()
132 # hidden dock icon on macOS needs extra activation
133 if OS == OS_MACOS and \
134 conf.icon_in_systray and \
135 conf.hide_macos_dock_icon:
136 NSApp.activateIgnoringOtherApps_(True)
137
138 def toggle_visibility(self, checkbox, widgets=None):
139 """
140 state of checkbox toggles visibility of widgets
141 some checkboxes might trigger an inverted behaviour - thus the 'inverted' value
142 """
143 if widgets is None:
144 widgets = []
145 if checkbox in self.TOGGLE_DEPS_INVERTED:
146 if checkbox.isChecked():
147 for widget in widgets:
148 widget.hide()
149 else:
150 for widget in widgets:
151 widget.show()
152 # normal case - click on checkbox activates more options
153 else:
154 if checkbox.isChecked():
155 for widget in widgets:
156 widget.show()
157 else:
158 for widget in widgets:
159 widget.hide()
160
161 @Slot(str)
162 def toggle(self, checkbox):
163 """
164 change state of the dependant widgets, slot for signals from checkboxes in UI
165 """
166 # Due to older Qt5 in Ubuntu 14.04 signal mapper has to use strings
167 self.toggle_visibility(self.window.__dict__[checkbox],
168 self.TOGGLE_DEPS[self.window.__dict__[checkbox]])
169
170 # adjust dialog window size after UI changes
171 self.window.adjustSize()
172
173 def toggle_toggles(self):
174 # apply toggle-dependencies between checkboxes as certain widgets
175 for checkbox, widgets in self.TOGGLE_DEPS.items():
176 # toggle visibility
177 self.toggle_visibility(checkbox, widgets)
178 # multiplex slot .toggle() by signal-mapping
179 # Due to older Qt5 in Ubuntu 14.04 signal mapper has to use strings
180 self.signalmapper_toggles.setMapping(checkbox, checkbox.objectName())
181 checkbox.toggled.connect(self.signalmapper_toggles.map)
182 checkbox.toggled.connect(self.window.adjustSize)
183
184 # finally, map signals with .sender() - [QWidget] is important!
185 self.signalmapper_toggles.mappedString[str].connect(self.toggle)
186
187 def fill_list(self, list_widget, config):
188 """
189 fill list widget with items from config
190 """
191 for config_item in sorted(config, key=str.lower):
192 list_item = QListWidgetItem(config_item)
193 if config[config_item].enabled is False:
194 list_item.setForeground(self.GRAY)
195 list_widget.addItem(list_item)
196
197 @Slot()
198 def ok(self):
199 """
200 as default closes dialog - might be refined, for example, by settings dialog
201 """
202 self.window.close()
203 # en reverse the dock icon might be hidden again after a potential keyboard input
204 self.check_macos_dock_icon_fix_show.emit()
205
206 @Slot()
207 def cancel(self):
208 """
209 as default closes dialog - might be refined, for example by settings dialog
210 """
211 self.window.close()
212 # en reverse the dock icon might be hidden again after a potential keyboard input
213 self.check_macos_dock_icon_fix_hide.emit()
214