animated loader with circles
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
#include "qt/view/QtViewOverlay.h"
|
||||
|
||||
#include <QWidget>
|
||||
#include <cmath>
|
||||
|
||||
#include <QEvent>
|
||||
#include <QPainter>
|
||||
#include <QPaintEvent>
|
||||
#include <QTimer>
|
||||
|
||||
ResizeFilter::ResizeFilter(QWidget* widget)
|
||||
: m_widget(widget)
|
||||
@@ -20,21 +24,69 @@ bool ResizeFilter::eventFilter(QObject* obj, QEvent* event)
|
||||
}
|
||||
|
||||
|
||||
QtOverlay::QtOverlay(QWidget* parent)
|
||||
: QWidget(parent)
|
||||
, m_count(0)
|
||||
, m_size(40)
|
||||
{
|
||||
}
|
||||
|
||||
void QtOverlay::animate()
|
||||
{
|
||||
m_count += 25;
|
||||
|
||||
QPoint center = geometry().center();
|
||||
update(QRect(center.x() - m_size / 2, center.y() - m_size / 2, m_size, m_size));
|
||||
}
|
||||
|
||||
void QtOverlay::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
QPainter painter(this);
|
||||
|
||||
painter.fillRect(geometry(), "#AAFFFFFF");
|
||||
|
||||
QPoint center = geometry().center();
|
||||
|
||||
size_t x = m_size / 2;
|
||||
size_t y = m_size / 2;
|
||||
size_t s = (m_count / 7) % 200;
|
||||
|
||||
if (s < 100)
|
||||
{
|
||||
x = std::cos(3.1415f * s / 100) * x;
|
||||
}
|
||||
else
|
||||
{
|
||||
y = std::cos(3.1415f * (s - 100) / 100) * y;
|
||||
}
|
||||
|
||||
painter.translate(center.x(), center.y());
|
||||
// painter.rotate(m_count / 8);
|
||||
|
||||
if (s > 50 && s < 150)
|
||||
{
|
||||
painter.setBrush(QBrush("#CC2E3C86"));
|
||||
}
|
||||
else
|
||||
{
|
||||
painter.setBrush(QBrush("#CC007AC2"));
|
||||
}
|
||||
|
||||
painter.setPen(QPen(Qt::transparent));
|
||||
|
||||
painter.drawEllipse(-x, -y, 2 * x, 2 * y);
|
||||
}
|
||||
|
||||
|
||||
QtViewOverlay::QtViewOverlay(QWidget* parent)
|
||||
: m_parent(parent)
|
||||
, m_beforeFunctor(std::bind(&QtViewOverlay::doBefore, this))
|
||||
, m_afterFunctor(std::bind(&QtViewOverlay::doAfter, this))
|
||||
{
|
||||
m_overlay = new QWidget(m_parent);
|
||||
m_overlay = new QtOverlay(m_parent);
|
||||
m_parent->installEventFilter(new ResizeFilter(m_overlay));
|
||||
|
||||
std::string frameStyle =
|
||||
"#overlay {"
|
||||
"background: #55FF0000; "
|
||||
"}";
|
||||
m_overlay->setStyleSheet(frameStyle.c_str());
|
||||
m_overlay->setObjectName("overlay");
|
||||
m_overlay->hide();
|
||||
m_timer = new QTimer(m_overlay);
|
||||
}
|
||||
|
||||
void QtViewOverlay::handleMessage(MessageLoadBefore* message)
|
||||
@@ -49,10 +101,16 @@ void QtViewOverlay::handleMessage(MessageLoadAfter* message)
|
||||
|
||||
void QtViewOverlay::doBefore()
|
||||
{
|
||||
QObject::connect(m_timer, SIGNAL(timeout()), m_overlay, SLOT(animate()));
|
||||
|
||||
m_timer->start(25);
|
||||
|
||||
m_overlay->show();
|
||||
}
|
||||
|
||||
void QtViewOverlay::doAfter()
|
||||
{
|
||||
m_timer->stop();
|
||||
|
||||
m_overlay->hide();
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
#ifndef QT_VIEW_OVERLAY
|
||||
#define QT_VIEW_OVERLAY
|
||||
|
||||
#include <QObject>
|
||||
#include <QWidget>
|
||||
|
||||
#include "qt/utility/QtThreadedFunctor.h"
|
||||
#include "utility/messaging/MessageListener.h"
|
||||
#include "utility/messaging/type/MessageLoadBefore.h"
|
||||
#include "utility/messaging/type/MessageLoadAfter.h"
|
||||
|
||||
class QWidget;
|
||||
class QTimer;
|
||||
|
||||
class ResizeFilter
|
||||
: public QObject
|
||||
@@ -29,6 +29,26 @@ private:
|
||||
};
|
||||
|
||||
|
||||
class QtOverlay
|
||||
: public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QtOverlay(QWidget* parent = nullptr);
|
||||
|
||||
public slots:
|
||||
void animate();
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event);
|
||||
|
||||
private:
|
||||
size_t m_count;
|
||||
size_t m_size;
|
||||
};
|
||||
|
||||
|
||||
class QtViewOverlay
|
||||
: public MessageListener<MessageLoadBefore>
|
||||
, public MessageListener<MessageLoadAfter>
|
||||
@@ -44,7 +64,9 @@ private:
|
||||
void doAfter();
|
||||
|
||||
QWidget* m_parent;
|
||||
QWidget* m_overlay;
|
||||
QtOverlay* m_overlay;
|
||||
|
||||
QTimer* m_timer;
|
||||
|
||||
QtThreadedFunctor<void> m_beforeFunctor;
|
||||
QtThreadedFunctor<void> m_afterFunctor;
|
||||
|
||||
Reference in New Issue
Block a user