KWidgetsAddons

kpixmapsequence.cpp
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(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
53 : d(new KPixmapSequencePrivate)
54{
55}
56
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(QPixmap(fullPath), QSize(size, size));
72}
73
77
79{
80 d = other.d;
81 return *this;
82}
83
85{
86 return !isEmpty();
87}
88
90{
91 return d->mFrames.isEmpty();
92}
93
95{
96 if (isEmpty()) {
97 qCWarning(KWidgetsAddonsLog) << "No frame loaded";
98 return QSize();
99 }
100 return d->mFrames[0].size();
101}
102
104{
105 return d->mFrames.size();
106}
107
109{
110 if (isEmpty() || index > frameCount() - 1) {
111 qCWarning(KWidgetsAddonsLog) << "No frame loaded";
112 return QPixmap();
113 }
114 return d->mFrames.at(index);
115}
Loads and gives access to the frames of a typical multi-row pixmap as often used for spinners.
QSize frameSize() const
~KPixmapSequence()
Destructor.
KPixmapSequence()
Create an empty sequence.
KPixmapSequence & operator=(const KPixmapSequence &other)
Create a copy of other.
bool isValid() const
int frameCount() const
The number of frames in this sequence.
bool isEmpty() const
QPixmap frameAt(int index) const
Retrieve the frame at index.
void resize(qsizetype size)
QPixmap copy(const QRect &rectangle) const const
int height() const const
bool isNull() const const
int width() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.