qpixmap.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtGui module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QPIXMAP_H
00043 #define QPIXMAP_H
00044 
00045 #include <QtGui/qpaintdevice.h>
00046 #include <QtGui/qcolor.h>
00047 #include <QtCore/qnamespace.h>
00048 #include <QtCore/qstring.h> // char*->QString conversion
00049 #include <QtCore/qsharedpointer.h>
00050 #include <QtGui/qimage.h>
00051 #include <QtGui/qtransform.h>
00052 
00053 QT_BEGIN_HEADER
00054 
00055 #if defined(Q_OS_SYMBIAN)
00056 class CFbsBitmap;
00057 class RSgImage;
00058 #endif
00059 
00060 QT_BEGIN_NAMESPACE
00061 
00062 QT_MODULE(Gui)
00063 
00064 class QImageWriter;
00065 class QImageReader;
00066 class QColor;
00067 class QVariant;
00068 class QX11Info;
00069 class QPixmapData;
00070 
00071 class Q_GUI_EXPORT QPixmap : public QPaintDevice
00072 {
00073 public:
00074     QPixmap();
00075     explicit QPixmap(QPixmapData *data);
00076     QPixmap(int w, int h);
00077     QPixmap(const QSize &);
00078     QPixmap(const QString& fileName, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
00079 #ifndef QT_NO_IMAGEFORMAT_XPM
00080     QPixmap(const char * const xpm[]);
00081 #endif
00082     QPixmap(const QPixmap &);
00083     ~QPixmap();
00084 
00085     QPixmap &operator=(const QPixmap &);
00086     operator QVariant() const;
00087 
00088     bool isNull() const; // ### Qt 5: make inline
00089     int devType() const;
00090 
00091     int width() const; // ### Qt 5: make inline
00092     int height() const; // ### Qt 5: make inline
00093     QSize size() const;
00094     QRect rect() const;
00095     int depth() const;
00096 
00097     static int defaultDepth();
00098 
00099     void fill(const QColor &fillColor = Qt::white);
00100     void fill(const QWidget *widget, const QPoint &ofs);
00101     inline void fill(const QWidget *widget, int xofs, int yofs) { fill(widget, QPoint(xofs, yofs)); }
00102 
00103     QBitmap mask() const;
00104     void setMask(const QBitmap &);
00105 
00106     QPixmap alphaChannel() const;
00107     void setAlphaChannel(const QPixmap &);
00108 
00109     bool hasAlpha() const;
00110     bool hasAlphaChannel() const;
00111 
00112 #ifndef QT_NO_IMAGE_HEURISTIC_MASK
00113     QBitmap createHeuristicMask(bool clipTight = true) const;
00114 #endif
00115     QBitmap createMaskFromColor(const QColor &maskColor) const; // ### Qt 5: remove
00116     QBitmap createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode) const;
00117 
00118     static QPixmap grabWindow(WId, int x=0, int y=0, int w=-1, int h=-1);
00119     static QPixmap grabWidget(QWidget *widget, const QRect &rect);
00120     static inline QPixmap grabWidget(QWidget *widget, int x=0, int y=0, int w=-1, int h=-1)
00121     { return grabWidget(widget, QRect(x, y, w, h)); }
00122 
00123 
00124     inline QPixmap scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
00125                           Qt::TransformationMode mode = Qt::FastTransformation) const
00126         { return scaled(QSize(w, h), aspectMode, mode); }
00127     QPixmap scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
00128                    Qt::TransformationMode mode = Qt::FastTransformation) const;
00129     QPixmap scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
00130     QPixmap scaledToHeight(int h, Qt::TransformationMode mode = Qt::FastTransformation) const;
00131     QPixmap transformed(const QMatrix &, Qt::TransformationMode mode = Qt::FastTransformation) const;
00132     static QMatrix trueMatrix(const QMatrix &m, int w, int h);
00133     QPixmap transformed(const QTransform &, Qt::TransformationMode mode = Qt::FastTransformation) const;
00134     static QTransform trueMatrix(const QTransform &m, int w, int h);
00135 
00136     QImage toImage() const;
00137     static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor);
00138     static QPixmap fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags = Qt::AutoColor);
00139 
00140     bool load(const QString& fileName, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
00141     bool loadFromData(const uchar *buf, uint len, const char* format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
00142     inline bool loadFromData(const QByteArray &data, const char* format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
00143     bool save(const QString& fileName, const char* format = 0, int quality = -1) const;
00144     bool save(QIODevice* device, const char* format = 0, int quality = -1) const;
00145 
00146     bool convertFromImage(const QImage &img, Qt::ImageConversionFlags flags = Qt::AutoColor);
00147 
00148 #if defined(Q_WS_WIN)
00149     enum HBitmapFormat {
00150         NoAlpha,
00151         PremultipliedAlpha,
00152         Alpha
00153     };
00154 
00155     HBITMAP toWinHBITMAP(HBitmapFormat format = NoAlpha) const;
00156     HICON toWinHICON() const;
00157 
00158     static QPixmap fromWinHBITMAP(HBITMAP hbitmap, HBitmapFormat format = NoAlpha);
00159     static QPixmap fromWinHICON(HICON hicon);
00160 #endif
00161 
00162 #if defined(Q_WS_MAC)
00163     CGImageRef toMacCGImageRef() const;
00164     static QPixmap fromMacCGImageRef(CGImageRef image);
00165 #endif
00166 
00167 #if defined(Q_OS_SYMBIAN)
00168     CFbsBitmap *toSymbianCFbsBitmap() const;
00169     static QPixmap fromSymbianCFbsBitmap(CFbsBitmap *bitmap);
00170     RSgImage* toSymbianRSgImage() const;
00171     static QPixmap fromSymbianRSgImage(RSgImage *sgImage);
00172 #endif
00173 
00174     inline QPixmap copy(int x, int y, int width, int height) const;
00175     QPixmap copy(const QRect &rect = QRect()) const;
00176 
00177     inline void scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed = 0);
00178     void scroll(int dx, int dy, const QRect &rect, QRegion *exposed = 0);
00179 
00180     int serialNumber() const;
00181     qint64 cacheKey() const;
00182 
00183     bool isDetached() const;
00184     void detach();
00185 
00186     bool isQBitmap() const;
00187 
00188 #if defined(Q_WS_QWS)
00189     const uchar *qwsBits() const;
00190     int qwsBytesPerLine() const;
00191     QRgb *clut() const;
00192 #ifdef QT_DEPRECATED
00193     QT_DEPRECATED int numCols() const;
00194 #endif
00195     int colorCount() const;
00196 #elif defined(Q_WS_MAC)
00197     Qt::HANDLE macQDHandle() const;
00198     Qt::HANDLE macQDAlphaHandle() const;
00199     Qt::HANDLE macCGHandle() const;
00200 #elif defined(Q_WS_X11)
00201     enum ShareMode { ImplicitlyShared, ExplicitlyShared };
00202 
00203     static QPixmap fromX11Pixmap(Qt::HANDLE pixmap, ShareMode mode = ImplicitlyShared);
00204     static int x11SetDefaultScreen(int screen);
00205     void x11SetScreen(int screen);
00206     const QX11Info &x11Info() const;
00207     Qt::HANDLE x11PictureHandle() const;
00208 #endif
00209 
00210 #if defined(Q_WS_X11) || defined(Q_WS_QWS)
00211     Qt::HANDLE handle() const;
00212 #endif
00213 
00214     QPaintEngine *paintEngine() const;
00215 
00216     inline bool operator!() const { return isNull(); }
00217 
00218 protected:
00219     int metric(PaintDeviceMetric) const;
00220 
00221 #ifdef QT3_SUPPORT
00222 public:
00223     enum ColorMode { Auto, Color, Mono };
00224     QT3_SUPPORT_CONSTRUCTOR QPixmap(const QString& fileName, const char *format, ColorMode mode);
00225     QT3_SUPPORT bool load(const QString& fileName, const char *format, ColorMode mode);
00226     QT3_SUPPORT bool loadFromData(const uchar *buf, uint len, const char* format, ColorMode mode);
00227     QT3_SUPPORT_CONSTRUCTOR QPixmap(const QImage& image);
00228     QT3_SUPPORT QPixmap &operator=(const QImage &);
00229     inline QT3_SUPPORT QImage convertToImage() const { return toImage(); }
00230     QT3_SUPPORT bool convertFromImage(const QImage &, ColorMode mode);
00231     inline QT3_SUPPORT operator QImage() const { return toImage(); }
00232     inline QT3_SUPPORT QPixmap xForm(const QMatrix &matrix) const { return transformed(QTransform(matrix)); }
00233     inline QT3_SUPPORT bool selfMask() const { return false; }
00234 private:
00235     void resize_helper(const QSize &s);
00236 public:
00237     inline QT3_SUPPORT void resize(const QSize &s) { resize_helper(s); }
00238     inline QT3_SUPPORT void resize(int width, int height) { resize_helper(QSize(width, height)); }
00239 #endif
00240 
00241 private:
00242     QExplicitlySharedDataPointer<QPixmapData> data;
00243 
00244     bool doImageIO(QImageWriter *io, int quality) const;
00245 
00246     // ### Qt5: remove the following three lines
00247     enum Type { PixmapType, BitmapType }; // must match QPixmapData::PixelType
00248     QPixmap(const QSize &s, Type);
00249     void init(int, int, Type = PixmapType);
00250 
00251     QPixmap(const QSize &s, int type);
00252     void init(int, int, int);
00253     void deref();
00254 #if defined(Q_WS_WIN)
00255     void initAlphaPixmap(uchar *bytes, int length, struct tagBITMAPINFO *bmi);
00256 #endif
00257     Q_DUMMY_COMPARISON_OPERATOR(QPixmap)
00258 #ifdef Q_WS_MAC
00259     friend CGContextRef qt_mac_cg_context(const QPaintDevice*);
00260     friend CGImageRef qt_mac_create_imagemask(const QPixmap&, const QRectF&);
00261     friend IconRef qt_mac_create_iconref(const QPixmap&);
00262     friend quint32 *qt_mac_pixmap_get_base(const QPixmap*);
00263     friend int qt_mac_pixmap_get_bytes_per_line(const QPixmap*);
00264 #endif
00265     friend class QPixmapData;
00266     friend class QX11PixmapData;
00267     friend class QMacPixmapData;
00268     friend class QS60PixmapData;
00269     friend class QBitmap;
00270     friend class QPaintDevice;
00271     friend class QPainter;
00272     friend class QGLWidget;
00273     friend class QX11PaintEngine;
00274     friend class QCoreGraphicsPaintEngine;
00275     friend class QWidgetPrivate;
00276     friend class QRasterBuffer;
00277 #if !defined(QT_NO_DATASTREAM)
00278     friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPixmap &);
00279 #endif
00280     friend Q_GUI_EXPORT qint64 qt_pixmap_id(const QPixmap &pixmap);
00281 
00282 public:
00283     QPixmapData* pixmapData() const;
00284 
00285 public:
00286     typedef QExplicitlySharedDataPointer<QPixmapData> DataPtr;
00287     inline DataPtr &data_ptr() { return data; }
00288 };
00289 
00290 Q_DECLARE_SHARED(QPixmap)
00291 
00292 inline QPixmap QPixmap::copy(int ax, int ay, int awidth, int aheight) const
00293 {
00294     return copy(QRect(ax, ay, awidth, aheight));
00295 }
00296 
00297 inline void QPixmap::scroll(int dx, int dy, int ax, int ay, int awidth, int aheight, QRegion *exposed)
00298 {
00299     scroll(dx, dy, QRect(ax, ay, awidth, aheight), exposed);
00300 }
00301 
00302 inline bool QPixmap::loadFromData(const QByteArray &buf, const char *format,
00303                                   Qt::ImageConversionFlags flags)
00304 {
00305     return loadFromData(reinterpret_cast<const uchar *>(buf.constData()), buf.size(), format, flags);
00306 }
00307 
00308 /*****************************************************************************
00309  QPixmap stream functions
00310 *****************************************************************************/
00311 
00312 #if !defined(QT_NO_DATASTREAM)
00313 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPixmap &);
00314 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPixmap &);
00315 #endif
00316 
00317 /*****************************************************************************
00318  QPixmap (and QImage) helper functions
00319 *****************************************************************************/
00320 #ifdef QT3_SUPPORT
00321 QT3_SUPPORT Q_GUI_EXPORT void copyBlt(QPixmap *dst, int dx, int dy, const QPixmap *src,
00322                                     int sx=0, int sy=0, int sw=-1, int sh=-1);
00323 #endif // QT3_SUPPORT
00324 
00325 QT_END_NAMESPACE
00326 
00327 QT_END_HEADER
00328 
00329 #endif // QPIXMAP_H