-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroundprogressbar.cpp
More file actions
155 lines (127 loc) · 3.32 KB
/
roundprogressbar.cpp
File metadata and controls
155 lines (127 loc) · 3.32 KB
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
#include "roundprogressbar.h"
#include <QPainter>
#include <QTimer>
#include <QPropertyAnimation>
RoundProgressBar::RoundProgressBar(QWidget *parent)
: QWidget(parent), _fontColor(Qt::blue), _outColor(Qt::green), _inColor(Qt::gray),
_interval(1000), _value(30), _minValue(0), _maxValue(100), _valueType(ValueRange)
{
}
void RoundProgressBar::start()
{
_t = new QTimer(this);
connect(_t, &QTimer::timeout, this, &RoundProgressBar::refresh);
_t->setInterval(_interval);
_t->start();
}
void RoundProgressBar::stop()
{
_t->stop();
disconnect(_t, &QTimer::timeout, this, &RoundProgressBar::refresh);
}
void RoundProgressBar::setInterval(int interval)
{
_interval = interval;
}
void RoundProgressBar::setValueType(ValueType type)
{
_valueType = type;
}
void RoundProgressBar::setRange(int min, int max)
{
if (min > max)
{
int tmp = min;
min = max;
max = tmp;
}
_minValue = min;
_maxValue = max;
}
void RoundProgressBar::setValue(int v)
{
QPropertyAnimation* animation=new QPropertyAnimation(this, "_value");
animation->setDuration(500);
animation->setStartValue(_value);
animation->setEndValue(v);
animation->setEasingCurve(QEasingCurve::OutQuad);
animation->start();
}
void RoundProgressBar::setFontColor(const QColor &color)
{
_fontColor = color;
}
void RoundProgressBar::setInColor(const QColor &color)
{
_inColor = color;
}
void RoundProgressBar::setOutColor(const QColor &color)
{
_outColor = color;
}
void RoundProgressBar::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
drawValue();
drawBound();
drawStartDot();
}
void RoundProgressBar::drawValue()
{
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
QPen pen(_fontColor);
pen.setWidth(10);
p.setFont(QFont("楷体", 24, 10));
p.setPen(pen);
p.drawText(rect(), Qt::AlignCenter, QString("%1/%2").arg(_value).arg(_maxValue));
}
void RoundProgressBar::drawBound()
{
int len = (width() < height() ? width() : height()) - 50;
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
QPen pen(_inColor);
pen.setWidth(10);
pen.setStyle(Qt::SolidLine);
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
p.setPen(pen);
p.drawArc(QRect(width()/2-len/2, height()/2-len/2, len, len), 0, 360*16);
pen.setColor(_outColor);
pen.setWidth(20);
pen.setStyle(Qt::SolidLine);
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
p.setPen(pen);
int spanAngle = 360 * (_value-_minValue) / (_maxValue-_minValue);
p.drawArc(QRect(width()/2-len/2, height()/2-len/2, len, len), 0, spanAngle*16);
}
void RoundProgressBar::drawStartDot()
{
if (_value/_maxValue < 3/100) return;
int len = (width() < height() ? width() : height()) - 50;
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
QPen pen(Qt::white);
QBrush brush(Qt::yellow);
p.setPen(pen);
p.setBrush(brush);
p.drawEllipse(QRect(len+17, height()/2-12, 15, 15));
}
void RoundProgressBar::_setValue(int v)
{
if (v > _maxValue) v = _maxValue;
else if (v < _minValue) v = _minValue;
else _value = v;
repaint();
}
int RoundProgressBar::_getValue()
{
return _value;
}
void RoundProgressBar::refresh()
{
setValue(qrand() % _maxValue);
repaint();
}