eric6/MicroPython/AddEditDevicesDialog.py

changeset 8080
218ca8019eb8
child 8110
c5af1d1d2a78
equal deleted inserted replaced
8079:331e717c458e 8080:218ca8019eb8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to add or edit data of unknown MicroPython
8 devices.
9 """
10
11 from PyQt5.QtCore import pyqtSlot, Qt, QUrl, QUrlQuery
12 from PyQt5.QtGui import QDesktopServices
13 from PyQt5.QtWidgets import QDialog, QDialogButtonBox
14
15 from .Ui_AddEditDevicesDialog import Ui_AddEditDevicesDialog
16
17 from .MicroPythonDevices import getSupportedDevices
18
19 from UI.Info import BugAddress
20
21
22 class AddEditDevicesDialog(QDialog, Ui_AddEditDevicesDialog):
23 """
24 Class implementing a dialog to add or edit data of unknown MicroPython
25 devices.
26 """
27 def __init__(self, vid=0, pid=0, description=0, deviceData=None,
28 parent=None):
29 """
30 Constructor
31
32 Note: Either vid and pid and description or deviceData dictionary
33 must be given.
34
35 @param vid vendor ID of the device (defaults to 0)
36 @type int (optional)
37 @param pid product ID of the device (defaults to 0)
38 @type int (optional)
39 @param description description for the device (defaults to "")
40 @type str (optional)
41 @param deviceData type of the device (defaults to None)
42 @type dict (optional)
43 @param parent reference to the parent widget (defaults to None)
44 @type QWidget (optional)
45 """
46 super(AddEditDevicesDialog, self).__init__(parent)
47 self.setupUi(self)
48
49 # populate the device type combo box
50 self.deviceTypeComboBox.addItem("", "")
51 for board, desc in sorted(getSupportedDevices(), key=lambda x: x[1]):
52 self.deviceTypeComboBox.addItem(desc, board)
53
54 if deviceData is not None:
55 self.vidEdit.setText("0x{0:04x}".format(deviceData["vid"]))
56 self.pidEdit.setText("0x{0:04x}".format(deviceData["pid"]))
57 self.descriptionEdit.setText(deviceData["description"])
58 self.deviceTypeComboBox.setCurrentIndex(
59 self.deviceTypeComboBox.findData(deviceData["type"]))
60 self.dataVolumeEdit.setText(deviceData["data_volume"])
61 self.flashVolumeEdit.setText(deviceData["flash_volume"])
62 else:
63 self.vidEdit.setText("0x{0:04x}".format(vid))
64 self.pidEdit.setText("0x{0:04x}".format(pid))
65 self.descriptionEdit.setText(description)
66 self.deviceTypeComboBox.setCurrentText("")
67 self.dataVolumeEdit.setText("")
68 self.flashVolumeEdit.setText("")
69
70 self.deviceTypeComboBox.setFocus(Qt.OtherFocusReason)
71
72 msh = self.minimumSizeHint()
73 self.resize(max(self.width(), msh.width()), msh.height())
74
75 @pyqtSlot(int)
76 def on_deviceTypeComboBox_currentIndexChanged(self, index):
77 """
78 Private slot to handle the selection of a device type.
79
80 @param index index of the current item
81 @type int
82 """
83 board = self.deviceTypeComboBox.currentData()
84 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(bool(board))
85 self.reportButton.setEnabled(bool(board))
86
87 @pyqtSlot()
88 def on_reportButton_clicked(self):
89 """
90 Private slot to report the entered data to the eric-bugs email address.
91 """
92 body = "\r\n".join([
93 "This is an unknow MicroPython device. Please add it.",
94 "",
95 "VID: {0}".format(self.vidEdit.text()),
96 "PID: {0}".format(self.pidEdit.text()),
97 "Description: {0}".format(self.descriptionEdit.text()),
98 "Device Type: {0}".format(self.deviceTypeComboBox.currentData()),
99 "Data Volume: {0}".format(self.dataVolumeEdit.text().strip()),
100 "Flash Volume: {0}".format(self.flashVolumeEdit.text().strip()),
101 ])
102
103 urlQuery = QUrlQuery()
104 urlQuery.addQueryItem("subject", "Unsupported MicroPython Device")
105 urlQuery.addQueryItem("body", body)
106
107 url = QUrl("mailto:{0}".format(BugAddress))
108 url.setQuery(urlQuery)
109
110 QDesktopServices.openUrl(url)
111
112 def getDeviceDict(self):
113 """
114 Public method to get the entered data as a dictionary.
115
116 @return dictionary containing the entered data
117 @rtype dict
118 """
119 return {
120 "vid": int(self.vidEdit.text(), 16),
121 "pid": int(self.pidEdit.text(), 16),
122 "description": self.descriptionEdit.text(),
123 "type": self.deviceTypeComboBox.currentData(),
124 "data_volume": self.dataVolumeEdit.text().strip(),
125 "flash_volume": self.flashVolumeEdit.text().strip(),
126 }

eric ide

mercurial