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