-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwaterprogressbar.cpp
More file actions
145 lines (116 loc) · 2.99 KB
/
waterprogressbar.cpp
File metadata and controls
145 lines (116 loc) · 2.99 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
#include "waterprogressbar.h"
#include <QPainter>
WaterProgressBar::WaterProgressBar(QWidget *parent)
: QWidget(parent), _value(30), _minValue(0), _maxValue(100), _radius(200)
{
init();
}
void WaterProgressBar::start()
{
connect(_t, &QTimer::timeout, this, &WaterProgressBar::refresh);
_t->setInterval(1000);
_t->start();
}
void WaterProgressBar::stop()
{
disconnect(_t, &QTimer::timeout, this, &WaterProgressBar::refresh);
_t->stop();
}
void WaterProgressBar::setValue(int v)
{
_ani->setStartValue(_value);
_ani->setEndValue(v);
_ani->start();
if (v < _minValue) _value = _minValue;
else if (v > _maxValue) _value = _maxValue;
else _value = v;
}
void WaterProgressBar::setRange(int min, int max)
{
if (min > max)
{
int tmp = min;
min = max;
max = tmp;
}
_minValue = min;
_maxValue = max;
}
void WaterProgressBar::setRadius(int r)
{
_radius = r;
}
void WaterProgressBar::setColor(const QColor &bgColor, const QColor &fontColor)
{
_bgColor = bgColor;
_fontColor = fontColor;
}
void WaterProgressBar::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
drawBorder();
drawWater();
drawText();
}
void WaterProgressBar::init()
{
_bgColor = QColor(Qt::blue);
_fontColor = QColor(Qt::green);
_t = new QTimer(this);
_ani = new QPropertyAnimation(this, "_value");
_ani->setDuration(500);
_ani->setEasingCurve(QEasingCurve::OutQuad);
}
void WaterProgressBar::drawWater()
{
int len = (width() > height() ? height() : width()) - 50;
QPainter p(this);
QPainterPath path;
// Calculate the angle
double startAngle = 270.0 - _value * 180 / _maxValue;
double endAngle = 360.0 - (startAngle - 90) * 2;
p.setBrush(_bgColor);
path.addEllipse(QRect(25, height()/2-len/2, len, len));
p.drawPath(path);
p.setBrush(Qt::black);
p.setRenderHints(QPainter:: Antialiasing | QPainter::SmoothPixmapTransform, true);
path.arcMoveTo(25, height()/2-len/2, len, len, startAngle);
path.arcTo(QRect(25, height()/2-len/2, len, len), startAngle, endAngle);
p.drawPath(path);
}
void WaterProgressBar::drawBorder()
{
int len = (width() > height() ? height() : width()) - 50;
QPainter p(this);
QPen pen(Qt::gray);
p.setPen(pen);
p.setRenderHint(QPainter::Antialiasing, true);
p.drawEllipse(25, height()/2-len/2, len, len);
}
void WaterProgressBar::drawText()
{
QPainter p(this);
QPen pen(_fontColor);
QFont font("楷体", 72, 10);
pen.setWidth(8);
p.setPen(pen);
p.setFont(font);
p.setRenderHint(QPainter::Antialiasing, true);
p.drawText(rect(), Qt::AlignCenter, QString::number(_value));
}
int WaterProgressBar::_getValue() const
{
return _value;
}
void WaterProgressBar::_setValue(int v)
{
if (v < _minValue) _value = _minValue;
else if (v > _maxValue) _value = _maxValue;
else _value = v;
repaint();
}
void WaterProgressBar::refresh()
{
setValue(qrand() % _maxValue);
repaint();
}