diff options
Diffstat (limited to 'src/gui/painting/qbrush.cpp')
-rw-r--r-- | src/gui/painting/qbrush.cpp | 218 |
1 files changed, 84 insertions, 134 deletions
diff --git a/src/gui/painting/qbrush.cpp b/src/gui/painting/qbrush.cpp index c9113cdc75..b053896233 100644 --- a/src/gui/painting/qbrush.cpp +++ b/src/gui/painting/qbrush.cpp @@ -1,42 +1,6 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtGui module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qbrush.h" #include "qpixmap.h" @@ -54,9 +18,17 @@ #include <QtCore/qnumeric.h> #include <QtCore/qfile.h> #include <QtCore/qmutex.h> +#include <QtCore/private/qoffsetstringarray_p.h> QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + +#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) +// Avoid an ABI break due to the QScopedPointer->std::unique_ptr change +static_assert(sizeof(QBrush::DataPtr) == sizeof(QScopedPointer<QBrushData, QBrushDataPointerDeleter>)); +#endif + const uchar *qt_patternForBrush(int brushStyle, bool invert) { Q_ASSERT(brushStyle > Qt::SolidPattern && brushStyle < Qt::LinearGradientPattern); @@ -109,7 +81,7 @@ Q_GUI_EXPORT QPixmap qt_pixmapForBrush(int brushStyle, bool invert) { QPixmap pm; - QString key = QLatin1String("$qt-brush$") + QString key = "$qt-brush$"_L1 % HexString<uint>(brushStyle) % QLatin1Char(invert ? '1' : '0'); if (!QPixmapCache::find(key, &pm)) { @@ -231,7 +203,7 @@ bool Q_GUI_EXPORT qHasPixmapTexture(const QBrush& brush) { if (brush.style() != Qt::TexturePattern) return false; - QTexturedBrushData *tx_data = static_cast<QTexturedBrushData *>(brush.d.data()); + QTexturedBrushData *tx_data = static_cast<QTexturedBrushData *>(brush.d.get()); return tx_data->m_has_pixmap_texture; } @@ -240,31 +212,27 @@ struct QGradientBrushData : public QBrushData QGradient gradient; }; -struct QBrushDataPointerDeleter +static void deleteData(QBrushData *d) { - static inline void deleteData(QBrushData *d) - { - switch (d->style) { - case Qt::TexturePattern: - delete static_cast<QTexturedBrushData*>(d); - break; - case Qt::LinearGradientPattern: - case Qt::RadialGradientPattern: - case Qt::ConicalGradientPattern: - delete static_cast<QGradientBrushData*>(d); - break; - default: - delete d; - } + switch (d->style) { + case Qt::TexturePattern: + delete static_cast<QTexturedBrushData*>(d); + break; + case Qt::LinearGradientPattern: + case Qt::RadialGradientPattern: + case Qt::ConicalGradientPattern: + delete static_cast<QGradientBrushData*>(d); + break; + default: + delete d; } +} - static inline void cleanup(QBrushData *d) - { - if (d && !d->ref.deref()) { - deleteData(d); - } - } -}; +void QBrushDataPointerDeleter::operator()(QBrushData *d) const noexcept +{ + if (d && !d->ref.deref()) + deleteData(d); +} /*! \class QBrush @@ -532,7 +500,7 @@ QBrush::QBrush(Qt::GlobalColor color, const QPixmap &pixmap) */ QBrush::QBrush(const QBrush &other) - : d(other.d.data()) + : d(other.d.get()) { d->ref.ref(); } @@ -559,7 +527,7 @@ QBrush::QBrush(const QGradient &gradient) }; init(QColor(), enum_table[gradient.type()]); - QGradientBrushData *grad = static_cast<QGradientBrushData *>(d.data()); + QGradientBrushData *grad = static_cast<QGradientBrushData *>(d.get()); grad->gradient = gradient; } @@ -571,12 +539,7 @@ QBrush::~QBrush() { } -void QBrush::cleanUp(QBrushData *x) -{ - QBrushDataPointerDeleter::deleteData(x); -} - -static Q_DECL_CONSTEXPR inline bool use_same_brushdata(Qt::BrushStyle lhs, Qt::BrushStyle rhs) +static constexpr inline bool use_same_brushdata(Qt::BrushStyle lhs, Qt::BrushStyle rhs) { return lhs == rhs // includes Qt::TexturePattern || (lhs >= Qt::NoBrush && lhs <= Qt::DiagCrossPattern && rhs >= Qt::NoBrush && rhs <= Qt::DiagCrossPattern) @@ -591,12 +554,12 @@ void QBrush::detach(Qt::BrushStyle newStyle) return; } - QScopedPointer<QBrushData, QBrushDataPointerDeleter> x; + DataPtr x; switch(newStyle) { case Qt::TexturePattern: { QTexturedBrushData *tbd = new QTexturedBrushData; if (d->style == Qt::TexturePattern) { - QTexturedBrushData *data = static_cast<QTexturedBrushData *>(d.data()); + QTexturedBrushData *data = static_cast<QTexturedBrushData *>(d.get()); if (data->m_has_pixmap_texture) tbd->setPixmap(data->pixmap()); else @@ -614,7 +577,7 @@ void QBrush::detach(Qt::BrushStyle newStyle) case Qt::RadialGradientPattern: case Qt::ConicalGradientPattern: gbd->gradient = - static_cast<QGradientBrushData *>(d.data())->gradient; + static_cast<QGradientBrushData *>(d.get())->gradient; break; default: break; @@ -647,7 +610,7 @@ QBrush &QBrush::operator=(const QBrush &b) return *this; b.d->ref.ref(); - d.reset(b.d.data()); + d.reset(b.d.get()); return *this; } @@ -672,7 +635,7 @@ QBrush &QBrush::operator=(const QBrush &b) */ QBrush::operator QVariant() const { - return QVariant(QMetaType::QBrush, this); + return QVariant::fromValue(*this); } /*! @@ -748,7 +711,7 @@ void QBrush::setColor(const QColor &c) QPixmap QBrush::texture() const { return d->style == Qt::TexturePattern - ? (static_cast<QTexturedBrushData *>(d.data()))->pixmap() + ? (static_cast<QTexturedBrushData *>(d.get()))->pixmap() : QPixmap(); } @@ -766,7 +729,7 @@ void QBrush::setTexture(const QPixmap &pixmap) { if (!pixmap.isNull()) { detach(Qt::TexturePattern); - QTexturedBrushData *data = static_cast<QTexturedBrushData *>(d.data()); + QTexturedBrushData *data = static_cast<QTexturedBrushData *>(d.get()); data->setPixmap(pixmap); } else { detach(Qt::NoBrush); @@ -789,7 +752,7 @@ void QBrush::setTexture(const QPixmap &pixmap) QImage QBrush::textureImage() const { return d->style == Qt::TexturePattern - ? (static_cast<QTexturedBrushData *>(d.data()))->image() + ? (static_cast<QTexturedBrushData *>(d.get()))->image() : QImage(); } @@ -814,7 +777,7 @@ void QBrush::setTextureImage(const QImage &image) { if (!image.isNull()) { detach(Qt::TexturePattern); - QTexturedBrushData *data = static_cast<QTexturedBrushData *>(d.data()); + QTexturedBrushData *data = static_cast<QTexturedBrushData *>(d.get()); data->setImage(image); } else { detach(Qt::NoBrush); @@ -830,7 +793,7 @@ const QGradient *QBrush::gradient() const if (d->style == Qt::LinearGradientPattern || d->style == Qt::RadialGradientPattern || d->style == Qt::ConicalGradientPattern) { - return &static_cast<const QGradientBrushData *>(d.data())->gradient; + return &static_cast<const QGradientBrushData *>(d.get())->gradient; } return nullptr; } @@ -866,7 +829,7 @@ Q_GUI_EXPORT bool qt_isExtendedRadialGradient(const QBrush &brush) bool QBrush::isOpaque() const { - bool opaqueColor = d->color.alpha() == 255; + bool opaqueColor = d->color.alphaF() >= 1.0f; // Test awfully simple case first if (d->style == Qt::SolidPattern) @@ -880,7 +843,7 @@ bool QBrush::isOpaque() const || d->style == Qt::ConicalGradientPattern) { QGradientStops stops = gradient()->stops(); for (int i=0; i<stops.size(); ++i) - if (stops.at(i).second.alpha() != 255) + if (stops.at(i).second.alphaF() < 1.0f) return false; return true; } else if (d->style == Qt::TexturePattern) { @@ -948,16 +911,16 @@ bool QBrush::operator==(const QBrush &b) const const QPixmap *us = nullptr, *them = nullptr; qint64 cacheKey1, cacheKey2; if (qHasPixmapTexture(*this)) { - us = (static_cast<QTexturedBrushData *>(d.data()))->m_pixmap; + us = (static_cast<QTexturedBrushData *>(d.get()))->m_pixmap; cacheKey1 = us->cacheKey(); } else - cacheKey1 = (static_cast<QTexturedBrushData *>(d.data()))->image().cacheKey(); + cacheKey1 = (static_cast<QTexturedBrushData *>(d.get()))->image().cacheKey(); if (qHasPixmapTexture(b)) { - them = (static_cast<QTexturedBrushData *>(b.d.data()))->m_pixmap; + them = (static_cast<QTexturedBrushData *>(b.d.get()))->m_pixmap; cacheKey2 = them->cacheKey(); } else - cacheKey2 = (static_cast<QTexturedBrushData *>(b.d.data()))->image().cacheKey(); + cacheKey2 = (static_cast<QTexturedBrushData *>(b.d.get()))->image().cacheKey(); if (cacheKey1 != cacheKey2) return false; @@ -974,8 +937,8 @@ bool QBrush::operator==(const QBrush &b) const case Qt::RadialGradientPattern: case Qt::ConicalGradientPattern: { - const QGradientBrushData *d1 = static_cast<QGradientBrushData *>(d.data()); - const QGradientBrushData *d2 = static_cast<QGradientBrushData *>(b.d.data()); + const QGradientBrushData *d1 = static_cast<QGradientBrushData *>(d.get()); + const QGradientBrushData *d2 = static_cast<QGradientBrushData *>(b.d.get()); return d1->gradient == d2->gradient; } default: @@ -989,7 +952,7 @@ bool QBrush::operator==(const QBrush &b) const */ QDebug operator<<(QDebug dbg, const QBrush &b) { - static const char BRUSH_STYLES[][24] = { + static constexpr auto BRUSH_STYLES = qOffsetStringArray( "NoBrush", "SolidPattern", "Dense1Pattern", @@ -1010,7 +973,7 @@ QDebug operator<<(QDebug dbg, const QBrush &b) "ConicalGradientPattern", "", "", "", "", "", "", "TexturePattern" // 24 - }; + ); QDebugStateSaver saver(dbg); dbg.nospace() << "QBrush(" << b.color() << ',' << BRUSH_STYLES[b.style()] << ')'; @@ -1086,6 +1049,8 @@ QDataStream &operator<<(QDataStream &s, const QBrush &b) s << static_cast<const QRadialGradient *>(gradient)->center(); s << static_cast<const QRadialGradient *>(gradient)->focalPoint(); s << (double) static_cast<const QRadialGradient *>(gradient)->radius(); + if (s.version() >= QDataStream::Qt_6_0) + s << (double) static_cast<const QRadialGradient *>(gradient)->focalRadius(); } else { // type == Conical s << static_cast<const QConicalGradient *>(gradient)->center(); s << (double) static_cast<const QConicalGradient *>(gradient)->angle(); @@ -1176,6 +1141,7 @@ QDataStream &operator>>(QDataStream &s, QBrush &b) } else if (type == QGradient::RadialGradient) { QPointF center, focal; double radius; + double focalRadius = 0; s >> center; s >> focal; s >> radius; @@ -1184,6 +1150,9 @@ QDataStream &operator>>(QDataStream &s, QBrush &b) rg.setSpread(spread); rg.setCoordinateMode(cmode); rg.setInterpolationMode(imode); + if (s.version() >= QDataStream::Qt_6_0) + s >> focalRadius; + rg.setFocalRadius(focalRadius); b = QBrush(rg); } else { // type == QGradient::ConicalGradient QPointF center; @@ -1271,7 +1240,7 @@ QDataStream &operator>>(QDataStream &s, QBrush &b) \snippet brush/brush.cpp 1 It is possible to repeat or reflect the gradient outside its area - by specifiying the \l {QGradient::Spread}{spread method} using the + by specifying the \l {QGradient::Spread}{spread method} using the setSpread() function. The default is to pad the outside area with the color at the closest stop point. The currently set \l {QGradient::Spread}{spread method} can be retrieved using the @@ -1312,7 +1281,7 @@ QDataStream &operator>>(QDataStream &s, QBrush &b) \internal */ QGradient::QGradient() - : m_type(NoGradient), dummy(nullptr) + : m_type(NoGradient) { } @@ -1507,10 +1476,9 @@ QGradient::QGradient() */ QGradient::QGradient(Preset preset) : m_type(LinearGradient) - , m_spread(PadSpread) , m_stops(qt_preset_gradient_stops(preset)) , m_data(qt_preset_gradient_data[preset - 1]) - , dummy(qt_preset_gradient_dummy()) + , m_coordinateMode(ObjectMode) { } @@ -1639,7 +1607,6 @@ static inline bool ok(const QGradientStops &stops) */ void QGradient::setStops(const QGradientStops &stops) { - // ## Qt 6: consider taking \a stops by value, so we can move into m_stops if (Q_LIKELY(ok(stops))) { // fast path for the common case: if everything is ok with the stops, just copy them m_stops = stops; @@ -1664,15 +1631,14 @@ void QGradient::setStops(const QGradientStops &stops) QGradientStops QGradient::stops() const { if (m_stops.isEmpty()) { - QGradientStops tmp; - tmp << QGradientStop(0, Qt::black) << QGradientStop(1, Qt::white); - return tmp; + static constexpr QGradientStop blackAndWhite[] = { + {0, QColorConstants::Black}, {1, QColorConstants::White}, + }; + return QGradientStops::fromReadOnlyData(blackAndWhite); } return m_stops; } -#define Q_DUMMY_ACCESSOR union {void *p; uint i;}; p = dummy; - /*! \enum QGradient::CoordinateMode \since 4.4 @@ -1705,8 +1671,7 @@ QGradientStops QGradient::stops() const */ QGradient::CoordinateMode QGradient::coordinateMode() const { - Q_DUMMY_ACCESSOR - return CoordinateMode(i & 0x03); + return m_coordinateMode; } /*! @@ -1717,10 +1682,7 @@ QGradient::CoordinateMode QGradient::coordinateMode() const */ void QGradient::setCoordinateMode(CoordinateMode mode) { - Q_DUMMY_ACCESSOR - i &= ~0x03; - i |= uint(mode); - dummy = p; + m_coordinateMode = mode; } /*! @@ -1743,8 +1705,7 @@ void QGradient::setCoordinateMode(CoordinateMode mode) */ QGradient::InterpolationMode QGradient::interpolationMode() const { - Q_DUMMY_ACCESSOR - return InterpolationMode((i >> 2) & 0x01); + return m_interpolationMode; } /*! @@ -1756,10 +1717,7 @@ QGradient::InterpolationMode QGradient::interpolationMode() const */ void QGradient::setInterpolationMode(InterpolationMode mode) { - Q_DUMMY_ACCESSOR - i &= ~(1 << 2); - i |= (uint(mode) << 2); - dummy = p; + m_interpolationMode = mode; } /*! @@ -1782,7 +1740,8 @@ bool QGradient::operator==(const QGradient &gradient) const { if (gradient.m_type != m_type || gradient.m_spread != m_spread - || gradient.dummy != dummy) return false; + || gradient.m_coordinateMode != m_coordinateMode + || gradient.m_interpolationMode != m_interpolationMode) return false; if (m_type == LinearGradient) { if (m_data.linear.x1 != gradient.m_data.linear.x1 @@ -1795,7 +1754,8 @@ bool QGradient::operator==(const QGradient &gradient) const || m_data.radial.cy != gradient.m_data.radial.cy || m_data.radial.fx != gradient.m_data.radial.fx || m_data.radial.fy != gradient.m_data.radial.fy - || m_data.radial.cradius != gradient.m_data.radial.cradius) + || m_data.radial.cradius != gradient.m_data.radial.cradius + || m_data.radial.fradius != gradient.m_data.radial.fradius) return false; } else { // m_type == ConicalGradient if (m_data.conical.cx != gradient.m_data.conical.cx @@ -2069,6 +2029,7 @@ QRadialGradient::QRadialGradient(const QPointF ¢er, qreal radius, const QPoi m_data.radial.cx = center.x(); m_data.radial.cy = center.y(); m_data.radial.cradius = radius; + m_data.radial.fradius = 0; QPointF adapted_focal = qt_radial_gradient_adapt_focal_point(center, radius, focalPoint); m_data.radial.fx = adapted_focal.x(); @@ -2088,6 +2049,7 @@ QRadialGradient::QRadialGradient(const QPointF ¢er, qreal radius) m_data.radial.cx = center.x(); m_data.radial.cy = center.y(); m_data.radial.cradius = radius; + m_data.radial.fradius = 0; m_data.radial.fx = center.x(); m_data.radial.fy = center.y(); } @@ -2133,6 +2095,7 @@ QRadialGradient::QRadialGradient() m_data.radial.cx = 0; m_data.radial.cy = 0; m_data.radial.cradius = 1; + m_data.radial.fradius = 0; m_data.radial.fx = 0; m_data.radial.fy = 0; } @@ -2150,6 +2113,7 @@ QRadialGradient::QRadialGradient(const QPointF ¢er, qreal centerRadius, cons m_data.radial.cx = center.x(); m_data.radial.cy = center.y(); m_data.radial.cradius = centerRadius; + m_data.radial.fradius = 0; m_data.radial.fx = focalPoint.x(); m_data.radial.fy = focalPoint.y(); @@ -2170,6 +2134,7 @@ QRadialGradient::QRadialGradient(qreal cx, qreal cy, qreal centerRadius, qreal f m_data.radial.cx = cx; m_data.radial.cy = cy; m_data.radial.cradius = centerRadius; + m_data.radial.fradius = 0; m_data.radial.fx = fx; m_data.radial.fy = fy; @@ -2289,12 +2254,7 @@ void QRadialGradient::setCenterRadius(qreal radius) qreal QRadialGradient::focalRadius() const { Q_ASSERT(m_type == RadialGradient); - Q_DUMMY_ACCESSOR - - // mask away low three bits - union { float f; quint32 i; } u; - u.i = i & ~0x07; - return u.f; + return m_data.radial.fradius; } /*! @@ -2306,17 +2266,7 @@ qreal QRadialGradient::focalRadius() const void QRadialGradient::setFocalRadius(qreal radius) { Q_ASSERT(m_type == RadialGradient); - Q_DUMMY_ACCESSOR - - // Since there's no QGradientData, we only have the dummy void * to - // store additional data in. The three lowest bits are already - // taken, thus we cut the three lowest bits from the significand - // and store the radius as a float. - union { float f; quint32 i; } u; - u.f = float(radius); - // add 0x04 to round up when we drop the three lowest bits - i |= (u.i + 0x04) & ~0x07; - dummy = p; + m_data.radial.fradius = radius; } /*! @@ -2561,6 +2511,6 @@ void QConicalGradient::setAngle(qreal angle) \sa setTransform() */ -#undef Q_DUMMY_ACCESSOR - QT_END_NAMESPACE + +#include "moc_qbrush.cpp" |