KWidgetsAddons

kbusyindicatorwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Harald Sitter <sitter@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "kbusyindicatorwidget.h"
8
9#include <QApplication>
10#include <QIcon>
11#include <QPainter>
12#include <QResizeEvent>
13#include <QStyle>
14#include <QVariantAnimation>
15
16class KBusyIndicatorWidgetPrivate
17{
18public:
19 KBusyIndicatorWidgetPrivate(KBusyIndicatorWidget *parent)
20 : q(parent)
21 {
22 animation.setLoopCount(-1);
23 animation.setDuration(2000);
24 animation.setStartValue(0);
25 animation.setEndValue(360);
26 QObject::connect(&animation, &QVariantAnimation::valueChanged, q, [this](QVariant value) {
27 rotation = value.toReal();
28 q->update(); // repaint new rotation
29 });
30 }
31
32 KBusyIndicatorWidget *const q;
33 QVariantAnimation animation;
34 QIcon icon = QIcon::fromTheme(QStringLiteral("view-refresh"));
35 qreal rotation = 0;
36 QPointF paintCenter;
37};
38
40 : QWidget(parent)
41 , d(new KBusyIndicatorWidgetPrivate(this))
42{
43}
44
46
48{
50 return QSize(extent, extent);
51}
52
54{
55 d->animation.start();
56}
57
59{
60 if (d->animation.state() == QAbstractAnimation::Running) // avoid warning if never started yet
61 d->animation.pause();
62}
63
64void KBusyIndicatorWidget::showEvent(QShowEvent *event)
65{
66 QWidget::showEvent(event);
67 start();
68}
69
70void KBusyIndicatorWidget::hideEvent(QHideEvent *event)
71{
72 QWidget::hideEvent(event);
73 stop();
74}
75
76void KBusyIndicatorWidget::resizeEvent(QResizeEvent *event)
77{
79 d->paintCenter = QPointF(event->size().width() / 2.0, //
80 event->size().height() / 2.0);
81}
82
83void KBusyIndicatorWidget::paintEvent(QPaintEvent *)
84{
85 QPainter painter(this);
86 painter.setRenderHint(QPainter::SmoothPixmapTransform);
87
88 // Rotate around the center and then reset back to origin for icon painting.
89 painter.translate(d->paintCenter);
90 painter.rotate(d->rotation);
91 painter.translate(-d->paintCenter);
92
93 d->icon.paint(&painter, rect());
94}
95
96bool KBusyIndicatorWidget::event(QEvent *event)
97{
98 // Only overridden to be flexible WRT binary compatible in the future.
99 // Overriding later has potential to change the call going through
100 // the vtable or not.
101 return QWidget::event(event);
102}
103
104#include "moc_kbusyindicatorwidget.cpp"
Rotating spinning icon to indicate busyness.
~KBusyIndicatorWidget() override
Destroy the widget.
void start()
Start the spinning animation.
void stop()
Stop the spinning animation.
QSize minimumSizeHint() const override
Return the smallest reasonable size for the widget.
KBusyIndicatorWidget(QWidget *parent=nullptr)
Create a new KBusyIndicatorWidget widget.
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
void setLoopCount(int loopCount)
QStyle * style()
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
SmoothPixmapTransform
PM_SmallIconSize
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const const=0
qreal toReal(bool *ok) const const
void setDuration(int msecs)
void setEndValue(const QVariant &value)
void setStartValue(const QVariant &value)
void valueChanged(const QVariant &value)
virtual bool event(QEvent *event) override
virtual void hideEvent(QHideEvent *event)
virtual void resizeEvent(QResizeEvent *event)
virtual void showEvent(QShowEvent *event)
void update()
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 3 2025 11:46:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.