summary refs log tree commit diff
path: root/src/ui/LoadingIndicator.cpp
blob: d2b1240d5489cb9f3ce836deedc15b659178342e (plain) (blame)
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
#include "LoadingIndicator.h"

#include <QPaintEvent>
#include <QPainter>
#include <QTimer>

LoadingIndicator::LoadingIndicator(QWidget *parent)
  : QWidget(parent)
  , interval_(70)
  , angle_(0)
  , color_(Qt::black)
{
        setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        setFocusPolicy(Qt::NoFocus);

        timer_ = new QTimer(this);
        connect(timer_, SIGNAL(timeout()), this, SLOT(onTimeout()));
}

void
LoadingIndicator::paintEvent(QPaintEvent *e)
{
        Q_UNUSED(e)

        if (!timer_->isActive())
                return;

        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);

        int width = qMin(this->width(), this->height());

        int outerRadius = (width - 4) * 0.5f;
        int innerRadius = outerRadius * 0.78f;

        int capsuleRadius = (outerRadius - innerRadius) / 2;

        for (int i = 0; i < 8; ++i) {
                QColor color = color_;

                color.setAlphaF(1.0f - (i / 8.0f));

                painter.setPen(Qt::NoPen);
                painter.setBrush(color);

                qreal radius = capsuleRadius * (1.0f - (i / 16.0f));

                painter.save();

                painter.translate(rect().center());
                painter.rotate(angle_ - i * 45.0f);

                QPointF center = QPointF(-capsuleRadius, -innerRadius);
                painter.drawEllipse(center, radius * 2, radius * 2);

                painter.restore();
        }
}

void
LoadingIndicator::start()
{
        timer_->start(interval_);
        show();
}

void
LoadingIndicator::stop()
{
        timer_->stop();
        hide();
}

void
LoadingIndicator::onTimeout()
{
        angle_ = (angle_ + 45) % 360;
        repaint();
}