-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathloadingindicator.cc
More file actions
501 lines (409 loc) · 12.9 KB
/
loadingindicator.cc
File metadata and controls
501 lines (409 loc) · 12.9 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#include "loadingindicator.hpp"
#include <QMovie>
#include <QPainter>
#include <QTimer>
#include <QtMath>
class LoadingIndicator::LoadingIndicatorPrivate
{
public:
explicit LoadingIndicatorPrivate(LoadingIndicator *q)
: q_ptr(q)
, timer(new QTimer(q))
{
setupTimer();
}
~LoadingIndicatorPrivate() = default;
void setupTimer()
{
timer->setInterval(animationSpeed);
q_ptr->connect(timer, &QTimer::timeout, q_ptr, [this]() {
currentFrame = (currentFrame + 1) % 1000;
// 更新脉冲动画进度
pulseProgress += 0.02 * pulseDirection;
if (pulseProgress >= 1.0) {
pulseProgress = 1.0;
pulseDirection = -1;
} else if (pulseProgress <= 0.0) {
pulseProgress = 0.0;
pulseDirection = 1;
}
q_ptr->update();
});
}
QRect calculateTextRect(const QRect &animationRect) const
{
if (text.isEmpty()) {
return QRect();
}
// 文本显示在动画下方,至少留出20像素间距
const int textTop = animationRect.bottom() + 20;
const int availableHeight = q_ptr->height() - textTop - 10; // 底部留10像素边距
if (availableHeight <= 0) {
return QRect(); // 没有足够空间显示文本
}
// 计算合适的字体大小
const int fontSize = qMax(12, qMin(q_ptr->width(), availableHeight) / 20);
QFont font;
font.setPixelSize(fontSize);
QFontMetrics fm(font);
const int textHeight = fm.height();
// 确保文本不会超出底部
const int finalTextTop = qMin(textTop, q_ptr->height() - textHeight - 10);
return QRect(10, finalTextTop, q_ptr->width() - 20, textHeight);
}
LoadingIndicator *q_ptr;
QTimer *timer;
QScopedPointer<QMovie> moviePtr;
// 动画状态
int currentFrame = 0;
double pulseProgress = 0.0;
int pulseDirection = 1;
// 配置
AnimationStyle animationStyle = AnimationStyle::RotatingDots;
QString text;
QColor textColor = QColor(4, 181, 200);
QColor color = QColor(70, 130, 230);
QColor backgroundColor = QColor(255, 255, 255, 100);
int animationSpeed = 100;
int dotCount = 8;
int dotRadius = 6;
int barCount = 8;
int barWidth = 6;
};
LoadingIndicator::LoadingIndicator(QWidget *parent)
: QWidget(parent)
, d_ptr(new LoadingIndicatorPrivate(this))
{}
LoadingIndicator::LoadingIndicator(AnimationStyle style, QWidget *parent)
: LoadingIndicator(parent)
{
setAnimationStyle(style);
}
LoadingIndicator::~LoadingIndicator() = default;
void LoadingIndicator::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// 绘制背景
painter.fillRect(rect(), d_ptr->backgroundColor);
// 根据样式绘制动画并获取动画区域
QRect animationRect;
switch (d_ptr->animationStyle) {
case AnimationStyle::RotatingDots: animationRect = drawRotatingDots(painter); break;
case AnimationStyle::PulsingCircle: animationRect = drawPulsingCircle(painter); break;
case AnimationStyle::BouncingBars: animationRect = drawBouncingBars(painter); break;
case AnimationStyle::CustomMovie: animationRect = drawMovie(painter); break;
}
// 绘制文本
drawText(painter, animationRect);
}
QRect LoadingIndicator::drawRotatingDots(QPainter &painter)
{
const double radius = qMin(width(), height()) * 0.25;
const QPointF center = rect().center();
const double totalRadius = radius + d_ptr->dotRadius;
for (int i = 0; i < d_ptr->dotCount; ++i) {
const double angle = 2 * M_PI * i / d_ptr->dotCount - 2 * M_PI * d_ptr->currentFrame / 60.0;
const double x = center.x() + radius * qCos(angle);
const double y = center.y() + radius * qSin(angle);
const double opacity = 0.3 + 0.7 * (qSin(angle + d_ptr->currentFrame * 0.1) + 1) / 2;
QColor color = d_ptr->color;
color.setAlphaF(opacity);
painter.setBrush(color);
painter.setPen(Qt::NoPen);
painter.drawEllipse(QPointF(x, y), d_ptr->dotRadius, d_ptr->dotRadius);
}
return QRect(center.x() - totalRadius,
center.y() - totalRadius,
totalRadius * 2,
totalRadius * 2);
}
QRect LoadingIndicator::drawPulsingCircle(QPainter &painter)
{
const QPointF center = rect().center();
const double maxRadius = qMin(width(), height()) * 0.3;
const double currentRadius = maxRadius * (0.5 + 0.5 * d_ptr->pulseProgress);
const double totalRadius = currentRadius + d_ptr->dotRadius; // 考虑线宽
QColor color = d_ptr->color;
color.setAlphaF(0.7 + 0.3 * d_ptr->pulseProgress);
painter.setBrush(Qt::NoBrush);
painter.setPen(QPen(color, d_ptr->dotRadius));
painter.drawEllipse(center, currentRadius, currentRadius);
return QRect(center.x() - totalRadius,
center.y() - totalRadius,
totalRadius * 2,
totalRadius * 2);
}
QRect LoadingIndicator::drawBouncingBars(QPainter &painter)
{
const QPointF center = rect().center();
const double totalWidth = d_ptr->barCount * d_ptr->barWidth * 2;
const double startX = center.x() - totalWidth / 2;
const double maxHeight = qMin(width(), height()) * 0.3;
for (int i = 0; i < d_ptr->barCount; ++i) {
const double progress = (qSin((d_ptr->currentFrame * 0.2) + i * 0.5) + 1) / 2;
const double height = maxHeight * (0.3 + 0.7 * progress);
const double x = startX + i * d_ptr->barWidth * 2;
const double y = center.y() - height / 2;
QColor color = d_ptr->color;
color.setAlphaF(0.3 + 0.7 * progress);
painter.setBrush(color);
painter.setPen(Qt::NoPen);
painter.drawRect(QRectF(x, y, d_ptr->barWidth, height));
}
return QRect(startX, center.y() - maxHeight / 2, totalWidth, maxHeight);
}
QRect LoadingIndicator::drawMovie(QPainter &painter)
{
if (!d_ptr->moviePtr) {
return drawRotatingDots(painter); // 回退到默认动画
}
const QPixmap currentFrame = d_ptr->moviePtr->currentPixmap();
if (currentFrame.isNull()) {
return QRect();
}
const QPoint center = rect().center();
const int maxWidth = width() / 2;
const int maxHeight = height() / 2;
const QSize scaledSize = currentFrame.size().scaled(maxWidth, maxHeight, Qt::KeepAspectRatio);
const QRect movieRect(center.x() - scaledSize.width() / 2,
center.y() - scaledSize.height() / 2,
scaledSize.width(),
scaledSize.height());
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.drawPixmap(movieRect, currentFrame);
return movieRect;
}
void LoadingIndicator::drawText(QPainter &painter, const QRect &animationRect)
{
if (d_ptr->text.isEmpty() || animationRect.isEmpty()) {
return;
}
const QRect textRect = d_ptr->calculateTextRect(animationRect);
if (textRect.isEmpty()) {
return;
}
painter.setPen(d_ptr->textColor);
// 设置合适的字体大小
QFont font = painter.font();
const int fontSize = qMax(12, qMin(width(), textRect.height()) / 12);
font.setPixelSize(fontSize);
font.setBold(true);
painter.setFont(font);
const QString displayText = painter.fontMetrics().elidedText(d_ptr->text,
Qt::ElideRight,
textRect.width());
painter.setRenderHint(QPainter::TextAntialiasing);
painter.drawText(textRect, Qt::AlignCenter, displayText);
}
void LoadingIndicator::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);
if (d_ptr->animationStyle == AnimationStyle::CustomMovie) {
if (d_ptr->moviePtr) {
d_ptr->moviePtr->start();
}
return;
}
d_ptr->timer->start(d_ptr->animationSpeed);
}
void LoadingIndicator::hideEvent(QHideEvent *event)
{
QWidget::hideEvent(event);
d_ptr->timer->stop();
if (d_ptr->moviePtr) {
d_ptr->moviePtr->stop();
}
}
bool LoadingIndicator::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::Resize && obj == parentWidget()) {
setGeometry(parentWidget()->rect());
}
return QWidget::eventFilter(obj, event);
}
void LoadingIndicator::showOverlay(QWidget *parent)
{
if (!parent) {
show();
return;
}
if (parentWidget()) {
parentWidget()->removeEventFilter(this);
}
setParent(parent);
setGeometry(parent->rect());
parent->installEventFilter(this);
show();
raise();
}
void LoadingIndicator::hideOverlay()
{
if (parentWidget()) {
parentWidget()->removeEventFilter(this);
}
hide();
}
// 属性设置实现
QString LoadingIndicator::text() const
{
return d_ptr->text;
}
void LoadingIndicator::setText(const QString &text)
{
if (d_ptr->text == text) {
return;
}
d_ptr->text = text;
setToolTip(text);
update();
emit textChanged(text);
}
QColor LoadingIndicator::textColor() const
{
return d_ptr->textColor;
}
void LoadingIndicator::setTextColor(const QColor &color)
{
if (d_ptr->textColor == color) {
return;
}
d_ptr->textColor = color;
update();
emit textColorChanged(color);
}
QColor LoadingIndicator::color() const
{
return d_ptr->color;
}
void LoadingIndicator::setColor(const QColor &color)
{
if (d_ptr->color == color) {
return;
}
d_ptr->color = color;
if (d_ptr->animationStyle != AnimationStyle::CustomMovie) {
update();
}
emit colorChanged(color);
}
QColor LoadingIndicator::backgroundColor() const
{
return d_ptr->backgroundColor;
}
void LoadingIndicator::setBackgroundColor(const QColor &color)
{
if (d_ptr->backgroundColor == color) {
return;
}
d_ptr->backgroundColor = color;
update();
emit backgroundColorChanged(color);
}
int LoadingIndicator::animationSpeed() const
{
return d_ptr->animationSpeed;
}
void LoadingIndicator::setAnimationSpeed(int ms)
{
if (d_ptr->animationSpeed == ms) {
return;
}
d_ptr->animationSpeed = qBound(16, ms, 1000);
d_ptr->timer->setInterval(d_ptr->animationSpeed);
emit animationSpeedChanged(ms);
}
LoadingIndicator::AnimationStyle LoadingIndicator::animationStyle() const
{
return d_ptr->animationStyle;
}
void LoadingIndicator::setAnimationStyle(AnimationStyle style)
{
if (d_ptr->animationStyle == style) {
return;
}
// 停止当前动画
if (d_ptr->animationStyle == AnimationStyle::CustomMovie && d_ptr->moviePtr) {
d_ptr->moviePtr->stop();
} else {
d_ptr->timer->stop();
}
d_ptr->animationStyle = style;
// 启动新动画
if (isVisible()) {
if (d_ptr->animationStyle == AnimationStyle::CustomMovie && d_ptr->moviePtr) {
d_ptr->moviePtr->start();
} else {
d_ptr->timer->start(d_ptr->animationSpeed);
}
}
update();
emit animationStyleChanged(style);
}
void LoadingIndicator::setMovie(QMovie *movie)
{
d_ptr->moviePtr.reset(movie);
if (!d_ptr->moviePtr) {
return;
}
connect(d_ptr->moviePtr.data(), &QMovie::frameChanged, this, [this](int frameNumber) {
Q_UNUSED(frameNumber)
if (d_ptr->animationStyle == AnimationStyle::CustomMovie) {
update();
}
});
if (isVisible() && d_ptr->animationStyle == AnimationStyle::CustomMovie) {
d_ptr->moviePtr->start();
}
}
void LoadingIndicator::setMovie(const QString &fileName)
{
std::unique_ptr<QMovie> movie(new QMovie(fileName, QByteArray()));
if (!movie->isValid()) {
qWarning() << "Failed to load movie:" << fileName;
return;
}
setMovie(movie.release());
setAnimationStyle(AnimationStyle::CustomMovie);
}
void LoadingIndicator::setDotCount(int count)
{
if (d_ptr->dotCount == count) {
return;
}
d_ptr->dotCount = qMax(3, count);
if (d_ptr->animationStyle == AnimationStyle::RotatingDots) {
update();
}
}
void LoadingIndicator::setDotRadius(int radius)
{
if (d_ptr->dotRadius == radius) {
return;
}
d_ptr->dotRadius = qMax(2, radius);
if (d_ptr->animationStyle == AnimationStyle::RotatingDots) {
update();
}
}
void LoadingIndicator::setBarCount(int count)
{
if (d_ptr->barCount == count) {
return;
}
d_ptr->barCount = qMax(2, count);
if (d_ptr->animationStyle == AnimationStyle::BouncingBars) {
update();
}
}
void LoadingIndicator::setBarWidth(int width)
{
if (d_ptr->barWidth == width) {
return;
}
d_ptr->barWidth = qMax(2, width);
if (d_ptr->animationStyle == AnimationStyle::BouncingBars) {
update();
}
}