1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QPIXMAP_H
43#define QPIXMAP_H
44
45#include <QtGui/qpaintdevice.h>
46#include <QtGui/qcolor.h>
47#include <QtCore/qnamespace.h>
48#include <QtCore/qstring.h> // char*->QString conversion
49#include <QtCore/qsharedpointer.h>
50#include <QtGui/qimage.h>
51#include <QtGui/qtransform.h>
52
53QT_BEGIN_HEADER
54
55#if defined(Q_OS_SYMBIAN)
56class CFbsBitmap;
57class RSgImage;
58#endif
59
60QT_BEGIN_NAMESPACE
61
62QT_MODULE(Gui)
63
64class QImageWriter;
65class QImageReader;
66class QColor;
67class QVariant;
68class QX11Info;
69class QPixmapData;
70
71class Q_GUI_EXPORT QPixmap : public QPaintDevice
72{
73public:
74 QPixmap();
75 explicit QPixmap(QPixmapData *data);
76 QPixmap(int w, int h);
77 QPixmap(const QSize &);
78 QPixmap(const QString& fileName, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
79#ifndef QT_NO_IMAGEFORMAT_XPM
80 QPixmap(const char * const xpm[]);
81#endif
82 QPixmap(const QPixmap &);
83 ~QPixmap();
84
85 QPixmap &operator=(const QPixmap &);
86#ifdef Q_COMPILER_RVALUE_REFS
87 inline QPixmap &operator=(QPixmap &&other)
88 { qSwap(data, other.data); return *this; }
89#endif
90 inline void swap(QPixmap &other) { qSwap(data, other.data); }
91
92 operator QVariant() const;
93
94 bool isNull() const; // ### Qt 5: make inline
95 int devType() const;
96
97 int width() const; // ### Qt 5: make inline
98 int height() const; // ### Qt 5: make inline
99 QSize size() const;
100 QRect rect() const;
101 int depth() const;
102
103 static int defaultDepth();
104
105 void fill(const QColor &fillColor = Qt::white);
106 void fill(const QWidget *widget, const QPoint &ofs);
107 inline void fill(const QWidget *widget, int xofs, int yofs) { fill(widget, QPoint(xofs, yofs)); }
108
109 QBitmap mask() const;
110 void setMask(const QBitmap &);
111
112#ifdef QT_DEPRECATED
113 QT_DEPRECATED QPixmap alphaChannel() const;
114 QT_DEPRECATED void setAlphaChannel(const QPixmap &);
115#endif
116
117 bool hasAlpha() const;
118 bool hasAlphaChannel() const;
119
120#ifndef QT_NO_IMAGE_HEURISTIC_MASK
121 QBitmap createHeuristicMask(bool clipTight = true) const;
122#endif
123 QBitmap createMaskFromColor(const QColor &maskColor) const; // ### Qt 5: remove
124 QBitmap createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode) const;
125
126 static QPixmap grabWindow(WId, int x=0, int y=0, int w=-1, int h=-1);
127 static QPixmap grabWidget(QWidget *widget, const QRect &rect);
128 static inline QPixmap grabWidget(QWidget *widget, int x=0, int y=0, int w=-1, int h=-1)
129 { return grabWidget(widget, QRect(x, y, w, h)); }
130
131
132 inline QPixmap scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
133 Qt::TransformationMode mode = Qt::FastTransformation) const
134 { return scaled(QSize(w, h), aspectMode, mode); }
135 QPixmap scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
136 Qt::TransformationMode mode = Qt::FastTransformation) const;
137 QPixmap scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
138 QPixmap scaledToHeight(int h, Qt::TransformationMode mode = Qt::FastTransformation) const;
139 QPixmap transformed(const QMatrix &, Qt::TransformationMode mode = Qt::FastTransformation) const;
140 static QMatrix trueMatrix(const QMatrix &m, int w, int h);
141 QPixmap transformed(const QTransform &, Qt::TransformationMode mode = Qt::FastTransformation) const;
142 static QTransform trueMatrix(const QTransform &m, int w, int h);
143
144 QImage toImage() const;
145 static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor);
146 static QPixmap fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags = Qt::AutoColor);
147
148 bool load(const QString& fileName, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
149 bool loadFromData(const uchar *buf, uint len, const char* format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
150 inline bool loadFromData(const QByteArray &data, const char* format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
151 bool save(const QString& fileName, const char* format = 0, int quality = -1) const;
152 bool save(QIODevice* device, const char* format = 0, int quality = -1) const;
153
154 bool convertFromImage(const QImage &img, Qt::ImageConversionFlags flags = Qt::AutoColor);
155
156#if defined(Q_WS_WIN)
157 enum HBitmapFormat {
158 NoAlpha,
159 PremultipliedAlpha,
160 Alpha
161 };
162
163 HBITMAP toWinHBITMAP(HBitmapFormat format = NoAlpha) const;
164 HICON toWinHICON() const;
165
166 static QPixmap fromWinHBITMAP(HBITMAP hbitmap, HBitmapFormat format = NoAlpha);
167 static QPixmap fromWinHICON(HICON hicon);
168#endif
169
170#if defined(Q_WS_MAC)
171 CGImageRef toMacCGImageRef() const;
172 static QPixmap fromMacCGImageRef(CGImageRef image);
173#endif
174
175#if defined(Q_OS_SYMBIAN)
176 CFbsBitmap *toSymbianCFbsBitmap() const;
177 static QPixmap fromSymbianCFbsBitmap(CFbsBitmap *bitmap);
178 RSgImage* toSymbianRSgImage() const;
179 static QPixmap fromSymbianRSgImage(RSgImage *sgImage);
180#endif
181
182 inline QPixmap copy(int x, int y, int width, int height) const;
183 QPixmap copy(const QRect &rect = QRect()) const;
184
185 inline void scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed = 0);
186 void scroll(int dx, int dy, const QRect &rect, QRegion *exposed = 0);
187
188#ifdef QT_DEPRECATED
189 QT_DEPRECATED int serialNumber() const;
190#endif
191 qint64 cacheKey() const;
192
193 bool isDetached() const;
194 void detach();
195
196 bool isQBitmap() const;
197
198#if defined(Q_WS_QWS)
199 const uchar *qwsBits() const;
200 int qwsBytesPerLine() const;
201 QRgb *clut() const;
202#ifdef QT_DEPRECATED
203 QT_DEPRECATED int numCols() const;
204#endif
205 int colorCount() const;
206#elif defined(Q_WS_MAC)
207 Qt::HANDLE macQDHandle() const;
208 Qt::HANDLE macQDAlphaHandle() const;
209 Qt::HANDLE macCGHandle() const;
210#elif defined(Q_WS_X11)
211 enum ShareMode { ImplicitlyShared, ExplicitlyShared };
212
213 static QPixmap fromX11Pixmap(Qt::HANDLE pixmap, ShareMode mode = ImplicitlyShared);
214 static int x11SetDefaultScreen(int screen);
215 void x11SetScreen(int screen);
216 const QX11Info &x11Info() const;
217 Qt::HANDLE x11PictureHandle() const;
218#endif
219
220#if defined(Q_WS_X11) || defined(Q_WS_QWS)
221 Qt::HANDLE handle() const;
222#endif
223
224 QPaintEngine *paintEngine() const;
225
226 inline bool operator!() const { return isNull(); }
227
228protected:
229 int metric(PaintDeviceMetric) const;
230
231#ifdef QT3_SUPPORT
232public:
233 enum ColorMode { Auto, Color, Mono };
234 QT3_SUPPORT_CONSTRUCTOR QPixmap(const QString& fileName, const char *format, ColorMode mode);
235 QT3_SUPPORT bool load(const QString& fileName, const char *format, ColorMode mode);
236 QT3_SUPPORT bool loadFromData(const uchar *buf, uint len, const char* format, ColorMode mode);
237 QT3_SUPPORT_CONSTRUCTOR QPixmap(const QImage& image);
238 QT3_SUPPORT QPixmap &operator=(const QImage &);
239 inline QT3_SUPPORT QImage convertToImage() const { return toImage(); }
240 QT3_SUPPORT bool convertFromImage(const QImage &, ColorMode mode);
241 inline QT3_SUPPORT operator QImage() const { return toImage(); }
242 inline QT3_SUPPORT QPixmap xForm(const QMatrix &matrix) const { return transformed(QTransform(matrix)); }
243 inline QT3_SUPPORT bool selfMask() const { return false; }
244private:
245 void resize_helper(const QSize &s);
246public:
247 inline QT3_SUPPORT void resize(const QSize &s) { resize_helper(s); }
248 inline QT3_SUPPORT void resize(int width, int height) { resize_helper(QSize(width, height)); }
249#endif
250
251private:
252 QExplicitlySharedDataPointer<QPixmapData> data;
253
254 bool doImageIO(QImageWriter *io, int quality) const;
255
256 // ### Qt5: remove the following three lines
257 enum Type { PixmapType, BitmapType }; // must match QPixmapData::PixelType
258 QPixmap(const QSize &s, Type);
259 void init(int, int, Type = PixmapType);
260
261 QPixmap(const QSize &s, int type);
262 void init(int, int, int);
263 void deref();
264#if defined(Q_WS_WIN)
265 void initAlphaPixmap(uchar *bytes, int length, struct tagBITMAPINFO *bmi);
266#endif
267 Q_DUMMY_COMPARISON_OPERATOR(QPixmap)
268#ifdef Q_WS_MAC
269 friend CGContextRef qt_mac_cg_context(const QPaintDevice*);
270 friend CGImageRef qt_mac_create_imagemask(const QPixmap&, const QRectF&);
271 friend IconRef qt_mac_create_iconref(const QPixmap&);
272 friend quint32 *qt_mac_pixmap_get_base(const QPixmap*);
273 friend int qt_mac_pixmap_get_bytes_per_line(const QPixmap*);
274#endif
275 friend class QPixmapData;
276 friend class QX11PixmapData;
277 friend class QMacPixmapData;
278 friend class QSymbianRasterPixmapData;
279 friend class QBitmap;
280 friend class QPaintDevice;
281 friend class QPainter;
282 friend class QGLWidget;
283 friend class QX11PaintEngine;
284 friend class QCoreGraphicsPaintEngine;
285 friend class QWidgetPrivate;
286 friend class QRasterBuffer;
287#if !defined(QT_NO_DATASTREAM)
288 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPixmap &);
289#endif
290 friend Q_GUI_EXPORT qint64 qt_pixmap_id(const QPixmap &pixmap);
291
292public:
293 QPixmapData* pixmapData() const;
294
295public:
296 typedef QExplicitlySharedDataPointer<QPixmapData> DataPtr;
297 inline DataPtr &data_ptr() { return data; }
298};
299
300Q_DECLARE_SHARED(QPixmap)
301
302inline QPixmap QPixmap::copy(int ax, int ay, int awidth, int aheight) const
303{
304 return copy(QRect(ax, ay, awidth, aheight));
305}
306
307inline void QPixmap::scroll(int dx, int dy, int ax, int ay, int awidth, int aheight, QRegion *exposed)
308{
309 scroll(dx, dy, QRect(ax, ay, awidth, aheight), exposed);
310}
311
312inline bool QPixmap::loadFromData(const QByteArray &buf, const char *format,
313 Qt::ImageConversionFlags flags)
314{
315 return loadFromData(reinterpret_cast<const uchar *>(buf.constData()), buf.size(), format, flags);
316}
317
318/*****************************************************************************
319 QPixmap stream functions
320*****************************************************************************/
321
322#if !defined(QT_NO_DATASTREAM)
323Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPixmap &);
324Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPixmap &);
325#endif
326
327/*****************************************************************************
328 QPixmap (and QImage) helper functions
329*****************************************************************************/
330#ifdef QT3_SUPPORT
331QT3_SUPPORT Q_GUI_EXPORT void copyBlt(QPixmap *dst, int dx, int dy, const QPixmap *src,
332 int sx=0, int sy=0, int sw=-1, int sh=-1);
333#endif // QT3_SUPPORT
334
335QT_END_NAMESPACE
336
337QT_END_HEADER
338
339#endif // QPIXMAP_H
340