-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathprogressbar.cc
More file actions
422 lines (333 loc) · 9.26 KB
/
progressbar.cc
File metadata and controls
422 lines (333 loc) · 9.26 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include "progressbar.hpp"
#include <QPainter>
#include <QPainterPath>
#include <QPropertyAnimation>
class ProgressBar::ProgressBarPrivate
{
public:
explicit ProgressBarPrivate(ProgressBar *q)
: q_ptr(q)
{}
ProgressBar *q_ptr;
// 可配置属性
double maxValue = 100.0;
double minValue = 0.0;
double value = minValue;
double radius = 5.0;
bool autoRadius = true;
bool showPercent = true;
// 颜色配置
QColor chunkColor = QColor(77, 161, 255);
QColor textColor = QColor(64, 65, 66);
QColor baseColor = QColor(179, 179, 179);
QColor backgroundColor = Qt::transparent;
// 动画配置
int animationDuration = 500;
// 动画
QPropertyAnimation *animation = nullptr;
double targetValue = 0.0;
// 字体配置
static constexpr double FONT_SIZE_RATIO = 0.8;
};
ProgressBar::ProgressBar(QWidget *parent)
: QWidget(parent)
, d_ptr(new ProgressBarPrivate(this))
{
d_ptr->animation = new QPropertyAnimation(this, "value", this);
d_ptr->animation->setDuration(d_ptr->animationDuration);
d_ptr->animation->setEasingCurve(QEasingCurve::OutCubic);
connect(d_ptr->animation,
&QPropertyAnimation::finished,
this,
&ProgressBar::onAnimationFinished);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
}
ProgressBar::~ProgressBar() = default;
auto ProgressBar::minimumSizeHint() const -> QSize
{
return {100, 10};
}
// Value
auto ProgressBar::value() const -> double
{
return d_ptr->value;
}
void ProgressBar::setValue(double value)
{
value = qBound(d_ptr->minValue, value, d_ptr->maxValue);
if (qFuzzyCompare(value, d_ptr->value))
return;
const double oldValue = d_ptr->value;
d_ptr->value = value;
update();
if (!isAnimating() || qFuzzyCompare(value, d_ptr->targetValue)) {
emit valueChanged(value);
if (value > oldValue) {
emit valueIncreased(value);
} else if (value < oldValue) {
emit valueDecreased(value);
}
}
}
void ProgressBar::setValueAnimated(double value)
{
value = qBound(d_ptr->minValue, value, d_ptr->maxValue);
if (qFuzzyCompare(value, d_ptr->value))
return;
d_ptr->targetValue = value;
emit animationStarted(d_ptr->value, value);
startAnimation(value);
}
// Min value
void ProgressBar::setMinValue(double min)
{
if (qFuzzyCompare(d_ptr->minValue, min))
return;
d_ptr->minValue = min;
if (d_ptr->value < min) {
setValue(min);
}
update();
}
auto ProgressBar::minValue() const -> double
{
return d_ptr->minValue;
}
// Max value
void ProgressBar::setMaxValue(double max)
{
if (qFuzzyCompare(d_ptr->maxValue, max))
return;
d_ptr->maxValue = max;
if (d_ptr->value > max) {
setValue(max);
}
update();
}
auto ProgressBar::maxValue() const -> double
{
return d_ptr->maxValue;
}
// Radius
void ProgressBar::setRadius(double radius)
{
if (qFuzzyCompare(d_ptr->radius, radius))
return;
d_ptr->radius = radius;
update();
}
auto ProgressBar::radius() const -> double
{
return d_ptr->radius;
}
// Auto radius
void ProgressBar::setAutoRadius(bool autoRadius)
{
if (d_ptr->autoRadius == autoRadius)
return;
d_ptr->autoRadius = autoRadius;
update();
}
auto ProgressBar::autoRadius() const -> bool
{
return d_ptr->autoRadius;
}
// Show percent
void ProgressBar::setShowPercent(bool percent)
{
if (d_ptr->showPercent == percent)
return;
d_ptr->showPercent = percent;
update();
}
auto ProgressBar::showPercent() const -> bool
{
return d_ptr->showPercent;
}
// Chunk color
void ProgressBar::setChunkColor(const QColor &color)
{
if (d_ptr->chunkColor == color)
return;
d_ptr->chunkColor = color;
update();
}
auto ProgressBar::chunkColor() const -> QColor
{
return d_ptr->chunkColor;
}
// Text color
void ProgressBar::setTextColor(const QColor &color)
{
if (d_ptr->textColor == color)
return;
d_ptr->textColor = color;
update();
}
auto ProgressBar::textColor() const -> QColor
{
return d_ptr->textColor;
}
// Base color
void ProgressBar::setBaseColor(const QColor &color)
{
if (d_ptr->baseColor == color)
return;
d_ptr->baseColor = color;
update();
}
auto ProgressBar::baseColor() const -> QColor
{
return d_ptr->baseColor;
}
// Background color
void ProgressBar::setBackgroundColor(const QColor &color)
{
if (d_ptr->backgroundColor == color)
return;
d_ptr->backgroundColor = color;
update();
}
auto ProgressBar::backgroundColor() const -> QColor
{
return d_ptr->backgroundColor;
}
// Animation duration
void ProgressBar::setAnimationDuration(int duration)
{
if (duration < 0 || d_ptr->animationDuration == duration)
return;
d_ptr->animationDuration = duration;
d_ptr->animation->setDuration(duration);
}
auto ProgressBar::animationDuration() const -> int
{
return d_ptr->animationDuration;
}
// Animation state
bool ProgressBar::isAnimating() const
{
return d_ptr->animation && d_ptr->animation->state() == QPropertyAnimation::Running;
}
// Public slots implementation
void ProgressBar::increaseValue(double increment)
{
if (increment <= 0.0)
return;
const double newValue = qMin(d_ptr->maxValue, d_ptr->value + increment);
if (qFuzzyCompare(newValue, d_ptr->value))
return;
setValueAnimated(newValue);
}
void ProgressBar::decreaseValue(double decrement)
{
if (decrement <= 0.0)
return;
const double newValue = qMax(d_ptr->minValue, d_ptr->value - decrement);
if (qFuzzyCompare(newValue, d_ptr->value))
return;
setValueAnimated(newValue);
}
void ProgressBar::reset()
{
if (qFuzzyCompare(d_ptr->value, d_ptr->minValue))
return;
setValueAnimated(d_ptr->minValue);
emit valueReset();
}
// Painting
void ProgressBar::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
if (d_ptr->backgroundColor != Qt::transparent) {
painter.fillRect(rect(), d_ptr->backgroundColor);
}
drawProgressBar(&painter);
drawText(&painter);
}
void ProgressBar::startAnimation(double targetValue)
{
if (qFuzzyCompare(targetValue, d_ptr->value) || !d_ptr->animation)
return;
if (d_ptr->animation->state() == QPropertyAnimation::Running) {
d_ptr->animation->stop();
}
d_ptr->animation->setStartValue(d_ptr->value);
d_ptr->animation->setEndValue(targetValue);
d_ptr->animation->start();
}
void ProgressBar::onAnimationFinished()
{
emit animationFinished(d_ptr->value);
if (qFuzzyCompare(d_ptr->value, d_ptr->targetValue)) {
emit valueChanged(d_ptr->value);
}
}
void ProgressBar::setupFont(QPainter *painter)
{
const double fontSize = height() * d_ptr->FONT_SIZE_RATIO;
auto font = painter->font();
font.setPixelSize(static_cast<int>(fontSize));
painter->setFont(font);
}
void ProgressBar::drawProgressBar(QPainter *painter)
{
const QRectF baseRect = rect();
double currentRadius = d_ptr->radius;
// 自动计算圆角半径
if (d_ptr->autoRadius) {
currentRadius = qMin(baseRect.height(), baseRect.width()) / 2.0;
}
painter->setPen(Qt::NoPen);
// 绘制背景
painter->setBrush(d_ptr->baseColor);
painter->drawRoundedRect(baseRect, currentRadius, currentRadius);
// 绘制进度条
if (d_ptr->value > d_ptr->minValue) {
const double progressWidth = baseRect.width()
* ((d_ptr->value - d_ptr->minValue)
/ (d_ptr->maxValue - d_ptr->minValue));
drawProgressChunk(painter, baseRect, progressWidth, currentRadius);
}
}
void ProgressBar::drawProgressChunk(QPainter *painter,
const QRectF &baseRect,
double progressWidth,
double baseRadius)
{
if (progressWidth <= 0)
return;
const double height = baseRect.height();
painter->setBrush(d_ptr->chunkColor);
painter->save();
// 设置裁剪区域,只显示进度部分
QPainterPath clipPath;
clipPath.addRect(0, 0, progressWidth, height);
painter->setClipPath(clipPath);
// 创建一个完整的圆角矩形(左右都有圆角)
// 宽度设为足够大,确保圆角完整显示
const double fullWidth = qMax(progressWidth, 2 * baseRadius);
QPainterPath fullRoundedPath;
fullRoundedPath.addRoundedRect(0, 0, fullWidth, height, baseRadius, baseRadius);
// 绘制完整的圆角矩形
painter->drawPath(fullRoundedPath);
painter->restore();
}
void ProgressBar::drawText(QPainter *painter)
{
painter->save();
setupFont(painter);
painter->setPen(d_ptr->textColor);
QString valueText;
if (d_ptr->showPercent) {
const double percent = (d_ptr->value - d_ptr->minValue)
/ (d_ptr->maxValue - d_ptr->minValue) * 100.0;
valueText = QString("%1%").arg(percent, 0, 'f', 2);
} else {
valueText = QString("%1").arg(d_ptr->value, 0, 'f', 2);
}
painter->drawText(rect(), Qt::AlignCenter, valueText);
painter->restore();
}