summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVolker Hilsheimer <[email protected]>2024-11-26 16:05:10 +0100
committerVolker Hilsheimer <[email protected]>2024-11-29 21:55:55 +0100
commitf0186862e16128343705abd0de5994e8ca05a909 (patch)
tree6b592e63d2dcc23e161b44b71f75a3be28081cc5
parent97ca5fd859d4e462ef620a9cce254eebac2eaee7 (diff)
QBrush, QPen: Provide explicit comparison for types we compare with
Instead of implicitly creating a QPen or a QBrush from a color or style just to be able to compare the result with the left-hand side pen or brush, provide comparesEqual friends that can take a shortcut, avoiding the allocation that comes with the implicit construction of the types. operator==(Brush, BrushStyle) needs some extra handling, as we will create a NoBrush brush if the style alone is not enough anyway. Change-Id: I06e3d585160ad4fb5d12a46baf8c6a0aaaeeb0b0 Reviewed-by: Christian Ehrlicher <[email protected]>
-rw-r--r--src/gui/painting/qbrush.h26
-rw-r--r--src/gui/painting/qpen.cpp15
-rw-r--r--src/gui/painting/qpen.h16
-rw-r--r--tests/auto/gui/painting/qbrush/tst_qbrush.cpp10
-rw-r--r--tests/auto/gui/painting/qpen/tst_qpen.cpp4
5 files changed, 68 insertions, 3 deletions
diff --git a/src/gui/painting/qbrush.h b/src/gui/painting/qbrush.h
index ca8112ff31f..b5958f853c3 100644
--- a/src/gui/painting/qbrush.h
+++ b/src/gui/painting/qbrush.h
@@ -82,6 +82,32 @@ private:
friend struct QSpanData;
friend class QPainter;
friend bool Q_GUI_EXPORT qHasPixmapTexture(const QBrush& brush);
+
+ friend bool comparesEqual(const QBrush &lhs, QColor rhs) noexcept
+ {
+ return lhs.color() == rhs && lhs.style() == Qt::SolidPattern
+ && lhs.transform().isIdentity();
+ }
+ Q_DECLARE_EQUALITY_COMPARABLE(QBrush, QColor)
+ Q_DECLARE_EQUALITY_COMPARABLE(QBrush, Qt::GlobalColor)
+
+ friend bool comparesEqual(const QBrush &lhs, Qt::BrushStyle rhs) noexcept
+ {
+ switch (rhs) {
+ case Qt::NoBrush:
+ case Qt::TexturePattern:
+ case Qt::LinearGradientPattern:
+ case Qt::RadialGradientPattern:
+ case Qt::ConicalGradientPattern:
+ // A brush constructed only from one of those styles will end up
+ // using NoBrush (see qbrush_check_type)
+ return lhs.style() == Qt::NoBrush;
+ default:
+ return lhs.style() == rhs && lhs.color() == QColor(0, 0, 0);
+ }
+ }
+ Q_DECLARE_EQUALITY_COMPARABLE(QBrush, Qt::BrushStyle)
+
void detach(Qt::BrushStyle newStyle);
void init(const QColor &color, Qt::BrushStyle bs);
DataPtr d;
diff --git a/src/gui/painting/qpen.cpp b/src/gui/painting/qpen.cpp
index fc151763aac..89dbdc0d3df 100644
--- a/src/gui/painting/qpen.cpp
+++ b/src/gui/painting/qpen.cpp
@@ -199,8 +199,8 @@ QPenPrivate::QPenPrivate(const QBrush &_brush, qreal _width, Qt::PenStyle penSty
joinStyle = _joinStyle;
}
-static const Qt::PenCapStyle qpen_default_cap = Qt::SquareCap;
-static const Qt::PenJoinStyle qpen_default_join = Qt::BevelJoin;
+static constexpr Qt::PenCapStyle qpen_default_cap = Qt::SquareCap;
+static constexpr Qt::PenJoinStyle qpen_default_join = Qt::BevelJoin;
class QPenDataHolder
{
@@ -769,7 +769,16 @@ void QPen::setCosmetic(bool cosmetic)
d->cosmetic = cosmetic;
}
-
+/*!
+ \internal
+*/
+bool QPen::isSolidDefaultLine() const noexcept
+{
+ return d->style == Qt::SolidLine && d->width == 1
+ && d->capStyle == qpen_default_cap && d->joinStyle == qpen_default_join
+ && qFuzzyCompare(d->dashOffset, 0) && qFuzzyCompare(d->miterLimit, 2)
+ && d->cosmetic == false;
+}
/*!
\fn bool QPen::operator!=(const QPen &pen) const
diff --git a/src/gui/painting/qpen.h b/src/gui/painting/qpen.h
index 3367b96c358..abd5d914628 100644
--- a/src/gui/painting/qpen.h
+++ b/src/gui/painting/qpen.h
@@ -86,6 +86,22 @@ private:
friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPen &);
friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPen &);
+ bool isSolidDefaultLine() const noexcept;
+
+ friend bool comparesEqual(const QPen &lhs, QColor rhs) noexcept
+ {
+ return lhs.brush() == rhs && lhs.isSolidDefaultLine();
+ }
+ Q_DECLARE_EQUALITY_COMPARABLE(QPen, QColor)
+
+ friend bool comparesEqual(const QPen &lhs, Qt::PenStyle rhs)
+ {
+ if (rhs == Qt::NoPen)
+ return lhs.style() == Qt::NoPen;
+ return lhs == QPen(rhs); // allocates
+ }
+ Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QPen, Qt::PenStyle)
+
public:
using DataPtr = QExplicitlySharedDataPointer<QPenPrivate>;
diff --git a/tests/auto/gui/painting/qbrush/tst_qbrush.cpp b/tests/auto/gui/painting/qbrush/tst_qbrush.cpp
index 678c8d9b326..c56fcc8216d 100644
--- a/tests/auto/gui/painting/qbrush/tst_qbrush.cpp
+++ b/tests/auto/gui/painting/qbrush/tst_qbrush.cpp
@@ -90,6 +90,16 @@ void tst_QBrush::operator_eq_eq()
QFETCH(QBrush, brush2);
QFETCH(bool, isEqual);
QCOMPARE(brush1 == brush2, isEqual);
+
+ // exercise operator== overloads
+ QCOMPARE(QBrush(brush1.color()), brush1.color());
+ QCOMPARE(QBrush(brush1.style()), brush1.style());
+ QCOMPARE(QBrush(Qt::black), Qt::black);
+
+ // exercise operator== overloads with implicit construction
+ const QGradient warmFlame = QGradient::WarmFlame;
+ QCOMPARE(QBrush(warmFlame), warmFlame);
+ QCOMPARE(QBrush(QImage()), QImage());
}
void tst_QBrush::stream_data()
diff --git a/tests/auto/gui/painting/qpen/tst_qpen.cpp b/tests/auto/gui/painting/qpen/tst_qpen.cpp
index b3ff1c76f9e..c9d43663d5c 100644
--- a/tests/auto/gui/painting/qpen/tst_qpen.cpp
+++ b/tests/auto/gui/painting/qpen/tst_qpen.cpp
@@ -155,6 +155,10 @@ void tst_QPen::operator_eq_eq()
QFETCH(QPen, pen2);
QFETCH(bool, isEqual);
QCOMPARE(pen1 == pen2, isEqual);
+
+ // exercise operator== overloads
+ QCOMPARE(QPen(pen1.style()), pen1.style());
+ QCOMPARE(QPen(pen1.color()), pen1.color());
}