Debian Codegraph @

Nagstamon/qui/widgets/model.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 from Nagstamon.qui.constants import HEADERS_HEADERS
20 from Nagstamon.qui.globals import font_icons
21 from Nagstamon.qui.qt import (QAbstractTableModel,
22 Qt,
23 QVariant,
24 Signal,
25 Slot)
26
27
28 class Model(QAbstractTableModel):
29 """
30 model for storing status data to be presented in Treeview-table
31 """
32
33 model_data_array_filled = Signal()
34
35 # list of lists for storage of status data
36 data_array = list()
37
38 # cache row and column count
39 row_count = 0
40 column_count = len(HEADERS_HEADERS)
41
42 # tell treeview if flags columns should be hidden or not
43 hosts_flags_column_needed = Signal(bool)
44 services_flags_column_needed = Signal(bool)
45
46 def __init__(self, server, parent=None):
47 QAbstractTableModel.__init__(self, parent=parent)
48 self.server = server
49
50 def rowCount(self, parent):
51 """
52 overridden method to get number of rows
53 """
54 return self.row_count
55
56 def columnCount(self, parent):
57 """
58 overridden method to get number of columns
59 """
60 return self.column_count
61
62 def headerData(self, column, orientation, role):
63 """
64 overridden method to get headers of columns
65 """
66 if role == Qt.ItemDataRole.DisplayRole:
67 return HEADERS_HEADERS[column]
68 return None
69
70 @Slot(list, dict)
71 # @Slot(list)
72 def fill_data_array(self, data_array, info):
73 """
74 fill data_array for model
75 """
76 # tell treeview that model is about to change - necessary because
77 # otherwise new number of rows would not be applied
78 self.beginResetModel()
79
80 # first empty the data storage
81 del self.data_array[:]
82
83 # use delivered data array
84 self.data_array = data_array
85
86 # cache row_count
87 self.row_count = len(self.data_array)
88
89 # tell treeview if flags columns are needed
90 self.hosts_flags_column_needed.emit(info['hosts_flags_column_needed'])
91 self.services_flags_column_needed.emit(info['services_flags_column_needed'])
92
93 # new model applied
94 self.endResetModel()
95
96 self.model_data_array_filled.emit()
97
98 def data(self, index, role):
99 """
100 overridden method for data delivery for treeview
101 """
102 if role == Qt.ItemDataRole.DisplayRole:
103 return self.data_array[index.row()][index.column()]
104
105 elif role == Qt.ItemDataRole.ForegroundRole:
106 return self.data_array[index.row()][10]
107
108 elif role == Qt.ItemDataRole.BackgroundRole:
109 return self.data_array[index.row()][11]
110
111 elif role == Qt.ItemDataRole.FontRole:
112 if index.column() == 1:
113 return font_icons
114 elif index.column() == 3:
115 return font_icons
116 else:
117 return QVariant
118 # provide icons via Qt.UserRole
119 elif role == Qt.ItemDataRole.UserRole:
120 # depending on host or service column return host or service icon list
121 return self.data_array[index.row()][7 + index.column()]
122
123 elif role == Qt.ItemDataRole.ToolTipRole:
124 # only if tooltips are wanted show them, combining host + service + status_info
125 if conf.show_tooltips:
126 return (f'<div style=white-space:pre;margin:3px;>'
127 f'<b>{self.data_array[index.row()][0]}: {self.data_array[index.row()][2]}</b>'
128 f'</div>'
129 f'{self.data_array[index.row()][8]}')
130 else:
131 return QVariant
132 return None