eric7/EricWidgets/EricIconBar.py

branch
eric7
changeset 8584
90391fda03d5
parent 8583
aac629a05f8b
child 8587
78971b458d25
equal deleted inserted replaced
8583:aac629a05f8b 8584:90391fda03d5
5 5
6 """ 6 """
7 Module implementing a bar widget showing just icons. 7 Module implementing a bar widget showing just icons.
8 """ 8 """
9 9
10 from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt 10 import contextlib
11 from PyQt6.QtGui import QColor 11
12 from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt, QCoreApplication
13 from PyQt6.QtGui import QColor, QIcon
12 from PyQt6.QtWidgets import QWidget, QBoxLayout, QWIDGETSIZE_MAX 14 from PyQt6.QtWidgets import QWidget, QBoxLayout, QWIDGETSIZE_MAX
13 15
14 from .EricClickableLabel import EricClickableLabel 16 from .EricClickableLabel import EricClickableLabel
15 17
16 18
21 @signal currentChanged(index) emitted to indicate a change of the current 23 @signal currentChanged(index) emitted to indicate a change of the current
22 index 24 index
23 @signal currentClicked(index) emitted to indicate, that the current icon 25 @signal currentClicked(index) emitted to indicate, that the current icon
24 was clicked 26 was clicked
25 """ 27 """
26 IconSize = 48 28 BarSizes = {
27 BorderSize = 2 29 # tuples with (icon size, border size, translated size string)
30 "xs": (
31 16, 1,
32 QCoreApplication.translate("EricIconBar", "extra small")
33 ),
34 "sm": (
35 22, 1,
36 QCoreApplication.translate("EricIconBar", "small")
37 ),
38 "md": (
39 32, 2,
40 QCoreApplication.translate("EricIconBar", "medium")
41 ),
42 "lg": (
43 48, 2,
44 QCoreApplication.translate("EricIconBar", "large")
45 ),
46 "xl": (
47 64, 3,
48 QCoreApplication.translate("EricIconBar", "extra large")
49 ),
50 "xxl": (
51 96, 3,
52 QCoreApplication.translate("EricIconBar", "very large")
53 ),
54 }
55 DefaultBarSize = "md"
28 56
29 WidgetStyleSheetTemplate = "QWidget {{ background-color: {0}; }}" 57 WidgetStyleSheetTemplate = "QWidget {{ background-color: {0}; }}"
30 LabelStyleSheetTemplate = "QLabel {{ background-color: {0}; }}" 58 LabelStyleSheetTemplate = "QLabel {{ background-color: {0}; }}"
31 59
32 currentChanged = pyqtSignal(int) 60 currentChanged = pyqtSignal(int)
33 currentClicked = pyqtSignal(int) 61 currentClicked = pyqtSignal(int)
34 62
35 def __init__(self, orientation=Qt.Orientation.Horizontal, parent=None): 63 def __init__(self, orientation=Qt.Orientation.Horizontal,
64 barSize=DefaultBarSize, parent=None):
36 """ 65 """
37 Constructor 66 Constructor
38 67
39 @param orientation orientation for the widget 68 @param orientation orientation for the widget
40 @type Qt.Orientation 69 @type Qt.Orientation
70 @param barSize size category for the bar (one of 'xs', 'sm', 'md',
71 'lg', 'xl', 'xxl')
72 @type str
41 @param parent reference to the parent widget (defaults to None) 73 @param parent reference to the parent widget (defaults to None)
42 @type QWidget (optional) 74 @type QWidget (optional)
43 """ 75 """
44 super().__init__(parent) 76 super().__init__(parent)
45 77
78 try:
79 self.__barSize, self.__borderSize = (
80 EricIconBar.BarSizes[barSize][:2])
81 self.__barSizeKey = barSize
82 except KeyError:
83 self.__barSize, self.__borderSize = (
84 EricIconBar.BarSizes[EricIconBar.DefaultBarSize][:2])
46 self.__fixedHeightWidth = ( 85 self.__fixedHeightWidth = (
47 EricIconBar.IconSize + 2 * EricIconBar.BorderSize 86 self.__barSize + 2 * self.__borderSize
48 ) 87 )
49 88
50 # set initial values 89 # set initial values
51 self.__color = QColor("#008800") 90 self.__color = QColor("#008800")
52 self.__orientation = Qt.Orientation.Horizontal 91 self.__orientation = Qt.Orientation.Horizontal
53 self.__currentIndex = -1 92 self.__currentIndex = -1
93 self.__icons = []
54 94
55 # initialize with horizontal layout and change later if needed 95 # initialize with horizontal layout and change later if needed
56 self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True) 96 self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True)
57 self.setFixedHeight(self.__fixedHeightWidth) 97 self.setFixedHeight(self.__fixedHeightWidth)
58 98
59 self.__layout = QBoxLayout(QBoxLayout.Direction.LeftToRight) 99 self.__layout = QBoxLayout(QBoxLayout.Direction.LeftToRight)
60 self.__layout.setContentsMargins( 100 self.__layout.setContentsMargins(
61 EricIconBar.BorderSize, EricIconBar.BorderSize, 101 self.__borderSize, self.__borderSize,
62 EricIconBar.BorderSize, EricIconBar.BorderSize) 102 self.__borderSize, self.__borderSize)
63 self.__layout.setSpacing(EricIconBar.BorderSize)
64 103
65 self.__layout.addStretch() 104 self.__layout.addStretch()
66 105
67 self.setLayout(self.__layout) 106 self.setLayout(self.__layout)
68 107
97 @return orientation of the widget 136 @return orientation of the widget
98 @rtype Qt.Orientation 137 @rtype Qt.Orientation
99 """ 138 """
100 return self.__orientation 139 return self.__orientation
101 140
141 def setBarSize(self, barSize):
142 """
143 Public method to set the icon bar size.
144
145 @param barSize size category for the bar (one of 'xs', 'sm', 'md',
146 'lg', 'xl', 'xxl')
147 @type str
148 """
149 self.__barSize, self.__borderSize = (
150 EricIconBar.BarSizes[barSize][:2])
151 self.__barSizeKey = barSize
152 self.__fixedHeightWidth = (
153 self.__barSize + 2 * self.__borderSize
154 )
155
156 if self.__orientation == Qt.Orientation.Horizontal:
157 self.setFixedHeight(self.__fixedHeightWidth)
158 elif self.__orientation == Qt.Orientation.Vertical:
159 self.setFixedWidth(self.__fixedHeightWidth)
160
161 self.__layout.setContentsMargins(
162 self.__borderSize, self.__borderSize,
163 self.__borderSize, self.__borderSize)
164
165 for index, icon in enumerate(self.__icons):
166 iconLabel = self.__layout.itemAt(index)
167 iconLabel.setFixedSize(self.__barSize, self.__barSize)
168 iconLabel.setPixmap(icon.pixmap(self.__barSize, self.__barSize))
169
170 def barSize(self):
171 """
172 Public method to get the icon bar size.
173
174 @return barSize size category for the bar (one of 'xs', 'sm', 'md',
175 'lg', 'xl', 'xxl')
176 @rtype str
177 """
178 return self.__barSizeKey
179
102 def setColor(self, color): 180 def setColor(self, color):
103 """ 181 """
104 Public method to set the color of the widget. 182 Public method to set the color of the widget.
105 183
106 @param color color of the widget 184 @param color color of the widget
126 @return current color 204 @return current color
127 @rtype QColor 205 @rtype QColor
128 """ 206 """
129 return self.__color 207 return self.__color
130 208
131 def __createIcon(self, iconPixmap, label=""): 209 def __createIcon(self, icon, label=""):
132 """ 210 """
133 Private method to creat an icon label. 211 Private method to creat an icon label.
134 212
135 @param iconPixmap reference to the icon 213 @param icon reference to the icon
136 @type QPixmap 214 @type QIcon
137 @param label label text to be shown as a tooltip (defaults to "") 215 @param label label text to be shown as a tooltip (defaults to "")
138 @type str (optional) 216 @type str (optional)
139 @return created and connected label 217 @return created and connected label
140 @rtype EricClickableLabel 218 @rtype EricClickableLabel
141 """ 219 """
142 iconLabel = EricClickableLabel(self) 220 iconLabel = EricClickableLabel(self)
143 iconLabel.setFixedSize(EricIconBar.IconSize, EricIconBar.IconSize) 221 iconLabel.setFixedSize(self.__barSize, self.__barSize)
144 iconLabel.setAlignment(Qt.AlignmentFlag.AlignCenter) 222 iconLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
145 iconLabel.setPixmap(iconPixmap) 223 iconLabel.setPixmap(icon.pixmap(self.__barSize, self.__barSize))
146 if label: 224 if label:
147 iconLabel.setToolTip(label) 225 iconLabel.setToolTip(label)
148 226
149 iconLabel.clicked.connect(lambda: self.__iconClicked(iconLabel)) 227 iconLabel.clicked.connect(lambda: self.__iconClicked(iconLabel))
150 228
151 return iconLabel 229 return iconLabel
152 230
153 def addIcon(self, iconPixmap, label=""): 231 def addIcon(self, icon, label=""):
154 """ 232 """
155 Public method to add an icon to the bar. 233 Public method to add an icon to the bar.
156 234
157 @param iconPixmap reference to the icon 235 @param icon reference to the icon
158 @type QPixmap 236 @type QIcon
159 @param label label text to be shown as a tooltip (defaults to "") 237 @param label label text to be shown as a tooltip (defaults to "")
160 @type str (optional) 238 @type str (optional)
161 """ 239 """
162 # the stretch item is always the last one 240 # the stretch item is always the last one
163 self.insertIcon(self.count(), iconPixmap, label=label) 241 self.insertIcon(self.count(), icon, label=label)
164 242 self.__icons.append(QIcon(icon))
165 def insertIcon(self, index, iconPixmap, label=""): 243
244 def insertIcon(self, index, icon, label=""):
166 """ 245 """
167 Public method to insert an icon into the bar. 246 Public method to insert an icon into the bar.
168 247
169 @param index position to insert the icon at 248 @param index position to insert the icon at
170 @type int 249 @type int
171 @param iconPixmap reference to the icon 250 @param icon reference to the icon
172 @type QPixmap 251 @type QIcon
173 @param label label text to be shown as a tooltip (defaults to "") 252 @param label label text to be shown as a tooltip (defaults to "")
174 @type str (optional) 253 @type str (optional)
175 """ 254 """
176 iconLabel = self.__createIcon(iconPixmap, label=label) 255 iconLabel = self.__createIcon(icon, label=label)
177 self.__layout.insertWidget(index, iconLabel) 256 self.__layout.insertWidget(index, iconLabel)
257 self.__icons.insert(index, QIcon(icon))
178 258
179 if self.__currentIndex < 0: 259 if self.__currentIndex < 0:
180 self.setCurrentIndex(index) 260 self.setCurrentIndex(index)
181 elif index <= self.__currentIndex: 261 elif index <= self.__currentIndex:
182 self.setCurrentIndex(self.__currentIndex + 1) 262 self.setCurrentIndex(self.__currentIndex + 1)
188 @param index index of the icon to be removed 268 @param index index of the icon to be removed
189 @type int 269 @type int
190 """ 270 """
191 label = self.__layout.itemAt(index) 271 label = self.__layout.itemAt(index)
192 self.__layout.removeWidget(label) 272 self.__layout.removeWidget(label)
273 with contextlib.suppress(IndexError):
274 del self.__icons[index]
193 275
194 if index == self.__currentIndex: 276 if index == self.__currentIndex:
195 self.setCurrentIndex(index) 277 self.setCurrentIndex(index)
196 elif index < self.__currentIndex: 278 elif index < self.__currentIndex:
197 self.setCurrentIndex(self.__currentIndex - 1) 279 self.setCurrentIndex(self.__currentIndex - 1)

eric ide

mercurial