source: trunk/src/gui/image/qpixmap.h@ 371

Last change on this file since 371 was 342, checked in by Dmitry A. Kuminov, 16 years ago

gui/image: Added QPixmap fromPmHBITMAP() to convert an OS/2 BITMAP to a QPixmap.

File size: 11.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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 <QtGui/qimage.h>
50#include <QtGui/qtransform.h>
51
52QT_BEGIN_HEADER
53
54QT_BEGIN_NAMESPACE
55
56QT_MODULE(Gui)
57
58class QImageWriter;
59class QColor;
60class QVariant;
61class QX11Info;
62class QIcon;
63
64class QPixmapData;
65
66class Q_GUI_EXPORT QPixmap : public QPaintDevice
67{
68public:
69 QPixmap();
70 explicit QPixmap(QPixmapData *data);
71 QPixmap(int w, int h);
72 QPixmap(const QSize &);
73 QPixmap(const QString& fileName, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
74#ifndef QT_NO_IMAGEFORMAT_XPM
75 QPixmap(const char * const xpm[]);
76#endif
77 QPixmap(const QPixmap &);
78 ~QPixmap();
79
80 QPixmap &operator=(const QPixmap &);
81 operator QVariant() const;
82
83 bool isNull() const;
84 int devType() const;
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 void fill(const QWidget *widget, const QPoint &ofs);
96 inline void fill(const QWidget *widget, int xofs, int yofs) { fill(widget, QPoint(xofs, yofs)); }
97
98 QBitmap mask() const;
99 void setMask(const QBitmap &);
100
101 QPixmap alphaChannel() const;
102 void setAlphaChannel(const QPixmap &);
103
104 bool hasAlpha() const;
105 bool hasAlphaChannel() const;
106
107#ifndef QT_NO_IMAGE_HEURISTIC_MASK
108 QBitmap createHeuristicMask(bool clipTight = true) const;
109#endif
110 QBitmap createMaskFromColor(const QColor &maskColor) const; // ### Qt 5: remove
111 QBitmap createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode) const;
112
113 static QPixmap grabWindow(WId, int x=0, int y=0, int w=-1, int h=-1);
114 static QPixmap grabWidget(QWidget *widget, const QRect &rect);
115 static inline QPixmap grabWidget(QWidget *widget, int x=0, int y=0, int w=-1, int h=-1)
116 { return grabWidget(widget, QRect(x, y, w, h)); }
117
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
134 bool load(const QString& fileName, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
135 bool loadFromData(const uchar *buf, uint len, const char* format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
136 inline bool loadFromData(const QByteArray &data, const char* format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
137 bool save(const QString& fileName, const char* format = 0, int quality = -1) const;
138 bool save(QIODevice* device, const char* format = 0, int quality = -1) const;
139
140#if defined(Q_WS_WIN)
141 enum HBitmapFormat {
142 NoAlpha,
143 PremultipliedAlpha,
144 Alpha
145 };
146
147 HBITMAP toWinHBITMAP(HBitmapFormat format = NoAlpha) const;
148 static QPixmap fromWinHBITMAP(HBITMAP hbitmap, HBitmapFormat format = NoAlpha);
149#endif
150
151#if defined(Q_WS_PM)
152 HBITMAP toPmHBITMAP(HBITMAP *mask = 0, bool embedRealAlpha = false) const;
153 static QPixmap fromPmHBITMAP(HBITMAP hbm);
154 static HPOINTER toPmHPOINTER(const QIcon &icon, bool isPointer = false,
155 int hotX = 0, int hotY = 0,
156 bool embedRealAlpha = false, bool isMini = true);
157#endif
158
159#if defined(Q_WS_MAC)
160 CGImageRef toMacCGImageRef() const;
161 static QPixmap fromMacCGImageRef(CGImageRef image);
162#endif
163
164 inline QPixmap copy(int x, int y, int width, int height) const;
165 QPixmap copy(const QRect &rect = QRect()) const;
166
167 int serialNumber() const;
168 qint64 cacheKey() const;
169
170 bool isDetached() const;
171 void detach();
172
173 bool isQBitmap() const;
174
175#if defined(Q_WS_QWS)
176 const uchar *qwsBits() const;
177 int qwsBytesPerLine() const;
178 QRgb *clut() const;
179 int numCols() const;
180#elif defined(Q_WS_MAC)
181 Qt::HANDLE macQDHandle() const;
182 Qt::HANDLE macQDAlphaHandle() const;
183 Qt::HANDLE macCGHandle() const;
184#elif defined(Q_WS_X11)
185 enum ShareMode { ImplicitlyShared, ExplicitlyShared };
186
187 static QPixmap fromX11Pixmap(Qt::HANDLE pixmap, ShareMode mode = ImplicitlyShared);
188 static int x11SetDefaultScreen(int screen);
189 void x11SetScreen(int screen);
190 const QX11Info &x11Info() const;
191 Qt::HANDLE x11PictureHandle() const;
192#endif
193
194#if defined(Q_WS_X11) || defined(Q_WS_QWS)
195 Qt::HANDLE handle() const;
196#endif
197
198 QPaintEngine *paintEngine() const;
199
200 inline bool operator!() const { return isNull(); }
201
202protected:
203 int metric(PaintDeviceMetric) const;
204
205#ifdef QT3_SUPPORT
206public:
207 enum ColorMode { Auto, Color, Mono };
208 QT3_SUPPORT_CONSTRUCTOR QPixmap(const QString& fileName, const char *format, ColorMode mode);
209 QT3_SUPPORT bool load(const QString& fileName, const char *format, ColorMode mode);
210 QT3_SUPPORT bool loadFromData(const uchar *buf, uint len, const char* format, ColorMode mode);
211 QT3_SUPPORT_CONSTRUCTOR QPixmap(const QImage& image);
212 QT3_SUPPORT QPixmap &operator=(const QImage &);
213 inline QT3_SUPPORT QImage convertToImage() const { return toImage(); }
214 QT3_SUPPORT bool convertFromImage(const QImage &, ColorMode mode);
215 QT3_SUPPORT bool convertFromImage(const QImage &img, Qt::ImageConversionFlags flags = Qt::AutoColor)
216 { (*this) = fromImage(img, flags); return !isNull(); }
217 inline QT3_SUPPORT operator QImage() const { return toImage(); }
218 inline QT3_SUPPORT QPixmap xForm(const QMatrix &matrix) const { return transformed(QTransform(matrix)); }
219 inline QT3_SUPPORT bool selfMask() const { return false; }
220private:
221 void resize_helper(const QSize &s);
222public:
223 inline QT3_SUPPORT void resize(const QSize &s) { resize_helper(s); }
224 inline QT3_SUPPORT void resize(int width, int height) { resize_helper(QSize(width, height)); }
225#endif
226
227private:
228 QPixmapData *data;
229
230 bool doImageIO(QImageWriter *io, int quality) const;
231
232 // ### Qt5: remove the following three lines
233 enum Type { PixmapType, BitmapType }; // must match QPixmapData::PixelType
234 QPixmap(const QSize &s, Type);
235 void init(int, int, Type = PixmapType);
236
237 QPixmap(const QSize &s, int type);
238 void init(int, int, int);
239 void deref();
240#if defined(Q_WS_WIN)
241 void initAlphaPixmap(uchar *bytes, int length, struct tagBITMAPINFO *bmi);
242#endif
243 Q_DUMMY_COMPARISON_OPERATOR(QPixmap)
244#ifdef Q_WS_MAC
245 friend CGContextRef qt_mac_cg_context(const QPaintDevice*);
246 friend CGImageRef qt_mac_create_imagemask(const QPixmap&, const QRectF&);
247 friend IconRef qt_mac_create_iconref(const QPixmap&);
248 friend quint32 *qt_mac_pixmap_get_base(const QPixmap*);
249 friend int qt_mac_pixmap_get_bytes_per_line(const QPixmap*);
250#endif
251 friend class QPixmapData;
252 friend class QX11PixmapData;
253 friend class QMacPixmapData;
254 friend class QBitmap;
255 friend class QPaintDevice;
256 friend class QPainter;
257 friend class QGLWidget;
258 friend class QX11PaintEngine;
259 friend class QCoreGraphicsPaintEngine;
260 friend class QWidgetPrivate;
261 friend class QRasterPaintEngine;
262 friend class QRasterBuffer;
263 friend class QDirect3DPaintEngine;
264 friend class QDirect3DPaintEnginePrivate;
265 friend class QDetachedPixmap;
266#if !defined(QT_NO_DATASTREAM)
267 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPixmap &);
268#endif
269 friend Q_GUI_EXPORT qint64 qt_pixmap_id(const QPixmap &pixmap);
270
271public:
272 QPixmapData* pixmapData() const;
273
274public:
275 typedef QPixmapData * DataPtr;
276 inline DataPtr &data_ptr() { return data; }
277};
278
279Q_DECLARE_SHARED(QPixmap)
280
281inline QPixmap QPixmap::copy(int ax, int ay, int awidth, int aheight) const
282{
283 return copy(QRect(ax, ay, awidth, aheight));
284}
285
286inline bool QPixmap::loadFromData(const QByteArray &buf, const char *format,
287 Qt::ImageConversionFlags flags)
288{
289 return loadFromData(reinterpret_cast<const uchar *>(buf.constData()), buf.size(), format, flags);
290}
291
292/*****************************************************************************
293 QPixmap stream functions
294*****************************************************************************/
295
296#if !defined(QT_NO_DATASTREAM)
297Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPixmap &);
298Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPixmap &);
299#endif
300
301/*****************************************************************************
302 QPixmap (and QImage) helper functions
303*****************************************************************************/
304#ifdef QT3_SUPPORT
305QT3_SUPPORT Q_GUI_EXPORT void copyBlt(QPixmap *dst, int dx, int dy, const QPixmap *src,
306 int sx=0, int sy=0, int sw=-1, int sh=-1);
307#endif // QT3_SUPPORT
308
309QT_END_NAMESPACE
310
311QT_END_HEADER
312
313#endif // QPIXMAP_H
Note: See TracBrowser for help on using the repository browser.