eric7/MicroPython/UnknownDevicesDialog.py

branch
eric7
changeset 8312
800c432b34c8
parent 8218
7c09585bd960
child 8318
962bce857696
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to manage the list of unknown devices.
8 """
9
10 from PyQt5.QtCore import pyqtSlot, Qt, QUrl, QUrlQuery
11 from PyQt5.QtGui import QDesktopServices
12 from PyQt5.QtWidgets import QDialog, QListWidgetItem
13
14 from E5Gui import E5MessageBox
15
16 from .Ui_UnknownDevicesDialog import Ui_UnknownDevicesDialog
17
18 import Preferences
19 from UI.Info import BugAddress
20
21
22 class UnknownDevicesDialog(QDialog, Ui_UnknownDevicesDialog):
23 """
24 Class implementing a dialog to manage the list of unknown devices.
25 """
26 DeviceDataRole = Qt.ItemDataRole.UserRole
27 ModifiedRole = Qt.ItemDataRole.UserRole + 1
28
29 def __init__(self, parent=None):
30 """
31 Constructor
32
33 @param parent reference to the parent widget (defaults to None)
34 @type QWidget (optional)
35 """
36 super().__init__(parent)
37 self.setupUi(self)
38
39 self.__loadDevices()
40
41 def __loadDevices(self):
42 """
43 Private method to load the list of unknown devices.
44 """
45 self.deviceList.clear()
46
47 devices = Preferences.getMicroPython("ManualDevices")
48 for device in devices:
49 itm = QListWidgetItem(
50 self.tr("{0} (0x{1:04x}/0x{2:04x})", "description, VID, PID")
51 .format(device["description"], device["vid"], device["pid"]),
52 self.deviceList)
53 itm.setData(self.DeviceDataRole, device)
54 itm.setData(self.ModifiedRole, False)
55
56 self.__initialDeviceCount = self.deviceList.count()
57
58 self.__checkButtons()
59
60 def __isDirty(self):
61 """
62 Private method to check, if the dialog contains unsaved data.
63
64 @return flag indicating the presence of unsaved data
65 @rtype bool
66 """
67 dirty = False
68 for row in range(self.deviceList.count()):
69 dirty |= self.deviceList.item(row).data(self.ModifiedRole)
70 dirty |= self.deviceList.count() != self.__initialDeviceCount
71 return dirty
72
73 def __editItem(self, item):
74 """
75 Private method to edit the given item.
76
77 @param item reference to the item to be edited
78 @type QListWidgetItem
79 """
80 if item is None:
81 # play it safe
82 return
83
84 from .AddEditDevicesDialog import AddEditDevicesDialog
85 dlg = AddEditDevicesDialog(deviceData=item.data(self.DeviceDataRole))
86 if dlg.exec() == QDialog.DialogCode.Accepted:
87 deviceDict = dlg.getDeviceDict()
88 item.setData(self.DeviceDataRole, deviceDict)
89 item.setData(self.ModifiedRole, True)
90
91 item.setText(self.tr("{0} (*)", "list entry is modified")
92 .format(item.text()))
93
94 def __saveDeviceData(self):
95 """
96 Private method to save the device data.
97
98 @return flag indicating a successful save
99 @rtype bool
100 """
101 devices = []
102
103 for row in range(self.deviceList.count()):
104 devices.append(self.deviceList.item(row).data(
105 self.DeviceDataRole))
106 Preferences.setMicroPython("ManualDevices", devices)
107
108 return True
109
110 @pyqtSlot()
111 def __checkButtons(self):
112 """
113 Private slot to set the enabled state of the buttons.
114 """
115 selectedItemsCount = len(self.deviceList.selectedItems())
116 self.editButton.setEnabled(selectedItemsCount == 1)
117 self.deleteButton.setEnabled(selectedItemsCount >= 1)
118
119 @pyqtSlot(QListWidgetItem)
120 def on_deviceList_itemActivated(self, item):
121 """
122 Private slot to edit the data of the activated item.
123
124 @param item reference to the activated item
125 @type QListWidgetItem
126 """
127 self.__editItem(item)
128
129 @pyqtSlot()
130 def on_deviceList_itemSelectionChanged(self):
131 """
132 Private slot to handle a change of selected items.
133 """
134 self.__checkButtons()
135
136 @pyqtSlot()
137 def on_editButton_clicked(self):
138 """
139 Private slot to edit the selected item.
140 """
141 itm = self.deviceList.selectedItems()[0]
142 self.__editItem(itm)
143
144 @pyqtSlot()
145 def on_deleteButton_clicked(self):
146 """
147 Private slot to delete the selected entries.
148 """
149 unsaved = False
150 for itm in self.deviceList.selectedItems():
151 unsaved |= itm.data(self.ModifiedRole)
152 if unsaved:
153 ok = E5MessageBox.yesNo(
154 self,
155 self.tr("Delete Unknown Devices"),
156 self.tr("The selected entries contain some with modified"
157 " data. Shall they really be deleted?"))
158 if not ok:
159 return
160
161 for itm in self.deviceList.selectedItems():
162 self.deviceList.takeItem(self.deviceList.row(itm))
163 del itm
164
165 @pyqtSlot()
166 def on_deleteAllButton_clicked(self):
167 """
168 Private slot to delete all devices.
169 """
170 if self.__isDirty():
171 ok = E5MessageBox.yesNo(
172 self,
173 self.tr("Delete Unknown Devices"),
174 self.tr("The list contains some devices with modified"
175 " data. Shall they really be deleted?"))
176 if not ok:
177 return
178
179 self.deviceList.clear()
180
181 @pyqtSlot()
182 def on_restoreButton_clicked(self):
183 """
184 Private slot to restore the list of unknown devices.
185 """
186 if self.__isDirty():
187 ok = E5MessageBox.yesNo(
188 self,
189 self.tr("Restore Unknown Devices"),
190 self.tr("Restoring the list of unknown devices will overwrite"
191 " all changes made. Do you really want to restore the"
192 " list?"))
193 if not ok:
194 return
195
196 self.__loadDevices()
197
198 @pyqtSlot()
199 def on_reportButton_clicked(self):
200 """
201 Private slot to report the data of all boards to the eric-bugs email
202 address.
203 """
204 if self.deviceList.count() > 0:
205 bodyList = [
206 "These are my MicroPython devices not yet known by eric."
207 " Please add them.",
208 "",
209 ]
210
211 for row in range(self.deviceList.count()):
212 deviceDict = self.deviceList.item(row).data(
213 self.DeviceDataRole)
214 bodyList += [
215 "Board #{0}:".format(row),
216 " VID: {0}".format(deviceDict["vid"]),
217 " PID: {0}".format(deviceDict["pid"]),
218 " Description: {0}".format(deviceDict["description"]),
219 " Device Type: {0}".format(deviceDict["type"]),
220 " Data Volume: {0}".format(deviceDict["data_volume"]),
221 " Flash Volume: {0}".format(deviceDict["flash_volume"]),
222 ""
223 ]
224
225 urlQuery = QUrlQuery()
226 urlQuery.addQueryItem("subject", "Unsupported MicroPython Devices")
227 urlQuery.addQueryItem("body", "\r\n".join(bodyList))
228
229 url = QUrl("mailto:{0}".format(BugAddress))
230 url.setQuery(urlQuery)
231
232 QDesktopServices.openUrl(url)
233
234 @pyqtSlot()
235 def on_buttonBox_accepted(self):
236 """
237 Private slot to handle the OK button press.
238
239 This action saves the edited list to the preferences store.
240 """
241 self.__saveDeviceData()
242 self.accept()
243
244 @pyqtSlot()
245 def on_buttonBox_rejected(self):
246 """
247 Private slot handling the cancellation of the dialog.
248 """
249 if self.__isDirty():
250 ok = E5MessageBox.okToClearData(
251 self,
252 self.tr("Unsaved Data"),
253 self.tr("""The list of devices contains some with modified"""
254 """ data."""),
255 self.__saveDeviceData)
256 if not ok:
257 return
258
259 self.reject()

eric ide

mercurial