forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MetroCircleProgress.py
196 lines (159 loc) · 5.95 KB
/
MetroCircleProgress.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年9月日
@author: Irony
@site: https://pyqt5.com , https://github.com/892768447
@email: [email protected]
@file: MetroCircleProgress
@description:
"""
from PyQt5.QtCore import QSequentialAnimationGroup, pyqtProperty,\
QPauseAnimation, QPropertyAnimation, QParallelAnimationGroup,\
QObject, QSize, Qt, pyqtSignal, QRectF
from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtWidgets import QWidget, QVBoxLayout
__Author__ = """By: Irony
QQ: 892768447
Email: [email protected]"""
__Copyright__ = 'Copyright (c) 2018 Irony'
__Version__ = 1.0
class CircleItem(QObject):
X = 0 # x坐标
Opacity = 1 # 透明度0~1
valueChanged = pyqtSignal()
@pyqtProperty(float)
def x(self) -> float:
return self.X
@x.setter
def x(self, x: float):
self.X = x
self.valueChanged.emit()
@pyqtProperty(float)
def opacity(self) -> float:
return self.Opacity
@opacity.setter
def opacity(self, opacity: float):
self.Opacity = opacity
def qBound(miv, cv, mxv):
return max(min(cv, mxv), miv)
class MetroCircleProgress(QWidget):
Radius = 5 # 半径
Color = QColor(24, 189, 155) # 圆圈颜色
BackgroundColor = QColor(Qt.transparent) # 背景颜色
def __init__(self, *args, radius=5, color=QColor(24, 189, 155),
backgroundColor=QColor(Qt.transparent), **kwargs):
super(MetroCircleProgress, self).__init__(*args, **kwargs)
self.Radius = radius
self.Color = color
self.BackgroundColor = backgroundColor
self._items = []
self._initAnimations()
@pyqtProperty(int)
def radius(self) -> int:
return self.Radius
@radius.setter
def radius(self, radius: int):
if self.Radius != radius:
self.Radius = radius
self.update()
@pyqtProperty(QColor)
def color(self) -> QColor:
return self.Color
@color.setter
def color(self, color: QColor):
if self.Color != color:
self.Color = color
self.update()
@pyqtProperty(QColor)
def backgroundColor(self) -> QColor:
return self.BackgroundColor
@backgroundColor.setter
def backgroundColor(self, backgroundColor: QColor):
if self.BackgroundColor != backgroundColor:
self.BackgroundColor = backgroundColor
self.update()
def paintEvent(self, event):
super(MetroCircleProgress, self).paintEvent(event)
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.fillRect(self.rect(), self.BackgroundColor)
painter.setPen(Qt.NoPen)
for item, _ in self._items:
painter.save()
color = self.Color.toRgb()
color.setAlphaF(item.opacity)
painter.setBrush(color)
# 5<= radius <=10
radius = qBound(self.Radius, self.Radius / 200 *
self.height(), 2 * self.Radius)
diameter = 2 * radius
painter.drawRoundedRect(
QRectF(
item.x / 100 * self.width() - diameter,
(self.height() - radius) / 2,
diameter, diameter
), radius, radius)
painter.restore()
def _initAnimations(self):
for index in range(5): # 5个小圆
item = CircleItem(self)
item.valueChanged.connect(self.update)
# 串行动画组
seqAnimation = QSequentialAnimationGroup(self)
seqAnimation.setLoopCount(-1)
self._items.append((item, seqAnimation))
# 暂停延迟动画
seqAnimation.addAnimation(QPauseAnimation(150 * index, self))
# 加速,并行动画组1
parAnimation1 = QParallelAnimationGroup(self)
# 透明度
parAnimation1.addAnimation(QPropertyAnimation(
item, b'opacity', self, duration=400, startValue=0, endValue=1.0))
# x坐标
parAnimation1.addAnimation(QPropertyAnimation(
item, b'x', self, duration=400, startValue=0, endValue=25.0))
seqAnimation.addAnimation(parAnimation1)
##
# 匀速
seqAnimation.addAnimation(QPropertyAnimation(
item, b'x', self, duration=2000, startValue=25.0, endValue=75.0))
# 加速,并行动画组2
parAnimation2 = QParallelAnimationGroup(self)
# 透明度
parAnimation2.addAnimation(QPropertyAnimation(
item, b'opacity', self, duration=400, startValue=1.0, endValue=0))
# x坐标
parAnimation2.addAnimation(QPropertyAnimation(
item, b'x', self, duration=400, startValue=75.0, endValue=100.0))
seqAnimation.addAnimation(parAnimation2)
##
# 暂停延迟动画
seqAnimation.addAnimation(
QPauseAnimation((5 - index - 1) * 150, self))
for _, animation in self._items:
animation.start()
def sizeHint(self):
return QSize(100, self.Radius * 2)
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.resize(800, 600)
layout = QVBoxLayout(self, spacing=0)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(MetroCircleProgress(self))
layout.addWidget(MetroCircleProgress(self, radius=10))
layout.addWidget(MetroCircleProgress(self, styleSheet="""
qproperty-color: rgb(255, 0, 0);
"""))
layout.addWidget(MetroCircleProgress(self, styleSheet="""
qproperty-color: rgb(0, 0, 255);
qproperty-backgroundColor: rgba(180, 180, 180, 180);
"""))
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())