eric7/EricWidgets/EricIconBar.py

branch
eric7
changeset 8584
90391fda03d5
parent 8583
aac629a05f8b
child 8587
78971b458d25
diff -r aac629a05f8b -r 90391fda03d5 eric7/EricWidgets/EricIconBar.py
--- a/eric7/EricWidgets/EricIconBar.py	Mon Sep 06 19:52:37 2021 +0200
+++ b/eric7/EricWidgets/EricIconBar.py	Wed Sep 08 19:47:32 2021 +0200
@@ -7,8 +7,10 @@
 Module implementing a bar widget showing just icons.
 """
 
-from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt
-from PyQt6.QtGui import QColor
+import contextlib
+
+from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt, QCoreApplication
+from PyQt6.QtGui import QColor, QIcon
 from PyQt6.QtWidgets import QWidget, QBoxLayout, QWIDGETSIZE_MAX
 
 from .EricClickableLabel import EricClickableLabel
@@ -23,8 +25,34 @@
     @signal currentClicked(index) emitted to indicate, that the current icon
         was clicked
     """
-    IconSize = 48
-    BorderSize = 2
+    BarSizes = {
+        # tuples with (icon size, border size, translated size string)
+        "xs": (
+            16, 1,
+            QCoreApplication.translate("EricIconBar", "extra small")
+        ),
+        "sm": (
+            22, 1,
+            QCoreApplication.translate("EricIconBar", "small")
+        ),
+        "md": (
+            32, 2,
+            QCoreApplication.translate("EricIconBar", "medium")
+        ),
+        "lg": (
+            48, 2,
+            QCoreApplication.translate("EricIconBar", "large")
+        ),
+        "xl": (
+            64, 3,
+            QCoreApplication.translate("EricIconBar", "extra large")
+        ),
+        "xxl": (
+            96, 3,
+            QCoreApplication.translate("EricIconBar", "very large")
+        ),
+    }
+    DefaultBarSize = "md"
     
     WidgetStyleSheetTemplate = "QWidget {{ background-color: {0}; }}"
     LabelStyleSheetTemplate = "QLabel {{ background-color: {0}; }}"
@@ -32,25 +60,37 @@
     currentChanged = pyqtSignal(int)
     currentClicked = pyqtSignal(int)
     
-    def __init__(self, orientation=Qt.Orientation.Horizontal, parent=None):
+    def __init__(self, orientation=Qt.Orientation.Horizontal,
+                 barSize=DefaultBarSize, parent=None):
         """
         Constructor
         
         @param orientation orientation for the widget
         @type Qt.Orientation
+        @param barSize size category for the bar (one of 'xs', 'sm', 'md',
+            'lg', 'xl', 'xxl')
+        @type str
         @param parent reference to the parent widget (defaults to None)
         @type QWidget (optional)
         """
         super().__init__(parent)
         
+        try:
+            self.__barSize, self.__borderSize = (
+                EricIconBar.BarSizes[barSize][:2])
+            self.__barSizeKey = barSize
+        except KeyError:
+            self.__barSize, self.__borderSize = (
+                EricIconBar.BarSizes[EricIconBar.DefaultBarSize][:2])
         self.__fixedHeightWidth = (
-            EricIconBar.IconSize + 2 * EricIconBar.BorderSize
+            self.__barSize + 2 * self.__borderSize
         )
         
         # set initial values
         self.__color = QColor("#008800")
         self.__orientation = Qt.Orientation.Horizontal
         self.__currentIndex = -1
+        self.__icons = []
         
         # initialize with horizontal layout and change later if needed
         self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True)
@@ -58,9 +98,8 @@
         
         self.__layout = QBoxLayout(QBoxLayout.Direction.LeftToRight)
         self.__layout.setContentsMargins(
-            EricIconBar.BorderSize, EricIconBar.BorderSize,
-            EricIconBar.BorderSize, EricIconBar.BorderSize)
-        self.__layout.setSpacing(EricIconBar.BorderSize)
+            self.__borderSize, self.__borderSize,
+            self.__borderSize, self.__borderSize)
         
         self.__layout.addStretch()
         
@@ -99,6 +138,45 @@
         """
         return self.__orientation
     
+    def setBarSize(self, barSize):
+        """
+        Public method to set the icon bar size.
+        
+        @param barSize size category for the bar (one of 'xs', 'sm', 'md',
+            'lg', 'xl', 'xxl')
+        @type str
+        """
+        self.__barSize, self.__borderSize = (
+            EricIconBar.BarSizes[barSize][:2])
+        self.__barSizeKey = barSize
+        self.__fixedHeightWidth = (
+            self.__barSize + 2 * self.__borderSize
+        )
+        
+        if self.__orientation == Qt.Orientation.Horizontal:
+            self.setFixedHeight(self.__fixedHeightWidth)
+        elif self.__orientation == Qt.Orientation.Vertical:
+            self.setFixedWidth(self.__fixedHeightWidth)
+        
+        self.__layout.setContentsMargins(
+            self.__borderSize, self.__borderSize,
+            self.__borderSize, self.__borderSize)
+        
+        for index, icon in enumerate(self.__icons):
+            iconLabel = self.__layout.itemAt(index)
+            iconLabel.setFixedSize(self.__barSize, self.__barSize)
+            iconLabel.setPixmap(icon.pixmap(self.__barSize, self.__barSize))
+    
+    def barSize(self):
+        """
+        Public method to get the icon bar size.
+        
+        @return barSize size category for the bar (one of 'xs', 'sm', 'md',
+            'lg', 'xl', 'xxl')
+        @rtype str
+        """
+        return self.__barSizeKey
+    
     def setColor(self, color):
         """
         Public method to set the color of the widget.
@@ -128,21 +206,21 @@
         """
         return self.__color
     
-    def __createIcon(self, iconPixmap, label=""):
+    def __createIcon(self, icon, label=""):
         """
         Private method to creat an icon label.
         
-        @param iconPixmap reference to the icon
-        @type QPixmap
+        @param icon reference to the icon
+        @type QIcon
         @param label label text to be shown as a tooltip (defaults to "")
         @type str (optional)
         @return created and connected label
         @rtype EricClickableLabel
         """
         iconLabel = EricClickableLabel(self)
-        iconLabel.setFixedSize(EricIconBar.IconSize, EricIconBar.IconSize)
+        iconLabel.setFixedSize(self.__barSize, self.__barSize)
         iconLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
-        iconLabel.setPixmap(iconPixmap)
+        iconLabel.setPixmap(icon.pixmap(self.__barSize, self.__barSize))
         if label:
             iconLabel.setToolTip(label)
         
@@ -150,31 +228,33 @@
         
         return iconLabel
     
-    def addIcon(self, iconPixmap, label=""):
+    def addIcon(self, icon, label=""):
         """
         Public method to add an icon to the bar.
         
-        @param iconPixmap reference to the icon
-        @type QPixmap
+        @param icon reference to the icon
+        @type QIcon
         @param label label text to be shown as a tooltip (defaults to "")
         @type str (optional)
         """
         # the stretch item is always the last one
-        self.insertIcon(self.count(), iconPixmap, label=label)
+        self.insertIcon(self.count(), icon, label=label)
+        self.__icons.append(QIcon(icon))
     
-    def insertIcon(self, index, iconPixmap, label=""):
+    def insertIcon(self, index, icon, label=""):
         """
         Public method to insert an icon into the bar.
         
         @param index position to insert the icon at
         @type int
-        @param iconPixmap reference to the icon
-        @type QPixmap
+        @param icon reference to the icon
+        @type QIcon
         @param label label text to be shown as a tooltip (defaults to "")
         @type str (optional)
         """
-        iconLabel = self.__createIcon(iconPixmap, label=label)
+        iconLabel = self.__createIcon(icon, label=label)
         self.__layout.insertWidget(index, iconLabel)
+        self.__icons.insert(index, QIcon(icon))
         
         if self.__currentIndex < 0:
             self.setCurrentIndex(index)
@@ -190,6 +270,8 @@
         """
         label = self.__layout.itemAt(index)
         self.__layout.removeWidget(label)
+        with contextlib.suppress(IndexError):
+            del self.__icons[index]
         
         if index == self.__currentIndex:
             self.setCurrentIndex(index)

eric ide

mercurial