diff --git a/src/kbusyindicatorwidget.cpp b/src/kbusyindicatorwidget.cpp new file mode 100644 index 0000000..5ad6e27 --- /dev/null +++ b/src/kbusyindicatorwidget.cpp @@ -0,0 +1,87 @@ +#include "kbusyindicatorwidget.h" + +#include +#include +#include +#include +#include +#include + +class KBusyIndicatorWidget::Private : public QObject +{ + Q_OBJECT + Q_PROPERTY(qreal rotation MEMBER rotation WRITE setRotation) +public: + Private(KBusyIndicatorWidget *parent) + : q(parent) + { + } + + void setRotation(qreal newRotation) + { + rotation = newRotation; + q->update(); // repaint new rotation + } + + KBusyIndicatorWidget *q = nullptr; + QIcon icon = QIcon::fromTheme(QStringLiteral("view-refresh")); + qreal rotation = -1; + bool running = false; + QPointF paintCenter; +}; + +KBusyIndicatorWidget::KBusyIndicatorWidget(QWidget *parent) + : QWidget(parent) + , d(new Private(this)) +{ + QPropertyAnimation *animation = new QPropertyAnimation(d, "rotation"); + animation->setLoopCount(-1); + animation->setDuration(1000); + animation->setStartValue(0); + animation->setEndValue(360); + animation->start(); +} + +KBusyIndicatorWidget::~KBusyIndicatorWidget() +{ + delete d; +} + +bool KBusyIndicatorWidget::running() const +{ + return d->running; +} + +void KBusyIndicatorWidget::setRunning(bool running) +{ + if (d->running == running) + return; + + d->running = running; + emit runningChanged(d->running); +} + +void KBusyIndicatorWidget::resizeEvent(QResizeEvent *event) +{ + QWidget::resizeEvent(event); + + d->paintCenter = QPointF(event->size().width() / 2.0, + event->size().height() / 2.0); +} + +void KBusyIndicatorWidget::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + painter.setRenderHint(QPainter::SmoothPixmapTransform); + qDebug() << d->icon.availableSizes(); + qDebug() << d->icon.actualSize(size()); + + // Rotate around the center and then reset back to 0,0 for icon painting. + painter.translate(d->paintCenter); + painter.rotate(d->rotation); + painter.translate(-d->paintCenter); + + d->icon.paint(&painter, 0, 0, width(), height()); +} + +#include "kbusyindicatorwidget.moc" diff --git a/src/kbusyindicatorwidget.h b/src/kbusyindicatorwidget.h new file mode 100644 index 0000000..6fa1b3e --- /dev/null +++ b/src/kbusyindicatorwidget.h @@ -0,0 +1,33 @@ +#ifndef KBUSYINDICATORWIDGET_H +#define KBUSYINDICATORWIDGET_H + +#include + +#include + +class KWIDGETSADDONS_EXPORT KBusyIndicatorWidget : public QWidget +{ + Q_OBJECT + Q_PROPERTY(bool running READ running WRITE setRunning NOTIFY runningChanged) +public: + explicit KBusyIndicatorWidget(QWidget *parent = nullptr); + virtual ~KBusyIndicatorWidget(); + + bool running() const; + +public Q_SLOTS: + void setRunning(bool running); + +Q_SIGNALS: + void runningChanged(bool running); + +protected: + virtual void resizeEvent(QResizeEvent *event) override; + virtual void paintEvent(QPaintEvent *) override; + +private: + class Private; + Private *d; +}; + +#endif // KBUSYINDICATORWIDGET_H