src/eric7/MicroPython/ConnectionSelectionDialog.py

Mon, 07 Nov 2022 17:19:58 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 07 Nov 2022 17:19:58 +0100
branch
eric7
changeset 9482
a2bc06a54d9d
parent 9473
3f23dbf37dbe
child 9653
e67609152c5e
permissions
-rw-r--r--

Corrected/acknowledged some bad import style and removed some obsolete code.

8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
8881
54e42bc2437a Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8318
diff changeset
3 # Copyright (c) 2021 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a dialog to select the port to connect to and the type of
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 the attached device.
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 """
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10
9473
3f23dbf37dbe Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9221
diff changeset
11 from PyQt6.QtCore import Qt, pyqtSlot
8318
962bce857696 Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
12 from PyQt6.QtWidgets import QDialog, QDialogButtonBox
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
9473
3f23dbf37dbe Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9221
diff changeset
14 from . import MicroPythonDevices
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 from .Ui_ConnectionSelectionDialog import Ui_ConnectionSelectionDialog
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 class ConnectionSelectionDialog(QDialog, Ui_ConnectionSelectionDialog):
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 """
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 Class implementing a dialog to select the port to connect to and the type
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 of the attached device.
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
23
8143
2c730d5fd177 Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8139
diff changeset
24 PortNameRole = Qt.ItemDataRole.UserRole
2c730d5fd177 Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8139
diff changeset
25 VidPidRole = Qt.ItemDataRole.UserRole + 1
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
26
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 def __init__(self, ports, currentPort, currentType, parent=None):
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 """
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 Constructor
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
30
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 @param ports list of detected ports
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 @type list of str
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 @param currentPort port most recently selected
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 @type str
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 @param currentType device type most recently selected
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 @type str
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 @param parent reference to the parent widget (defaults to None)
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 @type QWidget (optional)
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8143
diff changeset
40 super().__init__(parent)
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 self.setupUi(self)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
42
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 for index, (vid, pid, description, portName) in enumerate(
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 sorted(ports, key=lambda x: x[3])
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 ):
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 self.portNameComboBox.addItem(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
47 self.tr("{0} - {1}", "description - port name").format(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
48 description, portName
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
49 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
50 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
51 self.portNameComboBox.setItemData(index, portName, self.PortNameRole)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
52 self.portNameComboBox.setItemData(index, (vid, pid), self.VidPidRole)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
53
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 self.deviceTypeComboBox.addItem("", "")
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 for board, description in sorted(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
56 MicroPythonDevices.getSupportedDevices(), key=lambda x: x[1]
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 ):
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 self.deviceTypeComboBox.addItem(description, board)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
59
8135
7cbb1ebf8d2d MicroPython: fixed some logic issues related to handling of unknown devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8134
diff changeset
60 if self.portNameComboBox.currentText():
7cbb1ebf8d2d MicroPython: fixed some logic issues related to handling of unknown devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8134
diff changeset
61 # some ports were found; use the previously selected type as
7cbb1ebf8d2d MicroPython: fixed some logic issues related to handling of unknown devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8134
diff changeset
62 # default
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
63 portIndex = self.portNameComboBox.findData(currentPort, self.PortNameRole)
8135
7cbb1ebf8d2d MicroPython: fixed some logic issues related to handling of unknown devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8134
diff changeset
64 typeIndex = self.deviceTypeComboBox.findData(currentType)
7cbb1ebf8d2d MicroPython: fixed some logic issues related to handling of unknown devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8134
diff changeset
65 else:
7cbb1ebf8d2d MicroPython: fixed some logic issues related to handling of unknown devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8134
diff changeset
66 portIndex = 0
7cbb1ebf8d2d MicroPython: fixed some logic issues related to handling of unknown devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8134
diff changeset
67 typeIndex = 0
7cbb1ebf8d2d MicroPython: fixed some logic issues related to handling of unknown devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8134
diff changeset
68 self.portNameComboBox.setCurrentIndex(portIndex)
7cbb1ebf8d2d MicroPython: fixed some logic issues related to handling of unknown devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8134
diff changeset
69 self.deviceTypeComboBox.setCurrentIndex(typeIndex)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
70
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 self.__updateOK()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
72
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 msh = self.minimumSizeHint()
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 self.resize(max(self.width(), msh.width()), msh.height())
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
75
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 def __updateOK(self):
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 """
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 Private method to update the status of the OK button.
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 """
8143
2c730d5fd177 Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8139
diff changeset
80 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
81 bool(self.portNameComboBox.currentData(self.PortNameRole))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
82 and bool(self.deviceTypeComboBox.currentData())
8139
418c2d9a767d MicroPython: refined the selection of unknown dvices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8135
diff changeset
83 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
84
8139
418c2d9a767d MicroPython: refined the selection of unknown dvices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8135
diff changeset
85 @pyqtSlot(str)
418c2d9a767d MicroPython: refined the selection of unknown dvices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8135
diff changeset
86 def on_portNameComboBox_currentTextChanged(self, txt):
418c2d9a767d MicroPython: refined the selection of unknown dvices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8135
diff changeset
87 """
418c2d9a767d MicroPython: refined the selection of unknown dvices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8135
diff changeset
88 Private slot to handle the selection of a port name.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
89
8139
418c2d9a767d MicroPython: refined the selection of unknown dvices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8135
diff changeset
90 @param txt selected port
418c2d9a767d MicroPython: refined the selection of unknown dvices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8135
diff changeset
91 @type str
418c2d9a767d MicroPython: refined the selection of unknown dvices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8135
diff changeset
92 """
418c2d9a767d MicroPython: refined the selection of unknown dvices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8135
diff changeset
93 self.__updateOK()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
94
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 @pyqtSlot(str)
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 def on_deviceTypeComboBox_currentTextChanged(self, txt):
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 """
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 Private slot to handle the selection of a device type.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
99
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 @param txt selected device description
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 @type str
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 """
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 self.__updateOK()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
104
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 def getData(self):
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 """
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 Public method to get the entered data.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
108
8134
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 @return tuple containing the VID, PID and name of the selected port
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 and the selected device type
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 @rtype tuple of (int, int, str, str)
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 """
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 return (
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 *self.portNameComboBox.currentData(self.VidPidRole),
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 self.portNameComboBox.currentData(self.PortNameRole),
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 self.deviceTypeComboBox.currentData(),
a5c4ac339f2a MicroPython: finished adding manual connection capability with device type and port selection.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 )

eric ide

mercurial