src/eric7/EricWidgets/EricIconBar.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9298
8863f3e970a2
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
21 21
22 22
23 class EricIconBar(QWidget): 23 class EricIconBar(QWidget):
24 """ 24 """
25 Class implementing a bar widget showing just icons. 25 Class implementing a bar widget showing just icons.
26 26
27 @signal currentChanged(index) emitted to indicate a change of the current 27 @signal currentChanged(index) emitted to indicate a change of the current
28 index 28 index
29 @signal currentClicked(index) emitted to indicate, that the current icon 29 @signal currentClicked(index) emitted to indicate, that the current icon
30 was clicked 30 was clicked
31 """ 31 """
32
32 BarSizes = { 33 BarSizes = {
33 # tuples with (icon size, border size, translated size string) 34 # tuples with (icon size, border size, translated size string)
34 "xs": ( 35 "xs": (16, 1, QCoreApplication.translate("EricIconBar", "extra small")),
35 16, 1, 36 "sm": (22, 1, QCoreApplication.translate("EricIconBar", "small")),
36 QCoreApplication.translate("EricIconBar", "extra small") 37 "md": (32, 2, QCoreApplication.translate("EricIconBar", "medium")),
37 ), 38 "lg": (48, 2, QCoreApplication.translate("EricIconBar", "large")),
38 "sm": ( 39 "xl": (64, 3, QCoreApplication.translate("EricIconBar", "extra large")),
39 22, 1, 40 "xxl": (96, 3, QCoreApplication.translate("EricIconBar", "very large")),
40 QCoreApplication.translate("EricIconBar", "small")
41 ),
42 "md": (
43 32, 2,
44 QCoreApplication.translate("EricIconBar", "medium")
45 ),
46 "lg": (
47 48, 2,
48 QCoreApplication.translate("EricIconBar", "large")
49 ),
50 "xl": (
51 64, 3,
52 QCoreApplication.translate("EricIconBar", "extra large")
53 ),
54 "xxl": (
55 96, 3,
56 QCoreApplication.translate("EricIconBar", "very large")
57 ),
58 } 41 }
59 DefaultBarSize = "md" 42 DefaultBarSize = "md"
60 43
61 MoreLabelAspect = 36 / 96 44 MoreLabelAspect = 36 / 96
62 45
63 MenuStyleSheetTemplate = ( 46 MenuStyleSheetTemplate = (
64 "QMenu {{ background-color: {0}; " 47 "QMenu {{ background-color: {0}; "
65 "selection-background-color: {1}; " 48 "selection-background-color: {1}; "
66 "border: 1px solid; }}" 49 "border: 1px solid; }}"
67 ) 50 )
68 WidgetStyleSheetTemplate = "QWidget {{ background-color: {0}; }}" 51 WidgetStyleSheetTemplate = "QWidget {{ background-color: {0}; }}"
69 LabelStyleSheetTemplate = "QLabel {{ background-color: {0}; }}" 52 LabelStyleSheetTemplate = "QLabel {{ background-color: {0}; }}"
70 53
71 currentChanged = pyqtSignal(int) 54 currentChanged = pyqtSignal(int)
72 currentClicked = pyqtSignal(int) 55 currentClicked = pyqtSignal(int)
73 56
74 def __init__(self, orientation=Qt.Orientation.Horizontal, 57 def __init__(
75 barSize=DefaultBarSize, parent=None): 58 self, orientation=Qt.Orientation.Horizontal, barSize=DefaultBarSize, parent=None
59 ):
76 """ 60 """
77 Constructor 61 Constructor
78 62
79 @param orientation orientation for the widget 63 @param orientation orientation for the widget
80 @type Qt.Orientation 64 @type Qt.Orientation
81 @param barSize size category for the bar (one of 'xs', 'sm', 'md', 65 @param barSize size category for the bar (one of 'xs', 'sm', 'md',
82 'lg', 'xl', 'xxl') 66 'lg', 'xl', 'xxl')
83 @type str 67 @type str
84 @param parent reference to the parent widget (defaults to None) 68 @param parent reference to the parent widget (defaults to None)
85 @type QWidget (optional) 69 @type QWidget (optional)
86 """ 70 """
87 super().__init__(parent) 71 super().__init__(parent)
88 72
89 try: 73 try:
90 self.__barSize, self.__borderSize = ( 74 self.__barSize, self.__borderSize = EricIconBar.BarSizes[barSize][:2]
91 EricIconBar.BarSizes[barSize][:2])
92 self.__barSizeKey = barSize 75 self.__barSizeKey = barSize
93 except KeyError: 76 except KeyError:
94 self.__barSize, self.__borderSize = ( 77 self.__barSize, self.__borderSize = EricIconBar.BarSizes[
95 EricIconBar.BarSizes[EricIconBar.DefaultBarSize][:2]) 78 EricIconBar.DefaultBarSize
96 self.__fixedHeightWidth = ( 79 ][:2]
97 self.__barSize + 2 * self.__borderSize 80 self.__fixedHeightWidth = self.__barSize + 2 * self.__borderSize
81 self.__minimumHeightWidth = (
82 int(self.__barSize * self.MoreLabelAspect) + 2 * self.__borderSize
98 ) 83 )
99 self.__minimumHeightWidth = int( 84
100 self.__barSize * self.MoreLabelAspect) + 2 * self.__borderSize
101
102 # set initial values 85 # set initial values
103 self.__color = QColor("#008800") 86 self.__color = QColor("#008800")
104 self.__orientation = Qt.Orientation.Horizontal 87 self.__orientation = Qt.Orientation.Horizontal
105 self.__currentIndex = -1 88 self.__currentIndex = -1
106 self.__icons = [] 89 self.__icons = []
107 90
108 # initialize with horizontal layout and change later if needed 91 # initialize with horizontal layout and change later if needed
109 self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True) 92 self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True)
110 self.setFixedHeight(self.__fixedHeightWidth) 93 self.setFixedHeight(self.__fixedHeightWidth)
111 self.setMinimumWidth(self.__minimumHeightWidth) 94 self.setMinimumWidth(self.__minimumHeightWidth)
112 95
113 self.__layout = QBoxLayout(QBoxLayout.Direction.LeftToRight) 96 self.__layout = QBoxLayout(QBoxLayout.Direction.LeftToRight)
114 self.__layout.setContentsMargins( 97 self.__layout.setContentsMargins(
115 self.__borderSize, self.__borderSize, 98 self.__borderSize, self.__borderSize, self.__borderSize, self.__borderSize
116 self.__borderSize, self.__borderSize) 99 )
117 100
118 self.__layout.addStretch() 101 self.__layout.addStretch()
119 102
120 self.setLayout(self.__layout) 103 self.setLayout(self.__layout)
121 104
122 if orientation != self.__orientation: 105 if orientation != self.__orientation:
123 self.setOrientation(orientation) 106 self.setOrientation(orientation)
124 107
125 self.setColor(self.__color) 108 self.setColor(self.__color)
126 109
127 self.__createAndAddMoreLabel() 110 self.__createAndAddMoreLabel()
128 111
129 self.__adjustIconLabels() 112 self.__adjustIconLabels()
130 113
131 def setOrientation(self, orientation): 114 def setOrientation(self, orientation):
132 """ 115 """
133 Public method to set the widget orientation. 116 Public method to set the widget orientation.
134 117
135 @param orientation orientation to be set 118 @param orientation orientation to be set
136 @type Qt.Orientation 119 @type Qt.Orientation
137 """ 120 """
138 # reset list widget size constraints 121 # reset list widget size constraints
139 self.setFixedSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX) 122 self.setFixedSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)
140 123
141 # remove the 'More' icon 124 # remove the 'More' icon
142 itm = self.__layout.takeAt(self.__layout.count() - 1) 125 itm = self.__layout.takeAt(self.__layout.count() - 1)
143 itm.widget().deleteLater() 126 itm.widget().deleteLater()
144 del itm 127 del itm
145 128
146 if orientation == Qt.Orientation.Horizontal: 129 if orientation == Qt.Orientation.Horizontal:
147 self.setFixedHeight(self.__fixedHeightWidth) 130 self.setFixedHeight(self.__fixedHeightWidth)
148 self.setMinimumWidth(self.__minimumHeightWidth) 131 self.setMinimumWidth(self.__minimumHeightWidth)
149 self.__layout.setDirection(QBoxLayout.Direction.LeftToRight) 132 self.__layout.setDirection(QBoxLayout.Direction.LeftToRight)
150 elif orientation == Qt.Orientation.Vertical: 133 elif orientation == Qt.Orientation.Vertical:
151 self.setFixedWidth(self.__fixedHeightWidth) 134 self.setFixedWidth(self.__fixedHeightWidth)
152 self.setMinimumHeight(self.__minimumHeightWidth) 135 self.setMinimumHeight(self.__minimumHeightWidth)
153 self.__layout.setDirection(QBoxLayout.Direction.TopToBottom) 136 self.__layout.setDirection(QBoxLayout.Direction.TopToBottom)
154 137
155 self.__orientation = orientation 138 self.__orientation = orientation
156 139
157 self.__createAndAddMoreLabel() 140 self.__createAndAddMoreLabel()
158 141
159 self.__adjustIconLabels() 142 self.__adjustIconLabels()
160 143
161 def orientation(self): 144 def orientation(self):
162 """ 145 """
163 Public method to get the orientation of the widget. 146 Public method to get the orientation of the widget.
164 147
165 @return orientation of the widget 148 @return orientation of the widget
166 @rtype Qt.Orientation 149 @rtype Qt.Orientation
167 """ 150 """
168 return self.__orientation 151 return self.__orientation
169 152
170 def setBarSize(self, barSize): 153 def setBarSize(self, barSize):
171 """ 154 """
172 Public method to set the icon bar size. 155 Public method to set the icon bar size.
173 156
174 @param barSize size category for the bar (one of 'xs', 'sm', 'md', 157 @param barSize size category for the bar (one of 'xs', 'sm', 'md',
175 'lg', 'xl', 'xxl') 158 'lg', 'xl', 'xxl')
176 @type str 159 @type str
177 """ 160 """
178 # remove the 'More' icon 161 # remove the 'More' icon
179 itm = self.__layout.takeAt(self.__layout.count() - 1) 162 itm = self.__layout.takeAt(self.__layout.count() - 1)
180 itm.widget().deleteLater() 163 itm.widget().deleteLater()
181 del itm 164 del itm
182 165
183 self.__barSize, self.__borderSize = ( 166 self.__barSize, self.__borderSize = EricIconBar.BarSizes[barSize][:2]
184 EricIconBar.BarSizes[barSize][:2])
185 self.__barSizeKey = barSize 167 self.__barSizeKey = barSize
186 self.__fixedHeightWidth = ( 168 self.__fixedHeightWidth = self.__barSize + 2 * self.__borderSize
187 self.__barSize + 2 * self.__borderSize 169 self.__minimumHeightWidth = (
170 int(self.__barSize * self.MoreLabelAspect) + 2 * self.__borderSize
188 ) 171 )
189 self.__minimumHeightWidth = int( 172
190 self.__barSize * self.MoreLabelAspect) + 2 * self.__borderSize
191
192 if self.__orientation == Qt.Orientation.Horizontal: 173 if self.__orientation == Qt.Orientation.Horizontal:
193 self.setFixedHeight(self.__fixedHeightWidth) 174 self.setFixedHeight(self.__fixedHeightWidth)
194 self.setMinimumWidth(self.__minimumHeightWidth) 175 self.setMinimumWidth(self.__minimumHeightWidth)
195 elif self.__orientation == Qt.Orientation.Vertical: 176 elif self.__orientation == Qt.Orientation.Vertical:
196 self.setFixedWidth(self.__fixedHeightWidth) 177 self.setFixedWidth(self.__fixedHeightWidth)
197 self.setMinimumHeight(self.__minimumHeightWidth) 178 self.setMinimumHeight(self.__minimumHeightWidth)
198 179
199 self.__layout.setContentsMargins( 180 self.__layout.setContentsMargins(
200 self.__borderSize, self.__borderSize, 181 self.__borderSize, self.__borderSize, self.__borderSize, self.__borderSize
201 self.__borderSize, self.__borderSize) 182 )
202 183
203 for index, icon in enumerate(self.__icons): 184 for index, icon in enumerate(self.__icons):
204 iconLabel = self.__layout.itemAt(index) 185 iconLabel = self.__layout.itemAt(index)
205 if iconLabel: 186 if iconLabel:
206 widget = iconLabel.widget() 187 widget = iconLabel.widget()
207 widget.setFixedSize(self.__barSize, self.__barSize) 188 widget.setFixedSize(self.__barSize, self.__barSize)
208 widget.setPixmap( 189 widget.setPixmap(icon.pixmap(self.__barSize, self.__barSize))
209 icon.pixmap(self.__barSize, self.__barSize)) 190
210
211 self.__createAndAddMoreLabel() 191 self.__createAndAddMoreLabel()
212 192
213 self.__adjustIconLabels() 193 self.__adjustIconLabels()
214 194
215 def barSize(self): 195 def barSize(self):
216 """ 196 """
217 Public method to get the icon bar size. 197 Public method to get the icon bar size.
218 198
219 @return barSize size category for the bar (one of 'xs', 'sm', 'md', 199 @return barSize size category for the bar (one of 'xs', 'sm', 'md',
220 'lg', 'xl', 'xxl') 200 'lg', 'xl', 'xxl')
221 @rtype str 201 @rtype str
222 """ 202 """
223 return self.__barSizeKey 203 return self.__barSizeKey
224 204
225 def setColor(self, color): 205 def setColor(self, color):
226 """ 206 """
227 Public method to set the color of the widget. 207 Public method to set the color of the widget.
228 208
229 @param color color of the widget 209 @param color color of the widget
230 @type QColor 210 @type QColor
231 """ 211 """
232 self.__color = color 212 self.__color = color
233 self.__highlightColor = color.darker() 213 self.__highlightColor = color.darker()
234 214
235 self.setStyleSheet( 215 self.setStyleSheet(EricIconBar.WidgetStyleSheetTemplate.format(color.name()))
236 EricIconBar.WidgetStyleSheetTemplate.format(color.name())) 216
237
238 label = self.__layout.itemAt(self.__currentIndex) 217 label = self.__layout.itemAt(self.__currentIndex)
239 if label: 218 if label:
240 label.widget().setStyleSheet( 219 label.widget().setStyleSheet(
241 EricIconBar.LabelStyleSheetTemplate 220 EricIconBar.LabelStyleSheetTemplate.format(self.__highlightColor.name())
242 .format(self.__highlightColor.name())
243 ) 221 )
244 222
245 def color(self): 223 def color(self):
246 """ 224 """
247 Public method to return the current color. 225 Public method to return the current color.
248 226
249 @return current color 227 @return current color
250 @rtype QColor 228 @rtype QColor
251 """ 229 """
252 return self.__color 230 return self.__color
253 231
254 def __createIcon(self, icon, label=""): 232 def __createIcon(self, icon, label=""):
255 """ 233 """
256 Private method to creat an icon label. 234 Private method to creat an icon label.
257 235
258 @param icon reference to the icon 236 @param icon reference to the icon
259 @type QIcon 237 @type QIcon
260 @param label label text to be shown as a tooltip (defaults to "") 238 @param label label text to be shown as a tooltip (defaults to "")
261 @type str (optional) 239 @type str (optional)
262 @return created and connected label 240 @return created and connected label
266 iconLabel.setFixedSize(self.__barSize, self.__barSize) 244 iconLabel.setFixedSize(self.__barSize, self.__barSize)
267 iconLabel.setAlignment(Qt.AlignmentFlag.AlignCenter) 245 iconLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
268 iconLabel.setPixmap(icon.pixmap(self.__barSize, self.__barSize)) 246 iconLabel.setPixmap(icon.pixmap(self.__barSize, self.__barSize))
269 if label: 247 if label:
270 iconLabel.setToolTip(label) 248 iconLabel.setToolTip(label)
271 249
272 iconLabel.clicked.connect(lambda: self.__iconClicked(iconLabel)) 250 iconLabel.clicked.connect(lambda: self.__iconClicked(iconLabel))
273 251
274 return iconLabel 252 return iconLabel
275 253
276 def __createAndAddMoreLabel(self): 254 def __createAndAddMoreLabel(self):
277 """ 255 """
278 Private method to create the label to be shown for too many icons. 256 Private method to create the label to be shown for too many icons.
279 """ 257 """
280 self.__moreLabel = EricClickableLabel(self) 258 self.__moreLabel = EricClickableLabel(self)
281 self.__moreLabel.setAlignment(Qt.AlignmentFlag.AlignCenter) 259 self.__moreLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
282 if self.__orientation == Qt.Orientation.Horizontal: 260 if self.__orientation == Qt.Orientation.Horizontal:
283 self.__moreLabel.setFixedSize( 261 self.__moreLabel.setFixedSize(
284 int(self.__barSize * self.MoreLabelAspect), self.__barSize) 262 int(self.__barSize * self.MoreLabelAspect), self.__barSize
263 )
285 self.__moreLabel.setPixmap( 264 self.__moreLabel.setPixmap(
286 UI.PixmapCache.getIcon("sbDotsH96").pixmap( 265 UI.PixmapCache.getIcon("sbDotsH96").pixmap(
287 int(self.__barSize * self.MoreLabelAspect), self.__barSize 266 int(self.__barSize * self.MoreLabelAspect), self.__barSize
288 ) 267 )
289 ) 268 )
290 else: 269 else:
291 self.__moreLabel.setFixedSize( 270 self.__moreLabel.setFixedSize(
292 self.__barSize, int(self.__barSize * self.MoreLabelAspect)) 271 self.__barSize, int(self.__barSize * self.MoreLabelAspect)
272 )
293 self.__moreLabel.setPixmap( 273 self.__moreLabel.setPixmap(
294 UI.PixmapCache.getIcon("sbDotsV96").pixmap( 274 UI.PixmapCache.getIcon("sbDotsV96").pixmap(
295 self.__barSize, int(self.__barSize * self.MoreLabelAspect) 275 self.__barSize, int(self.__barSize * self.MoreLabelAspect)
296 ) 276 )
297 ) 277 )
298 278
299 self.__layout.addWidget(self.__moreLabel) 279 self.__layout.addWidget(self.__moreLabel)
300 280
301 self.__moreLabel.clicked.connect(self.__moreLabelClicked) 281 self.__moreLabel.clicked.connect(self.__moreLabelClicked)
302 282
303 def addIcon(self, icon, label=""): 283 def addIcon(self, icon, label=""):
304 """ 284 """
305 Public method to add an icon to the bar. 285 Public method to add an icon to the bar.
306 286
307 @param icon reference to the icon 287 @param icon reference to the icon
308 @type QIcon 288 @type QIcon
309 @param label label text to be shown as a tooltip (defaults to "") 289 @param label label text to be shown as a tooltip (defaults to "")
310 @type str (optional) 290 @type str (optional)
311 """ 291 """
312 # the stretch item is always the last one 292 # the stretch item is always the last one
313 self.insertIcon(self.count(), icon, label=label) 293 self.insertIcon(self.count(), icon, label=label)
314 294
315 def insertIcon(self, index, icon, label=""): 295 def insertIcon(self, index, icon, label=""):
316 """ 296 """
317 Public method to insert an icon into the bar. 297 Public method to insert an icon into the bar.
318 298
319 @param index position to insert the icon at 299 @param index position to insert the icon at
320 @type int 300 @type int
321 @param icon reference to the icon 301 @param icon reference to the icon
322 @type QIcon 302 @type QIcon
323 @param label label text to be shown as a tooltip (defaults to "") 303 @param label label text to be shown as a tooltip (defaults to "")
324 @type str (optional) 304 @type str (optional)
325 """ 305 """
326 iconLabel = self.__createIcon(icon, label=label) 306 iconLabel = self.__createIcon(icon, label=label)
327 self.__layout.insertWidget(index, iconLabel) 307 self.__layout.insertWidget(index, iconLabel)
328 self.__icons.insert(index, QIcon(icon)) 308 self.__icons.insert(index, QIcon(icon))
329 309
330 if self.__currentIndex < 0: 310 if self.__currentIndex < 0:
331 self.setCurrentIndex(index) 311 self.setCurrentIndex(index)
332 elif index <= self.__currentIndex: 312 elif index <= self.__currentIndex:
333 self.setCurrentIndex(self.__currentIndex + 1) 313 self.setCurrentIndex(self.__currentIndex + 1)
334 314
335 self.__adjustIconLabels() 315 self.__adjustIconLabels()
336 316
337 def removeIcon(self, index): 317 def removeIcon(self, index):
338 """ 318 """
339 Public method to remove an icon from the bar. 319 Public method to remove an icon from the bar.
340 320
341 @param index index of the icon to be removed 321 @param index index of the icon to be removed
342 @type int 322 @type int
343 """ 323 """
344 label = self.__layout.itemAt(index) 324 label = self.__layout.itemAt(index)
345 if label: 325 if label:
346 with contextlib.suppress(IndexError): 326 with contextlib.suppress(IndexError):
347 del self.__icons[index] 327 del self.__icons[index]
348 itm = self.__layout.takeAt(index) 328 itm = self.__layout.takeAt(index)
349 itm.widget().deleteLater() 329 itm.widget().deleteLater()
350 del itm 330 del itm
351 331
352 if index == self.__currentIndex: 332 if index == self.__currentIndex:
353 self.setCurrentIndex(index) 333 self.setCurrentIndex(index)
354 elif index < self.__currentIndex: 334 elif index < self.__currentIndex:
355 self.setCurrentIndex(self.__currentIndex - 1) 335 self.setCurrentIndex(self.__currentIndex - 1)
356 336
357 self.__adjustIconLabels() 337 self.__adjustIconLabels()
358 338
359 @pyqtSlot() 339 @pyqtSlot()
360 def __iconClicked(self, label): 340 def __iconClicked(self, label):
361 """ 341 """
362 Private slot to handle an icon been clicked. 342 Private slot to handle an icon been clicked.
363 343
364 @param label reference to the clicked label 344 @param label reference to the clicked label
365 @type EricClickableLabel 345 @type EricClickableLabel
366 """ 346 """
367 index = self.__layout.indexOf(label) 347 index = self.__layout.indexOf(label)
368 if index >= 0: 348 if index >= 0:
369 if index == self.__currentIndex: 349 if index == self.__currentIndex:
370 self.currentClicked.emit(self.__currentIndex) 350 self.currentClicked.emit(self.__currentIndex)
371 else: 351 else:
372 self.setCurrentIndex(index) 352 self.setCurrentIndex(index)
373 353
374 def setCurrentIndex(self, index): 354 def setCurrentIndex(self, index):
375 """ 355 """
376 Public method to set the current index. 356 Public method to set the current index.
377 357
378 @param index current index to be set 358 @param index current index to be set
379 @type int 359 @type int
380 """ 360 """
381 if index >= self.count(): 361 if index >= self.count():
382 index = -1 362 index = -1
383 363
384 if index != self.__currentIndex and index >= 0: 364 if index != self.__currentIndex and index >= 0:
385 # reset style of previous current icon 365 # reset style of previous current icon
386 oldLabel = self.__layout.itemAt(self.__currentIndex) 366 oldLabel = self.__layout.itemAt(self.__currentIndex)
387 if oldLabel: 367 if oldLabel:
388 widget = oldLabel.widget() 368 widget = oldLabel.widget()
389 if widget is not None: 369 if widget is not None:
390 widget.setStyleSheet("") 370 widget.setStyleSheet("")
391 371
392 # set style of new current icon 372 # set style of new current icon
393 newLabel = self.__layout.itemAt(index) 373 newLabel = self.__layout.itemAt(index)
394 if newLabel: 374 if newLabel:
395 newLabel.widget().setStyleSheet( 375 newLabel.widget().setStyleSheet(
396 EricIconBar.LabelStyleSheetTemplate 376 EricIconBar.LabelStyleSheetTemplate.format(
397 .format(self.__highlightColor.name()) 377 self.__highlightColor.name()
378 )
398 ) 379 )
399 380
400 self.__currentIndex = index 381 self.__currentIndex = index
401 self.currentChanged.emit(self.__currentIndex) 382 self.currentChanged.emit(self.__currentIndex)
402 383
403 def currentIndex(self): 384 def currentIndex(self):
404 """ 385 """
405 Public method to get the current index. 386 Public method to get the current index.
406 387
407 @return current index 388 @return current index
408 @rtype int 389 @rtype int
409 """ 390 """
410 return self.__currentIndex 391 return self.__currentIndex
411 392
412 def count(self): 393 def count(self):
413 """ 394 """
414 Public method to get the number of icon labels. 395 Public method to get the number of icon labels.
415 396
416 @return number of icon labels 397 @return number of icon labels
417 @rtype int 398 @rtype int
418 """ 399 """
419 return len(self.__icons) 400 return len(self.__icons)
420 401
421 def wheelEvent(self, evt): 402 def wheelEvent(self, evt):
422 """ 403 """
423 Protected method to handle a wheel event. 404 Protected method to handle a wheel event.
424 405
425 @param evt reference to the wheel event 406 @param evt reference to the wheel event
426 @type QWheelEvent 407 @type QWheelEvent
427 """ 408 """
428 delta = evt.angleDelta().y() 409 delta = evt.angleDelta().y()
429 if delta > 0: 410 if delta > 0:
430 self.previousIcon() 411 self.previousIcon()
431 else: 412 else:
432 self.nextIcon() 413 self.nextIcon()
433 414
434 @pyqtSlot() 415 @pyqtSlot()
435 def previousIcon(self): 416 def previousIcon(self):
436 """ 417 """
437 Public slot to set the icon before the current one. 418 Public slot to set the icon before the current one.
438 """ 419 """
439 index = self.__currentIndex - 1 420 index = self.__currentIndex - 1
440 if index < 0: 421 if index < 0:
441 # wrap around 422 # wrap around
442 index = self.count() - 1 423 index = self.count() - 1
443 424
444 self.setCurrentIndex(index) 425 self.setCurrentIndex(index)
445 426
446 @pyqtSlot() 427 @pyqtSlot()
447 def nextIcon(self): 428 def nextIcon(self):
448 """ 429 """
449 Public slot to set the icon after the current one. 430 Public slot to set the icon after the current one.
450 """ 431 """
451 index = self.__currentIndex + 1 432 index = self.__currentIndex + 1
452 if index == self.count(): 433 if index == self.count():
453 # wrap around 434 # wrap around
454 index = 0 435 index = 0
455 436
456 self.setCurrentIndex(index) 437 self.setCurrentIndex(index)
457 438
458 @pyqtSlot() 439 @pyqtSlot()
459 def __moreLabelClicked(self): 440 def __moreLabelClicked(self):
460 """ 441 """
461 Private slot to handle a click onto the 'More' label. 442 Private slot to handle a click onto the 'More' label.
462 """ 443 """
463 menu = QMenu(self) 444 menu = QMenu(self)
464 baseColor = ericApp().palette().color( 445 baseColor = ericApp().palette().color(QPalette.ColorRole.Base)
465 QPalette.ColorRole.Base) 446 highlightColor = ericApp().palette().color(QPalette.ColorRole.Highlight)
466 highlightColor = ericApp().palette().color(
467 QPalette.ColorRole.Highlight)
468 menu.setStyleSheet( 447 menu.setStyleSheet(
469 EricIconBar.MenuStyleSheetTemplate.format( 448 EricIconBar.MenuStyleSheetTemplate.format(
470 baseColor.name(), highlightColor.name())) 449 baseColor.name(), highlightColor.name()
471 450 )
451 )
452
472 for index in range(self.count()): 453 for index in range(self.count()):
473 iconLabel = self.__layout.itemAt(index) 454 iconLabel = self.__layout.itemAt(index)
474 if iconLabel: 455 if iconLabel:
475 widget = iconLabel.widget() 456 widget = iconLabel.widget()
476 if not widget.isVisible(): 457 if not widget.isVisible():
477 act = menu.addAction(widget.toolTip()) 458 act = menu.addAction(widget.toolTip())
478 act.setData(index) 459 act.setData(index)
479 460
480 selectedAction = menu.exec(QCursor.pos()) 461 selectedAction = menu.exec(QCursor.pos())
481 if selectedAction is not None: 462 if selectedAction is not None:
482 index = selectedAction.data() 463 index = selectedAction.data()
483 if index >= 0: 464 if index >= 0:
484 if index == self.__currentIndex: 465 if index == self.__currentIndex:
485 self.currentClicked.emit(self.__currentIndex) 466 self.currentClicked.emit(self.__currentIndex)
486 else: 467 else:
487 self.setCurrentIndex(index) 468 self.setCurrentIndex(index)
488 469
489 def resizeEvent(self, evt): 470 def resizeEvent(self, evt):
490 """ 471 """
491 Protected method to handle resizing of the icon bar. 472 Protected method to handle resizing of the icon bar.
492 473
493 @param evt reference to the event object 474 @param evt reference to the event object
494 @type QResizeEvent 475 @type QResizeEvent
495 """ 476 """
496 self.__adjustIconLabels() 477 self.__adjustIconLabels()
497 478
498 def __adjustIconLabels(self): 479 def __adjustIconLabels(self):
499 """ 480 """
500 Private method to adjust the visibility of the icon labels. 481 Private method to adjust the visibility of the icon labels.
501 """ 482 """
502 size = ( 483 size = (
503 self.width() 484 self.width()
504 if self.orientation() == Qt.Orientation.Horizontal else 485 if self.orientation() == Qt.Orientation.Horizontal
505 self.height() 486 else self.height()
506 ) - 2 * self.__borderSize 487 ) - 2 * self.__borderSize
507 488
508 iconsSize = self.count() * self.__barSize 489 iconsSize = self.count() * self.__barSize
509 490
510 if size < iconsSize: 491 if size < iconsSize:
511 self.__moreLabel.show() 492 self.__moreLabel.show()
512 iconsSize += int(self.__barSize * self.MoreLabelAspect) 493 iconsSize += int(self.__barSize * self.MoreLabelAspect)
513 for index in range(self.count() - 1, -1, -1): 494 for index in range(self.count() - 1, -1, -1):
514 iconLabel = self.__layout.itemAt(index) 495 iconLabel = self.__layout.itemAt(index)

eric ide

mercurial