MqttMonitor/MqttUserPropertiesEditor.py

branch
eric7
changeset 123
3d7e63ed4fd1
parent 114
8c0e9e602124
child 127
8982ef7b7d67
equal deleted inserted replaced
122:28d69b9e1b6a 123:3d7e63ed4fd1
7 Module implementing an editor for MQTT v5 user properties. 7 Module implementing an editor for MQTT v5 user properties.
8 """ 8 """
9 9
10 from PyQt6.QtCore import pyqtSlot, Qt 10 from PyQt6.QtCore import pyqtSlot, Qt
11 from PyQt6.QtWidgets import ( 11 from PyQt6.QtWidgets import (
12 QDialog, QDialogButtonBox, QTableWidgetItem, QVBoxLayout, QLabel, QWidget 12 QDialog,
13 QDialogButtonBox,
14 QTableWidgetItem,
15 QVBoxLayout,
16 QLabel,
17 QWidget,
13 ) 18 )
14 19
15 from .Ui_MqttUserPropertiesEditor import Ui_MqttUserPropertiesEditor 20 from .Ui_MqttUserPropertiesEditor import Ui_MqttUserPropertiesEditor
16 21
17 import UI.PixmapCache 22 import UI.PixmapCache
19 24
20 class MqttUserPropertiesEditor(QWidget, Ui_MqttUserPropertiesEditor): 25 class MqttUserPropertiesEditor(QWidget, Ui_MqttUserPropertiesEditor):
21 """ 26 """
22 Class implementing an editor for MQTT v5 user properties. 27 Class implementing an editor for MQTT v5 user properties.
23 """ 28 """
29
24 def __init__(self, parent=None): 30 def __init__(self, parent=None):
25 """ 31 """
26 Constructor 32 Constructor
27 33
28 @param parent reference to the parent widget (defaults to None) 34 @param parent reference to the parent widget (defaults to None)
29 @type QWidget (optional) 35 @type QWidget (optional)
30 """ 36 """
31 super().__init__(parent) 37 super().__init__(parent)
32 self.setupUi(self) 38 self.setupUi(self)
33 39
34 self.addButton.setIcon(UI.PixmapCache.getIcon("plus")) 40 self.addButton.setIcon(UI.PixmapCache.getIcon("plus"))
35 self.deleteButton.setIcon(UI.PixmapCache.getIcon("minus")) 41 self.deleteButton.setIcon(UI.PixmapCache.getIcon("minus"))
36 self.clearButton.setIcon(UI.PixmapCache.getIcon("editDelete")) 42 self.clearButton.setIcon(UI.PixmapCache.getIcon("editDelete"))
37 43
38 self.clearButton.clicked.connect(self.clear) 44 self.clearButton.clicked.connect(self.clear)
39 45
40 self.deleteButton.setEnabled(False) 46 self.deleteButton.setEnabled(False)
41 47
42 @pyqtSlot() 48 @pyqtSlot()
43 def on_propertiesTable_itemSelectionChanged(self): 49 def on_propertiesTable_itemSelectionChanged(self):
44 """ 50 """
45 Private slot to handle the selection of rows. 51 Private slot to handle the selection of rows.
46 """ 52 """
47 self.deleteButton.setEnabled( 53 self.deleteButton.setEnabled(bool(self.propertiesTable.selectedItems()))
48 bool(self.propertiesTable.selectedItems())) 54
49
50 @pyqtSlot() 55 @pyqtSlot()
51 def on_addButton_clicked(self): 56 def on_addButton_clicked(self):
52 """ 57 """
53 Private slot to add a row to the table. 58 Private slot to add a row to the table.
54 """ 59 """
55 self.propertiesTable.setRowCount(self.propertiesTable.rowCount() + 1) 60 self.propertiesTable.setRowCount(self.propertiesTable.rowCount() + 1)
56 self.propertiesTable.setCurrentCell( 61 self.propertiesTable.setCurrentCell(self.propertiesTable.rowCount() - 1, 0)
57 self.propertiesTable.rowCount() - 1, 0) 62
58
59 @pyqtSlot() 63 @pyqtSlot()
60 def on_deleteButton_clicked(self): 64 def on_deleteButton_clicked(self):
61 """ 65 """
62 Private slot to delete the selected rows. 66 Private slot to delete the selected rows.
63 """ 67 """
64 selectedRanges = self.propertiesTable.selectedRanges() 68 selectedRanges = self.propertiesTable.selectedRanges()
65 selectedRows = [(r.bottomRow(), r.topRow()) for r in selectedRanges] 69 selectedRows = [(r.bottomRow(), r.topRow()) for r in selectedRanges]
66 for bottomRow, topRow in sorted(selectedRows, reverse=True): 70 for bottomRow, topRow in sorted(selectedRows, reverse=True):
67 for row in range(bottomRow, topRow - 1, -1): 71 for row in range(bottomRow, topRow - 1, -1):
68 self.propertiesTable.removeRow(row) 72 self.propertiesTable.removeRow(row)
69 73
70 @pyqtSlot() 74 @pyqtSlot()
71 def clear(self): 75 def clear(self):
72 """ 76 """
73 Public slot to delete all properties. 77 Public slot to delete all properties.
74 """ 78 """
75 self.propertiesTable.clearContents() 79 self.propertiesTable.clearContents()
76 self.propertiesTable.setRowCount(10) 80 self.propertiesTable.setRowCount(10)
77 self.propertiesTable.setCurrentCell(0, 0) 81 self.propertiesTable.setCurrentCell(0, 0)
78 82
79 def setProperties(self, properties): 83 def setProperties(self, properties):
80 """ 84 """
81 Public method to populate the editor with a list of user properties. 85 Public method to populate the editor with a list of user properties.
82 86
83 @param properties list of defined user properties 87 @param properties list of defined user properties
84 @type list of tuple of (str, str) 88 @type list of tuple of (str, str)
85 """ 89 """
86 if properties: 90 if properties:
87 self.propertiesTable.setRowCount(len(properties)) 91 self.propertiesTable.setRowCount(len(properties))
88 for row, (key, value) in enumerate(properties): 92 for row, (key, value) in enumerate(properties):
89 self.propertiesTable.setItem(row, 0, QTableWidgetItem(key)) 93 self.propertiesTable.setItem(row, 0, QTableWidgetItem(key))
90 self.propertiesTable.setItem(row, 1, QTableWidgetItem(value)) 94 self.propertiesTable.setItem(row, 1, QTableWidgetItem(value))
91 else: 95 else:
92 self.clear() 96 self.clear()
93 97
94 def getProperties(self): 98 def getProperties(self):
95 """ 99 """
96 Public method to get the list of defined user properties. 100 Public method to get the list of defined user properties.
97 101
98 @return list of defined user properties 102 @return list of defined user properties
99 @rtype list of tuple of (str, str) 103 @rtype list of tuple of (str, str)
100 """ 104 """
101 properties = [] 105 properties = []
102 106
103 for row in range(self.propertiesTable.rowCount()): 107 for row in range(self.propertiesTable.rowCount()):
104 keyItem = self.propertiesTable.item(row, 0) 108 keyItem = self.propertiesTable.item(row, 0)
105 key = keyItem.text() if keyItem else "" 109 key = keyItem.text() if keyItem else ""
106 if key: 110 if key:
107 valueItem = self.propertiesTable.item(row, 1) 111 valueItem = self.propertiesTable.item(row, 1)
108 value = valueItem.text() if valueItem else "" 112 value = valueItem.text() if valueItem else ""
109 properties.append([key, value]) 113 properties.append([key, value])
110 114
111 return properties 115 return properties
112 116
113 117
114 class MqttUserPropertiesEditorDialog(QDialog): 118 class MqttUserPropertiesEditorDialog(QDialog):
115 """ 119 """
116 Class implementing an editor dialog for MQTT v5 user properties. 120 Class implementing an editor dialog for MQTT v5 user properties.
117 """ 121 """
122
118 def __init__(self, header, properties, parent=None): 123 def __init__(self, header, properties, parent=None):
119 """ 124 """
120 Constructor 125 Constructor
121 126
122 @param header text to be shown in the dialog header label 127 @param header text to be shown in the dialog header label
123 @type str 128 @type str
124 @param properties list of defined user properties 129 @param properties list of defined user properties
125 @type list of tuple of (str, str) 130 @type list of tuple of (str, str)
126 @param parent reference to the parent widget (defaults to None) 131 @param parent reference to the parent widget (defaults to None)
127 @type QWidget (optional) 132 @type QWidget (optional)
128 """ 133 """
129 super().__init__(parent) 134 super().__init__(parent)
130 135
131 self.setObjectName("MqttUserPropertiesEditor") 136 self.setObjectName("MqttUserPropertiesEditor")
132 self.resize(400, 300) 137 self.resize(400, 300)
133 self.setSizeGripEnabled(True) 138 self.setSizeGripEnabled(True)
134 self.setWindowTitle(self.tr("User Properties")) 139 self.setWindowTitle(self.tr("User Properties"))
135 140
136 self.__layout = QVBoxLayout(self) 141 self.__layout = QVBoxLayout(self)
137 142
138 self.__headerLabel = QLabel(header, self) 143 self.__headerLabel = QLabel(header, self)
139 self.__layout.addWidget(self.__headerLabel) 144 self.__layout.addWidget(self.__headerLabel)
140 145
141 self.__propertiesEditor = MqttUserPropertiesEditor(self) 146 self.__propertiesEditor = MqttUserPropertiesEditor(self)
142 self.__layout.addWidget(self.__propertiesEditor) 147 self.__layout.addWidget(self.__propertiesEditor)
143 148
144 self.__buttonBox = QDialogButtonBox(self) 149 self.__buttonBox = QDialogButtonBox(self)
145 self.__buttonBox.setOrientation(Qt.Orientation.Horizontal) 150 self.__buttonBox.setOrientation(Qt.Orientation.Horizontal)
146 self.__buttonBox.setStandardButtons( 151 self.__buttonBox.setStandardButtons(
147 QDialogButtonBox.StandardButton.Cancel | 152 QDialogButtonBox.StandardButton.Cancel | QDialogButtonBox.StandardButton.Ok
148 QDialogButtonBox.StandardButton.Ok) 153 )
149 self.__buttonBox.setObjectName("buttonBox") 154 self.__buttonBox.setObjectName("buttonBox")
150 self.__layout.addWidget(self.__buttonBox) 155 self.__layout.addWidget(self.__buttonBox)
151 156
152 self.__buttonBox.accepted.connect(self.accept) 157 self.__buttonBox.accepted.connect(self.accept)
153 self.__buttonBox.rejected.connect(self.reject) 158 self.__buttonBox.rejected.connect(self.reject)
154 159
155 self.__propertiesEditor.setProperties(properties) 160 self.__propertiesEditor.setProperties(properties)
156 161
157 def getProperties(self): 162 def getProperties(self):
158 """ 163 """
159 Public method to get the list of defined user properties. 164 Public method to get the list of defined user properties.
160 165
161 @return list of defined user properties 166 @return list of defined user properties
162 @rtype list of tuple of (str, str) 167 @rtype list of tuple of (str, str)
163 """ 168 """
164 return self.__propertiesEditor.getProperties() 169 return self.__propertiesEditor.getProperties()

eric ide

mercurial