File indexing completed on 2024-05-19 04:59:20

0001 /* ============================================================
0002 * VerticalTabs plugin for Falkon
0003 * Copyright (C) 2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "loadinganimator.h"
0019 
0020 #include "tabicon.h"
0021 #include "tabmodel.h"
0022 
0023 #include <QTimer>
0024 
0025 class LoadingAnimation : public QObject
0026 {
0027 public:
0028     explicit LoadingAnimation(LoadingAnimator *animator)
0029         : QObject(animator)
0030         , m_animator(animator)
0031     {
0032         auto *timer = new QTimer(this);
0033         timer->setInterval(TabIcon::data()->animationInterval);
0034         connect(timer, &QTimer::timeout, this, [this]() {
0035             m_currentFrame = (m_currentFrame + 1) % TabIcon::data()->framesCount;
0036             m_animator->updatePixmap(this);
0037         });
0038         timer->start();
0039     }
0040 
0041     QPixmap pixmap() const
0042     {
0043         const QPixmap p = TabIcon::data()->animationPixmap;
0044         const int size = 16;
0045         const int pixmapSize = qRound(size * p.devicePixelRatioF());
0046         return p.copy(m_currentFrame * pixmapSize, 0, pixmapSize, pixmapSize);
0047     }
0048 
0049 private:
0050     int m_currentFrame = 0;
0051     LoadingAnimator *m_animator;
0052 };
0053 
0054 LoadingAnimator::LoadingAnimator(QObject *parent)
0055     : QObject(parent)
0056 {
0057 }
0058 
0059 QPixmap LoadingAnimator::pixmap(const QModelIndex &index)
0060 {
0061     LoadingAnimation *animation = m_animations.value(index);
0062     if (!animation) {
0063         animation = new LoadingAnimation(this);
0064         m_indexes[animation] = index;
0065         m_animations[index] = animation;
0066     }
0067     return animation->pixmap();
0068 }
0069 
0070 void LoadingAnimator::updatePixmap(LoadingAnimation *animation)
0071 {
0072     const QModelIndex index = m_indexes.value(animation);
0073     if (!index.isValid() || !index.data(TabModel::LoadingRole).toBool()) {
0074         animation->deleteLater();
0075         m_indexes.remove(animation);
0076         m_animations.remove(index);
0077     } else {
0078         Q_EMIT updateIndex(index);
0079     }
0080 }