eric7/EricWidgets/EricIconBar.py

Mon, 06 Sep 2021 19:52:37 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 06 Sep 2021 19:52:37 +0200
branch
eric7
changeset 8583
aac629a05f8b
child 8584
90391fda03d5
permissions
-rw-r--r--

Modernized the look of the side bars.

8583
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a bar widget showing just icons.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 from PyQt6.QtGui import QColor
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 from PyQt6.QtWidgets import QWidget, QBoxLayout, QWIDGETSIZE_MAX
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 from .EricClickableLabel import EricClickableLabel
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 class EricIconBar(QWidget):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 Class implementing a bar widget showing just icons.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 @signal currentChanged(index) emitted to indicate a change of the current
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 index
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 @signal currentClicked(index) emitted to indicate, that the current icon
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 was clicked
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 IconSize = 48
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 BorderSize = 2
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 WidgetStyleSheetTemplate = "QWidget {{ background-color: {0}; }}"
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 LabelStyleSheetTemplate = "QLabel {{ background-color: {0}; }}"
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 currentChanged = pyqtSignal(int)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 currentClicked = pyqtSignal(int)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 def __init__(self, orientation=Qt.Orientation.Horizontal, parent=None):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 Constructor
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 @param orientation orientation for the widget
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 @type Qt.Orientation
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 @param parent reference to the parent widget (defaults to None)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 @type QWidget (optional)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 super().__init__(parent)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 self.__fixedHeightWidth = (
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 EricIconBar.IconSize + 2 * EricIconBar.BorderSize
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 )
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 # set initial values
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 self.__color = QColor("#008800")
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 self.__orientation = Qt.Orientation.Horizontal
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 self.__currentIndex = -1
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 # initialize with horizontal layout and change later if needed
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 self.setFixedHeight(self.__fixedHeightWidth)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 self.__layout = QBoxLayout(QBoxLayout.Direction.LeftToRight)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 self.__layout.setContentsMargins(
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 EricIconBar.BorderSize, EricIconBar.BorderSize,
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 EricIconBar.BorderSize, EricIconBar.BorderSize)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 self.__layout.setSpacing(EricIconBar.BorderSize)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 self.__layout.addStretch()
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 self.setLayout(self.__layout)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 if orientation != self.__orientation:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 self.setOrientation(orientation)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 self.setColor(self.__color)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 def setOrientation(self, orientation):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 Public method to set the widget orientation.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 @param orientation orientation to be set
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 @type Qt.Orientation
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 # reset list widget size constraints
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 self.setFixedSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 if orientation == Qt.Orientation.Horizontal:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 self.setFixedHeight(self.__fixedHeightWidth)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 self.__layout.setDirection(QBoxLayout.Direction.LeftToRight)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 elif orientation == Qt.Orientation.Vertical:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 self.setFixedWidth(self.__fixedHeightWidth)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 self.__layout.setDirection(QBoxLayout.Direction.TopToBottom)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 self.__orientation = orientation
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 def orientation(self):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 Public method to get the orientation of the widget.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 @return orientation of the widget
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 @rtype Qt.Orientation
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 return self.__orientation
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 def setColor(self, color):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 Public method to set the color of the widget.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 @param color color of the widget
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 @type QColor
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 self.__color = color
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 self.__highlightColor = color.darker()
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 self.setStyleSheet(
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 EricIconBar.WidgetStyleSheetTemplate.format(color.name()))
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 label = self.__layout.itemAt(self.__currentIndex)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 if label:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 label.widget().setStyleSheet(
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 EricIconBar.LabelStyleSheetTemplate
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 .format(self.__highlightColor.name())
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 )
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 def color(self):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 Public method to return the current color.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 @return current color
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 @rtype QColor
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 return self.__color
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 def __createIcon(self, iconPixmap, label=""):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 Private method to creat an icon label.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 @param iconPixmap reference to the icon
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 @type QPixmap
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 @param label label text to be shown as a tooltip (defaults to "")
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 @type str (optional)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 @return created and connected label
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 @rtype EricClickableLabel
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 iconLabel = EricClickableLabel(self)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 iconLabel.setFixedSize(EricIconBar.IconSize, EricIconBar.IconSize)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 iconLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 iconLabel.setPixmap(iconPixmap)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 if label:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 iconLabel.setToolTip(label)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 iconLabel.clicked.connect(lambda: self.__iconClicked(iconLabel))
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 return iconLabel
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 def addIcon(self, iconPixmap, label=""):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 Public method to add an icon to the bar.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 @param iconPixmap reference to the icon
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 @type QPixmap
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 @param label label text to be shown as a tooltip (defaults to "")
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 @type str (optional)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 # the stretch item is always the last one
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 self.insertIcon(self.count(), iconPixmap, label=label)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 def insertIcon(self, index, iconPixmap, label=""):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 Public method to insert an icon into the bar.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 @param index position to insert the icon at
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 @type int
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 @param iconPixmap reference to the icon
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 @type QPixmap
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 @param label label text to be shown as a tooltip (defaults to "")
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 @type str (optional)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 iconLabel = self.__createIcon(iconPixmap, label=label)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 self.__layout.insertWidget(index, iconLabel)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 if self.__currentIndex < 0:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 self.setCurrentIndex(index)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 elif index <= self.__currentIndex:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 self.setCurrentIndex(self.__currentIndex + 1)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 def removeIcon(self, index):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 Public method to remove an icon from the bar.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 @param index index of the icon to be removed
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 @type int
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 label = self.__layout.itemAt(index)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 self.__layout.removeWidget(label)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 if index == self.__currentIndex:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 self.setCurrentIndex(index)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 elif index < self.__currentIndex:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 self.setCurrentIndex(self.__currentIndex - 1)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 @pyqtSlot()
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 def __iconClicked(self, label):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 Private slot to handle an icon been clicked.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 @param label reference to the clicked label
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 @type EricClickableLabel
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 index = self.__layout.indexOf(label)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 if index == self.__currentIndex:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 self.currentClicked.emit(self.__currentIndex)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 else:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 self.setCurrentIndex(index)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 def setCurrentIndex(self, index):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 Public method to set the current index.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 @param index current index to be set
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 @type int
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 if index >= self.count():
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 index = -1
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 if index != self.__currentIndex:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 # reset style of previous current icon
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 oldLabel = self.__layout.itemAt(self.__currentIndex)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 if oldLabel:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 oldLabel.widget().setStyleSheet("")
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 # set style of new current icon
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 newLabel = self.__layout.itemAt(index)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 if newLabel:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 newLabel.widget().setStyleSheet(
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 EricIconBar.LabelStyleSheetTemplate
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 .format(self.__highlightColor.name())
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 )
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 self.__currentIndex = index
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 self.currentChanged.emit(self.__currentIndex)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 def currentIndex(self):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 Public method to get the current index.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 @return current index
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 @rtype int
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 return self.__currentIndex
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 def count(self):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 Public method to get the number of icon labels.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 @return number of icon labels
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 @rtype int
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 return self.__layout.count() - 1
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 def wheelEvent(self, evt):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 Protected method to handle a wheel event.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 @param evt reference to the wheel event
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 @type QWheelEvent
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 delta = evt.angleDelta().y()
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 if delta > 0:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 self.previousIcon()
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 else:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 self.nextIcon()
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 @pyqtSlot()
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 def previousIcon(self):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 Public slot to set the icon before the current one.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 index = self.__currentIndex - 1
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 if index < 0:
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 # wrap around
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 index = self.count() - 1
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 self.setCurrentIndex(index)
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 @pyqtSlot()
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 def nextIcon(self):
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 Public slot to set the icon after the current one.
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 """
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 index = self.__currentIndex + 1
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 if index == self.count():
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 # wrap around
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 index = 0
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292
aac629a05f8b Modernized the look of the side bars.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 self.setCurrentIndex(index)

eric ide

mercurial