src/eric7/MicroPython/Devices/CircuitPythonUpdater/ShowOutdatedDialog.py

branch
eric7
changeset 9756
9854647c8c5c
parent 9740
90072e10ae9b
child 9768
e2b622afb5ff
equal deleted inserted replaced
9755:1a09700229e7 9756:9854647c8c5c
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to show outdated modules of a connected device.
8 """
9
10 import circup
11
12 from PyQt6.QtCore import Qt, pyqtSlot
13 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QTreeWidgetItem
14 from semver import VersionInfo
15
16 from .Ui_ShowOutdatedDialog import Ui_ShowOutdatedDialog
17
18
19 class ShowOutdatedDialog(QDialog, Ui_ShowOutdatedDialog):
20 """
21 Class implementing a dialog to show outdated modules of a connected device.
22 """
23
24 def __init__(self, devicePath, selectionMode=False, parent=None):
25 """
26 Constructor
27
28 @param devicePath path to the connected board
29 @type str
30 @param selectionMode flag indicating the activation of the selection mode
31 (defaults to False)
32 @type bool (optional)
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.header.clear()
40 self.modulesList.clear()
41
42 self.__checkCount = 0
43 self.__selectionMode = selectionMode
44 if self.__selectionMode:
45 self.buttonBox.setStandardButtons(
46 QDialogButtonBox.StandardButton.Ok
47 | QDialogButtonBox.StandardButton.Cancel
48 )
49 else:
50 self.buttonBox.setStandardButtons(QDialogButtonBox.StandardButton.Close)
51
52 self.__modules = {
53 m.name: m
54 for m in circup.find_modules(devicePath, circup.get_bundles_list())
55 if m.outofdate
56 }
57 if self.__modules:
58 self.header.setText(
59 self.tr(
60 "The following modules are out of date or probably need an update."
61 "\nMajor Updates may include breaking changes. Review before"
62 " updating.\nMPY Format changes require an update."
63 )
64 )
65 for module in self.__modules.values():
66 if isinstance(module.bundle_version, str) and not VersionInfo.isvalid(
67 module.bundle_version
68 ):
69 reason = self.tr("Incorrect '__version__' Metadata")
70 needsUpdate = True
71 elif module.bad_format:
72 reason = self.tr("Corrupted or Unknown MPY Format")
73 needsUpdate = True
74 elif module.mpy_mismatch:
75 reason = self.tr("MPY Format")
76 needsUpdate = True
77 elif module.major_update:
78 reason = self.tr("Major Version")
79 needsUpdate = False
80 else:
81 reason = self.tr("Minor Version")
82 needsUpdate = False
83 itm = QTreeWidgetItem(
84 self.modulesList,
85 [
86 module.name,
87 module.device_version
88 if module.device_version
89 else self.tr("unknown"),
90 module.bundle_version
91 if module.bundle_version
92 else self.tr("unknown"),
93 reason,
94 ],
95 )
96 if self.__selectionMode:
97 itm.setFlags(itm.flags() | Qt.ItemFlag.ItemIsUserCheckable)
98 itm.setCheckState(
99 0,
100 Qt.CheckState.Checked
101 if needsUpdate
102 else Qt.CheckState.Unchecked,
103 )
104 if needsUpdate:
105 self.__checkCount += 1
106 else:
107 self.header.setText(self.tr("All modules are up-to-date."))
108
109 self.modulesList.sortItems(0, Qt.SortOrder.AscendingOrder)
110 for column in range(self.modulesList.columnCount()):
111 self.modulesList.resizeColumnToContents(column)
112
113 self.__checkCountUpdated()
114
115 @pyqtSlot(QTreeWidgetItem, int)
116 def on_modulesList_itemChanged(self, item, column):
117 """
118 Private slot to handle a change of the check state of an item.
119
120 @param item reference to the changed item
121 @type QTreeWidgetItem
122 @param column changed column
123 @type int
124 """
125 if self.__selectionMode:
126 if item.checkState(0) == Qt.CheckState.Checked:
127 self.__checkCount += 1
128 else:
129 self.__checkCount -= 1
130
131 self.__checkCountUpdated()
132
133 def __checkCountUpdated(self):
134 """
135 Private method to handle an update of the check count.
136 """
137 if self.__selectionMode:
138 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(
139 self.__checkCount > 0
140 )
141
142 def getSelection(self):
143 """
144 Public method to get the list of selected modules.
145
146 @return list of selected modules
147 @rtype circup.Module
148 """
149 results = []
150 if self.__selectionMode:
151 for row in range(self.modulesList.topLevelItemCount()):
152 itm = self.modulesList.topLevelItem(row)
153 if itm.checkState(0) == Qt.CheckState.Checked:
154 results.append(self.__modules[itm.text(0)])
155
156 return results

eric ide

mercurial