forked from ozmartian/QRoundProgressBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqroundprogressbar.py
319 lines (276 loc) · 12.1 KB
/
qroundprogressbar.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#######################################################
#
# Copyright 2017 Pete Alexandrou
#
# Ported to Python from the original works in C++ by:
#
# Sintegrial Technologies (c) 2015
# https://sourceforge.net/projects/qroundprogressbar
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#######################################################
import operator
from enum import Enum
from PyQt5.QtCore import pyqtSlot, QPointF, Qt, QRectF
from PyQt5.QtGui import (QPalette, QConicalGradient, QGradient, QRadialGradient,
QFontMetricsF, QFont, QPainter, QPen, QPainterPath, QImage,
QPaintEvent)
from PyQt5.QtWidgets import QWidget
class QRoundProgressBar(QWidget):
# CONSTANTS
PositionLeft = 180
PositionTop = 90
PositionRight = 0
PositionBottom = -90
# CONSTRUCTOR ---------------------------------------------------
def __init__(self, parent=None):
super(QRoundProgressBar, self).__init__(parent)
self.m_min = 0
self.m_max = 100
self.m_value = 25
self.m_nullPosition = QRoundProgressBar.PositionTop
self.m_barStyle = self.BarStyle.DONUT
self.m_outlinePenWidth = 1
self.m_dataPenWidth = 1
self.m_rebuildBrush = False
self.m_format = '%p%'
self.m_decimals = 1
self.m_updateFlags = self.UpdateFlags.PERCENT
self.m_gradientData = None
self.text_visability = True
# ENUMS ---------------------------------------------------------
class BarStyle(Enum):
DONUT = 0,
PIE = 1,
LINE = 2,
EXPAND = 3
class UpdateFlags(Enum):
VALUE = 0,
PERCENT = 1,
MAX = 2
# GETTERS -------------------------------------------------------
def minimum(self):
return self.m_min
def maximum(self):
return self.m_max
def isTextvisabile(self):
return self.text_visability
# SETTERS -------------------------------------------------------
def setNullPosition(self, position: float):
if position != self.m_nullPosition:
self.m_nullPosition = position
self.m_rebuildBrush = True
self.update()
def setBarStyle(self, style: BarStyle):
if style != self.m_barStyle:
self.m_barStyle = style
self.m_rebuildBrush = True
self.update()
def setOutlinePenWidth(self, width: float):
if width != self.m_outlinePenWidth:
self.m_outlinePenWidth = width
self.update()
def setDataPenWidth(self, width: float):
if width != self.m_dataPenWidth:
self.m_dataPenWidth = width
self.update()
def setDataColors(self, stopPoints: list):
if stopPoints != self.m_gradientData:
self.m_gradientData = stopPoints
self.m_rebuildBrush = True
self.update()
def setFormat(self, val: str):
if val != self.m_format:
self.m_format = val
self.valueFormatChanged()
def resetFormat(self):
self.m_format = None
self.valueFormatChanged()
def setDecimals(self, count: int):
if count >= 0 and count != self.m_decimals:
self.m_decimals = count
self.valueFormatChanged()
def setTextVisability(self, show: bool):
if show != self.text_visability:
self.text_visability = show
self.update()
# SLOTS ---------------------------------------------------------
@pyqtSlot(float, float)
def setRange(self, minval: float, maxval: float):
self.m_min = minval
self.m_max = maxval
if self.m_max < self.m_min:
self.m_min = maxval
self.m_max = minval
if self.m_value < self.m_min:
self.m_value = self.m_min
elif self.m_value > self.m_max:
self.m_value = self.m_max
self.m_rebuildBrush = True
self.update()
@pyqtSlot(float)
def setMinimum(self, val: float):
self.setRange(val, self.m_max)
@pyqtSlot(float)
def setMaximum(self, val: float):
self.setRange(self.m_min, val)
@pyqtSlot(int)
def setValue(self, val: int):
if self.m_value != val:
if val < self.m_min:
self.m_value = self.m_min
elif val > self.m_max:
self.m_value = self.m_max
else:
self.m_value = val
self.update()
# PAINTING ------------------------------------------------------
def paintEvent(self, event: QPaintEvent):
outerRadius = min(self.width(), self.height())
baseRect = QRectF(1, 1, outerRadius - 2, outerRadius - 2)
buffer = QImage(outerRadius, outerRadius, QImage.Format_ARGB32_Premultiplied)
p = QPainter(buffer)
p.setRenderHint(QPainter.Antialiasing)
self.rebuildDataBrushIfNeeded()
self.drawBackground(p, buffer.rect())
self.drawBase(p, baseRect)
if self.m_value > 0:
delta = (self.m_max - self.m_min) / (self.m_value - self.m_min)
else:
delta = 0
self.drawValue(p, baseRect, self.m_value, delta)
innerRect, innerRadius = self.calculateInnerRect(outerRadius)
self.drawInnerBackground(p, innerRect)
self.conditionalDrawText(self.text_visability, p, innerRect, innerRadius, self.m_value)
p.end()
painter = QPainter(self)
painter.fillRect(baseRect, self.palette().window())
painter.drawImage(0, 0, buffer)
def drawBackground(self, p: QPainter, baseRect: QRectF):
p.fillRect(baseRect, self.palette().window())
def drawBase(self, p: QPainter, baseRect: QRectF):
if self.m_barStyle == self.BarStyle.DONUT:
p.setPen(QPen(self.palette().shadow().color(), self.m_outlinePenWidth))
p.setBrush(self.palette().base())
p.drawEllipse(baseRect)
elif self.m_barStyle == self.BarStyle.LINE:
p.setPen(QPen(self.palette().base().color(), self.m_outlinePenWidth))
p.setBrush(Qt.NoBrush)
p.drawEllipse(baseRect.adjusted(self.m_outlinePenWidth / 2, self.m_outlinePenWidth / 2,
-self.m_outlinePenWidth / 2, -self.m_outlinePenWidth / 2))
elif self.m_barStyle in (self.BarStyle.PIE, self.BarStyle.EXPAND):
p.setPen(QPen(self.palette().base().color(), self.m_outlinePenWidth))
p.setBrush(self.palette().base())
p.drawEllipse(baseRect)
def drawValue(self, p: QPainter, baseRect: QRectF, value: float, delta: float):
if value == self.m_min:
return
if self.m_barStyle == self.BarStyle.EXPAND:
p.setBrush(self.palette().highlight())
p.setPen(QPen(self.palette().shadow().color(), self.m_dataPenWidth))
radius = (baseRect.height() / 2) / delta
p.drawEllipse(baseRect.center(), radius, radius)
return
if self.m_barStyle == self.BarStyle.LINE:
p.setPen(QPen(self.palette().highlight().color(), self.m_dataPenWidth))
p.setBrush(Qt.NoBrush)
if value == self.m_max:
p.drawEllipse(baseRect.adjusted(self.m_outlinePenWidth / 2, self.m_outlinePenWidth / 2,
-self.m_outlinePenWidth / 2, -self.m_outlinePenWidth / 2))
else:
arcLength = 360 / delta
p.drawArc(baseRect.adjusted(self.m_outlinePenWidth / 2, self.m_outlinePenWidth / 2,
-self.m_outlinePenWidth / 2, -self.m_outlinePenWidth / 2),
int(self.m_nullPosition * 16),
int(-arcLength * 16))
return
dataPath = QPainterPath()
dataPath.setFillRule(Qt.WindingFill)
if value == self.m_max:
dataPath.addEllipse(baseRect)
else:
arcLength = 360 / delta
dataPath.moveTo(baseRect.center())
dataPath.arcTo(baseRect, self.m_nullPosition, -arcLength)
dataPath.lineTo(baseRect.center())
p.setBrush(self.palette().highlight())
p.setPen(QPen(self.palette().shadow().color(), self.m_dataPenWidth))
p.drawPath(dataPath)
def calculateInnerRect(self, outerRadius: float):
if self.m_barStyle in (self.BarStyle.LINE, self.BarStyle.EXPAND):
innerRadius = outerRadius - self.m_outlinePenWidth
else:
innerRadius = outerRadius * 0.75
delta = (outerRadius - innerRadius) / 2
innerRect = QRectF(delta, delta, innerRadius, innerRadius)
return innerRect, innerRadius
def drawInnerBackground(self, p: QPainter, innerRect: QRectF):
if self.m_barStyle == self.BarStyle.DONUT:
p.setBrush(self.palette().alternateBase())
p.drawEllipse(innerRect)
def drawText(self, p: QPainter, innerRect: QRectF, innerRadius: float, value: float):
if not self.m_format:
return
f = QFont(self.font())
f.setPixelSize(10)
fm = QFontMetricsF(f)
maxWidth = fm.width(self.valueToText(self.m_max))
delta = innerRadius / maxWidth
fontSize = f.pixelSize() * delta * 0.75
f.setPixelSize(int(fontSize))
p.setFont(f)
textRect = QRectF(innerRect)
p.setPen(self.palette().text().color())
p.drawText(textRect, Qt.AlignCenter, self.valueToText(value))
def conditionalDrawText(self, visability, p: QPainter=None, innerRect: QRectF=None, innerRadius: float=None, value: float=None):
if visability:
self.drawText(p, innerRect, innerRadius, value)
def valueToText(self, value: float):
textToDraw = self.m_format
if self.m_updateFlags == self.UpdateFlags.VALUE:
textToDraw = textToDraw.replace('%v', str(round(value, self.m_decimals)))
if self.m_updateFlags == self.UpdateFlags.PERCENT:
procent = (value - self.m_min) / (self.m_max - self.m_min) * 100
textToDraw = textToDraw.replace('%p', str(round(procent, self.m_decimals)))
if self.m_updateFlags == self.UpdateFlags.MAX:
textToDraw = textToDraw.replace('%m', str(round(self.m_max - self.m_min + 1, self.m_decimals)))
return textToDraw
def valueFormatChanged(self):
if operator.contains(self.m_format, '%v'):
self.m_updateFlags = self.UpdateFlags.VALUE
if operator.contains(self.m_format, '%p'):
self.m_updateFlags = self.UpdateFlags.PERCENT
if operator.contains(self.m_format, '%m'):
self.m_updateFlags = self.UpdateFlags.MAX
self.update()
def rebuildDataBrushIfNeeded(self):
if not self.m_rebuildBrush or not self.m_gradientData or self.m_barStyle == self.BarStyle.LINE:
return
self.m_rebuildBrush = False
p = self.palette()
if self.m_barStyle == self.BarStyle.EXPAND:
dataBrush = QRadialGradient(0.5, 0.5, 0.5, 0.5, 0.5)
dataBrush.setCoordinateMode(QGradient.StretchToDeviceMode)
for i in range(0, len(self.m_gradientData)):
dataBrush.setColorAt(self.m_gradientData[i][0], self.m_gradientData[i][1])
p.setBrush(QPalette.Highlight, dataBrush)
else:
dataBrush = QConicalGradient(QPointF(0.5, 0.5), self.m_nullPosition)
dataBrush.setCoordinateMode(QGradient.StretchToDeviceMode)
for i in range(0, len(self.m_gradientData)):
dataBrush.setColorAt(1 - self.m_gradientData[i][0], self.m_gradientData[i][1])
p.setBrush(QPalette.Highlight, dataBrush)
self.setPalette(p)