implement comparison operators for QPixmap and QBitmap

it is that simple

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-03-30 12:46:30 +02:00
parent 5766fb7da0
commit e28900041a
11 changed files with 48 additions and 33 deletions

View file

@ -241,8 +241,9 @@ QPixmap QPixmapIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::St
QStyleOption opt(0);
opt.palette = QApplication::palette();
QPixmap active = QApplication::style()->generatedIconPixmap(QIcon::Active, pm, &opt);
if (pm.cacheKey() == active.cacheKey())
if (pm == active) {
return pm;
}
}
}

View file

@ -291,6 +291,36 @@ QPixmap &QPixmap::operator=(const QPixmap &pixmap)
fast and never fails.
*/
/*!
Returns true if this image and the given \a image have the same
contents; otherwise returns false.
The comparison can be slow, unless there is some obvious
difference (e.g. different size or format), in which case the
function will return quickly.
\sa operator=()
*/
bool QPixmap::operator==(const QPixmap &pixmap) const
{
return toImage() == pixmap.toImage();
}
/*!
Returns true if this pixmap and the given \a pixmap have different
contents; otherwise returns false.
The comparison can be slow, unless there is some obvious
difference, such as different widths, in which case the function
will return quickly.
\sa operator=()
*/
bool QPixmap::operator!=(const QPixmap &pixmap) const
{
return !(*this == pixmap);
}
/*!
Returns the pixmap as a QVariant.
*/

View file

@ -57,13 +57,15 @@ public:
{ qSwap(data, other.data); return *this; }
inline void swap(QPixmap &other) { qSwap(data, other.data); }
bool operator==(const QPixmap &) const;
bool operator!=(const QPixmap &) const;
operator QVariant() const;
bool isNull() const; // ### Qt 5: make inline
bool isNull() const;
int devType() const;
int width() const; // ### Qt 5: make inline
int height() const; // ### Qt 5: make inline
int width() const;
int height() const;
QSize size() const;
QRect rect() const;
int depth() const;

View file

@ -113,8 +113,7 @@ static bool compare(const QVariantPrivate *a, const QVariantPrivate *b)
return v_cast<QCursor>(a)->shape() == v_cast<QCursor>(b)->shape();
#endif
case QVariant::Bitmap:
return v_cast<QBitmap>(a)->cacheKey()
== v_cast<QBitmap>(b)->cacheKey();
return *v_cast<QBitmap>(a) == *v_cast<QBitmap>(b);
case QVariant::Polygon:
return *v_cast<QPolygon>(a) == *v_cast<QPolygon>(b);
case QVariant::Region:
@ -122,7 +121,7 @@ static bool compare(const QVariantPrivate *a, const QVariantPrivate *b)
case QVariant::Font:
return *v_cast<QFont>(a) == *v_cast<QFont>(b);
case QVariant::Pixmap:
return v_cast<QPixmap>(a)->cacheKey() == v_cast<QPixmap>(b)->cacheKey();
return *v_cast<QPixmap>(a) == *v_cast<QPixmap>(b);
case QVariant::Image:
return *v_cast<QImage>(a) == *v_cast<QImage>(b);
case QVariant::Brush:

View file

@ -794,7 +794,7 @@ bool QBrush::operator==(const QBrush &other) const
case Qt::TexturePattern: {
const QPixmap &us = (static_cast<QTexturedBrushData *>(d))->pixmap();
const QPixmap &them = (static_cast<QTexturedBrushData *>(other.d))->pixmap();
return ((us.isNull() && them.isNull()) || us.cacheKey() == them.cacheKey());
return (us == them);
}
case Qt::LinearGradientPattern:
case Qt::RadialGradientPattern: {

View file

@ -2490,28 +2490,29 @@ void QTextHtmlExporter::emitBlock(const QTextBlock &block)
extern bool qHasPixmapTexture(const QBrush& brush);
QString QTextHtmlExporter::findUrlForImage(const QTextDocument *doc, qint64 cacheKey, bool isPixmap)
QString QTextHtmlExporter::findUrlForImage(const QTextDocument *doc, const QBrush &brush)
{
QString url;
if (!doc)
return url;
if (QTextDocument *parent = qobject_cast<QTextDocument *>(doc->parent()))
return findUrlForImage(parent, cacheKey, isPixmap);
return findUrlForImage(parent, brush);
if (doc && doc->docHandle()) {
const bool isPixmap = qHasPixmapTexture(brush);
QTextDocumentPrivate *priv = doc->docHandle();
QMap<QUrl, QVariant>::const_iterator it = priv->cachedResources.constBegin();
for (; it != priv->cachedResources.constEnd(); ++it) {
const QVariant &v = it.value();
if (v.type() == QVariant::Image && !isPixmap) {
if (qvariant_cast<QImage>(v).cacheKey() == cacheKey)
if (qvariant_cast<QImage>(v) == brush.textureImage())
break;
}
if (v.type() == QVariant::Pixmap && isPixmap) {
if (qvariant_cast<QPixmap>(v).cacheKey() == cacheKey)
if (qvariant_cast<QPixmap>(v) == brush.texture())
break;
}
}
@ -2541,10 +2542,7 @@ void QTextHtmlExporter::emitBackgroundAttribute(const QTextFormat &format)
if (brush.style() == Qt::SolidPattern) {
emitAttribute("bgcolor", brush.color().name());
} else if (brush.style() == Qt::TexturePattern) {
const bool isPixmap = qHasPixmapTexture(brush);
const qint64 cacheKey = isPixmap ? brush.texture().cacheKey() : brush.textureImage().cacheKey();
const QString url = findUrlForImage(doc, cacheKey, isPixmap);
const QString url = findUrlForImage(doc, brush);
if (!url.isEmpty())
emitAttribute("background", url);

View file

@ -374,7 +374,7 @@ private:
void emitFontFamily(const QString &family);
void emitBackgroundAttribute(const QTextFormat &format);
QString findUrlForImage(const QTextDocument *doc, qint64 cacheKey, bool isPixmap);
QString findUrlForImage(const QTextDocument *doc, const QBrush &brush);
QString html;
QTextCharFormat defaultCharFormat;

View file

@ -303,7 +303,7 @@ void QLabel::clear()
void QLabel::setPixmap(const QPixmap &pixmap)
{
Q_D(QLabel);
if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) {
if (!d->pixmap || *d->pixmap != pixmap) {
d->clearContents();
d->pixmap = new QPixmap(pixmap);
}

View file

@ -33,7 +33,6 @@
#include <QtTest/qtestmouse.h>
#include <QtTest/qtestkeyboard.h>
#include <QtGui/qicon.h>
#include <QtGui/qpixmap.h>
QT_BEGIN_NAMESPACE
@ -50,13 +49,6 @@ inline bool qCompare(QIcon const &t1, QIcon const &t2, const char *actual, const
*reinterpret_cast<void * const *>(&t2), actual, expected, file, line);
}
template<>
inline bool qCompare(QPixmap const &t1, QPixmap const &t2, const char *actual, const char *expected,
const char *file, int line)
{
return qCompare(t1.toImage(), t2.toImage(), actual, expected, file, line);
}
}
#ifdef Q_WS_X11

View file

@ -2050,10 +2050,6 @@ bool QTest::compare_string_helper(const char *t1, const char *t2, const char *ac
\internal
*/
/*! \fn bool QTest::qCompare(QPixmap const &t1, QPixmap const &t2, const char *actual, const char *expected, const char *file, int line)
\internal
*/
/*! \fn bool QTest::qCompare(T const &t1, T const &t2, const char *actual, const char *expected, const char *file, int line)
\internal
*/

View file

@ -857,9 +857,6 @@ void tst_QDataStream::stream_QBrush_data()
void tst_QDataStream::stream_QBrush()
{
if (QString(QTest::currentDataTag()).endsWith("6"))
QSKIP("Custom brushes don't seem to be supported with QDataStream", SkipSingle);
STREAM_IMPL(QBrush);
}