Tue, 10 Dec 2024 15:48:52 +0100
Updated copyright for 2025.
# -*- coding: utf-8 -*- # Copyright (c) 2015 - 2025 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing the whitelist edit dialog. """ from PyQt6.QtCore import pyqtSlot from PyQt6.QtWidgets import QDialog, QInputDialog, QLineEdit from .Ui_EditWhiteListDialog import Ui_EditWhiteListDialog class EditWhiteListDialog(QDialog, Ui_EditWhiteListDialog): """ Class implementing the whitelist edit dialog. """ def __init__(self, whitelists, parent=None): """ Constructor @param whitelists dictionary containing the whitelists @type dict of list of str @param parent reference to the parent widget @type QWidget """ super().__init__(parent) self.setupUi(self) self.__lists = [ self.classesList, self.functionsList, self.methodsList, self.attributesList, self.variablesList, self.propertiesList, self.importsList, self.patternsList, ] self.classesList.addItems(whitelists["class"]) self.functionsList.addItems(whitelists["function"]) self.methodsList.addItems(whitelists["method"]) self.attributesList.addItems(whitelists["attribute"]) self.variablesList.addItems(whitelists["variable"]) self.propertiesList.addItems(whitelists["property"]) self.importsList.addItems(whitelists["import"]) self.patternsList.addItems(whitelists["__patterns__"]) self.listsWidget.setCurrentIndex(self.listsWidget.count() - 1) @pyqtSlot() def on_patternsList_itemSelectionChanged(self): """ Private slot to react upon a change of selection in the patterns list. """ self.__setButtonEnabledStates() @pyqtSlot() def on_propertiesList_itemSelectionChanged(self): """ Private slot to react upon a change of selection in the properties list. """ self.__setButtonEnabledStates() @pyqtSlot() def on_variablesList_itemSelectionChanged(self): """ Private slot to react upon a change of selection in the variables list. """ self.__setButtonEnabledStates() @pyqtSlot() def on_attributesList_itemSelectionChanged(self): """ Private slot to react upon a change of selection in the attributes list. """ self.__setButtonEnabledStates() @pyqtSlot() def on_functionsList_itemSelectionChanged(self): """ Private slot to react upon a change of selection in the functions list. """ self.__setButtonEnabledStates() @pyqtSlot() def on_methodsList_itemSelectionChanged(self): """ Private slot to react upon a change of selection in the methods list. """ self.__setButtonEnabledStates() @pyqtSlot() def on_classesList_itemSelectionChanged(self): """ Private slot to react upon a change of selection in the classes list. """ self.__setButtonEnabledStates() @pyqtSlot() def on_importsList_itemSelectionChanged(self): """ Private slot to react upon a change of selection in the imports list. """ self.__setButtonEnabledStates() def __isPattern(self, name): """ Private method to check, if a name is a wildcard pattern. @param name name to be checked @type str @return flag indicating a wildcard pattern @rtype bool """ return any(char in name for char in "*?[") @pyqtSlot() def on_addButton_clicked(self): """ Private slot to add an entry to the current list. """ name, ok = QInputDialog.getText( self, self.tr("Add to Whitelist"), self.tr( "Enter a name or wildcard pattern to be added to the" " current whitelist:" ), QLineEdit.EchoMode.Normal, ) if ok and bool(name): curr = self.__lists[self.listsWidget.currentIndex()] if curr is self.patternsList or self.__isPattern(name): self.patternsList.addItem(name) else: curr.addItem(name) @pyqtSlot() def on_removeButton_clicked(self): """ Private slot to remove the selected entries from the current list. """ curr = self.__lists[self.listsWidget.currentIndex()] for itm in curr.selectedItems(): row = curr.row(itm) curr.takeItem(row) del itm @pyqtSlot() def on_removeAllButton_clicked(self): """ Private slot to remove all entries from the current list. """ curr = self.__lists[self.listsWidget.currentIndex()] curr.clear() @pyqtSlot(int) def on_listsWidget_currentChanged(self, index): """ Private slot handling the selection of tab. @param index index of the selected tab @type int """ self.__setButtonEnabledStates() def __setButtonEnabledStates(self): """ Private slot to set the state of various buttons. """ curr = self.__lists[self.listsWidget.currentIndex()] self.removeButton.setEnabled(len(curr.selectedItems()) > 0) self.removeAllButton.setEnabled(curr.count() > 0) def __getWhiteList(self, listWidget): """ Private method to get the whitelisted names from a list widget. @param listWidget reference to the list widget @type QListWidget @return whitelisted names @rtype list of str """ whitelist = [] for row in range(listWidget.count()): whitelist.append(listWidget.item(row).text()) return whitelist def getWhiteLists(self): """ Public methods to retrieve the various whitelists. @return dictionary containing the whitelists @rtype dict of list of str """ return { "class": self.__getWhiteList(self.classesList), "function": self.__getWhiteList(self.functionsList), "method": self.__getWhiteList(self.methodsList), "attribute": self.__getWhiteList(self.attributesList), "variable": self.__getWhiteList(self.variablesList), "property": self.__getWhiteList(self.propertiesList), "import": self.__getWhiteList(self.importsList), "__patterns__": self.__getWhiteList(self.patternsList), }