|
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.ItemDataRole.UserRole |
|
25 VidPidRole = Qt.ItemDataRole.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().__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 |
|
54 self.deviceTypeComboBox.addItem("", "") |
|
55 for board, description in sorted( |
|
56 MicroPythonDevices.getSupportedDevices(), |
|
57 key=lambda x: x[1] |
|
58 ): |
|
59 self.deviceTypeComboBox.addItem(description, board) |
|
60 |
|
61 if self.portNameComboBox.currentText(): |
|
62 # some ports were found; use the previously selected type as |
|
63 # default |
|
64 portIndex = self.portNameComboBox.findData( |
|
65 currentPort, self.PortNameRole) |
|
66 typeIndex = self.deviceTypeComboBox.findData(currentType) |
|
67 else: |
|
68 portIndex = 0 |
|
69 typeIndex = 0 |
|
70 self.portNameComboBox.setCurrentIndex(portIndex) |
|
71 self.deviceTypeComboBox.setCurrentIndex(typeIndex) |
|
72 |
|
73 self.__updateOK() |
|
74 |
|
75 msh = self.minimumSizeHint() |
|
76 self.resize(max(self.width(), msh.width()), msh.height()) |
|
77 |
|
78 def __updateOK(self): |
|
79 """ |
|
80 Private method to update the status of the OK button. |
|
81 """ |
|
82 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( |
|
83 bool(self.portNameComboBox.currentData(self.PortNameRole)) and |
|
84 bool(self.deviceTypeComboBox.currentData()) |
|
85 ) |
|
86 |
|
87 @pyqtSlot(str) |
|
88 def on_portNameComboBox_currentTextChanged(self, txt): |
|
89 """ |
|
90 Private slot to handle the selection of a port name. |
|
91 |
|
92 @param txt selected port |
|
93 @type str |
|
94 """ |
|
95 self.__updateOK() |
|
96 |
|
97 @pyqtSlot(str) |
|
98 def on_deviceTypeComboBox_currentTextChanged(self, txt): |
|
99 """ |
|
100 Private slot to handle the selection of a device type. |
|
101 |
|
102 @param txt selected device description |
|
103 @type str |
|
104 """ |
|
105 self.__updateOK() |
|
106 |
|
107 def getData(self): |
|
108 """ |
|
109 Public method to get the entered data. |
|
110 |
|
111 @return tuple containing the VID, PID and name of the selected port |
|
112 and the selected device type |
|
113 @rtype tuple of (int, int, str, str) |
|
114 """ |
|
115 return ( |
|
116 *self.portNameComboBox.currentData(self.VidPidRole), |
|
117 self.portNameComboBox.currentData(self.PortNameRole), |
|
118 self.deviceTypeComboBox.currentData(), |
|
119 ) |