File indexing completed on 2025-02-16 13:11:36
0001 /* 0002 SPDX-FileCopyrightText: 2019 Harald Sitter <sitter@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "kbusyindicatorwidget.h" 0008 0009 #include <QApplication> 0010 #include <QIcon> 0011 #include <QPainter> 0012 #include <QResizeEvent> 0013 #include <QStyle> 0014 #include <QVariantAnimation> 0015 0016 class KBusyIndicatorWidgetPrivate 0017 { 0018 public: 0019 KBusyIndicatorWidgetPrivate(KBusyIndicatorWidget *parent) 0020 : q(parent) 0021 { 0022 animation.setLoopCount(-1); 0023 animation.setDuration(2000); 0024 animation.setStartValue(0); 0025 animation.setEndValue(360); 0026 QObject::connect(&animation, &QVariantAnimation::valueChanged, q, [=](QVariant value) { 0027 rotation = value.toReal(); 0028 q->update(); // repaint new rotation 0029 }); 0030 } 0031 0032 KBusyIndicatorWidget *const q; 0033 QVariantAnimation animation; 0034 QIcon icon = QIcon::fromTheme(QStringLiteral("view-refresh")); 0035 qreal rotation = 0; 0036 QPointF paintCenter; 0037 }; 0038 0039 KBusyIndicatorWidget::KBusyIndicatorWidget(QWidget *parent) 0040 : QWidget(parent) 0041 , d(new KBusyIndicatorWidgetPrivate(this)) 0042 { 0043 } 0044 0045 KBusyIndicatorWidget::~KBusyIndicatorWidget() = default; 0046 0047 QSize KBusyIndicatorWidget::minimumSizeHint() const 0048 { 0049 const auto extent = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize); 0050 return QSize(extent, extent); 0051 } 0052 0053 void KBusyIndicatorWidget::showEvent(QShowEvent *event) 0054 { 0055 QWidget::showEvent(event); 0056 d->animation.start(); 0057 } 0058 0059 void KBusyIndicatorWidget::hideEvent(QHideEvent *event) 0060 { 0061 QWidget::hideEvent(event); 0062 d->animation.pause(); 0063 } 0064 0065 void KBusyIndicatorWidget::resizeEvent(QResizeEvent *event) 0066 { 0067 QWidget::resizeEvent(event); 0068 d->paintCenter = QPointF(event->size().width() / 2.0, // 0069 event->size().height() / 2.0); 0070 } 0071 0072 void KBusyIndicatorWidget::paintEvent(QPaintEvent *) 0073 { 0074 QPainter painter(this); 0075 painter.setRenderHint(QPainter::SmoothPixmapTransform); 0076 0077 // Rotate around the center and then reset back to origin for icon painting. 0078 painter.translate(d->paintCenter); 0079 painter.rotate(d->rotation); 0080 painter.translate(-d->paintCenter); 0081 0082 d->icon.paint(&painter, rect()); 0083 } 0084 0085 bool KBusyIndicatorWidget::event(QEvent *event) 0086 { 0087 // Only overridden to be flexible WRT binary compatible in the future. 0088 // Overriding later has potential to change the call going through 0089 // the vtable or not. 0090 return QWidget::event(event); 0091 } 0092 0093 #include "moc_kbusyindicatorwidget.cpp"