8#include <qpa/qplatformpixmap.h>
16#include <private/qguiapplication_p.h>
29#include <qpa/qplatformintegration.h>
32#include "private/qhexstring_p.h"
34#include <qtgui_tracepoints_p.h>
40using namespace Qt::StringLiterals;
42Q_TRACE_PARAM_REPLACE(Qt::AspectRatioMode,
int);
43Q_TRACE_PARAM_REPLACE(Qt::TransformationMode,
int);
48QT_WARNING_DISABLE_MSVC(4723)
52 if (!QCoreApplication::instanceExists()) {
53 qFatal(
"QPixmap: Must construct a QGuiApplication before a QPixmap");
56 if (QGuiApplicationPrivate::instance()
57 && !QThread::isMainThread()
58 && Q_LIKELY(QGuiApplicationPrivate::platformIntegration())
59 && !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedPixmaps)) {
60 qWarning(
"QPixmap: It is not safe to use pixmaps outside the GUI thread on this platform");
66void QPixmap::doInit(
int w,
int h,
int type)
68 if ((w > 0 && h > 0) || type == QPlatformPixmap::BitmapType)
69 data = QPlatformPixmap::create(w, h, (QPlatformPixmap::PixelType) type);
75
76
77
78
83 (
void) qt_pixmap_thread_test();
84 doInit(0, 0, QPlatformPixmap::PixmapType);
88
89
90
91
92
93
94
95
96
97
98
99
101QPixmap::QPixmap(
int w,
int h)
102 : QPixmap(QSize(w, h))
107
108
109
110
111
112
113
114
116QPixmap::QPixmap(
const QSize &size)
117 : QPixmap(size, QPlatformPixmap::PixmapType)
122
123
124QPixmap::QPixmap(
const QSize &s,
int type)
126 if (!qt_pixmap_thread_test())
127 doInit(0, 0,
static_cast<QPlatformPixmap::PixelType>(type));
129 doInit(s.width(), s.height(),
static_cast<QPlatformPixmap::PixelType>(type));
133
134
135QPixmap::QPixmap(QPlatformPixmap *d)
136 : QPaintDevice(), data(d)
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
169QPixmap::QPixmap(
const QString& fileName,
const char *format, Qt::ImageConversionFlags flags)
172 doInit(0, 0, QPlatformPixmap::PixmapType);
173 if (!qt_pixmap_thread_test())
176 load(fileName, format, flags);
180
181
182
183
185QPixmap::QPixmap(
const QPixmap &pixmap)
188 if (!qt_pixmap_thread_test()) {
189 doInit(0, 0, QPlatformPixmap::PixmapType);
192 if (pixmap.paintingActive()) {
193 pixmap.copy().swap(*
this);
200
201
202
203
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222#ifndef QT_NO_IMAGEFORMAT_XPM
223QPixmap::QPixmap(
const char *
const xpm[])
226 doInit(0, 0, QPlatformPixmap::PixmapType);
231 if (!image.isNull()) {
232 if (data && data->pixelType() == QPlatformPixmap::BitmapType)
233 *
this = QBitmap::fromImage(std::move(image));
235 *
this = fromImage(std::move(image));
242
243
247 Q_ASSERT(!data || data->ref.loadRelaxed() >= 1);
251
252
253int QPixmap::devType()
const
255 return QInternal::Pixmap;
259
260
261
262
263
264
267
268
269
270
271
272
273
274
275
276
277
278QPixmap QPixmap::copy(
const QRect &rect)
const
283 QRect r(0, 0, width(), height());
285 r = r.intersected(rect);
287 QPlatformPixmap *d = data->createCompatiblePlatformPixmap();
288 d->copy(data.data(), r);
293
294
295
296
297
298
299
302
303
304
305
306
307
308
309
310
311
312void QPixmap::scroll(
int dx,
int dy,
const QRect &rect, QRegion *exposed)
314 if (isNull() || (dx == 0 && dy == 0))
316 QRect dest = rect &
this->rect();
317 QRect src = dest.translated(-dx, -dy) & dest;
326 if (!data->scroll(dx, dy, src)) {
329 QPainter painter(&pix);
330 painter.setCompositionMode(QPainter::CompositionMode_Source);
331 painter.drawPixmap(src.translated(dx, dy), *
this, src);
338 *exposed -= src.translated(dx, dy);
343
344
345
346
347
349QPixmap &QPixmap::operator=(
const QPixmap &pixmap)
351 if (paintingActive()) {
352 qWarning(
"QPixmap::operator=: Cannot assign to pixmap during painting");
355 if (pixmap.paintingActive()) {
356 pixmap.copy().swap(*
this);
364
365
366
367
368
369
372
373
374
377
378
379QPixmap::operator QVariant()
const
381 return QVariant::fromValue(*
this);
385
386
387
388
389
390
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408QImage QPixmap::toImage()
const
413 return data->toImage();
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432QTransform QPixmap::trueMatrix(
const QTransform &m,
int w,
int h)
434 return QImage::trueMatrix(m, w, h);
438
439
440
441
443bool QPixmap::isQBitmap()
const
445 return data && data->type == QPlatformPixmap::BitmapType;
449
450
451
452
453
454
455
456bool QPixmap::isNull()
const
458 return !data || data->isNull();
462
463
464
465
466
467
468int QPixmap::width()
const
470 return data ? data->width() : 0;
474
475
476
477
478
479
480int QPixmap::height()
const
482 return data ? data->height() : 0;
486
487
488
489
490
491
492
493QSize QPixmap::size()
const
495 return data ? QSize(data->width(), data->height()) : QSize(0, 0);
499
500
501
502
503
504
505QRect QPixmap::rect()
const
507 return data ? QRect(0, 0, data->width(), data->height()) : QRect();
511
512
513
514
515
516
517
518
519
520
521int QPixmap::depth()
const
523 return data ? data->depth() : 0;
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543void QPixmap::setMask(
const QBitmap &mask)
545 if (paintingActive()) {
546 qWarning(
"QPixmap::setMask: Cannot set mask while pixmap is being painted on");
550 if (!mask.isNull() && mask.size() != size()) {
551 qWarning(
"QPixmap::setMask() mask size differs from pixmap size");
558 if (
static_cast<
const QPixmap &>(mask).data == data)
566
567
568
569
570
571
572
573
574
575
576qreal QPixmap::devicePixelRatio()
const
580 return data->devicePixelRatio();
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604void QPixmap::setDevicePixelRatio(qreal scaleFactor)
609 if (scaleFactor == data->devicePixelRatio())
613 data->setDevicePixelRatio(scaleFactor);
617
618
619
620
621
622
623
624
625
626QSizeF QPixmap::deviceIndependentSize()
const
630 return QSizeF(data->width(), data->height()) / data->devicePixelRatio();
633#ifndef QT_NO_IMAGE_HEURISTIC_MASK
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653QBitmap QPixmap::createHeuristicMask(
bool clipTight)
const
655 QBitmap m = QBitmap::fromImage(toImage().createHeuristicMask(clipTight));
661
662
663
664
665
666
667
668
669
670
671QBitmap QPixmap::createMaskFromColor(
const QColor &maskColor, Qt::MaskMode mode)
const
673 QImage image = toImage().convertToFormat(QImage::Format_ARGB32);
674 return QBitmap::fromImage(std::move(image).createMaskFromColor(maskColor.rgba(), mode));
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
704bool QPixmap::load(
const QString &fileName,
const char *format, Qt::ImageConversionFlags flags)
706 if (!fileName.isEmpty()) {
708 QFileInfo info(fileName);
711 if (info.completeSuffix().isEmpty() || info.exists()) {
712 const bool inGuiThread = qApp->thread() == QThread::currentThread();
714 QString key =
"qt_pixmap"_L1
715 % info.absoluteFilePath()
716 % HexString<uint>(info.lastModified(QTimeZone::UTC).toSecsSinceEpoch())
717 % HexString<quint64>(info.size())
718 % HexString<uint>(data ? data->pixelType() : QPlatformPixmap::PixmapType);
720 if (inGuiThread && QPixmapCache::find(key,
this))
723 data = QPlatformPixmap::create(0, 0, data ? data->pixelType() : QPlatformPixmap::PixmapType);
725 if (data->fromFile(fileName, format, flags)) {
727 QPixmapCache::insert(key, *
this);
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
761bool QPixmap::loadFromData(
const uchar *buf, uint len,
const char *format, Qt::ImageConversionFlags flags)
763 if (len == 0 || buf ==
nullptr) {
768 data = QPlatformPixmap::create(0, 0, QPlatformPixmap::PixmapType);
770 if (data->fromData(buf, len, format, flags))
778
779
780
781
782
783
784
788
789
790
791
792
793
794
795
796
797
798
799
800
801
803bool QPixmap::save(
const QString &fileName,
const char *format,
int quality)
const
807 QImageWriter writer(fileName, format);
808 return doImageIO(&writer, quality);
812
813
814
815
816
817
818
819
821bool QPixmap::save(QIODevice* device,
const char* format,
int quality)
const
825 QImageWriter writer(device, format);
826 return doImageIO(&writer, quality);
830
831bool QPixmap::doImageIO(QImageWriter *writer,
int quality)
const
833 if (quality > 100 || quality < -1)
834 qWarning(
"QPixmap::save: quality out of range [-1,100]");
836 writer->setQuality(qMin(quality,100));
837 return writer->write(toImage());
842
843
844
845
846
847
848
850void QPixmap::fill(
const QColor &color)
857 if (paintingActive() && (color.alpha() != 255) && !hasAlphaChannel()) {
858 qWarning(
"QPixmap::fill: Cannot fill while pixmap is being painted on");
862 if (data->ref.loadRelaxed() == 1) {
869 QPlatformPixmap *d = data->createCompatiblePlatformPixmap();
870 d->resize(data->width(), data->height());
871 d->setDevicePixelRatio(data->devicePixelRatio());
878
879
880
881
882
883
884qint64 QPixmap::cacheKey()
const
890 return data->cacheKey();
894static void sendResizeEvents(QWidget *target)
896 QResizeEvent e(target->size(), QSize());
897 QApplication::sendEvent(target, &e);
899 const QObjectList children = target->children();
900 for (
int i = 0; i < children.size(); ++i) {
901 QWidget *child =
static_cast<QWidget*>(children.at(i));
902 if (child->isWidgetType() && !child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
903 sendResizeEvents(child);
910
911
912#if !defined(QT_NO_DATASTREAM)
914
915
916
917
918
919
920
921
923QDataStream &operator<<(QDataStream &stream,
const QPixmap &pixmap)
925 return stream << pixmap.toImage();
929
930
931
932
933
934
936QDataStream &operator>>(QDataStream &stream, QPixmap &pixmap)
941 if (image.isNull()) {
943 }
else if (image.depth() == 1) {
944 pixmap = QBitmap::fromImage(std::move(image));
946 pixmap = QPixmap::fromImage(std::move(image));
954
955
957bool QPixmap::isDetached()
const
959 return data && data->ref.loadRelaxed() == 1;
963
964
965
966
967
968
969
970
971bool QPixmap::convertFromImage(
const QImage &image, Qt::ImageConversionFlags flags)
974 if (image.isNull() || !data)
975 *
this = QPixmap::fromImage(image, flags);
977 data->fromImage(image, flags);
982
983
984
985
986
987
988
989
990
991
992
993
994
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029QPixmap Q_TRACE_INSTRUMENT(qtgui) QPixmap::scaled(
const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode)
const
1032 qWarning(
"QPixmap::scaled: Pixmap is a null pixmap");
1038 QSize newSize = size();
1039 newSize.scale(s, aspectMode);
1040 newSize.rwidth() = qMax(newSize.width(), 1);
1041 newSize.rheight() = qMax(newSize.height(), 1);
1042 if (newSize == size())
1045 Q_TRACE_SCOPE(QPixmap_scaled, s, aspectMode, mode);
1047 QTransform wm = QTransform::fromScale((qreal)newSize.width() / width(),
1048 (qreal)newSize.height() / height());
1049 QPixmap pix = transformed(wm, mode);
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067QPixmap Q_TRACE_INSTRUMENT(qtgui) QPixmap::scaledToWidth(
int w, Qt::TransformationMode mode)
const
1070 qWarning(
"QPixmap::scaleWidth: Pixmap is a null pixmap");
1076 Q_TRACE_SCOPE(QPixmap_scaledToWidth, w, mode);
1078 qreal factor = (qreal) w / width();
1079 QTransform wm = QTransform::fromScale(factor, factor);
1080 return transformed(wm, mode);
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097QPixmap Q_TRACE_INSTRUMENT(qtgui) QPixmap::scaledToHeight(
int h, Qt::TransformationMode mode)
const
1100 qWarning(
"QPixmap::scaleHeight: Pixmap is a null pixmap");
1106 Q_TRACE_SCOPE(QPixmap_scaledToHeight, h, mode);
1108 qreal factor = (qreal) h / height();
1109 QTransform wm = QTransform::fromScale(factor, factor);
1110 return transformed(wm, mode);
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131QPixmap QPixmap::transformed(
const QTransform &transform,
1132 Qt::TransformationMode mode)
const
1134 if (isNull() || transform.type() <= QTransform::TxTranslate)
1137 return data->transformed(transform, mode);
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1306
1307
1308
1311
1312
1313
1316
1317
1318
1319
1320
1321bool QPixmap::hasAlpha()
const
1323 return data && data->hasAlphaChannel();
1327
1328
1329
1330
1331
1332bool QPixmap::hasAlphaChannel()
const
1334 return data && data->hasAlphaChannel();
1338
1339
1340int QPixmap::metric(PaintDeviceMetric metric)
const
1342 return data ? data->metric(metric) : 0;
1346
1347
1348QPaintEngine *QPixmap::paintEngine()
const
1350 return data ? data->paintEngine() :
nullptr;
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363QBitmap QPixmap::mask()
const
1365 return data ? data->mask() : QBitmap();
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378int QPixmap::defaultDepth()
1380 QScreen *primary = QGuiApplication::primaryScreen();
1381 if (Q_LIKELY(primary))
1382 return primary->depth();
1383 qWarning(
"QPixmap: QGuiApplication must be created before calling defaultDepth().");
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404void QPixmap::detach()
1411 QPlatformPixmap *pd = handle();
1412 QPlatformPixmap::ClassId id = pd->classId();
1413 if (id == QPlatformPixmap::RasterClass) {
1414 QRasterPlatformPixmap *rasterData =
static_cast<QRasterPlatformPixmap*>(pd);
1415 rasterData->image.detach();
1418 if (data->is_cached && data->ref.loadRelaxed() == 1)
1419 QImagePixmapCleanupHooks::executePlatformPixmapModificationHooks(data.data());
1421 if (data->ref.loadRelaxed() != 1) {
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442QPixmap QPixmap::fromImage(
const QImage &image, Qt::ImageConversionFlags flags)
1447 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1448 qWarning(
"QPixmap::fromImage: QPixmap cannot be created without a QGuiApplication");
1452 std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1453 data->fromImage(image, flags);
1454 return QPixmap(data.release());
1458
1459
1460
1461
1462
1463
1467
1468
1469QPixmap QPixmap::fromImageInPlace(QImage &image, Qt::ImageConversionFlags flags)
1474 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1475 qWarning(
"QPixmap::fromImageInPlace: QPixmap cannot be created without a QGuiApplication");
1479 std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1480 data->fromImageInPlace(image, flags);
1481 return QPixmap(data.release());
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
1498 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1499 qWarning(
"QPixmap::fromImageReader: QPixmap cannot be created without a QGuiApplication");
1503 std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1504 data->fromImageReader(imageReader, flags);
1505 return QPixmap(data.release());
1509
1510
1511QPlatformPixmap* QPixmap::handle()
const
1516#ifndef QT_NO_DEBUG_STREAM
1517QDebug operator<<(QDebug dbg,
const QPixmap &r)
1519 QDebugStateSaver saver(dbg);
1526 dbg << r.size() <<
",depth=" << r.depth()
1527 <<
",devicePixelRatio=" << r.devicePixelRatio()
1528 <<
",cacheKey=" << Qt::showbase << Qt::hex << r.cacheKey() << Qt::dec << Qt::noshowbase;
Combined button and popup list for selecting options.
static bool qt_pixmap_thread_test()