File indexing completed on 2024-12-29 04:54:52

0001 /*
0002   SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-only
0005 */
0006 
0007 #include "sievetreewidgetitem.h"
0008 
0009 #include <KIconLoader>
0010 #include <KPixmapSequenceLoader>
0011 
0012 #include <QTimer>
0013 
0014 using namespace KSieveUi;
0015 SieveTreeWidgetProgress::SieveTreeWidgetProgress(SieveTreeWidgetItem *item, QObject *parent)
0016     : QObject(parent)
0017     , mItem(item)
0018 {
0019     KIconLoader loader;
0020     mProgressPix = KPixmapSequenceLoader::load(QStringLiteral("process-working"), KIconLoader::SizeSmallMedium);
0021     mProgressTimer = new QTimer(this);
0022     connect(mProgressTimer, &QTimer::timeout, this, &SieveTreeWidgetProgress::slotTimerDone);
0023 }
0024 
0025 SieveTreeWidgetProgress::~SieveTreeWidgetProgress() = default;
0026 
0027 void SieveTreeWidgetProgress::slotTimerDone()
0028 {
0029     mItem->setProgressAnimation(mProgressPix.frameAt(mProgressCount));
0030     ++mProgressCount;
0031     if (mProgressCount == 8) {
0032         mProgressCount = 0;
0033     }
0034 
0035     mProgressTimer->start(300);
0036 }
0037 
0038 void SieveTreeWidgetProgress::startAnimation()
0039 {
0040     mProgressCount = 0;
0041     mProgressTimer->start(300);
0042 }
0043 
0044 void SieveTreeWidgetProgress::stopAnimation()
0045 {
0046     if (mProgressTimer->isActive()) {
0047         mProgressTimer->stop();
0048     }
0049     mItem->setDefaultIcon();
0050 }
0051 
0052 class KSieveUi::SieveTreeWidgetItemPrivate
0053 {
0054 public:
0055     SieveTreeWidgetItemPrivate() = default;
0056 
0057     ~SieveTreeWidgetItemPrivate()
0058     {
0059         delete mProgress;
0060     }
0061 
0062     SieveTreeWidgetProgress *mProgress = nullptr;
0063 };
0064 
0065 SieveTreeWidgetItem::SieveTreeWidgetItem(QTreeWidget *treeWidget, QTreeWidgetItem *item)
0066     : QTreeWidgetItem(treeWidget, item)
0067     , d(new KSieveUi::SieveTreeWidgetItemPrivate)
0068 {
0069     d->mProgress = new SieveTreeWidgetProgress(this);
0070 }
0071 
0072 SieveTreeWidgetItem::~SieveTreeWidgetItem() = default;
0073 
0074 void SieveTreeWidgetItem::startAnimation()
0075 {
0076     d->mProgress->startAnimation();
0077 }
0078 
0079 void SieveTreeWidgetItem::stopAnimation()
0080 {
0081     d->mProgress->stopAnimation();
0082 }
0083 
0084 void SieveTreeWidgetItem::setProgressAnimation(const QPixmap &pix)
0085 {
0086     setIcon(0, QIcon(pix));
0087 }
0088 
0089 void SieveTreeWidgetItem::setDefaultIcon()
0090 {
0091     setIcon(0, QIcon::fromTheme(QStringLiteral("network-server")));
0092 }
0093 
0094 #include "moc_sievetreewidgetitem.cpp"