MqttMonitor/MqttUserPropertiesEditor.py

branch
eric7
changeset 103
5fe4f179975f
parent 102
70b8858199f5
child 114
8c0e9e602124
--- a/MqttMonitor/MqttUserPropertiesEditor.py	Wed Jul 21 20:10:36 2021 +0200
+++ b/MqttMonitor/MqttUserPropertiesEditor.py	Thu Jul 22 19:02:32 2021 +0200
@@ -7,26 +7,24 @@
 Module implementing an editor for MQTT v5 user properties.
 """
 
-from PyQt6.QtCore import pyqtSlot
-from PyQt6.QtWidgets import QDialog, QTableWidgetItem
+from PyQt6.QtCore import pyqtSlot, Qt
+from PyQt6.QtWidgets import (
+    QDialog, QDialogButtonBox, QTableWidgetItem, QVBoxLayout, QLabel, QWidget
+)
 
 from .Ui_MqttUserPropertiesEditor import Ui_MqttUserPropertiesEditor
 
 import UI.PixmapCache
 
 
-class MqttUserPropertiesEditor(QDialog, Ui_MqttUserPropertiesEditor):
+class MqttUserPropertiesEditor(QWidget, Ui_MqttUserPropertiesEditor):
     """
     Class implementing an editor for MQTT v5 user properties.
     """
-    def __init__(self, header, properties, parent=None):
+    def __init__(self, parent=None):
         """
         Constructor
         
-        @param header text to be shown in the dialog header label
-        @type  str
-        @param properties list of defined user properties
-        @type list of tuple of (str, str)
         @param parent reference to the parent widget (defaults to None)
         @type QWidget (optional)
         """
@@ -37,13 +35,7 @@
         self.deleteButton.setIcon(UI.PixmapCache.getIcon("minus"))
         self.clearButton.setIcon(UI.PixmapCache.getIcon("editDelete"))
         
-        self.headerLabel.setText(header)
-        
-        if properties:
-            self.propertiesTable.setRowCount(len(properties))
-            for row, (key, value) in enumerate(properties):
-                self.propertiesTable.setItem(row, 0, QTableWidgetItem(key))
-                self.propertiesTable.setItem(row, 1, QTableWidgetItem(value))
+        self.clearButton.clicked.connect(self.clear)
         
         self.deleteButton.setEnabled(False)
     
@@ -76,14 +68,29 @@
                 self.propertiesTable.removeRow(row)
     
     @pyqtSlot()
-    def on_clearButton_clicked(self):
+    def clear(self):
         """
-        Private slot to delete all properties.
+        Public slot to delete all properties.
         """
         self.propertiesTable.clearContents()
         self.propertiesTable.setRowCount(10)
         self.propertiesTable.setCurrentCell(0, 0)
     
+    def setProperties(self, properties):
+        """
+        Public method to populate the editor with a list of user properties.
+        
+        @param properties list of defined user properties
+        @type list of tuple of (str, str)
+        """
+        if properties:
+            self.propertiesTable.setRowCount(len(properties))
+            for row, (key, value) in enumerate(properties):
+                self.propertiesTable.setItem(row, 0, QTableWidgetItem(key))
+                self.propertiesTable.setItem(row, 1, QTableWidgetItem(value))
+        else:
+            self.clear()
+    
     def getProperties(self):
         """
         Public method to get the list of defined user properties.
@@ -99,6 +106,59 @@
             if key:
                 valueItem = self.propertiesTable.item(row, 1)
                 value = valueItem.text() if valueItem else ""
-                properties.append((key, value))
+                properties.append([key, value])
         
         return properties
+
+
+class MqttUserPropertiesEditorDialog(QDialog):
+    """
+    Class implementing an editor dialog for MQTT v5 user properties.
+    """
+    def __init__(self, header, properties, parent=None):
+        """
+        Constructor
+        
+        @param header text to be shown in the dialog header label
+        @type  str
+        @param properties list of defined user properties
+        @type list of tuple of (str, str)
+        @param parent reference to the parent widget (defaults to None)
+        @type QWidget (optional)
+        """
+        super().__init__(parent)
+        
+        self.setObjectName("MqttUserPropertiesEditor")
+        self.resize(400, 300)
+        self.setSizeGripEnabled(True)
+        self.setWindowTitle(self.tr("User Properties"))
+        
+        self.__layout = QVBoxLayout(self)
+        
+        self.__headerLabel = QLabel(header, self)
+        self.__layout.addWidget(self.__headerLabel)
+        
+        self.__propertiesEditor = MqttUserPropertiesEditor(self)
+        self.__layout.addWidget(self.__propertiesEditor)
+        
+        self.__buttonBox = QDialogButtonBox(self)
+        self.__buttonBox.setOrientation(Qt.Orientation.Horizontal)
+        self.__buttonBox.setStandardButtons(
+            QDialogButtonBox.StandardButton.Cancel |
+            QDialogButtonBox.StandardButton.Ok)
+        self.__buttonBox.setObjectName("buttonBox")
+        self.__layout.addWidget(self.__buttonBox)
+        
+        self.__buttonBox.accepted.connect(self.accept)
+        self.__buttonBox.rejected.connect(self.reject)
+        
+        self.__propertiesEditor.setProperties(properties)
+    
+    def getProperties(self):
+        """
+        Public method to get the list of defined user properties.
+        
+        @return list of defined user properties
+        @rtype list of tuple of (str, str)
+        """
+        return self.__propertiesEditor.getProperties()

eric ide

mercurial