eric6/MicroPython/ConnectionSelectionDialog.py

changeset 8134
a5c4ac339f2a
child 8135
7cbb1ebf8d2d
equal deleted inserted replaced
8133:4d1d1c248f79 8134:a5c4ac339f2a
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to select the port to connect to and the type of
8 the attached device.
9 """
10
11 from PyQt5.QtCore import pyqtSlot, Qt
12 from PyQt5.QtWidgets import QDialog, QDialogButtonBox
13
14 from .Ui_ConnectionSelectionDialog import Ui_ConnectionSelectionDialog
15
16 from . import MicroPythonDevices
17
18
19 class ConnectionSelectionDialog(QDialog, Ui_ConnectionSelectionDialog):
20 """
21 Class implementing a dialog to select the port to connect to and the type
22 of the attached device.
23 """
24 PortNameRole = Qt.UserRole
25 VidPidRole = Qt.UserRole + 1
26
27 def __init__(self, ports, currentPort, currentType, parent=None):
28 """
29 Constructor
30
31 @param ports list of detected ports
32 @type list of str
33 @param currentPort port most recently selected
34 @type str
35 @param currentType device type most recently selected
36 @type str
37 @param parent reference to the parent widget (defaults to None)
38 @type QWidget (optional)
39 """
40 super(ConnectionSelectionDialog, self).__init__(parent)
41 self.setupUi(self)
42
43 for index, (vid, pid, description, portName) in enumerate(
44 sorted(ports, key=lambda x: x[3])
45 ):
46 self.portNameComboBox.addItem(
47 self.tr("{0} - {1}", "description - port name")
48 .format(description, portName))
49 self.portNameComboBox.setItemData(
50 index, portName, self.PortNameRole)
51 self.portNameComboBox.setItemData(
52 index, (vid, pid), self.VidPidRole)
53 self.portNameComboBox.setCurrentText(currentPort)
54
55 self.deviceTypeComboBox.addItem("", "")
56 for board, description in sorted(
57 MicroPythonDevices.getSupportedDevices(),
58 key=lambda x: x[1]
59 ):
60 self.deviceTypeComboBox.addItem(description, board)
61 index = self.deviceTypeComboBox.findData(currentType)
62 self.deviceTypeComboBox.setCurrentIndex(index)
63
64 self.__updateOK()
65
66 msh = self.minimumSizeHint()
67 self.resize(max(self.width(), msh.width()), msh.height())
68
69 def __updateOK(self):
70 """
71 Private method to update the status of the OK button.
72 """
73 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(
74 bool(self.deviceTypeComboBox.currentData()))
75
76 @pyqtSlot(str)
77 def on_deviceTypeComboBox_currentTextChanged(self, txt):
78 """
79 Private slot to handle the selection of a device type.
80
81 @param txt selected device description
82 @type str
83 """
84 self.__updateOK()
85
86 def getData(self):
87 """
88 Public method to get the entered data.
89
90 @return tuple containing the VID, PID and name of the selected port
91 and the selected device type
92 @rtype tuple of (int, int, str, str)
93 """
94 return (
95 *self.portNameComboBox.currentData(self.VidPidRole),
96 self.portNameComboBox.currentData(self.PortNameRole),
97 self.deviceTypeComboBox.currentData(),
98 )

eric ide

mercurial