|
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 parameters for a WebREPL connection. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot |
|
11 from PyQt6.QtWidgets import QDialog, QDialogButtonBox |
|
12 |
|
13 from .Devices import getSupportedDevices |
|
14 from .Ui_MicroPythonWebreplUrlAddEditDialog import Ui_MicroPythonWebreplUrlAddEditDialog |
|
15 |
|
16 |
|
17 class MicroPythonWebreplUrlAddEditDialog( |
|
18 QDialog, Ui_MicroPythonWebreplUrlAddEditDialog |
|
19 ): |
|
20 """ |
|
21 Class implementing a dialog to edit the parameters for a WebREPL connection. |
|
22 """ |
|
23 |
|
24 def __init__(self, definedNames, connectionParams=None, parent=None): |
|
25 """ |
|
26 Constructor |
|
27 |
|
28 @param definedNames list of already define WebREPL connection names |
|
29 @type list of str |
|
30 @param connectionParams parameters for the WebREPL connection to be edited |
|
31 (default to None) |
|
32 @type tuple of (str, str, str) (optional) |
|
33 @param parent reference to the parent widget (defaults to None) |
|
34 @type QWidget (optional) |
|
35 """ |
|
36 super().__init__(parent) |
|
37 self.setupUi(self) |
|
38 |
|
39 self.__definedNames = definedNames[:] |
|
40 |
|
41 self.deviceTypeComboBox.addItem("", "") |
|
42 for board, description in sorted(getSupportedDevices(), key=lambda x: x[1]): |
|
43 self.deviceTypeComboBox.addItem(description, board) |
|
44 |
|
45 self.nameEdit.textChanged.connect(self.__updateOkButton) |
|
46 self.descriptionEdit.textChanged.connect(self.__updateOkButton) |
|
47 self.hostEdit.textChanged.connect(self.__updateOkButton) |
|
48 self.portEdit.textChanged.connect(self.__updateOkButton) |
|
49 self.deviceTypeComboBox.currentIndexChanged.connect(self.__updateOkButton) |
|
50 |
|
51 if connectionParams: |
|
52 self.__editName = connectionParams[0] |
|
53 self.__populateFields(connectionParams) |
|
54 else: |
|
55 self.__editName = "" |
|
56 |
|
57 msh = self.minimumSizeHint() |
|
58 self.resize(max(self.width(), msh.width()), msh.height()) |
|
59 |
|
60 def __populateFields(self, params): |
|
61 """ |
|
62 Private method to populate the various dialog fields with the given parameters. |
|
63 |
|
64 @param params arameters for the WebREPL connection to be edited |
|
65 @type tuple of (str, str, str) |
|
66 """ |
|
67 self.nameEdit.setText(params[0]) |
|
68 self.descriptionEdit.setText(params[1]) |
|
69 |
|
70 url = params[2].replace("ws://", "") |
|
71 if "@" in url: |
|
72 password, hostPort = url.split("@", 1) |
|
73 else: |
|
74 password, hostPort = "", url |
|
75 if ":" in hostPort: |
|
76 host, port = hostPort.split(":", 1) |
|
77 else: |
|
78 host, port = hostPort, "" |
|
79 self.hostEdit.setText(host) |
|
80 self.portEdit.setText(port) |
|
81 self.passwordEdit.setText(password) |
|
82 |
|
83 typeIndex = self.deviceTypeComboBox.findData(params["device_type"]) |
|
84 self.deviceTypeComboBox.setCurrentIndex(typeIndex) |
|
85 |
|
86 @pyqtSlot() |
|
87 def __updateOkButton(self): |
|
88 """ |
|
89 Private slot to update the enabled state of the OK button. |
|
90 """ |
|
91 port = self.portEdit.text() |
|
92 if port == "": |
|
93 portOk = True |
|
94 else: |
|
95 try: |
|
96 portNo = int(port) |
|
97 portOk = 1024 < portNo <= 65535 |
|
98 except ValueError: |
|
99 portOk = False |
|
100 |
|
101 name = self.nameEdit.text() |
|
102 nameOk = bool(name) and ( |
|
103 name == self.__editName or name not in self.__definedNames |
|
104 ) |
|
105 |
|
106 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( |
|
107 nameOk |
|
108 and bool(self.descriptionEdit.text()) |
|
109 and bool(self.hostEdit.text()) |
|
110 and portOk |
|
111 and bool(self.deviceTypeComboBox.currentData()) |
|
112 ) |
|
113 |
|
114 def getWebreplUrl(self): |
|
115 """ |
|
116 Public method to retrieve the entered WebREPL connection data. |
|
117 |
|
118 @return tuple containing the name, description and URL for the WebREPL |
|
119 connection |
|
120 @rtype tuple of (str, str, str) |
|
121 """ |
|
122 password = self.passwordEdit.text() |
|
123 host = self.hostEdit.text() |
|
124 port = self.portEdit.text() |
|
125 |
|
126 if password and port: |
|
127 url = f"ws://{password}@{host}:{port}" |
|
128 elif password: |
|
129 url = f"ws://{password}@{host}" |
|
130 elif port: |
|
131 url = f"ws://{host}:{port}" |
|
132 else: |
|
133 url = f"ws://{host}" |
|
134 |
|
135 return ( |
|
136 self.nameEdit.text(), |
|
137 self.descriptionEdit.text(), |
|
138 url, |
|
139 self.deviceTypeComboBox.currentData(), |
|
140 ) |