-
Notifications
You must be signed in to change notification settings - Fork 10
/
toolbutton.py
113 lines (94 loc) · 3.58 KB
/
toolbutton.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from qtpy import QtCore, QtWidgets
from .constants import RibbonButtonStyle
from .menu import RibbonMenu
class RibbonToolButton(QtWidgets.QToolButton):
"""Tool button that is showed in the ribbon."""
_buttonStyle: RibbonButtonStyle
_largeButtonIconSize = 64
_mediumButtonIconSize = 48
_smallButtonIconSize = 32
_maximumIconSize = 64
def __init__(self, parent=None):
"""Create a new ribbon tool button.
:param parent: The parent widget.
"""
super().__init__(parent)
# Styles
self.setButtonStyle(RibbonButtonStyle.Large)
self.setAutoRaise(True)
self.setFocusPolicy(QtCore.Qt.NoFocus)
def setMaximumIconSize(self, size: int):
"""Set the maximum icon size of the button.
:param size: The maximum icon size of the button.
"""
self._maximumIconSize = size
self.setButtonStyle(self._buttonStyle)
def maximumIconSize(self) -> int:
"""Get the maximum icon size of the button.
:return: The maximum icon size of the button.
"""
return self._maximumIconSize
def setButtonStyle(self, style: RibbonButtonStyle):
"""Set the button style of the button.
:param style: The button style of the button.
"""
self._buttonStyle = style
if style == RibbonButtonStyle.Small:
height = self._smallButtonIconSize
height = min(height, self._maximumIconSize)
self.setIconSize(QtCore.QSize(height, height))
self.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
self.setStyleSheet(
"""
RibbonToolButton::menu-indicator {
subcontrol-origin: padding;
subcontrol-position: right;
right: -5px;
}
"""
)
elif style == RibbonButtonStyle.Medium:
height = self._mediumButtonIconSize
height = min(height, self._maximumIconSize)
self.setIconSize(QtCore.QSize(height, height))
self.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
self.setStyleSheet(
"""
RibbonToolButton::menu-indicator {
subcontrol-origin: padding;
subcontrol-position: right;
right: -5px;
}
"""
)
elif style == RibbonButtonStyle.Large:
height = self._largeButtonIconSize
height = min(height, self._maximumIconSize)
self.setIconSize(QtCore.QSize(height, height))
self.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
self.setStyleSheet(
"""
RibbonToolButton[popupMode="0"]::menu-indicator {
subcontrol-origin: padding;
subcontrol-position: bottom;
bottom: -5px;
}
RibbonToolButton[popupMode="2"]::menu-indicator {
subcontrol-origin: padding;
subcontrol-position: bottom;
bottom: -5px;
}
"""
)
def buttonStyle(self) -> RibbonButtonStyle:
"""Get the button style of the button.
:return: The button style of the button.
"""
return self._buttonStyle
def addRibbonMenu(self) -> RibbonMenu:
"""Add a ribbon menu for the button.
:return: The added ribbon menu.
"""
menu = RibbonMenu()
self.setMenu(menu)
return menu