| 21 | |
| 22 | |
| 23 | class PercentProgressBar(QWidget): |
| 24 | MinValue = 0 |
| 25 | MaxValue = 100 |
| 26 | Value = 0 |
| 27 | BorderWidth = 8 |
| 28 | Clockwise = True # 顺时针还是逆时针 |
| 29 | ShowPercent = True # 是否显示百分比 |
| 30 | ShowFreeArea = False # 显示背后剩余 |
| 31 | ShowSmallCircle = False # 显示带头的小圆圈 |
| 32 | TextColor = QColor(255, 255, 255) # 文字颜色 |
| 33 | BorderColor = QColor(24, 189, 155) # 边框圆圈颜色 |
| 34 | BackgroundColor = QColor(70, 70, 70) # 背景颜色 |
| 35 | |
| 36 | def __init__(self, *args, value=0, minValue=0, maxValue=100, |
| 37 | borderWidth=8, clockwise=True, showPercent=True, |
| 38 | showFreeArea=False, showSmallCircle=False, |
| 39 | textColor=QColor(255, 255, 255), |
| 40 | borderColor=QColor(24, 189, 155), |
| 41 | backgroundColor=QColor(70, 70, 70), **kwargs): |
| 42 | super(PercentProgressBar, self).__init__(*args, **kwargs) |
| 43 | self.Value = value |
| 44 | self.MinValue = minValue |
| 45 | self.MaxValue = maxValue |
| 46 | self.BorderWidth = borderWidth |
| 47 | self.Clockwise = clockwise |
| 48 | self.ShowPercent = showPercent |
| 49 | self.ShowFreeArea = showFreeArea |
| 50 | self.ShowSmallCircle = showSmallCircle |
| 51 | self.TextColor = textColor |
| 52 | self.BorderColor = borderColor |
| 53 | self.BackgroundColor = backgroundColor |
| 54 | |
| 55 | def setRange(self, minValue: int, maxValue: int): |
| 56 | if minValue >= maxValue: # 最小值>=最大值 |
| 57 | return |
| 58 | self.MinValue = minValue |
| 59 | self.MaxValue = maxValue |
| 60 | self.update() |
| 61 | |
| 62 | def paintEvent(self, event): |
| 63 | super(PercentProgressBar, self).paintEvent(event) |
| 64 | width = self.width() |
| 65 | height = self.height() |
| 66 | side = min(width, height) |
| 67 | |
| 68 | painter = QPainter(self) |
| 69 | # 反锯齿 |
| 70 | painter.setRenderHints(QPainter.Antialiasing | |
| 71 | QPainter.TextAntialiasing) |
| 72 | # 坐标中心为中间点 |
| 73 | painter.translate(width / 2, height / 2) |
| 74 | # 按照100x100缩放 |
| 75 | painter.scale(side / 100.0, side / 100.0) |
| 76 | |
| 77 | # 绘制中心园 |
| 78 | self._drawCircle(painter, 50) |
| 79 | # 绘制圆弧 |
| 80 | self._drawArc(painter, 50 - self.BorderWidth / 2) |