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 <QList>
13#include <QPixmap>
14
15class KPixmapSequencePrivate : public QSharedData
16{
17public:
18 QList<QPixmap> mFrames;
19
20 void loadSequence(const QPixmap &bigPixmap, const QSize &frameSize);
21};
22
23void 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(size: 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(ax: col * size.width(), ay: row * size.height(), awidth: size.width(), aheight: size.height());
47 mFrames[pos++] = pix;
48 }
49 }
50}
51
52KPixmapSequence::KPixmapSequence()
53 : d(new KPixmapSequencePrivate)
54{
55}
56
57KPixmapSequence::KPixmapSequence(const KPixmapSequence &other)
58{
59 d = other.d;
60}
61
62KPixmapSequence::KPixmapSequence(const QPixmap &bigPixmap, const QSize &frameSize)
63 : d(new KPixmapSequencePrivate)
64{
65 d->loadSequence(bigPixmap, frameSize);
66}
67
68KPixmapSequence::KPixmapSequence(const QString &fullPath, int size)
69 : d(new KPixmapSequencePrivate)
70{
71 d->loadSequence(bigPixmap: QPixmap(fullPath), frameSize: QSize(size, size));
72}
73
74KPixmapSequence::~KPixmapSequence()
75{
76}
77
78KPixmapSequence &KPixmapSequence::operator=(const KPixmapSequence &other)
79{
80 d = other.d;
81 return *this;
82}
83
84bool KPixmapSequence::isValid() const
85{
86 return !isEmpty();
87}
88
89bool KPixmapSequence::isEmpty() const
90{
91 return d->mFrames.isEmpty();
92}
93
94QSize 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
103int KPixmapSequence::frameCount() const
104{
105 return d->mFrames.size();
106}
107
108QPixmap 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(i: index);
115}
116

source code of kwidgetsaddons/src/kpixmapsequence.cpp