File indexing completed on 2024-05-12 04:19:40

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2011 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "loadingindicator.h"
0023 
0024 // Local
0025 #include "gwenview_lib_debug.h"
0026 
0027 // KF
0028 #include <KIconLoader>
0029 #include <KPixmapSequence>
0030 
0031 #include <KPixmapSequenceLoader>
0032 
0033 // Qt
0034 #include <QPainter>
0035 #include <QTimer>
0036 
0037 namespace Gwenview
0038 {
0039 struct LoadingIndicatorPrivate {
0040     LoadingIndicator *q = nullptr;
0041     KPixmapSequence mSequence;
0042     int mIndex;
0043     QTimer *const mTimer;
0044 
0045     LoadingIndicatorPrivate(LoadingIndicator *qq)
0046         : q(qq)
0047         , mSequence(KPixmapSequenceLoader::load(QStringLiteral("process-working"), 22))
0048         , mIndex(0)
0049         , mTimer(new QTimer(qq))
0050     {
0051         mTimer->setInterval(100);
0052         QObject::connect(mTimer, SIGNAL(timeout()), q, SLOT(showNextFrame()));
0053     }
0054 };
0055 
0056 LoadingIndicator::LoadingIndicator(QGraphicsItem *parent)
0057     : QGraphicsWidget(parent)
0058     , d(new LoadingIndicatorPrivate(this))
0059 {
0060 }
0061 
0062 LoadingIndicator::~LoadingIndicator()
0063 {
0064     delete d;
0065 }
0066 
0067 QRectF LoadingIndicator::boundingRect() const
0068 {
0069     return QRectF(QPointF(0, 0), d->mSequence.frameSize());
0070 }
0071 
0072 void LoadingIndicator::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
0073 {
0074     painter->drawPixmap(0, 0, d->mSequence.frameAt(d->mIndex));
0075 }
0076 
0077 void LoadingIndicator::showNextFrame()
0078 {
0079     if (d->mSequence.frameCount() > 0) {
0080         d->mIndex = (d->mIndex + 1) % d->mSequence.frameCount();
0081         update();
0082     }
0083 }
0084 
0085 QVariant LoadingIndicator::itemChange(GraphicsItemChange change, const QVariant &value)
0086 {
0087     if (change == QGraphicsItem::ItemVisibleHasChanged) {
0088         if (value.toBool()) {
0089             d->mTimer->start();
0090         } else {
0091             d->mTimer->stop();
0092         }
0093     }
0094     return QGraphicsWidget::itemChange(change, value);
0095 }
0096 
0097 } // namespace
0098 
0099 #include "moc_loadingindicator.cpp"