-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
RubberBandButton.py
203 lines (178 loc) · 5.79 KB
/
RubberBandButton.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019年1月3日
@author: Irony
@site: https://pyqt.site https://github.com/PyQt5
@email: [email protected]
@file: Widgets.RubberBandButton
@description:
"""
try:
from PyQt5.QtCore import (
QEasingCurve,
QParallelAnimationGroup,
QPropertyAnimation,
QRectF,
Qt,
pyqtProperty,
)
from PyQt5.QtGui import QColor, QPainter
from PyQt5.QtWidgets import (
QPushButton,
QStyle,
QStyleOptionButton,
QStylePainter,
QWidget,
QApplication,
QHBoxLayout,
)
except ImportError:
from PySide2.QtCore import (
QEasingCurve,
QParallelAnimationGroup,
QPropertyAnimation,
QRectF,
Qt,
Property as pyqtProperty,
)
from PySide2.QtGui import QColor, QPainter
from PySide2.QtWidgets import (
QPushButton,
QStyle,
QStyleOptionButton,
QApplication,
QStylePainter,
QWidget,
QHBoxLayout,
)
class RubberBandButton(QPushButton):
def __init__(self, *args, **kwargs):
self._bgcolor = QColor(kwargs.pop("bgcolor", Qt.green))
super(RubberBandButton, self).__init__(*args, **kwargs)
self.setFlat(True)
self.setCursor(Qt.PointingHandCursor)
self._width = 0
self._height = 0
def paintEvent(self, event):
self._initAnimate()
painter = QStylePainter(self)
painter.setRenderHint(QPainter.Antialiasing, True)
painter.setRenderHint(QPainter.HighQualityAntialiasing, True)
painter.setRenderHint(QPainter.SmoothPixmapTransform, True)
painter.setBrush(QColor(self._bgcolor))
painter.setPen(QColor(self._bgcolor))
painter.drawEllipse(
QRectF(
(self.minimumWidth() - self._width) / 2,
(self.minimumHeight() - self._height) / 2,
self._width,
self._height,
)
)
# 绘制本身的文字和图标
options = QStyleOptionButton()
options.initFrom(self)
size = options.rect.size()
size.transpose()
options.rect.setSize(size)
options.features = QStyleOptionButton.Flat
options.text = self.text()
options.icon = self.icon()
options.iconSize = self.iconSize()
painter.drawControl(QStyle.CE_PushButton, options)
event.accept()
def _initAnimate(self):
if hasattr(self, "_animate"):
return
self._width = self.minimumWidth() * 7 / 8
self._height = self.minimumHeight() * 7 / 8
# self._width=175
# self._height=175
# 宽度动画
wanimate = QPropertyAnimation(self, b"rWidth")
wanimate.setEasingCurve(QEasingCurve.OutElastic)
wanimate.setDuration(700)
wanimate.valueChanged.connect(self.update)
# 插入宽度线性值
wanimate.setKeyValueAt(0, self._width)
# wanimate.setKeyValueAt(0.1, 180)
# wanimate.setKeyValueAt(0.2, 185)
# wanimate.setKeyValueAt(0.3, 190)
# wanimate.setKeyValueAt(0.4, 195)
wanimate.setKeyValueAt(0.5, self._width + 6)
# wanimate.setKeyValueAt(0.6, 195)
# wanimate.setKeyValueAt(0.7, 190)
# wanimate.setKeyValueAt(0.8, 185)
# wanimate.setKeyValueAt(0.9, 180)
wanimate.setKeyValueAt(1, self._width)
# 高度动画
hanimate = QPropertyAnimation(self, b"rHeight")
hanimate.setEasingCurve(QEasingCurve.OutElastic)
hanimate.setDuration(700)
# 插入高度线性值
hanimate.setKeyValueAt(0, self._height)
# hanimate.setKeyValueAt(0.1, 170)
# hanimate.setKeyValueAt(0.3, 165)
hanimate.setKeyValueAt(0.5, self._height - 6)
# hanimate.setKeyValueAt(0.7, 165)
# hanimate.setKeyValueAt(0.9, 170)
hanimate.setKeyValueAt(1, self._height)
# 设置动画组
self._animate = QParallelAnimationGroup(self)
self._animate.addAnimation(wanimate)
self._animate.addAnimation(hanimate)
def enterEvent(self, event):
super(RubberBandButton, self).enterEvent(event)
self._animate.stop()
self._animate.start()
@pyqtProperty(int)
def rWidth(self):
return self._width
@rWidth.setter
def rWidth(self, value):
self._width = value
@pyqtProperty(int)
def rHeight(self):
return self._height
@rHeight.setter
def rHeight(self, value):
self._height = value
@pyqtProperty(QColor)
def bgColor(self):
return self._bgcolor
@bgColor.setter
def bgColor(self, color):
self._bgcolor = QColor(color)
class TestWindow(QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
layout = QHBoxLayout(self)
layout.addWidget(RubberBandButton("d", self, bgcolor="#01847E"))
layout.addWidget(RubberBandButton("v", self, bgcolor="#7FA91F"))
layout.addWidget(RubberBandButton("N", self, bgcolor="#FC8416"))
layout.addWidget(RubberBandButton("U", self, bgcolor="#66D3c0"))
layout.addWidget(RubberBandButton("a", self, bgcolor="#F28195"))
if __name__ == "__main__":
import cgitb, sys
cgitb.enable(1, None, 5, "text")
app = QApplication(sys.argv)
app.setStyleSheet(
"""
RubberBandButton {
min-width: 100px;
max-width: 100px;
min-height: 100px;
max-height: 100px;
border: none;
color: white;
outline: none;
margin: 4px;
font-family: webdings;
font-size: 60px;
}
"""
)
w = TestWindow()
w.show()
sys.exit(app.exec_())