diff -r d527cfe919ae -r 90072e10ae9b src/eric7/MicroPython/CircuitPythonUpdater/ShowModulesDialog.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/eric7/MicroPython/CircuitPythonUpdater/ShowModulesDialog.py Mon Feb 06 10:09:18 2023 +0100 @@ -0,0 +1,128 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing a dialog to show the available modules of all bundles. +""" + +import circup + +from PyQt6.QtCore import Qt, pyqtSlot +from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QListWidgetItem + +from eric7.EricGui import EricPixmapCache + +from .Ui_ShowModulesDialog import Ui_ShowModulesDialog + + +class ShowModulesDialog(QDialog, Ui_ShowModulesDialog): + """ + Class implementing a dialog to show the available modules of all bundles. + """ + + def __init__(self, selectionMode=False, parent=None): + """ + Constructor + + @param selectionMode flag indicating the activation of the selection mode + (defaults to False) + @type bool (optional) + @param parent reference to the parent widget (defaults to None) + @type QWidget (optional) + """ + super().__init__(parent) + self.setupUi(self) + + self.filterButton.setIcon(EricPixmapCache.getIcon("check")) + self.filterButton.clicked.connect(self.__applyFilter) + + self.__checkCount = 0 + self.__selectionMode = selectionMode + if self.__selectionMode: + self.buttonBox.setStandardButtons( + QDialogButtonBox.StandardButton.Ok + | QDialogButtonBox.StandardButton.Cancel + ) + else: + self.buttonBox.setStandardButtons(QDialogButtonBox.StandardButton.Close) + + availableModules = circup.get_bundle_versions(circup.get_bundles_list()) + moduleNames = [m.replace(".py", "") for m in availableModules] + if self.__selectionMode: + for moduleName in moduleNames: + itm = QListWidgetItem(moduleName) + itm.setFlags(itm.flags() | Qt.ItemFlag.ItemIsUserCheckable) + itm.setCheckState(Qt.CheckState.Unchecked) + self.modulesList.addItem(itm) + else: + self.modulesList.addItems(moduleNames) + self.modulesList.sortItems(Qt.SortOrder.AscendingOrder) + + self.__applyFilter() + + self.__checkCountUpdated() + + @pyqtSlot() + def __applyFilter(self): + """ + Private slot to apply the filter to the list of available modules. + """ + filterStr = self.filterEdit.text() + counter = 0 + for row in range(self.modulesList.count()): + itm = self.modulesList.item(row) + visible = filterStr in itm.text() if filterStr else True + itm.setHidden(not visible) + if visible: + counter += 1 + + self.statusLabel.setText( + self.tr("Showing {0} of {1} modules/packages").format( + counter, self.modulesList.count() + ) + ) + self.filterEdit.selectAll() + self.filterEdit.setFocus(Qt.FocusReason.OtherFocusReason) + + @pyqtSlot(QListWidgetItem) + def on_modulesList_itemChanged(self, item): + """ + Private slot to handle a change of the check state of an item. + + @param item reference to the changed item + @type QTreeWidgetItem + """ + if self.__selectionMode: + if item.checkState() == Qt.CheckState.Checked: + self.__checkCount += 1 + else: + self.__checkCount -= 1 + + self.__checkCountUpdated() + + def __checkCountUpdated(self): + """ + Private method to handle an update of the check count. + """ + if self.__selectionMode: + self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( + self.__checkCount > 0 + ) + + def getSelection(self): + """ + Public method to get the list of selected modules. + + @return list of selected modules + @rtype circup.Module + """ + results = [] + if self.__selectionMode: + for row in range(self.modulesList.count()): + itm = self.modulesList.item(row) + if itm.checkState() == Qt.CheckState.Checked: + results.append(itm.text()) + + return results