MqttMonitor/MqttUserPropertiesEditor.py

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

eric ide

mercurial