Debian Codegraph @

Nagstamon/qui/widgets/labels.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 from time import time
18
19 from Nagstamon.config import (conf,
20 OS,
21 OS_MACOS)
22 from Nagstamon.qui.constants import COLOR_STATUS_LABEL
23 from Nagstamon.qui.globals import (font,
24 statuswindow_properties)
25
26 from Nagstamon.qui.qt import (QLabel,
27 QSizePolicy,
28 Qt,
29 Signal,
30 Slot)
31
32
33 class LabelAllOK(QLabel):
34 """
35 label which is shown in fullscreen and windowed mode when all is OK - pretty seldomly
36 """
37
38 def __init__(self, text='', parent=None):
39 QLabel.__init__(self, text='OK', parent=parent)
40 self.setSizePolicy(QSizePolicy.Policy.MinimumExpanding, QSizePolicy.Policy.MinimumExpanding)
41 self.setAlignment(Qt.AlignmentFlag.AlignCenter)
42 self.setFont(font)
43 self.set_color()
44
45 @Slot()
46 def set_color(self):
47 self.setStyleSheet(f'''padding-left: 1px;
48 padding-right: 1px;
49 color: {conf.__dict__['color_ok_text']};
50 background-color: {conf.__dict__['color_ok_background']};
51 font-size: 92px;
52 font-weight: bold;
53 ''')
54
55
56 class ClosingLabel(QLabel):
57 """
58 modified QLabel which might close the status window if left-clicked
59 """
60 # neede to close status window
61 mouse_released = Signal()
62
63 def __init__(self, text='', parent=None):
64 QLabel.__init__(self, text, parent=parent)
65 self.parent_statuswindow = None
66
67 def mouseReleaseEvent(self, event):
68 """
69 left click and configured close-if-clicking-somewhere makes status window close
70 """
71 # update access to status window
72 self.parent_statuswindow = self.parentWidget().parentWidget()
73 if event.button() == Qt.MouseButton.LeftButton and conf.close_details_clicking_somewhere:
74 # if popup window should be closed by clicking do it now
75 if statuswindow_properties.is_shown and \
76 not conf.fullscreen and \
77 not conf.windowed:
78 statuswindow_properties.is_hiding_timestamp = time()
79 self.mouse_released.emit()
80
81
82 class ServerStatusLabel(ClosingLabel):
83 """
84 label for ServerVBox to show server connection state
85 extra class to apply simple slots for changing text or color
86 """
87
88 def __init__(self, parent=None):
89 QLabel.__init__(self, parent=parent)
90 # storage for label text if it needs to be restored
91 self.text_old = ''
92 self.stylesheet_old = None
93
94 @Slot(str, str)
95 def change(self, text, style=''):
96 # store old text and stylesheet in case it needs to be reused
97 self.text_old = self.text()
98 self.stylesheet_old = self.styleSheet()
99
100 # set stylesheet depending on submitted style
101 if style in COLOR_STATUS_LABEL:
102 if OS == OS_MACOS:
103 self.setStyleSheet(f'''background: {COLOR_STATUS_LABEL[style]};
104 border-radius: 3px;
105 ''')
106 else:
107 self.setStyleSheet(f'''background: {COLOR_STATUS_LABEL[style]};
108 margin-top: 8px;
109 padding-top: 3px;
110 margin-bottom: 8px;
111 padding-bottom: 3px;
112 border-radius: 4px;
113 ''')
114 elif style == '':
115 self.setStyleSheet('')
116
117 # in case of unknown errors try to avoid freaking out status window with too
118 # big status label
119 if style != 'unknown':
120 # set new text with some space
121 self.setText(' {0} '.format(text))
122 self.setToolTip('')
123 else:
124 # set new text to first word of text, delegate full text to tooltip
125 self.setText(text.split(' ')[0])
126 self.setToolTip(text)
127
128 @Slot()
129 def reset(self):
130 self.setStyleSheet(self.stylesheet_old)
131 self.setText('')
132
133 @Slot()
134 def restore(self):
135 # restore text, used by recheck_all of tablewidget worker
136 self.setStyleSheet(self.stylesheet_old)
137 self.setText(self.text_old)