From 861c4d854879b4e0358ad2f02d22753c37d57006 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Fri, 11 Sep 2020 15:24:17 +0200 Subject: Deprecate implicit QPixmap conversion to QBitmap It is lossy, so should be requested explicitly, using a dedicated fromPixmap factory function. Deprecate the constructor and assignment operator, and make the constructor explicit. [ChangeLog][QtGui][QBitmap] Implicitly constructing and assigning to a QBitmap from a QPixmap has been deprecated, and the respective constructor has been made explicit. Use the fromPixmap factory function instead. Change-Id: I68ce85b26c901415137b664a1db687021d48bae0 Reviewed-by: Lars Knoll --- .../widgets/mainwindows/mainwindow/colorswatch.cpp | 2 +- src/gui/image/qbitmap.cpp | 105 +++++++++++++-------- src/gui/image/qbitmap.h | 9 +- src/gui/kernel/qguivariant.cpp | 2 +- src/gui/kernel/qshapedpixmapdndwindow.cpp | 2 +- .../serialization/qdatastream/tst_qdatastream.cpp | 2 +- tests/auto/gui/painting/qregion/tst_qregion.cpp | 2 +- 7 files changed, 76 insertions(+), 48 deletions(-) diff --git a/examples/widgets/mainwindows/mainwindow/colorswatch.cpp b/examples/widgets/mainwindows/mainwindow/colorswatch.cpp index 77e59d764e..cb568a7a5a 100644 --- a/examples/widgets/mainwindows/mainwindow/colorswatch.cpp +++ b/examples/widgets/mainwindows/mainwindow/colorswatch.cpp @@ -680,7 +680,7 @@ void BlueTitleBar::updateMask() Q_ASSERT(dw); QRect rect = dw->rect(); - QPixmap bitmap(dw->size()); + QBitmap bitmap(dw->size()); { QPainter painter(&bitmap); diff --git a/src/gui/image/qbitmap.cpp b/src/gui/image/qbitmap.cpp index 58953e8d9a..0505717fb3 100644 --- a/src/gui/image/qbitmap.cpp +++ b/src/gui/image/qbitmap.cpp @@ -138,20 +138,6 @@ QBitmap::QBitmap(const QSize &size) Clears the bitmap, setting all its bits to Qt::color0. */ -/*! - Constructs a bitmap that is a copy of the given \a pixmap. - - If the pixmap has a depth greater than 1, the resulting bitmap - will be dithered automatically. - - \sa QPixmap::depth(), fromImage(), fromData() -*/ - -QBitmap::QBitmap(const QPixmap &pixmap) -{ - QBitmap::operator=(pixmap); -} - /*! Constructs a bitmap from the file specified by the given \a fileName. If the file does not exist, or has an unknown format, @@ -170,30 +156,6 @@ QBitmap::QBitmap(const QString& fileName, const char *format) load(fileName, format, Qt::MonoOnly); } -/*! - \overload - - Assigns the given \a pixmap to this bitmap and returns a reference - to this bitmap. - - If the pixmap has a depth greater than 1, the resulting bitmap - will be dithered automatically. - - \sa QPixmap::depth() - */ - -QBitmap &QBitmap::operator=(const QPixmap &pixmap) -{ - if (pixmap.isNull()) { // a null pixmap - QBitmap(0, 0).swap(*this); - } else if (pixmap.depth() == 1) { // 1-bit pixmap - QPixmap::operator=(pixmap); // shallow assignment - } else { // n-bit depth pixmap - *this = fromImage(pixmap.toImage()); // will dither image - } - return *this; -} - /*! \fn void QBitmap::swap(QBitmap &other) \since 4.8 @@ -225,7 +187,7 @@ static QBitmap makeBitmap(QImage &&image, Qt::ImageConversionFlags flags) QScopedPointer data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::BitmapType)); data->fromImageInPlace(image, flags | Qt::MonoOnly); - return QPixmap(data.take()); + return QBitmap::fromPixmap(QPixmap(data.take())); } /*! @@ -287,6 +249,68 @@ QBitmap QBitmap::fromData(const QSize &size, const uchar *bits, QImage::Format m return QBitmap::fromImage(std::move(image)); } +/*! + Returns a copy of the given \a pixmap converted to a bitmap. + + If the pixmap has a depth greater than 1, the resulting bitmap + will be dithered automatically. + + \sa QPixmap::depth() +*/ + +QBitmap QBitmap::fromPixmap(const QPixmap &pixmap) +{ + if (pixmap.isNull()) { // a null pixmap + return QBitmap(0, 0); + } else if (pixmap.depth() == 1) { // 1-bit pixmap + QBitmap bm; + if (pixmap.paintingActive()) { // make a deep copy + pixmap.copy().swap(bm); + } else { + bm.data = pixmap.data; // shallow assignment + } + return bm; + } + // n-bit depth pixmap, will dither image + return fromImage(pixmap.toImage()); +} + +#if QT_DEPRECATED_SINCE(6, 0) +/*! + \obsolete Use fromPixmap instead. + Constructs a bitmap that is a copy of the given \a pixmap. + + If the pixmap has a depth greater than 1, the resulting bitmap + will be dithered automatically. + + \sa QPixmap::depth(), fromImage(), fromData() +*/ + +QBitmap::QBitmap(const QPixmap &pixmap) +{ + *this = QBitmap::fromPixmap(pixmap); +} + +/*! + \obsolete Use fromPixmap instead. + \overload + + Assigns the given \a pixmap to this bitmap and returns a reference + to this bitmap. + + If the pixmap has a depth greater than 1, the resulting bitmap + will be dithered automatically. + + \sa QPixmap::depth() + */ + +QBitmap &QBitmap::operator=(const QPixmap &pixmap) +{ + *this = QBitmap::fromPixmap(pixmap); + return *this; +} +#endif + /*! Returns a copy of this bitmap, transformed according to the given \a matrix. @@ -295,8 +319,7 @@ QBitmap QBitmap::fromData(const QSize &size, const uchar *bits, QImage::Format m */ QBitmap QBitmap::transformed(const QTransform &matrix) const { - QBitmap bm = QPixmap::transformed(matrix); - return bm; + return QBitmap::fromPixmap(QPixmap::transformed(matrix)); } QT_END_NAMESPACE diff --git a/src/gui/image/qbitmap.h b/src/gui/image/qbitmap.h index a114db17e0..9052e62db6 100644 --- a/src/gui/image/qbitmap.h +++ b/src/gui/image/qbitmap.h @@ -52,12 +52,16 @@ class Q_GUI_EXPORT QBitmap : public QPixmap { public: QBitmap(); - QBitmap(const QPixmap &); +#if QT_DEPRECATED_SINCE(6, 0) + QT_DEPRECATED_VERSION_X_6_0("Use fromPixmap instead.") explicit QBitmap(const QPixmap &); +#endif QBitmap(int w, int h); explicit QBitmap(const QSize &); explicit QBitmap(const QString &fileName, const char *format = nullptr); - QBitmap &operator=(const QPixmap &); +#if QT_DEPRECATED_SINCE(6, 0) + QT_DEPRECATED_VERSION_X_6_0("Use fromPixmap instead.") QBitmap &operator=(const QPixmap &); +#endif inline void swap(QBitmap &other) { QPixmap::swap(other); } // prevent QBitmap<->QPixmap swaps operator QVariant() const; @@ -67,6 +71,7 @@ public: static QBitmap fromImage(QImage &&image, Qt::ImageConversionFlags flags = Qt::AutoColor); static QBitmap fromData(const QSize &size, const uchar *bits, QImage::Format monoFormat = QImage::Format_MonoLSB); + static QBitmap fromPixmap(const QPixmap &pixmap); QBitmap transformed(const QTransform &matrix) const; diff --git a/src/gui/kernel/qguivariant.cpp b/src/gui/kernel/qguivariant.cpp index 1af0692173..d7e88025b9 100644 --- a/src/gui/kernel/qguivariant.cpp +++ b/src/gui/kernel/qguivariant.cpp @@ -155,7 +155,7 @@ static const struct : QMetaTypeModuleHelper QMETATYPE_CONVERTER(QPixmap, QImage, result = QPixmap::fromImage(source); return true;); QMETATYPE_CONVERTER(QImage, QPixmap, result = source.toImage(); return true;); QMETATYPE_CONVERTER(QPixmap, QBitmap, result = source; return true;); - QMETATYPE_CONVERTER(QBitmap, QPixmap, result = source; return true;); + QMETATYPE_CONVERTER(QBitmap, QPixmap, result = QBitmap::fromPixmap(source); return true;); QMETATYPE_CONVERTER(QImage, QBitmap, result = source.toImage(); return true;); QMETATYPE_CONVERTER(QBitmap, QImage, result = QBitmap::fromImage(source); return true;); QMETATYPE_CONVERTER(QPixmap, QBrush, result = source.texture(); return true;); diff --git a/src/gui/kernel/qshapedpixmapdndwindow.cpp b/src/gui/kernel/qshapedpixmapdndwindow.cpp index 1a85a5e853..e58d37693a 100644 --- a/src/gui/kernel/qshapedpixmapdndwindow.cpp +++ b/src/gui/kernel/qshapedpixmapdndwindow.cpp @@ -76,7 +76,7 @@ void QShapedPixmapWindow::setPixmap(const QPixmap &pixmap) const auto pixmapDpr = m_pixmap.devicePixelRatio(); const auto winDpr = devicePixelRatio(); const auto maskSize = (QSizeF(m_pixmap.size()) * winDpr / pixmapDpr).toSize(); - platformWindow->setMask(QBitmap(mask.scaled(maskSize))); + platformWindow->setMask(QBitmap::fromPixmap(mask.scaled(maskSize))); } } } diff --git a/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp b/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp index c992b487bc..f262996980 100644 --- a/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp +++ b/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp @@ -1176,7 +1176,7 @@ static QCursor qCursorData(int index) case 3: return QCursor(Qt::BlankCursor); case 4: return QCursor(Qt::BlankCursor); case 5: return QCursor(QPixmap(open_xpm), 1, 1); - case 6: { QPixmap pm(open_xpm); return QCursor(QBitmap(pm), pm.mask(), 3, 4); } + case 6: { QPixmap pm(open_xpm); return QCursor(QBitmap::fromPixmap(pm), pm.mask(), 3, 4); } case 7: return QCursor(QPixmap(open_xpm), -1, 5); case 8: return QCursor(QPixmap(open_xpm), 5, -1); } diff --git a/tests/auto/gui/painting/qregion/tst_qregion.cpp b/tests/auto/gui/painting/qregion/tst_qregion.cpp index dc694e1107..27327d8d1a 100644 --- a/tests/auto/gui/painting/qregion/tst_qregion.cpp +++ b/tests/auto/gui/painting/qregion/tst_qregion.cpp @@ -382,7 +382,7 @@ void tst_QRegion::bitmapRegion() QVERIFY(region.isEmpty()); } { - circle = QPixmap(circle_xpm); + circle = QBitmap::fromPixmap(QPixmap(circle_xpm)); QRegion region(circle); //// These should not be inside the circe -- cgit v1.2.3