From 99132276fcde2c032e870d832ca5532af9b50889 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Mon, 24 Oct 2022 16:02:46 +0200 Subject: [PATCH] first adjustments for QskGradient improvements --- skins/squiek/QskSquiekSkin.cpp | 8 +- src/common/QskGradient.cpp | 136 ++++++++++++++++----------------- src/common/QskGradient.h | 111 ++++++++++++++------------- src/nodes/QskBoxNode.cpp | 2 +- 4 files changed, 129 insertions(+), 128 deletions(-) diff --git a/skins/squiek/QskSquiekSkin.cpp b/skins/squiek/QskSquiekSkin.cpp index 52400ffb..22cac9c0 100644 --- a/skins/squiek/QskSquiekSkin.cpp +++ b/skins/squiek/QskSquiekSkin.cpp @@ -201,7 +201,7 @@ void Editor::setButton( QskAspect aspect, PanelStyle style, qreal border ) { borderColors.setGradientAt( Qt::TopEdge | Qt::LeftEdge, m_pal.lighter135 ); borderColors.setGradientAt( Qt::RightEdge | Qt::BottomEdge, m_pal.darker200 ); - gradient.setColors( m_pal.lighter125, m_pal.lighter110 ); + gradient.setStops( m_pal.lighter125, m_pal.lighter110 ); break; } @@ -209,14 +209,14 @@ void Editor::setButton( QskAspect aspect, PanelStyle style, qreal border ) { borderColors.setGradientAt( Qt::TopEdge | Qt::LeftEdge, m_pal.darker200 ); borderColors.setGradientAt( Qt::RightEdge | Qt::BottomEdge, m_pal.lighter135 ); - gradient.setColors( m_pal.lighter110, m_pal.lighter125 ); + gradient.setStops( m_pal.lighter110, m_pal.lighter125 ); break; } case Plain: { borderColors.setGradients( m_pal.darker125 ); - gradient.setColor( m_pal.lighter125 ); + gradient.setStops( m_pal.lighter125 ); break; } @@ -227,7 +227,7 @@ void Editor::setButton( QskAspect aspect, PanelStyle style, qreal border ) noColor.setAlpha( 0 ); borderColors.setGradients( noColor ); - gradient.setColor( noColor ); + gradient.setStops( noColor ); if ( style == NoPanel ) border = 0; diff --git a/src/common/QskGradient.cpp b/src/common/QskGradient.cpp index 2d7a7f76..129ea200 100644 --- a/src/common/QskGradient.cpp +++ b/src/common/QskGradient.cpp @@ -230,7 +230,7 @@ static inline QskGradientStops qskColorStops( return stops; } -QskGradient::QskGradient( Orientation orientation ) +QskGradient::QskGradient( Orientation orientation ) noexcept : m_orientation( orientation ) , m_isDirty( false ) , m_isValid( false ) @@ -242,7 +242,7 @@ QskGradient::QskGradient( Orientation orientation ) QskGradient::QskGradient( const QColor& color ) : QskGradient( Vertical ) { - setColor( color ); + setStops( color ); } QskGradient::QskGradient( Qt::Orientation orientation, @@ -255,7 +255,7 @@ QskGradient::QskGradient( Orientation orientation, const QColor& startColor, const QColor& stopColor ) : QskGradient( orientation ) { - setColors( startColor, stopColor ); + setStops( startColor, stopColor ); } QskGradient::QskGradient( Qt::Orientation orientation, const QskGradientStops& stops ) @@ -284,7 +284,31 @@ QskGradient::~QskGradient() { } -bool QskGradient::isValid() const +bool QskGradient::operator==( const QskGradient& other ) const noexcept +{ + return ( m_orientation == other.m_orientation ) && ( m_stops == other.m_stops ); +} + +void QskGradient::updateStatusBits() const +{ + // doing all bits in one loop ? + m_isValid = qskIsGradientValid( m_stops ); + + if ( m_isValid ) + { + m_isMonchrome = qskIsMonochrome( m_stops ); + m_isVisible = qskIsVisible( m_stops ); + } + else + { + m_isMonchrome = true; + m_isVisible = false; + } + + m_isDirty = false; +} + +bool QskGradient::isValid() const noexcept { if ( m_isDirty ) updateStatusBits(); @@ -292,16 +316,7 @@ bool QskGradient::isValid() const return m_isValid; } -void QskGradient::invalidate() -{ - if ( !m_stops.isEmpty() ) - { - m_stops.clear(); - m_isDirty = true; - } -} - -bool QskGradient::isMonochrome() const +bool QskGradient::isMonochrome() const noexcept { if ( m_isDirty ) updateStatusBits(); @@ -309,7 +324,7 @@ bool QskGradient::isMonochrome() const return m_isMonchrome; } -bool QskGradient::isVisible() const +bool QskGradient::isVisible() const noexcept { if ( m_isDirty ) updateStatusBits(); @@ -317,18 +332,18 @@ bool QskGradient::isVisible() const return m_isVisible; } -void QskGradient::setOrientation( Qt::Orientation orientation ) +void QskGradient::setOrientation( Qt::Orientation orientation ) noexcept { setOrientation( qskOrientation( orientation ) ); } -void QskGradient::setOrientation( Orientation orientation ) +void QskGradient::setOrientation( Orientation orientation ) noexcept { // does not change m_isDirty m_orientation = orientation; } -void QskGradient::setColor( const QColor& color ) +void QskGradient::setStops( const QColor& color ) { m_stops.clear(); m_stops.reserve( 2 ); @@ -339,7 +354,7 @@ void QskGradient::setColor( const QColor& color ) m_isDirty = true; } -void QskGradient::setColors( const QColor& startColor, const QColor& stopColor ) +void QskGradient::setStops( const QColor& startColor, const QColor& stopColor ) { m_stops.clear(); m_stops.reserve( 2 ); @@ -352,31 +367,19 @@ void QskGradient::setColors( const QColor& startColor, const QColor& stopColor ) void QskGradient::setStops( const QskGradientStops& stops ) { - if ( !qskIsGradientValid( stops ) ) + if ( !stops.isEmpty() && !qskIsGradientValid( stops ) ) { qWarning( "Invalid gradient stops" ); - invalidate(); - return; + m_stops.clear(); + } + else + { + m_stops = stops; } - m_stops = stops; m_isDirty = true; } -const QskGradientStops& QskGradient::stops() const -{ -#if 1 - /* - Returning a const& so that it is possible to write: - for ( const auto& stop : qAsConst( gradient.stops() ) ) - - Once we have changed QskGradientStop from QColor to QRgb - we should check if there is a better solution possible - */ -#endif - return m_stops; -} - int QskGradient::stopCount() const { return m_stops.count(); @@ -413,7 +416,7 @@ void QskGradient::setAlpha( int alpha ) m_isDirty = true; } -bool QskGradient::hasStopAt( qreal value ) const +bool QskGradient::hasStopAt( qreal value ) const noexcept { // better use binary search TODO ... for ( auto& stop : m_stops ) @@ -428,20 +431,6 @@ bool QskGradient::hasStopAt( qreal value ) const return false; } -QskHashValue QskGradient::hash( QskHashValue seed ) const -{ - if ( m_stops.isEmpty() ) - return seed; - - const auto o = orientation(); - - auto hash = qHashBits( &o, sizeof( o ), seed ); - for ( const auto& stop : m_stops ) - hash = stop.hash( hash ); - - return hash; -} - void QskGradient::reverse() { if ( isMonochrome() ) @@ -622,25 +611,6 @@ QVariant QskGradient::interpolate( return QVariant::fromValue( from.interpolated( to, progress ) ); } -void QskGradient::updateStatusBits() const -{ - // doing all bits in one loop ? - m_isValid = qskIsGradientValid( m_stops ); - - if ( m_isValid ) - { - m_isMonchrome = qskIsMonochrome( m_stops ); - m_isVisible = qskIsVisible( m_stops ); - } - else - { - m_isMonchrome = true; - m_isVisible = false; - } - - m_isDirty = false; -} - QskGradientStops QskGradient::colorStops( const QVector< QRgb >& rgb, bool discrete ) { @@ -674,6 +644,30 @@ QGradientStops QskGradient::qtStops() const return qstops; } +void QskGradient::clearStops() +{ + if ( !m_stops.isEmpty() ) + { + m_stops.clear(); + m_isDirty = true; + } +} + +QskHashValue QskGradient::hash( QskHashValue seed ) const +{ + if ( m_stops.isEmpty() ) + return seed; + + const auto o = orientation(); + + auto hash = qHashBits( &o, sizeof( o ), seed ); + for ( const auto& stop : m_stops ) + hash = stop.hash( hash ); + + return hash; +} + + #ifndef QT_NO_DEBUG_STREAM #include diff --git a/src/common/QskGradient.h b/src/common/QskGradient.h index c7ed3ffe..305d3ac8 100644 --- a/src/common/QskGradient.h +++ b/src/common/QskGradient.h @@ -38,11 +38,11 @@ class QSK_EXPORT QskGradient Vertical, Diagonal }; - Q_ENUM( Orientation ) - QskGradient(); - QskGradient( Orientation ); + QskGradient() noexcept = default; + + QskGradient( Orientation ) noexcept; QskGradient( Qt::GlobalColor ); QskGradient( QRgb ); QskGradient( const QColor& ); @@ -58,32 +58,33 @@ class QSK_EXPORT QskGradient ~QskGradient(); - void setOrientation( Qt::Orientation ); - void setOrientation( Orientation ); - Orientation orientation() const; + bool operator==( const QskGradient& ) const noexcept; + bool operator!=( const QskGradient& ) const noexcept; - bool isValid() const; - Q_INVOKABLE void invalidate(); + void setOrientation( Qt::Orientation ) noexcept; + void setOrientation( Orientation ) noexcept; + Orientation orientation() const noexcept; - bool operator==( const QskGradient& ) const; - bool operator!=( const QskGradient& ) const; - - void setColor( const QColor& ); - void setColors( const QColor&, const QColor& ); - - Q_INVOKABLE QColor startColor() const; - Q_INVOKABLE QColor endColor() const; + bool isValid() const noexcept; + bool isMonochrome() const noexcept; + bool isVisible() const noexcept; Q_INVOKABLE void setStops( const QVector< QskGradientStop >& ); - Q_INVOKABLE const QVector< QskGradientStop >& stops() const; + Q_INVOKABLE const QVector< QskGradientStop >& stops() const noexcept; - Q_INVOKABLE bool hasStopAt( qreal value ) const; + void setStops( const QColor& ); + void setStops( const QColor&, const QColor& ); + void setStops( QGradient::Preset ); + + void clearStops(); + + Q_INVOKABLE bool hasStopAt( qreal value ) const noexcept; + + Q_INVOKABLE QColor startColor() const noexcept; + Q_INVOKABLE QColor endColor() const noexcept; void setAlpha( int alpha ); - bool isMonochrome() const; - bool isVisible() const; - void reverse(); QskGradient reversed() const; @@ -99,6 +100,7 @@ class QSK_EXPORT QskGradient Q_INVOKABLE qreal stopAt( int index ) const; Q_INVOKABLE QColor colorAt( int index ) const; + Q_INVOKABLE int stopCount() const; QGradientStops qtStops() const; @@ -109,23 +111,19 @@ class QSK_EXPORT QskGradient private: void updateStatusBits() const; + private: QVector< QskGradientStop > m_stops; - int m_orientation : 4; + Orientation m_orientation = Vertical; - mutable bool m_isDirty : 1; - mutable bool m_isValid : 1; - mutable bool m_isMonchrome : 1; - mutable bool m_isVisible : 1; + mutable bool m_isDirty = false; + mutable bool m_isValid = false; + mutable bool m_isMonchrome = true; + mutable bool m_isVisible = false; }; Q_DECLARE_METATYPE( QskGradient ) -inline QskGradient::QskGradient() - : QskGradient( Vertical ) -{ -} - inline QskGradient::QskGradient( Qt::GlobalColor color ) : QskGradient( QColor( color ) ) { @@ -141,31 +139,40 @@ inline QskGradient::QskGradient( QGradient::Preset preset ) { } -inline QskGradient::Orientation QskGradient::orientation() const -{ - return static_cast< Orientation >( m_orientation ); -} - -inline QColor QskGradient::startColor() const -{ - return ( m_stops.size() >= 2 ) ? m_stops.first().color() : QColor(); -} - -inline QColor QskGradient::endColor() const -{ - return ( m_stops.size() >= 2 ) ? m_stops.last().color() : QColor(); -} - -inline bool QskGradient::operator==( const QskGradient& other ) const -{ - return ( m_orientation == other.m_orientation ) && ( m_stops == other.m_stops ); -} - -inline bool QskGradient::operator!=( const QskGradient& other ) const +inline bool QskGradient::operator!=( const QskGradient& other ) const noexcept { return ( !( *this == other ) ); } +inline QskGradient::Orientation QskGradient::orientation() const noexcept +{ + return m_orientation; +} + +inline const QskGradientStops& QskGradient::stops() const noexcept +{ +#if 1 + /* + Returning a const& so that it is possible to write: + for ( const auto& stop : qAsConst( gradient.stops() ) ) + + Once we have changed QskGradientStop from QColor to QRgb + we should check if there is a better solution possible + */ +#endif + return m_stops; +} + +inline QColor QskGradient::startColor() const noexcept +{ + return m_stops.isEmpty() ? QColor() : m_stops.first().color(); +} + +inline QColor QskGradient::endColor() const noexcept +{ + return m_stops.isEmpty() ? QColor() : m_stops.last().color(); +} + #ifndef QT_NO_DEBUG_STREAM class QDebug; diff --git a/src/nodes/QskBoxNode.cpp b/src/nodes/QskBoxNode.cpp index bc052a67..33dc0ca8 100644 --- a/src/nodes/QskBoxNode.cpp +++ b/src/nodes/QskBoxNode.cpp @@ -84,7 +84,7 @@ void QskBoxNode::setBoxData( const QRectF& rect, // Renderer is buggy for monochrome gradients with stops. TODO ... if ( fillGradient.stops().count() > 2 && fillGradient.isMonochrome() ) { - fillGradient.setColor( fillGradient.startColor() ); + fillGradient.setStops( fillGradient.startColor() ); } #endif