frompathlibimportPathfromqtpy.QtCoreimportQSizefromqtpy.QtGuiimportQIconfromqtpy.QtWidgetsimportQListWidget,QListWidgetItemfrompathlibimportPathfromPyQt5.QtWidgetsimportQListWidget,QListWidgetItemfromPyQt5.QtGuiimportQIconfromPyQt5.QtCoreimportQSizeICON_ROOT=Path(__file__).parent/"icons"STYLES=r""" QListWidget { min-width: 294px; background: none; font-size: 16pt; color: palette(text); } QListWidget::item { width: 95px; height: 100px; border-radius: 4px; margin: 5px; padding: 5px; background: #D2E3A4; color: palette(text); } QListWidget::item:hover { background: palette(dark); } QListWidget::item:selected { background: #ABD149; color: palette(highlightedText); }"""def_get_icon(name):""" Retrieves the file path for an icon based on the given name. Args: name: The name of the icon file (without extension). Returns: The file path of the icon as a string if it exists, otherwise an empty string. """path=ICON_ROOT/f'{name.lower().replace(" ","_")}.png'ifnotpath.exists():return""returnstr(path)
[docs]classIcons(QListWidget):""" Icons is a custom QListWidget that displays a grid of icons with labels. It supports adding individual items with optional tooltips and styling. Methods: addItem: Adds an item with a label and an optional tooltip. addItems: Adds multiple items to the list using a dictionary of labels. """def__init__(self,parent=None):""" Initializes the Icons widget. Args: parent: The parent widget for this QListWidget. """super().__init__(parent=parent)self.setMovement(self.Static)# The items cannot be moved by the user.self.setViewMode(self.IconMode)# Make items appear as icons.self.setResizeMode(self.Adjust)# Re-layout when the view is resized.self.setUniformItemSizes(True)# Better performance.self.setIconSize(QSize(80,80))self.setWordWrap(True)self.setStyleSheet(STYLES)
[docs]defaddItem(self,label:str,tool_tip:str=None):""" Adds an item to the QListWidget with a label and an optional tooltip. Args: label: The label for the item. tool_tip: Optional tooltip text for the item. """ifisinstance(label,QListWidgetItem):super().addItem(label)returnitem=QListWidgetItem(QIcon(_get_icon(label)),label)iftool_tipisnotNone:item.setToolTip(tool_tip)super().addItem(item)
[docs]defaddItems(self,labels:dict)->None:""" Adds multiple items to the QListWidget from a dictionary of labels. Args: labels: A dictionary where the keys are labels and the values can contain optional tooltips. """forlabelinlabels:ifhasattr(labels[label],"tool_tip"):self.addItem(label,labels[label].tool_tip)else:self.addItem(label)