1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QPIXMAP_H
41#define QPIXMAP_H
42
43#include <QtGui/qtguiglobal.h>
44#include <QtGui/qpaintdevice.h>
45#include <QtGui/qcolor.h>
46#include <QtCore/qnamespace.h>
47#include <QtCore/qstring.h> // char*->QString conversion
48#include <QtCore/qsharedpointer.h>
49#include <QtGui/qimage.h>
50#include <QtGui/qtransform.h>
51
52QT_BEGIN_NAMESPACE
53
54
55class QImageWriter;
56class QImageReader;
57class QColor;
58class QVariant;
59class QPlatformPixmap;
60
61class Q_GUI_EXPORT QPixmap : public QPaintDevice
62{
63public:
64 QPixmap();
65 explicit QPixmap(QPlatformPixmap *data);
66 QPixmap(int w, int h);
67 explicit QPixmap(const QSize &);
68 QPixmap(const QString& fileName, const char *format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor);
69#ifndef QT_NO_IMAGEFORMAT_XPM
70 explicit QPixmap(const char * const xpm[]);
71#endif
72 QPixmap(const QPixmap &);
73 ~QPixmap();
74
75 QPixmap &operator=(const QPixmap &);
76#ifdef Q_COMPILER_RVALUE_REFS
77 inline QPixmap &operator=(QPixmap &&other) Q_DECL_NOEXCEPT
78 { qSwap(data, other.data); return *this; }
79#endif
80 inline void swap(QPixmap &other) Q_DECL_NOEXCEPT
81 { qSwap(data, other.data); }
82
83 operator QVariant() const;
84
85 bool isNull() const;
86 int devType() const override;
87
88 int width() const;
89 int height() const;
90 QSize size() const;
91 QRect rect() const;
92 int depth() const;
93
94 static int defaultDepth();
95
96 void fill(const QColor &fillColor = Qt::white);
97 void fill(const QPaintDevice *device, const QPoint &ofs);
98 inline void fill(const QPaintDevice *device, int xofs, int yofs) { fill(device, QPoint(xofs, yofs)); }
99
100 QBitmap mask() const;
101 void setMask(const QBitmap &);
102
103 qreal devicePixelRatio() const;
104 void setDevicePixelRatio(qreal scaleFactor);
105
106 bool hasAlpha() const;
107 bool hasAlphaChannel() const;
108
109#ifndef QT_NO_IMAGE_HEURISTIC_MASK
110 QBitmap createHeuristicMask(bool clipTight = true) const;
111#endif
112 QBitmap createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode = Qt::MaskInColor) const;
113
114 static QPixmap grabWindow(WId, int x=0, int y=0, int w=-1, int h=-1);
115 static QPixmap grabWidget(QObject *widget, const QRect &rect);
116 static inline QPixmap grabWidget(QObject *widget, int x=0, int y=0, int w=-1, int h=-1)
117 { return grabWidget(widget, QRect(x, y, w, h)); }
118
119 inline QPixmap scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
120 Qt::TransformationMode mode = Qt::FastTransformation) const
121 { return scaled(QSize(w, h), aspectMode, mode); }
122 QPixmap scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
123 Qt::TransformationMode mode = Qt::FastTransformation) const;
124 QPixmap scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
125 QPixmap scaledToHeight(int h, Qt::TransformationMode mode = Qt::FastTransformation) const;
126 QPixmap transformed(const QMatrix &, Qt::TransformationMode mode = Qt::FastTransformation) const;
127 static QMatrix trueMatrix(const QMatrix &m, int w, int h);
128 QPixmap transformed(const QTransform &, Qt::TransformationMode mode = Qt::FastTransformation) const;
129 static QTransform trueMatrix(const QTransform &m, int w, int h);
130
131 QImage toImage() const;
132 static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor);
133 static QPixmap fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags = Qt::AutoColor);
134#ifdef Q_COMPILER_RVALUE_REFS
135 static QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags = Qt::AutoColor)
136 {
137 return fromImageInPlace(image, flags);
138 }
139#endif
140
141 bool load(const QString& fileName, const char *format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor);
142 bool loadFromData(const uchar *buf, uint len, const char* format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor);
143 inline bool loadFromData(const QByteArray &data, const char* format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor);
144 bool save(const QString& fileName, const char* format = nullptr, int quality = -1) const;
145 bool save(QIODevice* device, const char* format = nullptr, int quality = -1) const;
146
147 bool convertFromImage(const QImage &img, Qt::ImageConversionFlags flags = Qt::AutoColor);
148
149 inline QPixmap copy(int x, int y, int width, int height) const;
150 QPixmap copy(const QRect &rect = QRect()) const;
151
152 inline void scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed = nullptr);
153 void scroll(int dx, int dy, const QRect &rect, QRegion *exposed = nullptr);
154
155#if QT_DEPRECATED_SINCE(5, 0)
156 QT_DEPRECATED inline int serialNumber() const { return cacheKey() >> 32; }
157#endif
158 qint64 cacheKey() const;
159
160 bool isDetached() const;
161 void detach();
162
163 bool isQBitmap() const;
164
165 QPaintEngine *paintEngine() const override;
166
167 inline bool operator!() const { return isNull(); }
168
169#if QT_DEPRECATED_SINCE(5, 0)
170 QT_DEPRECATED inline QPixmap alphaChannel() const;
171 QT_DEPRECATED inline void setAlphaChannel(const QPixmap &);
172#endif
173
174protected:
175 int metric(PaintDeviceMetric) const override;
176 static QPixmap fromImageInPlace(QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor);
177
178private:
179 QExplicitlySharedDataPointer<QPlatformPixmap> data;
180
181 bool doImageIO(QImageWriter *io, int quality) const;
182
183 QPixmap(const QSize &s, int type);
184 void doInit(int, int, int);
185 Q_DUMMY_COMPARISON_OPERATOR(QPixmap)
186 friend class QPlatformPixmap;
187 friend class QBitmap;
188 friend class QPaintDevice;
189 friend class QPainter;
190 friend class QOpenGLWidget;
191 friend class QWidgetPrivate;
192 friend class QRasterBuffer;
193#if !defined(QT_NO_DATASTREAM)
194 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPixmap &);
195#endif
196
197public:
198 QPlatformPixmap* handle() const;
199
200public:
201 typedef QExplicitlySharedDataPointer<QPlatformPixmap> DataPtr;
202 inline DataPtr &data_ptr() { return data; }
203};
204
205Q_DECLARE_SHARED(QPixmap)
206
207inline QPixmap QPixmap::copy(int ax, int ay, int awidth, int aheight) const
208{
209 return copy(QRect(ax, ay, awidth, aheight));
210}
211
212inline void QPixmap::scroll(int dx, int dy, int ax, int ay, int awidth, int aheight, QRegion *exposed)
213{
214 scroll(dx, dy, QRect(ax, ay, awidth, aheight), exposed);
215}
216
217inline bool QPixmap::loadFromData(const QByteArray &buf, const char *format,
218 Qt::ImageConversionFlags flags)
219{
220 return loadFromData(reinterpret_cast<const uchar *>(buf.constData()), buf.size(), format, flags);
221}
222
223#if QT_DEPRECATED_SINCE(5, 0)
224inline QPixmap QPixmap::alphaChannel() const
225{
226 return QPixmap::fromImage(toImage().alphaChannel());
227}
228
229inline void QPixmap::setAlphaChannel(const QPixmap &p)
230{
231 QImage image = toImage();
232 image.setAlphaChannel(p.toImage());
233 *this = QPixmap::fromImage(image);
234
235}
236#endif
237
238/*****************************************************************************
239 QPixmap stream functions
240*****************************************************************************/
241
242#if !defined(QT_NO_DATASTREAM)
243Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPixmap &);
244Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPixmap &);
245#endif
246
247#ifndef QT_NO_DEBUG_STREAM
248Q_GUI_EXPORT QDebug operator<<(QDebug, const QPixmap &);
249#endif
250
251QT_END_NAMESPACE
252
253#endif // QPIXMAP_H
254