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 
52 QT_BEGIN_NAMESPACE
53 
54 
55 class QImageWriter;
56 class QImageReader;
57 class QColor;
58 class QVariant;
59 class QPlatformPixmap;
60 
61 class Q_GUI_EXPORT QPixmap : public QPaintDevice
62 {
63 public:
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     inline QPixmap &operator=(QPixmap &&other) noexcept
77     { qSwap(data, other.data); return *this; }
swap(QPixmap & other)78     inline void swap(QPixmap &other) noexcept
79     { qSwap(data, other.data); }
80 
81     operator QVariant() const;
82 
83     bool isNull() const;
84     int devType() const override;
85 
86     int width() const;
87     int height() const;
88     QSize size() const;
89     QRect rect() const;
90     int depth() const;
91 
92     static int defaultDepth();
93 
94     void fill(const QColor &fillColor = Qt::white);
95 #if QT_DEPRECATED_SINCE(5, 13)
96     QT_DEPRECATED_X("Use QPainter or fill(QColor)")
97     void fill(const QPaintDevice *device, const QPoint &ofs);
98     QT_DEPRECATED_X("Use QPainter or fill(QColor)")
99     void fill(const QPaintDevice *device, int xofs, int yofs);
100 #endif
101 
102     QBitmap mask() const;
103     void setMask(const QBitmap &);
104 
105     qreal devicePixelRatio() const;
106     void setDevicePixelRatio(qreal scaleFactor);
107 
108     bool hasAlpha() const;
109     bool hasAlphaChannel() const;
110 
111 #ifndef QT_NO_IMAGE_HEURISTIC_MASK
112     QBitmap createHeuristicMask(bool clipTight = true) const;
113 #endif
114     QBitmap createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode = Qt::MaskInColor) const;
115 
116 #if QT_DEPRECATED_SINCE(5, 13)
117     QT_DEPRECATED_X("Use QScreen::grabWindow() instead")
118     static QPixmap grabWindow(WId, int x = 0, int y = 0, int w = -1, int h = -1);
119     QT_DEPRECATED_X("Use QWidget::grab() instead")
120     static QPixmap grabWidget(QObject *widget, const QRect &rect);
121     QT_DEPRECATED_X("Use QWidget::grab() instead")
122     static QPixmap grabWidget(QObject *widget, int x = 0, int y = 0, int w = -1, int h = -1);
123 #endif
124 
125     inline QPixmap scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
126                           Qt::TransformationMode mode = Qt::FastTransformation) const
127         { return scaled(QSize(w, h), aspectMode, mode); }
128     QPixmap scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
129                    Qt::TransformationMode mode = Qt::FastTransformation) const;
130     QPixmap scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
131     QPixmap scaledToHeight(int h, Qt::TransformationMode mode = Qt::FastTransformation) const;
132 #if QT_DEPRECATED_SINCE(5, 15)
133     QT_DEPRECATED_X("Use transformed(const QTransform &, Qt::TransformationMode mode)")
134     QPixmap transformed(const QMatrix &, Qt::TransformationMode mode = Qt::FastTransformation) const;
135     QT_DEPRECATED_X("Use trueMatrix(const QTransform &m, int w, int h)")
136     static QMatrix trueMatrix(const QMatrix &m, int w, int h);
137 #endif // QT_DEPRECATED_SINCE(5, 15)
138     QPixmap transformed(const QTransform &, Qt::TransformationMode mode = Qt::FastTransformation) const;
139     static QTransform trueMatrix(const QTransform &m, int w, int h);
140 
141     QImage toImage() const;
142     static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor);
143     static QPixmap fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags = Qt::AutoColor);
144     static QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags = Qt::AutoColor)
145     {
146         return fromImageInPlace(image, flags);
147     }
148 
149     bool load(const QString& fileName, const char *format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor);
150     bool loadFromData(const uchar *buf, uint len, const char* format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor);
151     inline bool loadFromData(const QByteArray &data, const char* format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor);
152     bool save(const QString& fileName, const char* format = nullptr, int quality = -1) const;
153     bool save(QIODevice* device, const char* format = nullptr, int quality = -1) const;
154 
155     bool convertFromImage(const QImage &img, Qt::ImageConversionFlags flags = Qt::AutoColor);
156 
157     inline QPixmap copy(int x, int y, int width, int height) const;
158     QPixmap copy(const QRect &rect = QRect()) const;
159 
160     inline void scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed = nullptr);
161     void scroll(int dx, int dy, const QRect &rect, QRegion *exposed = nullptr);
162 
163 #if QT_DEPRECATED_SINCE(5, 0)
serialNumber()164     QT_DEPRECATED inline int serialNumber() const { return cacheKey() >> 32; }
165 #endif
166     qint64 cacheKey() const;
167 
168     bool isDetached() const;
169     void detach();
170 
171     bool isQBitmap() const;
172 
173     QPaintEngine *paintEngine() const override;
174 
175     inline bool operator!() const { return isNull(); }
176 
177 #if QT_DEPRECATED_SINCE(5, 0)
178     QT_DEPRECATED inline QPixmap alphaChannel() const;
179     QT_DEPRECATED inline void setAlphaChannel(const QPixmap &);
180 #endif
181 
182 protected:
183     int metric(PaintDeviceMetric) const override;
184     static QPixmap fromImageInPlace(QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor);
185 
186 private:
187     QExplicitlySharedDataPointer<QPlatformPixmap> data;
188 
189     bool doImageIO(QImageWriter *io, int quality) const;
190 
191     QPixmap(const QSize &s, int type);
192     void doInit(int, int, int);
193     Q_DUMMY_COMPARISON_OPERATOR(QPixmap)
194     friend class QPlatformPixmap;
195     friend class QBitmap;
196     friend class QPaintDevice;
197     friend class QPainter;
198     friend class QOpenGLWidget;
199     friend class QWidgetPrivate;
200     friend class QRasterBuffer;
201 #if !defined(QT_NO_DATASTREAM)
202     friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPixmap &);
203 #endif
204 
205 public:
206     QPlatformPixmap* handle() const;
207 
208 public:
209     typedef QExplicitlySharedDataPointer<QPlatformPixmap> DataPtr;
data_ptr()210     inline DataPtr &data_ptr() { return data; }
211 };
212 
Q_DECLARE_SHARED(QPixmap)213 Q_DECLARE_SHARED(QPixmap)
214 
215 inline QPixmap QPixmap::copy(int ax, int ay, int awidth, int aheight) const
216 {
217     return copy(QRect(ax, ay, awidth, aheight));
218 }
219 
scroll(int dx,int dy,int ax,int ay,int awidth,int aheight,QRegion * exposed)220 inline void QPixmap::scroll(int dx, int dy, int ax, int ay, int awidth, int aheight, QRegion *exposed)
221 {
222     scroll(dx, dy, QRect(ax, ay, awidth, aheight), exposed);
223 }
224 
loadFromData(const QByteArray & buf,const char * format,Qt::ImageConversionFlags flags)225 inline bool QPixmap::loadFromData(const QByteArray &buf, const char *format,
226                                   Qt::ImageConversionFlags flags)
227 {
228     return loadFromData(reinterpret_cast<const uchar *>(buf.constData()), buf.size(), format, flags);
229 }
230 
231 #if QT_DEPRECATED_SINCE(5, 0)
alphaChannel()232 inline QPixmap QPixmap::alphaChannel() const
233 {
234     QT_WARNING_PUSH
235     QT_WARNING_DISABLE_DEPRECATED
236     return QPixmap::fromImage(toImage().alphaChannel());
237     QT_WARNING_POP
238 }
239 
setAlphaChannel(const QPixmap & p)240 inline void QPixmap::setAlphaChannel(const QPixmap &p)
241 {
242     QImage image = toImage();
243     image.setAlphaChannel(p.toImage());
244     *this = QPixmap::fromImage(image);
245 
246 }
247 #endif
248 
249 /*****************************************************************************
250  QPixmap stream functions
251 *****************************************************************************/
252 
253 #if !defined(QT_NO_DATASTREAM)
254 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPixmap &);
255 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPixmap &);
256 #endif
257 
258 #ifndef QT_NO_DEBUG_STREAM
259 Q_GUI_EXPORT QDebug operator<<(QDebug, const QPixmap &);
260 #endif
261 
262 QT_END_NAMESPACE
263 
264 #endif // QPIXMAP_H
265