|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing an editor for MQTT v5 user properties. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot |
|
11 from PyQt6.QtWidgets import QDialog, QTableWidgetItem |
|
12 |
|
13 from .Ui_MqttUserPropertiesEditor import Ui_MqttUserPropertiesEditor |
|
14 |
|
15 import UI.PixmapCache |
|
16 |
|
17 |
|
18 class MqttUserPropertiesEditor(QDialog, Ui_MqttUserPropertiesEditor): |
|
19 """ |
|
20 Class implementing an editor for MQTT v5 user properties. |
|
21 """ |
|
22 def __init__(self, header, properties, parent=None): |
|
23 """ |
|
24 Constructor |
|
25 |
|
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) |
|
31 @type QWidget (optional) |
|
32 """ |
|
33 super().__init__(parent) |
|
34 self.setupUi(self) |
|
35 |
|
36 self.addButton.setIcon(UI.PixmapCache.getIcon("plus")) |
|
37 self.deleteButton.setIcon(UI.PixmapCache.getIcon("minus")) |
|
38 self.clearButton.setIcon(UI.PixmapCache.getIcon("editDelete")) |
|
39 |
|
40 self.headerLabel.setText(header) |
|
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 |
|
48 self.deleteButton.setEnabled(False) |
|
49 |
|
50 @pyqtSlot() |
|
51 def on_propertiesTable_itemSelectionChanged(self): |
|
52 """ |
|
53 Private slot to handle the selection of rows. |
|
54 """ |
|
55 self.deleteButton.setEnabled( |
|
56 bool(self.propertiesTable.selectedItems())) |
|
57 |
|
58 @pyqtSlot() |
|
59 def on_addButton_clicked(self): |
|
60 """ |
|
61 Private slot to add a row to the table. |
|
62 """ |
|
63 self.propertiesTable.setRowCount(self.propertiesTable.rowCount() + 1) |
|
64 self.propertiesTable.setCurrentCell( |
|
65 self.propertiesTable.rowCount() - 1, 0) |
|
66 |
|
67 @pyqtSlot() |
|
68 def on_deleteButton_clicked(self): |
|
69 """ |
|
70 Private slot to delete the selected rows. |
|
71 """ |
|
72 selectedRanges = self.propertiesTable.selectedRanges() |
|
73 selectedRows = [(r.bottomRow(), r.topRow()) for r in selectedRanges] |
|
74 for bottomRow, topRow in sorted(selectedRows, reverse=True): |
|
75 for row in range(bottomRow, topRow - 1, -1): |
|
76 self.propertiesTable.removeRow(row) |
|
77 |
|
78 @pyqtSlot() |
|
79 def on_clearButton_clicked(self): |
|
80 """ |
|
81 Private slot to delete all properties. |
|
82 """ |
|
83 self.propertiesTable.clearContents() |
|
84 self.propertiesTable.setRowCount(10) |
|
85 self.propertiesTable.setCurrentCell(0, 0) |
|
86 |
|
87 def getProperties(self): |
|
88 """ |
|
89 Public method to get the list of defined user properties. |
|
90 |
|
91 @return list of defined user properties |
|
92 @rtype list of tuple of (str, str) |
|
93 """ |
|
94 properties = [] |
|
95 |
|
96 for row in range(self.propertiesTable.rowCount()): |
|
97 keyItem = self.propertiesTable.item(row, 0) |
|
98 key = keyItem.text() if keyItem else "" |
|
99 if key: |
|
100 valueItem = self.propertiesTable.item(row, 1) |
|
101 value = valueItem.text() if valueItem else "" |
|
102 properties.append((key, value)) |
|
103 |
|
104 return properties |