Wed, 21 Jul 2021 20:10:36 +0200
Started 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 from PyQt6.QtWidgets import QDialog, QTableWidgetItem from .Ui_MqttUserPropertiesEditor import Ui_MqttUserPropertiesEditor import UI.PixmapCache class MqttUserPropertiesEditor(QDialog, Ui_MqttUserPropertiesEditor): """ Class implementing an editor 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.setupUi(self) self.addButton.setIcon(UI.PixmapCache.getIcon("plus")) 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.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 on_clearButton_clicked(self): """ Private slot to delete all properties. """ self.propertiesTable.clearContents() self.propertiesTable.setRowCount(10) self.propertiesTable.setCurrentCell(0, 0) 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