-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathprogressarc.cpp
269 lines (220 loc) · 5.76 KB
/
progressarc.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
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
#include "progressarc.h"
#include <QPainter>
#include <QPropertyAnimation>
struct ProgressArc::ProgressArcPrivate
{
double minValue = 0;
double maxValue = 100;
double value = 0;
double startAngle = -90;
double endAngle = 270;
QColor arcColor = QColor(QLatin1String("#4da1ff"));
QColor textColor = QColor(QLatin1String("#4da1ff"));
QColor titleColor = QColor(80, 80, 80);
QColor baseColor = QColor(179, 179, 179);
QColor backgroundColor = Qt::transparent;
bool percent = true;
QString title;
QPropertyAnimation *animation;
};
ProgressArc::ProgressArc(const QString &title, QWidget *parent)
: QWidget(parent)
, d_ptr(new ProgressArcPrivate)
{
setTitle(title);
d_ptr->animation = new QPropertyAnimation(this, "value", this);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(this, &ProgressArc::valueChanged, this, &ProgressArc::onStartAnimation);
}
ProgressArc::~ProgressArc() = default;
void ProgressArc::setTitle(const QString &title)
{
d_ptr->title = title;
update();
}
auto ProgressArc::title() const -> QString
{
return d_ptr->title;
}
void ProgressArc::setPercent(const bool percent)
{
d_ptr->percent = percent;
update();
}
auto ProgressArc::percent() const -> bool
{
return d_ptr->percent;
}
void ProgressArc::setMin(const double min)
{
d_ptr->minValue = min;
update();
}
auto ProgressArc::min() const -> double
{
return d_ptr->minValue;
}
void ProgressArc::setmax(const double max)
{
d_ptr->maxValue = max;
update();
}
auto ProgressArc::max() const -> double
{
return d_ptr->maxValue;
}
void ProgressArc::setStartAngle(const double startAngle)
{
d_ptr->startAngle = startAngle;
update();
}
auto ProgressArc::startAngle() const -> double
{
return d_ptr->startAngle;
}
void ProgressArc::setEndAngle(const double endAngle)
{
d_ptr->endAngle = endAngle;
update();
}
auto ProgressArc::endAngle() const -> double
{
return d_ptr->endAngle;
}
void ProgressArc::setArcColor(const QColor &color)
{
d_ptr->arcColor = color;
update();
}
auto ProgressArc::arcColor() const -> QColor
{
return d_ptr->arcColor;
}
void ProgressArc::setTextColor(const QColor &color)
{
d_ptr->textColor = color;
update();
}
auto ProgressArc::textColor() const -> QColor
{
return d_ptr->textColor;
}
void ProgressArc::setTitleColor(const QColor &color)
{
d_ptr->titleColor = color;
update();
}
auto ProgressArc::titleColor() const -> QColor
{
return d_ptr->titleColor;
}
void ProgressArc::setBaseColor(const QColor &color)
{
d_ptr->baseColor = color;
update();
}
auto ProgressArc::baseColor() const -> QColor
{
return d_ptr->baseColor;
}
void ProgressArc::setBackgroundColor(const QColor &color)
{
d_ptr->backgroundColor = color;
update();
}
auto ProgressArc::backgroundColor() const -> QColor
{
return d_ptr->backgroundColor;
}
auto ProgressArc::sizeHint() const -> QSize
{
return {200, 200};
}
auto ProgressArc::minimumSizeHint() const -> QSize
{
return {80, 80};
}
void ProgressArc::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
// 背景
if (d_ptr->backgroundColor != Qt::transparent) {
painter.setPen(Qt::NoPen);
painter.fillRect(rect(), d_ptr->backgroundColor);
}
// 平移中心
painter.translate(width() / 2, height() / 2);
// 圆弧
drawArc(&painter);
// text
drawText(&painter);
}
void ProgressArc::onStartAnimation(const double value)
{
if (value < d_ptr->minValue || value > d_ptr->maxValue || value == d_ptr->value) {
return;
}
int start = d_ptr->value;
int end = value;
d_ptr->animation->setStartValue(start);
d_ptr->animation->setEndValue(end);
d_ptr->animation->start();
}
auto ProgressArc::value() const -> double
{
return d_ptr->value;
}
void ProgressArc::setValue(const double value)
{
d_ptr->value = value;
update();
}
void ProgressArc::drawArc(QPainter *painter)
{
double min = qMin(width(), height());
double arcWidth = min / 10.0;
double radius = min / 2.0 - arcWidth - 5;
QRectF rect = QRectF(-radius, -radius, radius * 2, radius * 2);
QPen pen;
pen.setWidthF(arcWidth);
pen.setCapStyle(Qt::RoundCap);
// 圆弧背景
double angle = d_ptr->endAngle - d_ptr->startAngle;
pen.setColor(d_ptr->baseColor);
painter->setPen(pen);
painter->drawArc(rect, d_ptr->startAngle * 16, angle * 16);
// 圆弧进度
double spanAngle = angle
* ((d_ptr->value - d_ptr->minValue) / (d_ptr->maxValue - d_ptr->minValue));
double startAngle = d_ptr->endAngle - spanAngle;
pen.setColor(d_ptr->arcColor);
painter->setPen(pen);
painter->drawArc(rect, startAngle * 16, spanAngle * 16);
}
void ProgressArc::drawText(QPainter *painter)
{
double min = qMin(width(), height());
double arcWidth = min / 10.0;
double radius = min / 2.0 - arcWidth - 5;
painter->setPen(d_ptr->textColor);
QFont font("Microsoft YaHei", radius / 4.0);
painter->setFont(font);
QString strValue;
if (d_ptr->percent) {
double temp = d_ptr->value / (d_ptr->maxValue - d_ptr->minValue) * 100;
strValue = QString("%1%").arg(temp, 0, 'f', 2);
} else {
strValue = QString("%1").arg(d_ptr->value, 0, 'f', 2);
}
// value
QRectF valueRect(-radius, 0, radius * 2, radius / 3);
painter->drawText(valueRect, Qt::AlignCenter, strValue);
painter->setPen(d_ptr->titleColor);
font.setPixelSize(radius / 4.0);
painter->setFont(font);
// title
QRectF textRect(-radius, -radius / 2.5, radius * 2, radius / 3);
painter->drawText(textRect, Qt::AlignCenter, d_ptr->title);
}