This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
raisedbutton.py
65 lines (48 loc) · 2.52 KB
/
raisedbutton.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
from .flatbutton import *
class QMaterialRaisedButton(QMaterialFlatButton):
def __init__(self, text: str = None, parent: QWidget = None, **kwargs):
QMaterialFlatButton.__init__(self, text=text, parent=parent, **kwargs)
self.setText(text)
self.m_shadowStateMachine = QStateMachine(self)
self.m_normalState = QState()
self.m_pressedState = QState()
self.m_effect = QGraphicsDropShadowEffect()
self.m_effect.setBlurRadius(7)
self.m_effect.setOffset(QPointF(0, 2))
self.m_effect.setColor(QColor(0, 0, 0, 75))
self.setBackgroundMode(Qt.OpaqueMode)
self.setMinimumHeight(42)
self.setGraphicsEffect(self.m_effect)
self.setBaseOpacity(0.3)
self.m_shadowStateMachine.addState(self.m_normalState)
self.m_shadowStateMachine.addState(self.m_pressedState)
self.m_normalState.assignProperty(self.m_effect, "offset", QPointF(0, 2))
self.m_normalState.assignProperty(self.m_effect, "blurRadius", 7)
self.m_pressedState.assignProperty(self.m_effect, "offset", QPointF(0, 5))
self.m_pressedState.assignProperty(self.m_effect, "blurRadius", 29)
transition = QEventTransition(self, QEvent.MouseButtonPress)
transition.setTargetState(self.m_pressedState)
self.m_normalState.addTransition(transition)
transition = QEventTransition(self, QEvent.MouseButtonDblClick)
transition.setTargetState(self.m_pressedState)
self.m_normalState.addTransition(transition)
transition = QEventTransition(self, QEvent.MouseButtonRelease)
transition.setTargetState(self.m_normalState)
self.m_pressedState.addTransition(transition)
animation = QPropertyAnimation(self.m_effect, b"offset", self)
animation.setDuration(100)
self.m_shadowStateMachine.addDefaultAnimation(animation)
animation = QPropertyAnimation(self.m_effect, b"blurRadius", self)
animation.setDuration(100)
self.m_shadowStateMachine.addDefaultAnimation(animation)
self.m_shadowStateMachine.setInitialState(self.m_normalState)
self.m_shadowStateMachine.start()
def event(self, event: QEvent) -> bool:
if QEvent.EnabledChange == event.type():
if self.isEnabled():
self.m_shadowStateMachine.start()
self.m_effect.setEnabled(True)
else:
self.m_shadowStateMachine.stop()
self.m_effect.setEnabled(False)
return QMaterialFlatButton.event(self, event)