Qt 4.8
qpixmap.cpp
Go to the documentation of this file.
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 #include <qglobal.h>
43 
44 #include "qpixmap.h"
45 #include "qpixmapdata_p.h"
47 
48 #include "qbitmap.h"
49 #include "qcolormap.h"
50 #include "qimage.h"
51 #include "qwidget.h"
52 #include "qpainter.h"
53 #include "qdatastream.h"
54 #include "qbuffer.h"
55 #include "qapplication.h"
56 #include <private/qapplication_p.h>
57 #include <private/qgraphicssystem_p.h>
58 #include <private/qwidget_p.h>
59 #include "qevent.h"
60 #include "qfile.h"
61 #include "qfileinfo.h"
62 #include "qpixmapcache.h"
63 #include "qdatetime.h"
64 #include "qimagereader.h"
65 #include "qimagewriter.h"
66 #include "qpaintengine.h"
67 #include "qthread.h"
68 
69 #ifdef Q_WS_MAC
70 # include "private/qt_mac_p.h"
71 # include "private/qpixmap_mac_p.h"
72 #endif
73 
74 #ifdef Q_WS_QPA
76 #endif
77 
78 #if defined(Q_WS_X11)
79 # include "qx11info_x11.h"
80 # include <private/qt_x11_p.h>
81 # include <private/qpixmap_x11_p.h>
82 #endif
83 
84 #if defined(Q_OS_SYMBIAN)
85 # include <private/qt_s60_p.h>
86 #endif
87 
88 #include "qpixmap_raster_p.h"
89 #include "private/qstylehelper_p.h"
90 
92 
93 // ### Qt 5: remove
95 {
96  return pixmap.cacheKey();
97 }
98 
99 static bool qt_pixmap_thread_test()
100 {
101  if (!qApp) {
102  qFatal("QPixmap: Must construct a QApplication before a QPaintDevice");
103  return false;
104  }
105 
106  if (qApp->thread() != QThread::currentThread()) {
107  bool fail = false;
108 #if defined (Q_WS_X11)
110  fail = true;
111 #elif defined (Q_WS_QPA)
112  if (!QApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedPixmaps)) {
113  printf("Lighthouse plugin does not support threaded pixmaps!\n");
114  fail = true;
115  }
116 #else
118  fail = true;
119 #endif
120  if (fail) {
121  qWarning("QPixmap: It is not safe to use pixmaps outside the GUI thread");
122  return false;
123  }
124  }
125  return true;
126 }
127 
128 void QPixmap::init(int w, int h, Type type)
129 {
130  init(w, h, int(type));
131 }
132 
134 
135 void QPixmap::init(int w, int h, int type)
136 {
137  if (qt_appType == QApplication::Tty) {
138  qWarning("QPixmap: Cannot create a QPixmap when no GUI is being used");
139  data = 0;
140  return;
141  }
142 
143  if ((w > 0 && h > 0) || type == QPixmapData::BitmapType)
145  else
146  data = 0;
147 }
148 
175  : QPaintDevice()
176 {
177  (void) qt_pixmap_thread_test();
179 }
180 
198 QPixmap::QPixmap(int w, int h)
199  : QPaintDevice()
200 {
201  if (!qt_pixmap_thread_test())
203  else
205 }
206 
221  : QPaintDevice()
222 {
223  if (!qt_pixmap_thread_test())
225  else
226  init(size.width(), size.height(), QPixmapData::PixmapType);
227 }
228 
233 {
234  if (!qt_pixmap_thread_test())
235  init(0, 0, type);
236  else
237  init(s.width(), s.height(), type);
238 }
239 
244 {
245  if (!qt_pixmap_thread_test())
246  init(0, 0, static_cast<QPixmapData::PixelType>(type));
247  else
248  init(s.width(), s.height(), static_cast<QPixmapData::PixelType>(type));
249 }
250 
255  : QPaintDevice(), data(d)
256 {
257 }
258 
287 QPixmap::QPixmap(const QString& fileName, const char *format, Qt::ImageConversionFlags flags)
288  : QPaintDevice()
289 {
291  if (!qt_pixmap_thread_test())
292  return;
293 
294  load(fileName, format, flags);
295 }
296 
303 QPixmap::QPixmap(const QPixmap &pixmap)
304  : QPaintDevice()
305 {
306  if (!qt_pixmap_thread_test()) {
308  return;
309  }
310  if (pixmap.paintingActive()) { // make a deep copy
311  operator=(pixmap.copy());
312  } else {
313  data = pixmap.data;
314  }
315 }
316 
332 #ifndef QT_NO_IMAGEFORMAT_XPM
333 QPixmap::QPixmap(const char * const xpm[])
334  : QPaintDevice()
335 {
337  if (!xpm)
338  return;
339 
340  QImage image(xpm);
341  if (!image.isNull()) {
343  *this = QBitmap::fromImage(image);
344  else
345  *this = fromImage(image);
346  }
347 }
348 #endif
349 
350 
356 {
357  Q_ASSERT(!data || data->ref >= 1); // Catch if ref-counting changes again
358 }
359 
363 int QPixmap::devType() const
364 {
365  return QInternal::Pixmap;
366 }
367 
395 {
396  if (isNull())
397  return QPixmap();
398 
399  QRect r(0, 0, width(), height());
400  if (!rect.isEmpty())
401  r = r.intersected(rect);
402 
404  d->copy(data.data(), r);
405  return QPixmap(d);
406 }
407 
434 void QPixmap::scroll(int dx, int dy, const QRect &rect, QRegion *exposed)
435 {
436  if (isNull() || (dx == 0 && dy == 0))
437  return;
438  QRect dest = rect & this->rect();
439  QRect src = dest.translated(-dx, -dy) & dest;
440  if (src.isEmpty()) {
441  if (exposed)
442  *exposed += dest;
443  return;
444  }
445 
446  detach();
447 
448  if (!data->scroll(dx, dy, src)) {
449  // Fallback
450  QPixmap pix = *this;
451  QPainter painter(&pix);
453  painter.drawPixmap(src.translated(dx, dy), *this, src);
454  painter.end();
455  *this = pix;
456  }
457 
458  if (exposed) {
459  *exposed += dest;
460  *exposed -= src.translated(dx, dy);
461  }
462 }
463 
472 {
473  if (paintingActive()) {
474  qWarning("QPixmap::operator=: Cannot assign to pixmap during painting");
475  return *this;
476  }
477  if (pixmap.paintingActive()) { // make a deep copy
478  *this = pixmap.copy();
479  } else {
480  data = pixmap.data;
481  }
482  return *this;
483 }
484 
499 QPixmap::operator QVariant() const
500 {
501  return QVariant(QVariant::Pixmap, this);
502 }
503 
543 {
544  if (isNull())
545  return QImage();
546 
547  return data->toImage();
548 }
549 
570 {
571  return QImage::trueMatrix(m, w, h);
572 }
573 
584 QMatrix QPixmap::trueMatrix(const QMatrix &m, int w, int h)
585 {
586  return trueMatrix(QTransform(m), w, h).toAffine();
587 }
588 
589 
599 bool QPixmap::isQBitmap() const
600 {
601  return data->type == QPixmapData::BitmapType;
602 }
603 
615 bool QPixmap::isNull() const
616 {
617  return !data || data->isNull();
618 }
619 
630 int QPixmap::width() const
631 {
632  return data ? data->width() : 0;
633 }
634 
645 int QPixmap::height() const
646 {
647  return data ? data->height() : 0;
648 }
649 
662 {
663  return data ? QSize(data->width(), data->height()) : QSize(0, 0);
664 }
665 
677 {
678  return data ? QRect(0, 0, data->width(), data->height()) : QRect();
679 }
680 
695 int QPixmap::depth() const
696 {
697  return data ? data->depth() : 0;
698 }
699 
716 #ifdef QT3_SUPPORT
717 void QPixmap::resize_helper(const QSize &s)
718 {
719  int w = s.width();
720  int h = s.height();
721  if (w < 1 || h < 1) {
722  *this = QPixmap();
723  return;
724  }
725 
726  if (size() == s)
727  return;
728 
729  // QPixmap.data member may be QRuntimePixmapData so use pixmapData() function to get
730  // the actual underlaying runtime pixmap data.
731  QPixmapData *pd = pixmapData();
732 
733  // Create new pixmap
734  QPixmap pm(QSize(w, h), pd ? pd->type : QPixmapData::PixmapType);
735  bool uninit = false;
736 #if defined(Q_WS_X11)
737  QX11PixmapData *x11Data = pd && pd->classId() == QPixmapData::X11Class ? static_cast<QX11PixmapData*>(pd) : 0;
738  if (x11Data) {
739  pm.x11SetScreen(x11Data->xinfo.screen());
740  uninit = x11Data->flags & QX11PixmapData::Uninitialized;
741  }
742 #elif defined(Q_WS_MAC)
743  QMacPixmapData *macData = pd && pd->classId() == QPixmapData::MacClass ? static_cast<QMacPixmapData*>(pd) : 0;
744  if (macData)
745  uninit = macData->uninit;
746 #endif
747  if (!uninit && !isNull()) {
748  // Copy old pixmap
749  if (hasAlphaChannel())
750  pm.fill(Qt::transparent);
751  QPainter p(&pm);
752  p.drawPixmap(0, 0, *this, 0, 0, qMin(width(), w), qMin(height(), h));
753  }
754 
755 #if defined(Q_WS_X11)
756  if (x11Data && x11Data->x11_mask) {
757  QPixmapData *newPd = pm.pixmapData();
758  QX11PixmapData *pmData = (newPd && newPd->classId() == QPixmapData::X11Class)
759  ? static_cast<QX11PixmapData*>(newPd) : 0;
760  if (pmData) {
761  pmData->x11_mask = (Qt::HANDLE)XCreatePixmap(X11->display,
762  RootWindow(x11Data->xinfo.display(),
763  x11Data->xinfo.screen()),
764  w, h, 1);
765  GC gc = XCreateGC(X11->display, pmData->x11_mask, 0, 0);
766  XCopyArea(X11->display, x11Data->x11_mask, pmData->x11_mask, gc, 0, 0,
767  qMin(width(), w), qMin(height(), h), 0, 0);
768  XFreeGC(X11->display, gc);
769  }
770  }
771 #endif
772  *this = pm;
773 }
774 #endif
775 
823 {
824  if (paintingActive()) {
825  qWarning("QPixmap::setMask: Cannot set mask while pixmap is being painted on");
826  return;
827  }
828 
829  if (!mask.isNull() && mask.size() != size()) {
830  qWarning("QPixmap::setMask() mask size differs from pixmap size");
831  return;
832  }
833 
834  if (isNull())
835  return;
836 
837  if (static_cast<const QPixmap &>(mask).data == data) // trying to selfmask
838  return;
839 
840  detach();
841  data->setMask(mask);
842 }
843 
844 #ifndef QT_NO_IMAGE_HEURISTIC_MASK
845 
865 {
867  return m;
868 }
869 #endif
870 
883 {
885  return QBitmap::fromImage(image.createMaskFromColor(maskColor.rgba(), mode));
886 }
887 
900 {
901  return createMaskFromColor(maskColor, Qt::MaskInColor);
902 }
903 
930 bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConversionFlags flags)
931 {
932  if (fileName.isEmpty())
933  return false;
934 
935  QFileInfo info(fileName);
936  QString key = QLatin1Literal("qt_pixmap")
937  % info.absoluteFilePath()
939  % HexString<quint64>(info.size())
941 
942  // Note: If no extension is provided, we try to match the
943  // file against known plugin extensions
944  if (!info.completeSuffix().isEmpty() && !info.exists())
945  return false;
946 
947  if (QPixmapCache::find(key, *this))
948  return true;
949 
951  if (tmp->fromFile(fileName, format, flags)) {
952  data = tmp.take();
953  QPixmapCache::insert(key, *this);
954  return true;
955  }
956 
957  return false;
958 }
959 
979 bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
980 {
981  if (len == 0 || buf == 0)
982  return false;
983 
984  if (!data)
986 
987  return data->fromData(buf, len, format, flags);
988 }
989 
1016 bool QPixmap::save(const QString &fileName, const char *format, int quality) const
1017 {
1018  if (isNull())
1019  return false; // nothing to save
1020  QImageWriter writer(fileName, format);
1021  return doImageIO(&writer, quality);
1022 }
1023 
1037 bool QPixmap::save(QIODevice* device, const char* format, int quality) const
1038 {
1039  if (isNull())
1040  return false; // nothing to save
1041  QImageWriter writer(device, format);
1042  return doImageIO(&writer, quality);
1043 }
1044 
1047 bool QPixmap::doImageIO(QImageWriter *writer, int quality) const
1048 {
1049  if (quality > 100 || quality < -1)
1050  qWarning("QPixmap::save: quality out of range [-1,100]");
1051  if (quality >= 0)
1052  writer->setQuality(qMin(quality,100));
1053  return writer->write(toImage());
1054 }
1055 
1056 
1057 // The implementation (and documentation) of
1058 // QPixmap::fill(const QWidget *, const QPoint &)
1059 // is in qwidget.cpp
1060 
1080 void QPixmap::fill(const QColor &color)
1081 {
1082  if (isNull())
1083  return;
1084 
1085  // Some people are probably already calling fill while a painter is active, so to not break
1086  // their programs, only print a warning and return when the fill operation could cause a crash.
1087  if (paintingActive() && (color.alpha() != 255) && !hasAlphaChannel()) {
1088  qWarning("QPixmap::fill: Cannot fill while pixmap is being painted on");
1089  return;
1090  }
1091 
1092  if (data->ref == 1) {
1093  // detach() will also remove this pixmap from caches, so
1094  // it has to be called even when ref == 1.
1095  detach();
1096  } else {
1097  // Don't bother to make a copy of the data object, since
1098  // it will be filled with new pixel data anyway.
1100  d->resize(data->width(), data->height());
1101  data = d;
1102  }
1103  data->fill(color);
1104 }
1105 
1123 {
1124  if (isNull())
1125  return 0;
1126  return data->serialNumber();
1127 }
1128 
1137 {
1138  if (isNull())
1139  return 0;
1140 
1141  Q_ASSERT(data);
1142  return data->cacheKey();
1143 }
1144 
1145 static void sendResizeEvents(QWidget *target)
1146 {
1147  QResizeEvent e(target->size(), QSize());
1148  QApplication::sendEvent(target, &e);
1149 
1150  const QObjectList children = target->children();
1151  for (int i = 0; i < children.size(); ++i) {
1152  QWidget *child = static_cast<QWidget*>(children.at(i));
1153  if (child->isWidgetType() && !child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
1154  sendResizeEvents(child);
1155  }
1156 }
1157 
1189 {
1190  if (!widget)
1191  return QPixmap();
1192 
1194  sendResizeEvents(widget);
1195 
1196  widget->d_func()->prepareToRender(QRegion(),
1198 
1199  QRect r(rect);
1200  if (r.width() < 0)
1201  r.setWidth(widget->width() - rect.x());
1202  if (r.height() < 0)
1203  r.setHeight(widget->height() - rect.y());
1204 
1205  if (!r.intersects(widget->rect()))
1206  return QPixmap();
1207 
1208  QPixmap res(r.size());
1209  if (!qt_widget_private(widget)->isOpaque)
1210  res.fill(Qt::transparent);
1211 
1212  widget->d_func()->render(&res, QPoint(), r, QWidget::DrawWindowBackground
1214  return res;
1215 }
1216 
1279 #if defined(Q_WS_X11) || defined(Q_WS_QWS)
1280 
1300 {
1301 #if defined(Q_WS_X11)
1302  const QPixmapData *pd = pixmapData();
1303  if (pd && pd->classId() == QPixmapData::X11Class)
1304  return static_cast<const QX11PixmapData*>(pd)->handle();
1305 #endif
1306  return 0;
1307 }
1308 #endif
1309 
1310 
1311 #ifdef QT3_SUPPORT
1312 static Qt::ImageConversionFlags colorModeToFlags(QPixmap::ColorMode mode)
1313 {
1314  Qt::ImageConversionFlags flags = Qt::AutoColor;
1315  switch (mode) {
1316  case QPixmap::Color:
1317  flags |= Qt::ColorOnly;
1318  break;
1319  case QPixmap::Mono:
1320  flags |= Qt::MonoOnly;
1321  break;
1322  default:
1323  break;// Nothing.
1324  }
1325  return flags;
1326 }
1327 
1332 QPixmap::QPixmap(const QString& fileName, const char *format, ColorMode mode)
1333  : QPaintDevice()
1334 {
1336  if (!qt_pixmap_thread_test())
1337  return;
1338 
1339  load(fileName, format, colorModeToFlags(mode));
1340 }
1341 
1347 QPixmap::QPixmap(const QImage& image)
1348  : QPaintDevice()
1349 {
1351  if (!qt_pixmap_thread_test())
1352  return;
1353 
1355  *this = QBitmap::fromImage(image);
1356  else
1357  *this = fromImage(image);
1358 }
1359 
1372 QPixmap &QPixmap::operator=(const QImage &image)
1373 {
1375  *this = QBitmap::fromImage(image);
1376  else
1377  *this = fromImage(image);
1378  return *this;
1379 }
1380 
1385 bool QPixmap::load(const QString &fileName, const char *format, ColorMode mode)
1386 {
1387  return load(fileName, format, colorModeToFlags(mode));
1388 }
1389 
1394 bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, ColorMode mode)
1395 {
1396  return loadFromData(buf, len, format, colorModeToFlags(mode));
1397 }
1398 
1402 bool QPixmap::convertFromImage(const QImage &image, ColorMode mode)
1403 {
1405  *this = QBitmap::fromImage(image, colorModeToFlags(mode));
1406  else
1407  *this = fromImage(image, colorModeToFlags(mode));
1408  return !isNull();
1409 }
1410 
1411 #endif
1412 
1413 /*****************************************************************************
1414  QPixmap stream functions
1415  *****************************************************************************/
1416 #if !defined(QT_NO_DATASTREAM)
1417 
1431 {
1432  return stream << pixmap.toImage();
1433 }
1434 
1447 {
1448  QImage image;
1449  stream >> image;
1450 
1451  if (image.isNull()) {
1452  pixmap = QPixmap();
1453  } else if (image.depth() == 1) {
1454  pixmap = QBitmap::fromImage(image);
1455  } else {
1456  pixmap = QPixmap::fromImage(image);
1457  }
1458  return stream;
1459 }
1460 
1461 #endif // QT_NO_DATASTREAM
1462 
1463 #ifdef QT3_SUPPORT
1464 Q_GUI_EXPORT void copyBlt(QPixmap *dst, int dx, int dy,
1465  const QPixmap *src, int sx, int sy, int sw, int sh)
1466 {
1467  Q_ASSERT_X(dst, "::copyBlt", "Destination pixmap must be non-null");
1468  Q_ASSERT_X(src, "::copyBlt", "Source pixmap must be non-null");
1469 
1470  if (src->hasAlphaChannel()) {
1472  QPainter p(dst);
1474  p.drawPixmap(dx, dy, *src, sx, sy, sw, sh);
1475  } else {
1477  QPainter p(&image);
1479  p.drawPixmap(dx, dy, *src, sx, sy, sw, sh);
1480  p.end();
1481  *dst = QPixmap::fromImage(image);
1482  }
1483  } else {
1484  QPainter p(dst);
1485  p.drawPixmap(dx, dy, *src, sx, sy, sw, sh);
1486  }
1487 
1488 }
1489 #endif
1490 
1496 {
1497  return data && data->ref == 1;
1498 }
1499 
1504 {
1505  Q_ASSERT_X(false, "QPixmap::deref()", "Do not call this function anymore!");
1506 }
1507 
1531 bool QPixmap::convertFromImage(const QImage &image, Qt::ImageConversionFlags flags)
1532 {
1533  if (image.isNull() || !data)
1534  *this = QPixmap::fromImage(image, flags);
1535  else
1536  data->fromImage(image, flags);
1537  return !isNull();
1538 }
1539 
1604 {
1605  if (isNull()) {
1606  qWarning("QPixmap::scaled: Pixmap is a null pixmap");
1607  return QPixmap();
1608  }
1609  if (s.isEmpty())
1610  return QPixmap();
1611 
1612  QSize newSize = size();
1613  newSize.scale(s, aspectMode);
1614  newSize.rwidth() = qMax(newSize.width(), 1);
1615  newSize.rheight() = qMax(newSize.height(), 1);
1616  if (newSize == size())
1617  return *this;
1618 
1619  QTransform wm = QTransform::fromScale((qreal)newSize.width() / width(),
1620  (qreal)newSize.height() / height());
1621  QPixmap pix = transformed(wm, mode);
1622  return pix;
1623 }
1624 
1643 {
1644  if (isNull()) {
1645  qWarning("QPixmap::scaleWidth: Pixmap is a null pixmap");
1646  return copy();
1647  }
1648  if (w <= 0)
1649  return QPixmap();
1650 
1651  qreal factor = (qreal) w / width();
1652  QTransform wm = QTransform::fromScale(factor, factor);
1653  return transformed(wm, mode);
1654 }
1655 
1674 {
1675  if (isNull()) {
1676  qWarning("QPixmap::scaleHeight: Pixmap is a null pixmap");
1677  return copy();
1678  }
1679  if (h <= 0)
1680  return QPixmap();
1681 
1682  qreal factor = (qreal) h / height();
1683  QTransform wm = QTransform::fromScale(factor, factor);
1684  return transformed(wm, mode);
1685 }
1686 
1706  Qt::TransformationMode mode) const
1707 {
1708  if (isNull() || transform.type() <= QTransform::TxTranslate)
1709  return *this;
1710 
1711  return data->transformed(transform, mode);
1712 }
1713 
1724 {
1725  return transformed(QTransform(matrix), mode);
1726 }
1727 
1728 
1729 
1730 
1731 
1732 
1733 
1734 
1938 bool QPixmap::hasAlpha() const
1939 {
1940 #if defined(Q_WS_X11)
1941  if (data && data->hasAlphaChannel())
1942  return true;
1943  QPixmapData *pd = pixmapData();
1944  if (pd && pd->classId() == QPixmapData::X11Class) {
1945  QX11PixmapData *x11Data = static_cast<QX11PixmapData*>(pd);
1946 #ifndef QT_NO_XRENDER
1947  if (x11Data->picture && x11Data->d == 32)
1948  return true;
1949 #endif
1950  if (x11Data->d == 1 || x11Data->x11_mask)
1951  return true;
1952  }
1953  return false;
1954 #else
1955  return data && data->hasAlphaChannel();
1956 #endif
1957 }
1958 
1966 {
1967  return data && data->hasAlphaChannel();
1968 }
1969 
1974 {
1975  return data ? data->metric(metric) : 0;
1976 }
1977 
2000 {
2001  if (alphaChannel.isNull())
2002  return;
2003 
2004  if (paintingActive()) {
2005  qWarning("QPixmap::setAlphaChannel: "
2006  "Cannot set alpha channel while pixmap is being painted on");
2007  return;
2008  }
2009 
2010  if (width() != alphaChannel.width() && height() != alphaChannel.height()) {
2011  qWarning("QPixmap::setAlphaChannel: "
2012  "The pixmap and the alpha channel pixmap must have the same size");
2013  return;
2014  }
2015 
2016  detach();
2017  data->setAlphaChannel(alphaChannel);
2018 }
2019 
2052 {
2053  return data ? data->alphaChannel() : QPixmap();
2054 }
2055 
2060 {
2061  return data ? data->paintEngine() : 0;
2062 }
2063 
2078 {
2079  return data ? data->mask() : QBitmap();
2080 }
2081 
2093 {
2094 #if defined(Q_WS_QWS)
2095  return QScreen::instance()->depth();
2096 #elif defined(Q_WS_X11)
2097  return QX11Info::appDepth();
2098 #elif defined(Q_WS_WINCE)
2099  return QColormap::instance().depth();
2100 #elif defined(Q_WS_WIN)
2101  return 32; // XXX
2102 #elif defined(Q_WS_MAC)
2103  return 32;
2104 #elif defined(Q_OS_SYMBIAN)
2105  return S60->screenDepth;
2106 #elif defined(Q_WS_QPA)
2107  return 32; //LITE: use graphicssystem (we should do that in general)
2108 #endif
2109 }
2110 
2129 {
2130  if (!data)
2131  return;
2132 
2133  // QPixmap.data member may be QRuntimePixmapData so use pixmapData() function to get
2134  // the actual underlaying runtime pixmap data.
2135  QPixmapData *pd = pixmapData();
2136  QPixmapData::ClassId id = pd->classId();
2137  if (id == QPixmapData::RasterClass) {
2138  QRasterPixmapData *rasterData = static_cast<QRasterPixmapData*>(pd);
2139  rasterData->image.detach();
2140  }
2141 
2142  if (data->is_cached && data->ref == 1)
2144 
2145 #if defined(Q_WS_MAC)
2146  QMacPixmapData *macData = id == QPixmapData::MacClass ? static_cast<QMacPixmapData*>(pd) : 0;
2147  if (macData) {
2148  if (macData->cg_mask) {
2149  CGImageRelease(macData->cg_mask);
2150  macData->cg_mask = 0;
2151  }
2152  }
2153 #endif
2154 
2155  if (data->ref != 1) {
2156  *this = copy();
2157  }
2158  ++data->detach_no;
2159 
2160 #if defined(Q_WS_X11)
2161  if (pd->classId() == QPixmapData::X11Class) {
2162  QX11PixmapData *d = static_cast<QX11PixmapData*>(pd);
2164 
2165  // reset the cache data
2166  if (d->hd2) {
2167  XFreePixmap(X11->display, d->hd2);
2168  d->hd2 = 0;
2169  }
2170  }
2171 #elif defined(Q_WS_MAC)
2172  if (macData) {
2173  macData->macReleaseCGImageRef();
2174  macData->uninit = false;
2175  }
2176 #endif
2177 }
2178 
2197 QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
2198 {
2199  if (image.isNull())
2200  return QPixmap();
2201 
2205  data->fromImage(image, flags);
2206  return QPixmap(data.take());
2207 }
2208 
2221 QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
2222 {
2226  data->fromImageReader(imageReader, flags);
2227  return QPixmap(data.take());
2228 }
2229 
2278 {
2279  if (data) {
2280  QPixmapData* pm = data.data();
2281  return pm->runtimeData() ? pm->runtimeData() : pm;
2282  }
2283 
2284  return 0;
2285 }
2286 
2287 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
virtual QPixmapData * createCompatiblePixmapData() const
Definition: qpixmapdata.cpp:96
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
QT_DEPRECATED int serialNumber() const
Returns a number that identifies the contents of this QPixmap object.
Definition: qpixmap.cpp:1122
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
double d
Definition: qnumeric_p.h:62
int devType() const
Definition: qpixmap.cpp:363
QPixmap scaledToHeight(int h, Qt::TransformationMode mode=Qt::FastTransformation) const
Definition: qpixmap.cpp:1673
virtual bool fromFile(const QString &filename, const char *format, Qt::ImageConversionFlags flags)
QImage toImage() const
Converts the pixmap to a QImage.
Definition: qpixmap.cpp:542
static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Converts the given image to a pixmap using the specified flags to control the conversion.
Definition: qpixmap.cpp:2197
virtual QPixmap transformed(const QTransform &matrix, Qt::TransformationMode mode) const
static int defaultDepth()
Returns the default pixmap depth used by the application.
Definition: qpixmap.cpp:2092
static QColormap instance(int screen=-1)
The QLatin1Literal class provides a thin wrapper around string literals used in source code...
QPixmap & operator=(const QPixmap &)
Assigns the given pixmap to this pixmap and returns a reference to this pixmap.
Definition: qpixmap.cpp:471
void setHeight(int h)
Sets the height of the rectangle to the given height.
Definition: qrect.h:445
int type
Definition: qmetatype.cpp:239
double qreal
Definition: qglobal.h:1193
QPixmap()
Constructs a null pixmap.
Definition: qpixmap.cpp:174
static mach_timebase_info_data_t info
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
Q_GUI_EXPORT qint64 qt_pixmap_id(const QPixmap &pixmap)
Definition: qpixmap.cpp:94
static QMatrix trueMatrix(const QMatrix &m, int w, int h)
This convenience function loads the matrix m into a QTransform and calls the overloaded function with...
Definition: qpixmap.cpp:584
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QT_DEPRECATED void setAlphaChannel(const QPixmap &)
Sets the alpha channel of this pixmap to the given alphaChannel by converting the alphaChannel into 3...
Definition: qpixmap.cpp:1999
QPixmap scaledToWidth(int w, Qt::TransformationMode mode=Qt::FastTransformation) const
Returns a scaled copy of the image.
Definition: qpixmap.cpp:1642
int width() const
Returns the width of the pixmap.
Definition: qpixmap.cpp:630
QPointer< QWidget > widget
QSize size() const
Returns the size of the pixmap.
Definition: qpixmap.cpp:661
int width
the width of the widget excluding any window frame
Definition: qwidget.h:166
virtual QPixmapData * runtimeData() const
virtual void fill(const QColor &color)=0
Qt::HANDLE picture
The QMatrix class specifies 2D transformations of a coordinate system.
Definition: qmatrix.h:61
friend Q_GUI_EXPORT QDataStream & operator>>(QDataStream &, QPixmap &)
Reads an image from the given stream into the given pixmap.
Definition: qpixmap.cpp:1446
bool isWindow() const
Returns true if the widget is an independent window, otherwise returns false.
Definition: qwidget.h:945
void detach()
Detaches the pixmap from shared pixmap data.
Definition: qpixmap.cpp:2128
MaskMode
Definition: qnamespace.h:1490
static int appDepth(int screen=-1)
Returns the color depth (bits per pixel) used by the application on the given screen.
void setMask(const QBitmap &)
Sets a mask bitmap.
Definition: qpixmap.cpp:822
QExplicitlySharedDataPointer< QPixmapData > data
Definition: qpixmap.h:252
void init(int, int, Type=PixmapType)
Definition: qpixmap.cpp:128
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
bool isNull() const
Returns true if it is a null image, otherwise returns false.
Definition: qimage.cpp:1542
T * take()
Returns the value of the pointer referenced by this object.
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
virtual void resize(int width, int height)=0
int width() const
uint toTime_t() const
Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).
Definition: qdatetime.cpp:2505
QString completeSuffix() const
Returns the complete suffix of the file.
Definition: qfileinfo.cpp:811
QDataStream & operator<<(QDataStream &stream, const QPixmap &pixmap)
Writes the given pixmap to the given stream as a PNG image.
Definition: qpixmap.cpp:1430
QPixmap scaled(int w, int h, Qt::AspectRatioMode aspectMode=Qt::IgnoreAspectRatio, Qt::TransformationMode mode=Qt::FastTransformation) const
Definition: qpixmap.h:132
QRect translated(int dx, int dy) const
Returns a copy of the rectangle that is translated dx along the x axis and dy along the y axis...
Definition: qrect.h:328
void setQuality(int quality)
This is an image format specific function that sets the quality level of the image to quality...
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
QPixmap transformed(const QMatrix &, Qt::TransformationMode mode=Qt::FastTransformation) const
This convenience function loads the matrix into a QTransform and calls the overloaded function...
Definition: qpixmap.cpp:1723
QString absoluteFilePath() const
Returns an absolute path including the file name.
Definition: qfileinfo.cpp:534
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool exists() const
Returns true if the file exists; otherwise returns false.
Definition: qfileinfo.cpp:675
void deref()
Definition: qpixmap.cpp:1503
Qt::HANDLE handle() const
Returns the pixmap&#39;s handle to the device context.
Definition: qpixmap.cpp:1299
QRect intersected(const QRect &other) const
Returns the intersection of this rectangle and the given rectangle.
Definition: qrect.h:481
virtual QPaintEngine * paintEngine() const =0
int depth() const
Returns the depth of the pixmap.
Definition: qpixmap.cpp:695
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
bool load(const QString &fileName, const char *format=0, Qt::ImageConversionFlags flags=Qt::AutoColor)
Loads a pixmap from the file with the given fileName.
Definition: qpixmap.cpp:930
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual QBitmap mask() const
TransformationType type() const
Returns the transformation type of this matrix.
#define X11
Definition: qt_x11_p.h:724
virtual bool fromData(const uchar *buffer, uint len, const char *format, Qt::ImageConversionFlags flags)
QT_DEPRECATED QPixmap alphaChannel() const
Returns the alpha channel of the pixmap as a new grayscale QPixmap in which each pixel&#39;s red...
Definition: qpixmap.cpp:2051
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
bool paintingActive() const
Definition: qpaintdevice.h:170
static QPixmap * find(const QString &key)
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon dest...
static bool qt_pixmap_thread_test()
Definition: qpixmap.cpp:99
PixelType pixelType() const
virtual QPixmap alphaChannel() const
unsigned char uchar
Definition: qglobal.h:994
The QBitmap class provides monochrome (1-bit depth) pixmaps.
Definition: qbitmap.h:55
virtual QImage toImage() const =0
int width() const
Returns the width.
Definition: qsize.h:126
static QThread * currentThread()
Returns a pointer to a QThread which manages the currently executing thread.
Definition: qthread.cpp:419
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isDetached() const
Definition: qpixmap.cpp:1495
static FILE * stream
~QPixmap()
Destroys the pixmap.
Definition: qpixmap.cpp:355
int metric(PaintDeviceMetric) const
Definition: qpixmap.cpp:1973
static void sendResizeEvents(QWidget *target)
Definition: qpixmap.cpp:1145
int serialNumber() const
int height
the height of the widget excluding any window frame
Definition: qwidget.h:167
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QImageWriter class provides a format independent interface for writing images to files or other d...
Definition: qimagewriter.h:59
int depth() const
#define qApp
virtual void fromImage(const QImage &image, Qt::ImageConversionFlags flags)=0
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
void scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed=0)
Definition: qpixmap.h:307
TransformationMode
Definition: qnamespace.h:1510
void detach()
If multiple images share common data, this image makes a copy of the data and detaches itself from th...
Definition: qimage.cpp:1359
The QResizeEvent class contains event parameters for resize events.
Definition: qevent.h:349
Q_CORE_EXPORT void qWarning(const char *,...)
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
bool isQBitmap() const
Returns true if this is a QBitmap; otherwise returns false.
Definition: qpixmap.cpp:599
unsigned int uint
Definition: qglobal.h:996
static QScreen * instance()
Returns a pointer to the application&#39;s QScreen instance.
Definition: qscreen_qws.h:201
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
virtual int metric(QPaintDevice::PaintDeviceMetric metric) const =0
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
ClassId classId() const
QSize size
the size of the widget excluding any window frame
Definition: qwidget.h:165
__int64 qint64
Definition: qglobal.h:942
void * HANDLE
Definition: qnamespace.h:1671
bool loadFromData(const uchar *buf, uint len, const char *format=0, Qt::ImageConversionFlags flags=Qt::AutoColor)
Loads a pixmap from the len first bytes of the given binary data.
Definition: qpixmap.cpp:979
The QPaintEngine class provides an abstract definition of how QPainter draws to a given device on a g...
Definition: qpaintengine.h:90
PixelType type
static QString graphics_system_name
The QImageReader class provides a format independent interface for reading images from files or other...
Definition: qimagereader.h:62
virtual bool hasAlphaChannel() const =0
static void executePixmapDataModificationHooks(QPixmapData *)
void fill(const QColor &fillColor=Qt::white)
Fills the pixmap with the given color.
Definition: qpixmap.cpp:1080
static QPixmap fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags=Qt::AutoColor)
Create a QPixmap from an image read directly from an imageReader.
Definition: qpixmap.cpp:2221
bool isEmpty() const
Returns true if the rectangle is empty, otherwise returns false.
Definition: qrect.h:234
Q_CORE_EXPORT void qFatal(const char *,...)
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
bool isWidgetType() const
Returns true if the object is a widget; otherwise returns false.
Definition: qobject.h:146
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const Q_REQUIRED_RESULT
Returns a copy of the image in the given format.
Definition: qimage.cpp:3966
int depth() const
qint64 size() const
Returns the file size in bytes.
Definition: qfileinfo.cpp:1248
Qt::HANDLE x11_mask
void setCompositionMode(CompositionMode mode)
Sets the composition mode to the given mode.
Definition: qpainter.cpp:2422
QBitmap mask() const
Extracts a bitmap mask from the pixmap&#39;s alpha channel.
Definition: qpixmap.cpp:2077
QBitmap createMaskFromColor(const QColor &maskColor) const
Creates and returns a mask for this pixmap based on the given maskColor.
Definition: qpixmap.cpp:899
ushort alpha
Returns the alpha color component of this color.
Definition: qcolor.h:242
QRect rect
the internal geometry of the widget excluding any window frame
Definition: qwidget.h:168
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
bool doImageIO(QImageWriter *io, int quality) const
Definition: qpixmap.cpp:1047
bool hasAlpha() const
Returns true if this pixmap has an alpha channel, or has a mask, otherwise returns false...
Definition: qpixmap.cpp:1938
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
QApplication::Type qt_appType
static bool insert(const QString &key, const QPixmap &pixmap)
Inserts a copy of the pixmap pixmap associated with the key into the cache.
int key
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QAtomicInt ref
int & rheight()
Returns a reference to the height.
Definition: qsize.h:144
void setWidth(int w)
Sets the width of the rectangle to the given width.
Definition: qrect.h:442
virtual void copy(const QPixmapData *data, const QRect &rect)
int height() const
Returns the height.
Definition: qsize.h:129
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
static QPixmapData * create(int w, int h, PixelType type)
Definition: qpixmapdata.cpp:55
virtual void setMask(const QBitmap &mask)
const QObjectList & children() const
Returns a list of child objects.
Definition: qobject.h:197
qint64 cacheKey() const
Returns a number that identifies this QPixmap.
Definition: qpixmap.cpp:1136
QRect rect() const
Returns the pixmap&#39;s enclosing rectangle.
Definition: qpixmap.cpp:676
bool convertFromImage(const QImage &img, Qt::ImageConversionFlags flags=Qt::AutoColor)
Replaces this pixmap&#39;s data with the given image using the specified flags to control the conversion...
Definition: qpixmap.cpp:1531
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
void scale(int w, int h, Qt::AspectRatioMode mode)
Scales the size to a rectangle with the given width and height, according to the specified mode: ...
Definition: qsize.h:138
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device...
Definition: qpainter.cpp:5619
struct _XGC * GC
Definition: qwindowdefs.h:117
int height() const
Returns the height of the pixmap.
Definition: qpixmap.cpp:645
static QBitmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Returns a copy of the given image converted to a bitmap using the specified image conversion flags...
Definition: qbitmap.cpp:281
int height() const
bool isNull() const
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
virtual void setAlphaChannel(const QPixmap &alphaChannel)
static QTransform fromScale(qreal dx, qreal dy)
Creates a matrix which corresponds to a scaling of sx horizontally and sy vertically.
Definition: qtransform.cpp:528
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
bool intersects(const QRect &r) const
Returns true if this rectangle intersects with the given rectangle (i.
Definition: qrect.cpp:1429
virtual QPixmapData * createPixmapData(QPixmapData::PixelType type) const =0
Q_GUI_EXPORT QWidgetPrivate * qt_widget_private(QWidget *widget)
Definition: qwidget.cpp:12920
QRgb rgba() const
Returns the RGB value of the color, including its alpha.
Definition: qcolor.cpp:1019
QPaintEngine * paintEngine() const
Definition: qpixmap.cpp:2059
static bool testAttribute(Qt::ApplicationAttribute attribute)
Returns true if attribute attribute is set; otherwise returns false.
bool isEmpty() const
Returns true if either of the width and height is less than or equal to 0; otherwise returns false...
Definition: qsize.h:120
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615
static QGraphicsSystem * graphicsSystem()
QPixmap copy(int x, int y, int width, int height) const
Returns a deep copy of the subset of the pixmap that is specified by the rectangle QRect( x...
Definition: qpixmap.h:302
qint64 cacheKey() const
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
bool hasAlphaChannel() const
Returns true if the pixmap has a format that respects the alpha channel, otherwise returns false...
Definition: qpixmap.cpp:1965
static QPixmapData * createDefaultPixmapData(QPixmapData::PixelType type)
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
QBitmap createHeuristicMask(bool clipTight=true) const
Creates and returns a heuristic mask for this pixmap.
Definition: qpixmap.cpp:864
bool write(const QImage &image)
Writes the image image to the assigned device or file name.
bool save(const QString &fileName, const char *format=0, int quality=-1) const
Saves the pixmap to the file with the given fileName using the specified image file format and qualit...
Definition: qpixmap.cpp:1016
void render(QPaintDevice *target, const QPoint &targetOffset=QPoint(), const QRegion &sourceRegion=QRegion(), RenderFlags renderFlags=RenderFlags(DrawWindowBackground|DrawChildren))
Renders the sourceRegion of this widget into the target using renderFlags to determine how to render...
Definition: qwidget.cpp:5372
Qt::HANDLE hd2
static QString fileName(const QString &fileUrl)
QImage createMaskFromColor(QRgb color, Qt::MaskMode mode=Qt::MaskInColor) const
Creates and returns a mask for this image based on the given color value.
Definition: qimage.cpp:4865
virtual bool scroll(int dx, int dy, const QRect &rect)
static QPixmap grabWidget(QWidget *widget, const QRect &rect)
Creates a pixmap and paints the given widget, restricted by the given rectangle, in it...
Definition: qpixmap.cpp:1188
friend class QBitmap
Definition: qpixmap.h:279
static Display * display()
Returns the default display for the application.
bool end()
Ends painting.
Definition: qpainter.cpp:1929
static QMatrix trueMatrix(const QMatrix &, int w, int h)
Returns the actual matrix used for transforming an image with the given width, height and matrix...
Definition: qimage.cpp:4680
virtual void fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
int & rwidth()
Returns a reference to the width.
Definition: qsize.h:141
int depth() const
Returns the depth of the framebuffer, in bits per pixel.
Definition: qscreen_qws.h:229
int screen() const
Returns the number of the screen currently in use.
bool hasFeature(PaintEngineFeatures feature) const
Returns true if the paint engine supports the specified feature; otherwise returns false...
Definition: qpaintengine.h:229
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65
QPixmapData * pixmapData() const
Definition: qpixmap.cpp:2277
AspectRatioMode
Definition: qnamespace.h:1317
QDateTime lastModified() const
Returns the date and time when the file was last modified.
Definition: qfileinfo.cpp:1296