1 /*
2     SPDX-FileCopyrightText: 2008 Aurélien Gâteau <agateau@kde.org>
3     SPDX-FileCopyrightText: 2009 Sebastian Trueg <trueg@kde.org>
4 
5     SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7 
8 #include "kpixmapsequence.h"
9 
10 #include "loggingcategory.h"
11 
12 #include <QPixmap>
13 #include <QVector>
14 
15 class KPixmapSequencePrivate : public QSharedData
16 {
17 public:
18     QVector<QPixmap> mFrames;
19 
20     void loadSequence(const QPixmap &bigPixmap, const QSize &frameSize);
21 };
22 
loadSequence(const QPixmap & bigPixmap,const QSize & frameSize)23 void KPixmapSequencePrivate::loadSequence(const QPixmap &bigPixmap, const QSize &frameSize)
24 {
25     if (bigPixmap.isNull()) {
26         qCWarning(KWidgetsAddonsLog) << "Invalid pixmap specified.";
27         return;
28     }
29 
30     QSize size(frameSize);
31     if (!size.isValid()) {
32         size = QSize(bigPixmap.width(), bigPixmap.width());
33     }
34     if (bigPixmap.width() % size.width() || bigPixmap.height() % size.height()) {
35         qCWarning(KWidgetsAddonsLog) << "Invalid framesize.";
36         return;
37     }
38 
39     const int rowCount = bigPixmap.height() / size.height();
40     const int colCount = bigPixmap.width() / size.width();
41     mFrames.resize(rowCount * colCount);
42 
43     int pos = 0;
44     for (int row = 0; row < rowCount; ++row) {
45         for (int col = 0; col < colCount; ++col) {
46             QPixmap pix = bigPixmap.copy(col * size.width(), row * size.height(), size.width(), size.height());
47             mFrames[pos++] = pix;
48         }
49     }
50 }
51 
KPixmapSequence()52 KPixmapSequence::KPixmapSequence()
53     : d(new KPixmapSequencePrivate)
54 {
55 }
56 
KPixmapSequence(const KPixmapSequence & other)57 KPixmapSequence::KPixmapSequence(const KPixmapSequence &other)
58 {
59     d = other.d;
60 }
61 
KPixmapSequence(const QPixmap & bigPixmap,const QSize & frameSize)62 KPixmapSequence::KPixmapSequence(const QPixmap &bigPixmap, const QSize &frameSize)
63     : d(new KPixmapSequencePrivate)
64 {
65     d->loadSequence(bigPixmap, frameSize);
66 }
67 
KPixmapSequence(const QString & fullPath,int size)68 KPixmapSequence::KPixmapSequence(const QString &fullPath, int size)
69     : d(new KPixmapSequencePrivate)
70 {
71     d->loadSequence(QPixmap(fullPath), QSize(size, size));
72 }
73 
~KPixmapSequence()74 KPixmapSequence::~KPixmapSequence()
75 {
76 }
77 
operator =(const KPixmapSequence & other)78 KPixmapSequence &KPixmapSequence::operator=(const KPixmapSequence &other)
79 {
80     d = other.d;
81     return *this;
82 }
83 
isValid() const84 bool KPixmapSequence::isValid() const
85 {
86     return !isEmpty();
87 }
88 
isEmpty() const89 bool KPixmapSequence::isEmpty() const
90 {
91     return d->mFrames.isEmpty();
92 }
93 
frameSize() const94 QSize KPixmapSequence::frameSize() const
95 {
96     if (isEmpty()) {
97         qCWarning(KWidgetsAddonsLog) << "No frame loaded";
98         return QSize();
99     }
100     return d->mFrames[0].size();
101 }
102 
frameCount() const103 int KPixmapSequence::frameCount() const
104 {
105     return d->mFrames.size();
106 }
107 
frameAt(int index) const108 QPixmap KPixmapSequence::frameAt(int index) const
109 {
110     if (isEmpty() || index > frameCount() - 1) {
111         qCWarning(KWidgetsAddonsLog) << "No frame loaded";
112         return QPixmap();
113     }
114     return d->mFrames.at(index);
115 }
116