Debian Codegraph @

Nagstamon/qui/widgets/toparea.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 base64 import b64encode
18 from os import sep
19
20 from Nagstamon.config import AppInfo, RESOURCES
21 from Nagstamon.qui.constants import SPACE
22 from Nagstamon.qui.globals import app
23 from Nagstamon.qui.qt import (QAction,
24 QByteArray,
25 QHBoxLayout,
26 QIcon,
27 QPainter,
28 QPixmap,
29 QPalette,
30 QSizePolicy,
31 QSvgRenderer,
32 Qt,
33 QXmlStreamReader,
34 QWidget,
35 Signal,
36 Slot)
37 from Nagstamon.qui.widgets.buttons import (Button,
38 CSS_CLOSE_BUTTON,
39 PushButtonHamburger)
40 from Nagstamon.qui.widgets.combobox_servers import (ComboBoxServers)
41 from Nagstamon.qui.widgets.draggables import DraggableLabel
42 from Nagstamon.qui.widgets.layout import HBoxLayout
43 from Nagstamon.qui.widgets.menu import MenuAtCursor
44 from Nagstamon.qui.widgets.nagstamon_logo import NagstamonLogo
45
46
47 class TopArea(QWidget):
48 """
49 top area of status window
50 """
51
52 mouse_entered = Signal()
53
54 def __init__(self, parent=None):
55 QWidget.__init__(self)
56
57 self.hbox = HBoxLayout(spacing=SPACE, parent=self) # top HBox containing buttons
58 self.hbox.setSizeConstraint(QHBoxLayout.SizeConstraint.SetMinimumSize)
59
60 self.icons = dict()
61 self.create_icons()
62
63 # top button box
64 self.logo = NagstamonLogo(self.icons['nagstamon_logo_toparea'], width=150, height=42, parent=self)
65 self.label_version = DraggableLabel(text=AppInfo.VERSION, parent=self)
66 self.label_empty_space = DraggableLabel(text='', parent=self)
67 self.label_empty_space.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Ignored)
68 self.combobox_servers = ComboBoxServers(parent=self)
69 self.button_filters = Button("Filters", parent=self)
70 self.button_recheck_all = Button("Recheck all", parent=self)
71 self.button_refresh = Button("Refresh", parent=self)
72 self.button_settings = Button("Settings", parent=self)
73
74 # fill default order fields combobox with server names
75 self.combobox_servers.fill()
76
77 # hamburger menu
78 self.button_hamburger_menu = PushButtonHamburger()
79 self.button_hamburger_menu.setIcon(self.icons['menu'])
80 self.hamburger_menu = MenuAtCursor()
81 self.action_exit = QAction("Exit", self)
82 self.hamburger_menu.addAction(self.action_exit)
83 self.button_hamburger_menu.setMenu(self.hamburger_menu)
84
85 # X
86 self.button_close = Button()
87 self.button_close.setIcon(self.icons['close'])
88 self.button_close.setStyleSheet(CSS_CLOSE_BUTTON)
89
90 self.hbox.addWidget(self.logo)
91 self.hbox.addWidget(self.label_version)
92 self.hbox.addWidget(self.label_empty_space)
93 self.hbox.addWidget(self.combobox_servers)
94 self.hbox.addWidget(self.button_filters)
95 self.hbox.addWidget(self.button_recheck_all)
96 self.hbox.addWidget(self.button_refresh)
97 self.hbox.addWidget(self.button_settings)
98 self.hbox.addWidget(self.button_hamburger_menu)
99 self.hbox.addWidget(self.button_close)
100
101 self.setLayout(self.hbox)
102
103 def enterEvent(self, event):
104 # unlock statuswindow if pointer touches statusbar
105 self.mouse_entered.emit()
106
107 @Slot()
108 def create_icons(self):
109 """
110 create icons from template, applying colors
111 """
112 # get rgb values of current foreground color to be used for SVG icons (menu)
113 r, g, b, a = app.palette().color(QPalette.ColorRole.Text).getRgb()
114
115 for icon in 'nagstamon_logo_toparea', 'close', 'menu':
116 # get template from file
117 svg_template_file = open(f'{RESOURCES}{sep}{icon}_template.svg')
118 svg_template_xml = svg_template_file.readlines()
119
120 # current SVG XML for state icon, derived from svg_template_cml
121 svg_icon_xml = list()
122
123 # replace dummy text and background colors with configured ones
124 for line in svg_template_xml:
125 line = line.replace('fill:#ff00ff', 'fill:#{0:x}{1:x}{2:x}'.format(r, g, b))
126 svg_icon_xml.append(line)
127
128 # create XML stream of SVG
129 svg_xml_stream = QXmlStreamReader(''.join(svg_icon_xml))
130
131 # create renderer for SVG and put SVG XML into renderer
132 svg_renderer = QSvgRenderer(svg_xml_stream)
133 # pixmap to be painted on - arbitrarily choosen 128x128 px
134 svg_pixmap = QPixmap(128, 128)
135 # fill transparent backgound
136 svg_pixmap.fill(Qt.GlobalColor.transparent)
137 # initiate painter which paints onto paintdevice pixmap
138 svg_painter = QPainter(svg_pixmap)
139 # render svg to pixmap
140 svg_renderer.render(svg_painter)
141 # close painting
142 svg_painter.end()
143
144 # two ways...
145 if icon == 'nagstamon_logo_toparea':
146 # first get a base64 version of the SVG
147 svg_base64 = b64encode(bytes(''.join(svg_icon_xml), 'utf8'))
148 # create a QByteArray for NagstamonLogo aka QSvgWidget
149 svg_bytes = QByteArray.fromBase64(svg_base64)
150 self.icons[icon] = svg_bytes
151 else:
152 # put pixmap into icon
153 self.icons[icon] = QIcon(svg_pixmap)