|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to edit the list of configured WebREPL URLs. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import Qt, pyqtSlot |
|
11 from PyQt6.QtWidgets import QDialog, QTreeWidgetItem |
|
12 |
|
13 from eric7.EricWidgets import EricMessageBox |
|
14 |
|
15 from .MicroPythonWebreplUrlAddEditDialog import MicroPythonWebreplUrlAddEditDialog |
|
16 from .Ui_MicroPythonWebreplUrlsConfigDialog import Ui_MicroPythonWebreplUrlsConfigDialog |
|
17 |
|
18 |
|
19 class MicroPythonWebreplUrlsConfigDialog( |
|
20 QDialog, Ui_MicroPythonWebreplUrlsConfigDialog |
|
21 ): |
|
22 """ |
|
23 Class implementing a dialog to edit the list of configured WebREPL URLs. |
|
24 """ |
|
25 |
|
26 def __init__(self, webreplDict, parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param webreplDict dictionary containing the configured WebREPL URLs |
|
31 @type dict |
|
32 @param parent reference to the parent widget (defaults to None) |
|
33 @type QWidget (optional) |
|
34 """ |
|
35 super().__init__(parent) |
|
36 self.setupUi(self) |
|
37 |
|
38 for name in webreplDict: |
|
39 itm = QTreeWidgetItem( |
|
40 self.webreplUrlsList, |
|
41 [name, webreplDict[name]["description"], webreplDict[name]["url"]], |
|
42 ) |
|
43 itm.setData(0, Qt.ItemDataRole.UserRole, webreplDict["device_type"]) |
|
44 |
|
45 self.__sortItems() |
|
46 self.__resizeColumns() |
|
47 self.__updateActionButtons() |
|
48 |
|
49 self.webreplUrlsList.itemSelectionChanged.connect(self.__updateActionButtons) |
|
50 |
|
51 @pyqtSlot() |
|
52 def __sortItems(self): |
|
53 """ |
|
54 Private slot to sort the list by name column (i.e. column 0). |
|
55 """ |
|
56 self.webreplUrlsList.sortItems(0, Qt.SortOrder.AscendingOrder) |
|
57 |
|
58 @pyqtSlot() |
|
59 def __resizeColumns(self): |
|
60 """ |
|
61 Private slot to resize the columns to their contents. |
|
62 """ |
|
63 for column in range(self.webreplUrlsList.columnCount()): |
|
64 self.webreplUrlsList.resizeColumnToContents(column) |
|
65 |
|
66 @pyqtSlot() |
|
67 def __updateActionButtons(self): |
|
68 """ |
|
69 Private slot to change the enabled state of the action buttons. |
|
70 """ |
|
71 selectedItemsCount = len(self.webreplUrlsList.selectedItems()) |
|
72 self.editButton.setEnabled(selectedItemsCount == 1) |
|
73 self.removeButton.setEnabled(selectedItemsCount > 0) |
|
74 |
|
75 self.removeAllButton.setEnabled(self.webreplUrlsList.topLevelItemCount() > 0) |
|
76 |
|
77 def __definedNames(self): |
|
78 """ |
|
79 Private method to get a list of defined connection names. |
|
80 |
|
81 @return list of defined connection names |
|
82 @rtype list of str |
|
83 """ |
|
84 return [ |
|
85 self.webreplUrlsList.topLevelItem(row).text(0) |
|
86 for row in range(self.webreplUrlsList.topLevelItemCount()) |
|
87 ] |
|
88 |
|
89 @pyqtSlot() |
|
90 def on_addButton_clicked(self): |
|
91 """ |
|
92 Private slot to add a new WebREPL connection. |
|
93 """ |
|
94 dlg = MicroPythonWebreplUrlAddEditDialog(self.__definedNames(), parent=self) |
|
95 if dlg.exec() == QDialog.DialogCode.Accepted: |
|
96 name, description, url, deviceType = dlg.getWebreplUrl() |
|
97 itm = QTreeWidgetItem(self.webreplUrlsList, [name, description, url]) |
|
98 itm.setData(0, Qt.ItemDataRole.UserRole, deviceType) |
|
99 |
|
100 self.__sortItems() |
|
101 self.__resizeColumns() |
|
102 self.__updateActionButtons() |
|
103 |
|
104 @pyqtSlot() |
|
105 def on_editButton_clicked(self): |
|
106 """ |
|
107 Private slot to edit the selected WebREPL connection. |
|
108 """ |
|
109 itm = self.webreplUrlsList.selectedItems()[0] |
|
110 dlg = MicroPythonWebreplUrlAddEditDialog( |
|
111 self.__definedNames(), |
|
112 connectionParams=(itm.text(0), itm.text(1), itm.text(2)), |
|
113 parent=self, |
|
114 ) |
|
115 if dlg.exec() == QDialog.DialogCode.Accepted: |
|
116 name, description, url, deviceType = dlg.getWebreplUrl() |
|
117 itm.setText(0, name) |
|
118 itm.setText(1, description) |
|
119 itm.setText(2, url) |
|
120 itm.setData(0, Qt.ItemDataRole.UserRole, deviceType) |
|
121 |
|
122 self.__sortItems() |
|
123 self.__resizeColumns() |
|
124 self.__updateActionButtons() |
|
125 |
|
126 @pyqtSlot() |
|
127 def on_removeButton_clicked(self): |
|
128 """ |
|
129 Private slot to remove the selected entries. |
|
130 """ |
|
131 ok = EricMessageBox.yesNo( |
|
132 self, |
|
133 self.tr("Remove Selected WebREPL URLs"), |
|
134 self.tr("""Shall the selected WebREPL URLs really be removed?"""), |
|
135 ) |
|
136 if ok: |
|
137 for itm in self.webreplUrlsList.selectedItems(): |
|
138 self.webreplUrlsList.takeTopLevelItem( |
|
139 self.webreplUrlsList.indexOfTopLevelItem(itm) |
|
140 ) |
|
141 del itm |
|
142 |
|
143 @pyqtSlot() |
|
144 def on_removeAllButton_clicked(self): |
|
145 """ |
|
146 Private slot to remove all entries. |
|
147 """ |
|
148 ok = EricMessageBox.yesNo( |
|
149 self, |
|
150 self.tr("Remove All WebREPL URLs"), |
|
151 self.tr("""Shall all WebREPL URLs really be removed?"""), |
|
152 ) |
|
153 if ok: |
|
154 self.webreplUrlsList.clear() |
|
155 |
|
156 def getWebreplDict(self): |
|
157 """ |
|
158 Public method to retrieve a dictionary containing the configured WebREPL URLs. |
|
159 |
|
160 @return dictionary containing the configured WebREPL URLs |
|
161 @rtype dict |
|
162 """ |
|
163 webreplDict = {} |
|
164 for row in range(self.webreplUrlsList.topLevelItemCount()): |
|
165 itm = self.webreplUrlsList.topLevelItem(row) |
|
166 webreplDict[itm.text(0)] = { |
|
167 "description": itm.text(1), |
|
168 "url": itm.text(2), |
|
169 "device_type": itm.data(0, Qt.ItemDataRole.UserRole), |
|
170 } |
|
171 |
|
172 return webreplDict |