|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter the WebREPL connection parameters. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot |
|
11 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QLineEdit |
|
12 |
|
13 from eric7.EricGui import EricPixmapCache |
|
14 |
|
15 from .Devices import getSupportedDevices |
|
16 from .Ui_MicroPythonWebreplConnectionDialog import Ui_MicroPythonWebreplConnectionDialog |
|
17 |
|
18 |
|
19 class MicroPythonWebreplConnectionDialog( |
|
20 QDialog, Ui_MicroPythonWebreplConnectionDialog |
|
21 ): |
|
22 """ |
|
23 Class implementing a dialog to enter the WebREPL connection parameters. |
|
24 """ |
|
25 |
|
26 def __init__(self, currentWebreplUrl, currentType, parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param currentWebreplUrl WebREPL URL most recently configured |
|
31 @type str |
|
32 @param currentType device type most recently selected |
|
33 @type str |
|
34 @param parent reference to the parent widget (defaults to None) |
|
35 @type QWidget (optional) |
|
36 """ |
|
37 super().__init__(parent) |
|
38 self.setupUi(self) |
|
39 |
|
40 self.deviceTypeComboBox.addItem("", "") |
|
41 for board, description in sorted(getSupportedDevices(), key=lambda x: x[1]): |
|
42 self.deviceTypeComboBox.addItem(description, board) |
|
43 |
|
44 self.showPasswordButton.setIcon(EricPixmapCache.getIcon("showPassword")) |
|
45 |
|
46 self.hostEdit.textChanged.connect(self.__updateOkButton) |
|
47 self.portEdit.textChanged.connect(self.__updateOkButton) |
|
48 self.deviceTypeComboBox.currentIndexChanged.connect(self.__updateOkButton) |
|
49 |
|
50 if currentWebreplUrl: |
|
51 url = currentWebreplUrl.replace("ws://", "") |
|
52 password, hostPort = url.split("@", 1) if "@" in url else ("", url) |
|
53 host, port = hostPort.split(":", 1) if ":" in hostPort else (hostPort, "") |
|
54 self.hostEdit.setText(host) |
|
55 self.portEdit.setText(port) |
|
56 self.passwordEdit.setText(password) |
|
57 |
|
58 typeIndex = self.deviceTypeComboBox.findData(currentType) |
|
59 self.deviceTypeComboBox.setCurrentIndex(typeIndex) |
|
60 else: |
|
61 self.__updateOkButton() |
|
62 |
|
63 msh = self.minimumSizeHint() |
|
64 self.resize(max(self.width(), msh.width()), msh.height()) |
|
65 |
|
66 @pyqtSlot() |
|
67 def __updateOkButton(self): |
|
68 """ |
|
69 Private slot to update the enabled state of the OK button. |
|
70 """ |
|
71 port = self.portEdit.text() |
|
72 if port == "": |
|
73 portOk = True |
|
74 else: |
|
75 try: |
|
76 portNo = int(port) |
|
77 portOk = 1024 < portNo <= 65535 |
|
78 except ValueError: |
|
79 portOk = False |
|
80 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( |
|
81 bool(self.hostEdit.text()) |
|
82 and portOk |
|
83 and bool(self.deviceTypeComboBox.currentData()) |
|
84 ) |
|
85 |
|
86 @pyqtSlot(bool) |
|
87 def on_showPasswordButton_clicked(self, checked): |
|
88 """ |
|
89 Private slot to show or hide the password. |
|
90 |
|
91 @param checked state of the button |
|
92 @type bool |
|
93 """ |
|
94 if checked: |
|
95 self.passwordEdit.setEchoMode(QLineEdit.EchoMode.Normal) |
|
96 self.showPasswordButton.setIcon(EricPixmapCache.getIcon("hidePassword")) |
|
97 self.showPasswordButton.setToolTip(self.tr("Press to hide the password.")) |
|
98 else: |
|
99 self.passwordEdit.setEchoMode(QLineEdit.EchoMode.Password) |
|
100 self.showPasswordButton.setIcon(EricPixmapCache.getIcon("showPassword")) |
|
101 self.showPasswordButton.setToolTip(self.tr("Press to show the password.")) |
|
102 |
|
103 def getWebreplConnectionParameters(self): |
|
104 """ |
|
105 Public method to retrieve the entered WebREPL connection data. |
|
106 |
|
107 @return tuple containing the URL and device type for the WebREPL connection |
|
108 @rtype tuple of (str, str) |
|
109 """ |
|
110 password = self.passwordEdit.text() |
|
111 host = self.hostEdit.text() |
|
112 port = self.portEdit.text() |
|
113 |
|
114 if password and port: |
|
115 url = f"ws://{password}@{host}:{port}" |
|
116 elif password: |
|
117 url = f"ws://{password}@{host}" |
|
118 elif port: |
|
119 url = f"ws://{host}:{port}" |
|
120 else: |
|
121 url = f"ws://{host}" |
|
122 |
|
123 return (url, self.deviceTypeComboBox.currentData()) |