forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColourfulProgress.py
217 lines (184 loc) · 7.37 KB
/
ColourfulProgress.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2022/02/25
@author: Irony
@site: https://pyqt.site, https://github.com/PyQt5
@email: [email protected]
@file: ColourfulProgress.py
@description:
"""
try:
from PyQt5.QtCore import QLineF, QRect, QRectF, Qt
from PyQt5.QtGui import QColor, QPainter, QPainterPath, QPen, QTransform
from PyQt5.QtWidgets import (QApplication, QGridLayout, QProgressBar,
QSlider, QStyleOptionProgressBar, QWidget)
except ImportError:
from PySide2.QtCore import QRect, Qt, QRectF, QLineF
from PySide2.QtGui import QColor, QPainter, QPen, QTransform, QPainterPath
from PySide2.QtWidgets import (QApplication, QProgressBar,
QStyleOptionProgressBar, QWidget, QSlider,
QGridLayout)
from Lib.QStyleAnimation import QProgressStyleAnimation
class ColourfulProgress(QProgressBar):
def __init__(self, *args, **kwargs):
self._color = kwargs.pop('color', QColor(43, 194, 83))
self._fps = kwargs.pop('fps', 60)
self._lineWidth = kwargs.pop('lineWidth', 50) # 线条宽度
self._radius = kwargs.pop('radius', None) # None为自动计算圆角
self._animation = None
super(ColourfulProgress, self).__init__(*args, **kwargs)
self.setColor(self._color)
self.setFps(self._fps)
self.setLineWidth(self._lineWidth)
self.setRadius(self._radius)
def setColor(self, color):
"""
:type color: QColor
:param color: 颜色
"""
self._color = QColor(color) if isinstance(
color, (QColor, Qt.GlobalColor)) else QColor(43, 194, 83)
def setFps(self, fps):
"""
:type fps: int
:param fps: 帧率
"""
self._fps = max(int(fps), 1) if isinstance(fps, (int, float)) else 60
def setLineWidth(self, width):
"""
:type width: int
:param width: 线条宽度
"""
self._lineWidth = max(int(width), 0) if isinstance(width,
(int, float)) else 50
def setRadius(self, radius):
"""
:type radius: int
:param radius: 半径
"""
self._radius = max(int(radius), 1) if isinstance(radius,
(int, float)) else None
def paintEvent(self, _):
"""
重写绘制事件,参考 qfusionstyle.cpp 中的 CE_ProgressBarContents 绘制方法
"""
option = QStyleOptionProgressBar()
self.initStyleOption(option)
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.translate(0.5, 0.5)
vertical = option.orientation == Qt.Vertical # 是否垂直
inverted = option.invertedAppearance # 是否反转
# 是否显示动画
indeterminate = (option.minimum == option.maximum) or (
option.minimum < option.progress < option.maximum)
rect = option.rect
if vertical:
rect = QRect(rect.left(), rect.top(), rect.height(),
rect.width()) # 翻转宽度和高度
m = QTransform.fromTranslate(rect.height(), 0)
m.rotate(90.0)
painter.setTransform(m, True)
maxWidth = rect.width()
progress = max(option.progress, option.minimum)
totalSteps = max(1, option.maximum - option.minimum)
progressSteps = progress - option.minimum
progressBarWidth = int(progressSteps * maxWidth / totalSteps)
width = progressBarWidth # 已进行的进度宽度
radius = max(1, (min(width,
self.width() if vertical else self.height()) //
4) if self._radius is None else self._radius)
reverse = (not vertical and
option.direction == Qt.RightToLeft) or vertical
if inverted:
reverse = not reverse
# 绘制范围
path = QPainterPath()
if not reverse:
progressBar = QRectF(rect.left(), rect.top(), width, rect.height())
else:
progressBar = QRectF(rect.right() - width, rect.top(), width,
rect.height())
# 切割范围
path.addRoundedRect(progressBar, radius, radius)
painter.setClipPath(path)
# 绘制背景颜色
painter.setPen(Qt.NoPen)
painter.setBrush(self._color)
painter.drawRoundedRect(progressBar, radius, radius)
if not indeterminate:
if self._animation:
self._animation.stop()
self._animation = None
else:
# 叠加颜色覆盖后出现类似线条间隔的效果
color = self._color.lighter(320)
color.setAlpha(80)
painter.setPen(QPen(color, self._lineWidth))
if self._animation:
if self._animation.state() == QProgressStyleAnimation.Stopped:
# FIXME: 最小化后动画会停止
self._animation.start()
step = int(self._animation.animationStep() % self._lineWidth)
else:
step = 0
self._animation = QProgressStyleAnimation(self._fps, self)
self._animation.start()
# 动画斜线绘制
startX = int(progressBar.left() - rect.height() - self._lineWidth)
endX = int(rect.right() + self._lineWidth)
if (not inverted and not vertical) or (inverted and vertical):
lines = [
QLineF(x + step, progressBar.bottom(),
x + rect.height() + step, progressBar.top())
for x in range(startX, endX, self._lineWidth)
]
else:
lines = [
QLineF(x - step, progressBar.bottom(),
x + rect.height() - step, progressBar.top())
for x in range(startX, endX, self._lineWidth)
]
painter.drawLines(lines)
if __name__ == '__main__':
import cgitb
import sys
cgitb.enable(format='text')
app = QApplication(sys.argv)
w = QWidget()
layout = QGridLayout(w)
w1 = ColourfulProgress(color=QColor('#85c440'))
w1.setMinimumWidth(300)
w1.setMaximumWidth(300)
w1.setRange(0, 100)
layout.addWidget(w1, 0, 0, 1, 1)
w2 = ColourfulProgress(color=QColor('#f2b63c'))
w2.setMinimumWidth(300)
w2.setMaximumWidth(300)
w2.setInvertedAppearance(True)
w2.setRange(0, 100)
layout.addWidget(w2, 1, 0, 1, 1)
w3 = ColourfulProgress(color=QColor('#db3a27'))
w3.setMinimumHeight(300)
w3.setMaximumHeight(300)
w3.setOrientation(Qt.Vertical)
w3.setRange(0, 100)
layout.addWidget(w3, 0, 1, 2, 1)
w4 = ColourfulProgress(color=QColor('#5aaadb'))
w4.setMinimumHeight(300)
w4.setMaximumHeight(300)
w4.setInvertedAppearance(True)
w4.setOrientation(Qt.Vertical)
w4.setRange(0, 100)
layout.addWidget(w4, 0, 2, 2, 1)
slider = QSlider(Qt.Horizontal)
slider.setRange(0, 100)
slider.valueChanged.connect(w1.setValue)
slider.valueChanged.connect(w2.setValue)
slider.valueChanged.connect(w3.setValue)
slider.valueChanged.connect(w4.setValue)
slider.setValue(50)
layout.addWidget(slider, 2, 0, 1, 3)
w.show()
sys.exit(app.exec_())