matrixion/src/ui/LoadingIndicator.cpp

84 lines
1.6 KiB
C++
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2017-09-10 12:58:00 +03:00
#include "LoadingIndicator.h"
2020-01-31 08:12:02 +03:00
#include <QPaintEvent>
#include <QPainter>
#include <QTimer>
2017-09-10 12:58:00 +03:00
LoadingIndicator::LoadingIndicator(QWidget *parent)
: QWidget(parent)
, interval_(70)
, angle_(0)
, color_(Qt::black)
{
2021-09-18 01:22:33 +03:00
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setFocusPolicy(Qt::NoFocus);
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
timer_ = new QTimer(this);
connect(timer_, SIGNAL(timeout()), this, SLOT(onTimeout()));
2017-09-10 12:58:00 +03:00
}
void
LoadingIndicator::paintEvent(QPaintEvent *e)
{
2021-09-18 01:22:33 +03:00
Q_UNUSED(e)
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
if (!timer_->isActive())
return;
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
int width = qMin(this->width(), this->height());
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
int outerRadius = (width - 4) * 0.5f;
int innerRadius = outerRadius * 0.78f;
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
int capsuleRadius = (outerRadius - innerRadius) / 2;
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
for (int i = 0; i < 8; ++i) {
QColor color = color_;
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
color.setAlphaF(1.0f - (i / 8.0f));
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
painter.setPen(Qt::NoPen);
painter.setBrush(color);
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
qreal radius = capsuleRadius * (1.0f - (i / 16.0f));
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
painter.save();
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
painter.translate(rect().center());
painter.rotate(angle_ - i * 45.0f);
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
QPointF center = QPointF(-capsuleRadius, -innerRadius);
painter.drawEllipse(center, radius * 2, radius * 2);
2017-09-10 12:58:00 +03:00
2021-09-18 01:22:33 +03:00
painter.restore();
}
2017-09-10 12:58:00 +03:00
}
void
LoadingIndicator::start()
{
2021-09-18 01:22:33 +03:00
timer_->start(interval_);
show();
2017-09-10 12:58:00 +03:00
}
void
LoadingIndicator::stop()
{
2021-09-18 01:22:33 +03:00
timer_->stop();
hide();
2017-09-10 12:58:00 +03:00
}
void
LoadingIndicator::onTimeout()
{
2021-09-18 01:22:33 +03:00
angle_ = (angle_ + 45) % 360;
repaint();
2017-09-10 12:58:00 +03:00
}