MqttMonitor/MqttUserPropertiesEditor.py

Thu, 22 Jul 2021 19:02:32 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 22 Jul 2021 19:02:32 +0200
branch
eric7
changeset 103
5fe4f179975f
parent 102
70b8858199f5
child 114
8c0e9e602124
permissions
-rw-r--r--

Continued implementing support for MQTT v5 user properties.

# -*- coding: utf-8 -*-

# Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing an editor for MQTT v5 user properties.
"""

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(QWidget, Ui_MqttUserPropertiesEditor):
    """
    Class implementing an editor for MQTT v5 user properties.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget (defaults to None)
        @type QWidget (optional)
        """
        super().__init__(parent)
        self.setupUi(self)
        
        self.addButton.setIcon(UI.PixmapCache.getIcon("plus"))
        self.deleteButton.setIcon(UI.PixmapCache.getIcon("minus"))
        self.clearButton.setIcon(UI.PixmapCache.getIcon("editDelete"))
        
        self.clearButton.clicked.connect(self.clear)
        
        self.deleteButton.setEnabled(False)
    
    @pyqtSlot()
    def on_propertiesTable_itemSelectionChanged(self):
        """
        Private slot to handle the selection of rows.
        """
        self.deleteButton.setEnabled(
            bool(self.propertiesTable.selectedItems()))
    
    @pyqtSlot()
    def on_addButton_clicked(self):
        """
        Private slot to add a row to the table.
        """
        self.propertiesTable.setRowCount(self.propertiesTable.rowCount() + 1)
        self.propertiesTable.setCurrentCell(
            self.propertiesTable.rowCount() - 1, 0)
    
    @pyqtSlot()
    def on_deleteButton_clicked(self):
        """
        Private slot to delete the selected rows.
        """
        selectedRanges = self.propertiesTable.selectedRanges()
        selectedRows = [(r.bottomRow(), r.topRow()) for r in selectedRanges]
        for bottomRow, topRow in sorted(selectedRows, reverse=True):
            for row in range(bottomRow, topRow - 1, -1):
                self.propertiesTable.removeRow(row)
    
    @pyqtSlot()
    def clear(self):
        """
        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.
        
        @return list of defined user properties
        @rtype list of tuple of (str, str)
        """
        properties = []
        
        for row in range(self.propertiesTable.rowCount()):
            keyItem = self.propertiesTable.item(row, 0)
            key = keyItem.text() if keyItem else ""
            if key:
                valueItem = self.propertiesTable.item(row, 1)
                value = valueItem.text() if valueItem else ""
                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