Falkon Develop
Cross-platform Qt-based web browser
loadinganimator.cpp
Go to the documentation of this file.
1/* ============================================================
2* VerticalTabs plugin for Falkon
3* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "loadinganimator.h"
19
20#include "tabicon.h"
21#include "tabmodel.h"
22
23#include <QTimer>
24
25class LoadingAnimation : public QObject
26{
27public:
29 : QObject(animator)
30 , m_animator(animator)
31 {
32 auto *timer = new QTimer(this);
33 timer->setInterval(TabIcon::data()->animationInterval);
34 connect(timer, &QTimer::timeout, this, [this]() {
35 m_currentFrame = (m_currentFrame + 1) % TabIcon::data()->framesCount;
36 m_animator->updatePixmap(this);
37 });
38 timer->start();
39 }
40
41 QPixmap pixmap() const
42 {
43 const QPixmap p = TabIcon::data()->animationPixmap;
44 const int size = 16;
45 const int pixmapSize = qRound(size * p.devicePixelRatioF());
46 return p.copy(m_currentFrame * pixmapSize, 0, pixmapSize, pixmapSize);
47 }
48
49private:
50 int m_currentFrame = 0;
51 LoadingAnimator *m_animator;
52};
53
55 : QObject(parent)
56{
57}
58
59QPixmap LoadingAnimator::pixmap(const QModelIndex &index)
60{
61 LoadingAnimation *animation = m_animations.value(index);
62 if (!animation) {
63 animation = new LoadingAnimation(this);
64 m_indexes[animation] = index;
65 m_animations[index] = animation;
66 }
67 return animation->pixmap();
68}
69
70void LoadingAnimator::updatePixmap(LoadingAnimation *animation)
71{
72 const QModelIndex index = m_indexes.value(animation);
73 if (!index.isValid() || !index.data(TabModel::LoadingRole).toBool()) {
74 animation->deleteLater();
75 m_indexes.remove(animation);
76 m_animations.remove(index);
77 } else {
78 Q_EMIT updateIndex(index);
79 }
80}
QPixmap pixmap() const
LoadingAnimation(LoadingAnimator *animator)
LoadingAnimator(QObject *parent=nullptr)
friend class LoadingAnimation
void updateIndex(const QModelIndex &index)
QPixmap pixmap(const QModelIndex &index)
static Data * data()
Definition: tabicon.cpp:99
@ LoadingRole
Definition: tabmodel.h:60
int framesCount
Definition: tabicon.h:36
QPixmap animationPixmap
Definition: tabicon.h:38