|
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.UserRole |
|
27 ModifiedRole = Qt.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(UnknownDevicesDialog, self).__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.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 devices = [] |
|
99 |
|
100 for row in range(self.deviceList.count()): |
|
101 devices.append(self.deviceList.item(row).data( |
|
102 self.DeviceDataRole)) |
|
103 Preferences.setMicroPython("ManualDevices", devices) |
|
104 |
|
105 return True |
|
106 |
|
107 @pyqtSlot() |
|
108 def __checkButtons(self): |
|
109 """ |
|
110 Private slot to set the enabled state of the buttons. |
|
111 """ |
|
112 selectedItemsCount = len(self.deviceList.selectedItems()) |
|
113 self.editButton.setEnabled(selectedItemsCount == 1) |
|
114 self.deleteButton.setEnabled(selectedItemsCount >= 1) |
|
115 |
|
116 @pyqtSlot(QListWidgetItem) |
|
117 def on_deviceList_itemActivated(self, item): |
|
118 """ |
|
119 Private slot to edit the data of the activated item. |
|
120 |
|
121 @param item reference to the activated item |
|
122 @type QListWidgetItem |
|
123 """ |
|
124 self.__editItem(item) |
|
125 |
|
126 @pyqtSlot() |
|
127 def on_deviceList_itemSelectionChanged(self): |
|
128 """ |
|
129 Private slot to handle a change of selected items. |
|
130 """ |
|
131 self.__checkButtons() |
|
132 |
|
133 @pyqtSlot() |
|
134 def on_editButton_clicked(self): |
|
135 """ |
|
136 Private slot to edit the selected item. |
|
137 """ |
|
138 itm = self.deviceList.selectedItems()[0] |
|
139 self.__editItem(itm) |
|
140 |
|
141 @pyqtSlot() |
|
142 def on_deleteButton_clicked(self): |
|
143 """ |
|
144 Private slot to delete the selected entries. |
|
145 """ |
|
146 unsaved = False |
|
147 for itm in self.deviceList.selectedItems(): |
|
148 unsaved |= itm.data(self.ModifiedRole) |
|
149 if unsaved: |
|
150 ok = E5MessageBox.yesNo( |
|
151 self, |
|
152 self.tr("Delete Unknown Devices"), |
|
153 self.tr("The selected entries contain some with modified" |
|
154 " data. Shall they really be deleted?")) |
|
155 if not ok: |
|
156 return |
|
157 |
|
158 for itm in self.deviceList.selectedItems(): |
|
159 self.deviceList.takeItem(self.deviceList.row(itm)) |
|
160 del itm |
|
161 |
|
162 @pyqtSlot() |
|
163 def on_deleteAllButton_clicked(self): |
|
164 """ |
|
165 Private slot to delete all devices. |
|
166 """ |
|
167 if self.__isDirty(): |
|
168 ok = E5MessageBox.yesNo( |
|
169 self, |
|
170 self.tr("Delete Unknown Devices"), |
|
171 self.tr("The list contains some devices with modified" |
|
172 " data. Shall they really be deleted?")) |
|
173 if not ok: |
|
174 return |
|
175 |
|
176 self.deviceList.clear() |
|
177 |
|
178 @pyqtSlot() |
|
179 def on_restoreButton_clicked(self): |
|
180 """ |
|
181 Private slot to restore the list of unknown devices. |
|
182 """ |
|
183 if self.__isDirty(): |
|
184 ok = E5MessageBox.yesNo( |
|
185 self, |
|
186 self.tr("Restore Unknown Devices"), |
|
187 self.tr("Restoring the list of unknown devices will overwrite" |
|
188 " all changes made. Do you really want to restore the" |
|
189 " list?")) |
|
190 if not ok: |
|
191 return |
|
192 |
|
193 self.__loadDevices() |
|
194 |
|
195 @pyqtSlot() |
|
196 def on_reportButton_clicked(self): |
|
197 """ |
|
198 Private slot to report the data of all boards to the eric-bugs email |
|
199 address. |
|
200 """ |
|
201 if self.deviceList.count() > 0: |
|
202 bodyList = [ |
|
203 "These are my MicroPython devices not yet known by eric." |
|
204 " Please add them.", |
|
205 "", |
|
206 ] |
|
207 |
|
208 for row in range(self.deviceList.count()): |
|
209 deviceDict = self.deviceList.item(row).data( |
|
210 self.DeviceDataRole) |
|
211 bodyList += [ |
|
212 "Board #{0}:".format(row), |
|
213 " VID: {0}".format(deviceDict["vid"]), |
|
214 " PID: {0}".format(deviceDict["pid"]), |
|
215 " Description: {0}".format(deviceDict["description"]), |
|
216 " Device Type: {0}".format(deviceDict["type"]), |
|
217 " Data Volume: {0}".format(deviceDict["data_volume"]), |
|
218 " Flash Volume: {0}".format(deviceDict["flash_volume"]), |
|
219 "" |
|
220 ] |
|
221 |
|
222 urlQuery = QUrlQuery() |
|
223 urlQuery.addQueryItem("subject", "Unsupported MicroPython Devices") |
|
224 urlQuery.addQueryItem("body", "\r\n".join(bodyList)) |
|
225 |
|
226 url = QUrl("mailto:{0}".format(BugAddress)) |
|
227 url.setQuery(urlQuery) |
|
228 |
|
229 QDesktopServices.openUrl(url) |
|
230 |
|
231 @pyqtSlot() |
|
232 def on_buttonBox_accepted(self): |
|
233 """ |
|
234 Private slot to handle the OK button press. |
|
235 |
|
236 This action saves the edited list to the preferences store. |
|
237 """ |
|
238 self.__saveDeviceData() |
|
239 self.accept() |
|
240 |
|
241 @pyqtSlot() |
|
242 def on_buttonBox_rejected(self): |
|
243 """ |
|
244 Private slot handling the cancellation of the dialog. |
|
245 """ |
|
246 if self.__isDirty(): |
|
247 ok = E5MessageBox.okToClearData( |
|
248 self, |
|
249 self.tr("Unsaved Data"), |
|
250 self.tr("""The list of devices contains some with modified""" |
|
251 """ data."""), |
|
252 self.__saveDeviceData) |
|
253 if not ok: |
|
254 return |
|
255 |
|
256 self.reject() |