eric6/E5Gui/E5ToolButton.py

changeset 8268
6b8128e0c9d1
parent 8228
772103b14c18
child 8758
c3f57225c305
equal deleted inserted replaced
8267:6baca884c73a 8268:6b8128e0c9d1
4 # 4 #
5 5
6 """ 6 """
7 Module implementing a specialized tool button subclass. 7 Module implementing a specialized tool button subclass.
8 """ 8 """
9
10 import enum
9 11
10 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QTimer, QSize 12 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QTimer, QSize
11 from PyQt5.QtWidgets import ( 13 from PyQt5.QtWidgets import (
12 QToolButton, QStyle, QStyleOptionToolButton, QStyleOption, QApplication, 14 QToolButton, QStyle, QStyleOptionToolButton, QStyleOption, QApplication,
13 QLabel 15 QLabel
14 ) 16 )
17
18
19 class E5ToolButtonOptions(enum.IntEnum):
20 """
21 Class defining the tool button options.
22 """
23 DEFAULT = 0
24 SHOW_MENU_INSIDE = 1
25 TOOLBAR_LOOKUP = 2
15 26
16 27
17 class E5ToolButton(QToolButton): 28 class E5ToolButton(QToolButton):
18 """ 29 """
19 Class implementing a specialized tool button subclass. 30 Class implementing a specialized tool button subclass.
24 @signal controlClicked() emitted when the left mouse button was 35 @signal controlClicked() emitted when the left mouse button was
25 clicked while pressing the Ctrl key 36 clicked while pressing the Ctrl key
26 @signal doubleClicked() emitted when the left mouse button was 37 @signal doubleClicked() emitted when the left mouse button was
27 double clicked 38 double clicked
28 """ 39 """
29 NoOptions = 0
30 ShowMenuInsideOption = 1
31 ToolBarLookOption = 2
32
33 aboutToShowMenu = pyqtSignal() 40 aboutToShowMenu = pyqtSignal()
34 aboutToHideMenu = pyqtSignal() 41 aboutToHideMenu = pyqtSignal()
35 middleClicked = pyqtSignal() 42 middleClicked = pyqtSignal()
36 controlClicked = pyqtSignal() 43 controlClicked = pyqtSignal()
37 doubleClicked = pyqtSignal() 44 doubleClicked = pyqtSignal()
46 super().__init__(parent) 53 super().__init__(parent)
47 54
48 self.setMinimumWidth(16) 55 self.setMinimumWidth(16)
49 56
50 self.__menu = None 57 self.__menu = None
51 self.__options = E5ToolButton.NoOptions 58 self.__options = E5ToolButtonOptions.DEFAULT
52 59
53 self.__badgeLabel = QLabel(self) 60 self.__badgeLabel = QLabel(self)
54 font = self.__badgeLabel.font() 61 font = self.__badgeLabel.font()
55 font.setPixelSize(self.__badgeLabel.height() / 2.5) 62 font.setPixelSize(self.__badgeLabel.height() / 2.5)
56 self.__badgeLabel.setFont(font) 63 self.__badgeLabel.setFont(font)
102 the button. 109 the button.
103 110
104 @return flag indicating that the menu edge shall be aligned 111 @return flag indicating that the menu edge shall be aligned
105 @rtype bool 112 @rtype bool
106 """ 113 """
107 return bool(self.__options & E5ToolButton.ShowMenuInsideOption) 114 return bool(self.__options & E5ToolButtonOptions.SHOW_MENU_INSIDE)
108 115
109 def setShowMenuInside(self, enable): 116 def setShowMenuInside(self, enable):
110 """ 117 """
111 Public method to set a flag to show the menu edge aligned with 118 Public method to set a flag to show the menu edge aligned with
112 the button. 119 the button.
113 120
114 @param enable flag indicating to align the menu edge to the button 121 @param enable flag indicating to align the menu edge to the button
115 @type bool 122 @type bool
116 """ 123 """
117 if enable: 124 if enable:
118 self.__options |= E5ToolButton.ShowMenuInsideOption 125 self.__options |= E5ToolButtonOptions.SHOW_MENU_INSIDE
119 else: 126 else:
120 self.__options &= ~E5ToolButton.ShowMenuInsideOption 127 self.__options &= ~E5ToolButtonOptions.SHOW_MENU_INSIDE
121 128
122 @pyqtSlot() 129 @pyqtSlot()
123 def __showMenu(self): 130 def __showMenu(self):
124 """ 131 """
125 Private slot to show the tool button menu. 132 Private slot to show the tool button menu.
127 if self.__menu is None or self.__menu.isVisible(): 134 if self.__menu is None or self.__menu.isVisible():
128 return 135 return
129 136
130 self.aboutToShowMenu.emit() 137 self.aboutToShowMenu.emit()
131 138
132 if self.__options & E5ToolButton.ShowMenuInsideOption: 139 if self.__options & E5ToolButtonOptions.SHOW_MENU_INSIDE:
133 pos = self.mapToGlobal(self.rect().bottomRight()) 140 pos = self.mapToGlobal(self.rect().bottomRight())
134 if ( 141 if (
135 QApplication.layoutDirection() == 142 QApplication.layoutDirection() ==
136 Qt.LayoutDirection.RightToLeft 143 Qt.LayoutDirection.RightToLeft
137 ): 144 ):
160 Public method to check, if the button has the toolbar look. 167 Public method to check, if the button has the toolbar look.
161 168
162 @return flag indicating toolbar look 169 @return flag indicating toolbar look
163 @rtype bool 170 @rtype bool
164 """ 171 """
165 return bool(self.__options & E5ToolButton.ToolBarLookOption) 172 return bool(self.__options & E5ToolButtonOptions.TOOLBAR_LOOKUP)
166 173
167 def setToolbarButtonLook(self, enable): 174 def setToolbarButtonLook(self, enable):
168 """ 175 """
169 Public method to set the toolbar look state. 176 Public method to set the toolbar look state.
170 177
171 @param enable flag indicating toolbar look 178 @param enable flag indicating toolbar look
172 @type bool 179 @type bool
173 """ 180 """
174 if enable: 181 if enable:
175 self.__options |= E5ToolButton.ToolBarLookOption 182 self.__options |= E5ToolButtonOptions.TOOLBAR_LOOKUP
176 183
177 opt = QStyleOption() 184 opt = QStyleOption()
178 opt.initFrom(self) 185 opt.initFrom(self)
179 size = self.style().pixelMetric( 186 size = self.style().pixelMetric(
180 QStyle.PixelMetric.PM_ToolBarIconSize, opt, self) 187 QStyle.PixelMetric.PM_ToolBarIconSize, opt, self)
181 self.setIconSize(QSize(size, size)) 188 self.setIconSize(QSize(size, size))
182 else: 189 else:
183 self.__options &= ~E5ToolButton.ToolBarLookOption 190 self.__options &= ~E5ToolButtonOptions.TOOLBAR_LOOKUP
184 191
185 self.setProperty("toolbar-look", enable) 192 self.setProperty("toolbar-look", enable)
186 self.style().unpolish(self) 193 self.style().unpolish(self)
187 self.style().polish(self) 194 self.style().polish(self)
188 195

eric ide

mercurial