-
Notifications
You must be signed in to change notification settings - Fork 18
/
ProgressCircle.cpp
238 lines (202 loc) · 5.21 KB
/
ProgressCircle.cpp
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
/////////////////////////////////////////////////////////////////////////////
// Date of creation: 04.07.2013
// Creator: Alexander Egorov aka mofr
// Authors: mofr
/////////////////////////////////////////////////////////////////////////////
#include "ProgressCircle.h"
#include <QPainter>
#include <QPixmapCache>
ProgressCircle::ProgressCircle(QWidget *parent) :
QWidget(parent),
mValue(0),
mMaximum(0),
mInnerRadius(0.6),
mOuterRadius(1.0),
mColor(110,190,235),
mVisibleValue(0),
mValueAnimation(this, "visibleValue"),
mInfiniteAnimation(this, "infiniteAnimationValue"),
mInfiniteAnimationValue(0.0)
{
mInfiniteAnimation.setLoopCount(-1);//infinite
mInfiniteAnimation.setDuration(1000);
mInfiniteAnimation.setStartValue(0.0);
mInfiniteAnimation.setEndValue(1.0);
mInfiniteAnimation.start();
}
int ProgressCircle::value() const
{
return mValue;
}
int ProgressCircle::maximum() const
{
return mMaximum;
}
qreal ProgressCircle::innerRadius() const
{
return mInnerRadius;
}
qreal ProgressCircle::outerRadius() const
{
return mOuterRadius;
}
QColor ProgressCircle::color() const
{
return mColor;
}
void ProgressCircle::setValue(int value)
{
if(value < 0) value = 0;
if(mValue != value)
{
mValueAnimation.stop();
mValueAnimation.setEndValue(value);
mValueAnimation.setDuration(250);
mValueAnimation.start();
mValue = value;
emit valueChanged(value);
}
}
void ProgressCircle::setMaximum(int maximum)
{
if(maximum < 0) maximum = 0;
if(mMaximum != maximum)
{
mMaximum = maximum;
update();
emit maximumChanged(maximum);
if(mMaximum == 0)
{
mInfiniteAnimation.start();
}
else
{
mInfiniteAnimation.stop();
}
}
}
void ProgressCircle::setInnerRadius(qreal innerRadius)
{
if(innerRadius > 1.0) innerRadius = 1.0;
if(innerRadius < 0.0) innerRadius = 0.0;
if(mInnerRadius != innerRadius)
{
mInnerRadius = innerRadius;
update();
}
}
void ProgressCircle::setOuterRadius(qreal outerRadius)
{
if(outerRadius > 1.0) outerRadius = 1.0;
if(outerRadius < 0.0) outerRadius = 0.0;
if(mOuterRadius != outerRadius)
{
mOuterRadius = outerRadius;
update();
}
}
void ProgressCircle::setColor(QColor color)
{
if(color != mColor)
{
mColor = color;
update();
}
}
QRectF squared(QRectF rect)
{
if(rect.width() > rect.height())
{
qreal diff = rect.width() - rect.height();
return rect.adjusted(diff/2, 0, -diff/2, 0);
}
else
{
qreal diff = rect.height() - rect.width();
return rect.adjusted(0, diff/2, 0, -diff/2);
}
}
void ProgressCircle::paintEvent(QPaintEvent *)
{
QPixmap pixmap;
if (!QPixmapCache::find(key(), pixmap))
{
pixmap = generatePixmap();
QPixmapCache::insert(key(), pixmap);
}
// Draw pixmap at center of item
QPainter painter(this);
painter.drawPixmap( 0.5 * ( width() - pixmap.width() ), 0.5 * ( height() - pixmap.height() ), pixmap );
}
void ProgressCircle::setInfiniteAnimationValue(qreal value)
{
mInfiniteAnimationValue = value;
update();
}
void ProgressCircle::setVisibleValue(int value)
{
if(mVisibleValue != value)
{
mVisibleValue = value;
update();
}
}
QString ProgressCircle::key() const
{
return QString("%1,%2,%3,%4,%5,%6,%7,%8")
.arg(mInfiniteAnimationValue)
.arg(mVisibleValue)
.arg(mMaximum)
.arg(mInnerRadius)
.arg(mOuterRadius)
.arg(width())
.arg(height())
.arg(mColor.rgb())
;
}
QPixmap ProgressCircle::generatePixmap() const
{
QPixmap pixmap(squared(rect()).size().toSize());
pixmap.fill(QColor(0,0,0,0));
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing, true);
QRectF rect = pixmap.rect().adjusted(1,1,-1,-1);
qreal margin = rect.width()*(1.0 - mOuterRadius)/2.0;
rect.adjust(margin,margin,-margin,-margin);
qreal innerRadius = mInnerRadius*rect.width()/2.0;
//background grey circle
painter.setBrush(QColor(225,225,225));
painter.setPen(QColor(225,225,225));
painter.drawPie(rect, 0, 360*16);
painter.setBrush(mColor);
painter.setPen(mColor);
if(mMaximum == 0)
{
//draw as infinite process
int startAngle = -mInfiniteAnimationValue * 360 * 16;
int spanAngle = 0.15 * 360 * 16;
painter.drawPie(rect, startAngle, spanAngle);
}
else
{
int value = qMin(mVisibleValue, mMaximum);
int startAngle = 90 * 16;
int spanAngle = -qreal(value) * 360 * 16 / mMaximum;
painter.drawPie(rect, startAngle, spanAngle);
}
//inner circle and frame
painter.setBrush(QColor(255,255,255));
painter.setPen(QColor(0,0,0, 60));
painter.drawEllipse(rect.center(), innerRadius, innerRadius);
//outer frame
painter.drawArc(rect, 0, 360*16);
return pixmap;
}
qreal ProgressCircle::infiniteAnimationValue() const
{
return mInfiniteAnimationValue;
}
int ProgressCircle::visibleValue() const
{
return mVisibleValue;
}