File indexing completed on 2024-04-28 03:59:12

0001 /*
0002     SPDX-FileCopyrightText: 2008 Aurélien Gâteau <agateau@kde.org>
0003     SPDX-FileCopyrightText: 2009 Sebastian Trueg <trueg@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "kpixmapsequence.h"
0009 
0010 #include "loggingcategory.h"
0011 
0012 #include <QList>
0013 #include <QPixmap>
0014 
0015 class KPixmapSequencePrivate : public QSharedData
0016 {
0017 public:
0018     QList<QPixmap> mFrames;
0019 
0020     void loadSequence(const QPixmap &bigPixmap, const QSize &frameSize);
0021 };
0022 
0023 void KPixmapSequencePrivate::loadSequence(const QPixmap &bigPixmap, const QSize &frameSize)
0024 {
0025     if (bigPixmap.isNull()) {
0026         qCWarning(KWidgetsAddonsLog) << "Invalid pixmap specified.";
0027         return;
0028     }
0029 
0030     QSize size(frameSize);
0031     if (!size.isValid()) {
0032         size = QSize(bigPixmap.width(), bigPixmap.width());
0033     }
0034     if (bigPixmap.width() % size.width() || bigPixmap.height() % size.height()) {
0035         qCWarning(KWidgetsAddonsLog) << "Invalid framesize.";
0036         return;
0037     }
0038 
0039     const int rowCount = bigPixmap.height() / size.height();
0040     const int colCount = bigPixmap.width() / size.width();
0041     mFrames.resize(rowCount * colCount);
0042 
0043     int pos = 0;
0044     for (int row = 0; row < rowCount; ++row) {
0045         for (int col = 0; col < colCount; ++col) {
0046             QPixmap pix = bigPixmap.copy(col * size.width(), row * size.height(), size.width(), size.height());
0047             mFrames[pos++] = pix;
0048         }
0049     }
0050 }
0051 
0052 KPixmapSequence::KPixmapSequence()
0053     : d(new KPixmapSequencePrivate)
0054 {
0055 }
0056 
0057 KPixmapSequence::KPixmapSequence(const KPixmapSequence &other)
0058 {
0059     d = other.d;
0060 }
0061 
0062 KPixmapSequence::KPixmapSequence(const QPixmap &bigPixmap, const QSize &frameSize)
0063     : d(new KPixmapSequencePrivate)
0064 {
0065     d->loadSequence(bigPixmap, frameSize);
0066 }
0067 
0068 KPixmapSequence::KPixmapSequence(const QString &fullPath, int size)
0069     : d(new KPixmapSequencePrivate)
0070 {
0071     d->loadSequence(QPixmap(fullPath), QSize(size, size));
0072 }
0073 
0074 KPixmapSequence::~KPixmapSequence()
0075 {
0076 }
0077 
0078 KPixmapSequence &KPixmapSequence::operator=(const KPixmapSequence &other)
0079 {
0080     d = other.d;
0081     return *this;
0082 }
0083 
0084 bool KPixmapSequence::isValid() const
0085 {
0086     return !isEmpty();
0087 }
0088 
0089 bool KPixmapSequence::isEmpty() const
0090 {
0091     return d->mFrames.isEmpty();
0092 }
0093 
0094 QSize KPixmapSequence::frameSize() const
0095 {
0096     if (isEmpty()) {
0097         qCWarning(KWidgetsAddonsLog) << "No frame loaded";
0098         return QSize();
0099     }
0100     return d->mFrames[0].size();
0101 }
0102 
0103 int KPixmapSequence::frameCount() const
0104 {
0105     return d->mFrames.size();
0106 }
0107 
0108 QPixmap KPixmapSequence::frameAt(int index) const
0109 {
0110     if (isEmpty() || index > frameCount() - 1) {
0111         qCWarning(KWidgetsAddonsLog) << "No frame loaded";
0112         return QPixmap();
0113     }
0114     return d->mFrames.at(index);
0115 }