first adjustments for QskGradient improvements

This commit is contained in:
Uwe Rathmann 2022-10-24 16:02:46 +02:00
parent 4a04a27d0d
commit 99132276fc
4 changed files with 129 additions and 128 deletions

View File

@ -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::TopEdge | Qt::LeftEdge, m_pal.lighter135 );
borderColors.setGradientAt( Qt::RightEdge | Qt::BottomEdge, m_pal.darker200 ); 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; 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::TopEdge | Qt::LeftEdge, m_pal.darker200 );
borderColors.setGradientAt( Qt::RightEdge | Qt::BottomEdge, m_pal.lighter135 ); 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; break;
} }
case Plain: case Plain:
{ {
borderColors.setGradients( m_pal.darker125 ); borderColors.setGradients( m_pal.darker125 );
gradient.setColor( m_pal.lighter125 ); gradient.setStops( m_pal.lighter125 );
break; break;
} }
@ -227,7 +227,7 @@ void Editor::setButton( QskAspect aspect, PanelStyle style, qreal border )
noColor.setAlpha( 0 ); noColor.setAlpha( 0 );
borderColors.setGradients( noColor ); borderColors.setGradients( noColor );
gradient.setColor( noColor ); gradient.setStops( noColor );
if ( style == NoPanel ) if ( style == NoPanel )
border = 0; border = 0;

View File

@ -230,7 +230,7 @@ static inline QskGradientStops qskColorStops(
return stops; return stops;
} }
QskGradient::QskGradient( Orientation orientation ) QskGradient::QskGradient( Orientation orientation ) noexcept
: m_orientation( orientation ) : m_orientation( orientation )
, m_isDirty( false ) , m_isDirty( false )
, m_isValid( false ) , m_isValid( false )
@ -242,7 +242,7 @@ QskGradient::QskGradient( Orientation orientation )
QskGradient::QskGradient( const QColor& color ) QskGradient::QskGradient( const QColor& color )
: QskGradient( Vertical ) : QskGradient( Vertical )
{ {
setColor( color ); setStops( color );
} }
QskGradient::QskGradient( Qt::Orientation orientation, QskGradient::QskGradient( Qt::Orientation orientation,
@ -255,7 +255,7 @@ QskGradient::QskGradient( Orientation orientation,
const QColor& startColor, const QColor& stopColor ) const QColor& startColor, const QColor& stopColor )
: QskGradient( orientation ) : QskGradient( orientation )
{ {
setColors( startColor, stopColor ); setStops( startColor, stopColor );
} }
QskGradient::QskGradient( Qt::Orientation orientation, const QskGradientStops& stops ) 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 ) if ( m_isDirty )
updateStatusBits(); updateStatusBits();
@ -292,16 +316,7 @@ bool QskGradient::isValid() const
return m_isValid; return m_isValid;
} }
void QskGradient::invalidate() bool QskGradient::isMonochrome() const noexcept
{
if ( !m_stops.isEmpty() )
{
m_stops.clear();
m_isDirty = true;
}
}
bool QskGradient::isMonochrome() const
{ {
if ( m_isDirty ) if ( m_isDirty )
updateStatusBits(); updateStatusBits();
@ -309,7 +324,7 @@ bool QskGradient::isMonochrome() const
return m_isMonchrome; return m_isMonchrome;
} }
bool QskGradient::isVisible() const bool QskGradient::isVisible() const noexcept
{ {
if ( m_isDirty ) if ( m_isDirty )
updateStatusBits(); updateStatusBits();
@ -317,18 +332,18 @@ bool QskGradient::isVisible() const
return m_isVisible; return m_isVisible;
} }
void QskGradient::setOrientation( Qt::Orientation orientation ) void QskGradient::setOrientation( Qt::Orientation orientation ) noexcept
{ {
setOrientation( qskOrientation( orientation ) ); setOrientation( qskOrientation( orientation ) );
} }
void QskGradient::setOrientation( Orientation orientation ) void QskGradient::setOrientation( Orientation orientation ) noexcept
{ {
// does not change m_isDirty // does not change m_isDirty
m_orientation = orientation; m_orientation = orientation;
} }
void QskGradient::setColor( const QColor& color ) void QskGradient::setStops( const QColor& color )
{ {
m_stops.clear(); m_stops.clear();
m_stops.reserve( 2 ); m_stops.reserve( 2 );
@ -339,7 +354,7 @@ void QskGradient::setColor( const QColor& color )
m_isDirty = true; 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.clear();
m_stops.reserve( 2 ); m_stops.reserve( 2 );
@ -352,29 +367,17 @@ void QskGradient::setColors( const QColor& startColor, const QColor& stopColor )
void QskGradient::setStops( const QskGradientStops& stops ) void QskGradient::setStops( const QskGradientStops& stops )
{ {
if ( !qskIsGradientValid( stops ) ) if ( !stops.isEmpty() && !qskIsGradientValid( stops ) )
{ {
qWarning( "Invalid gradient stops" ); qWarning( "Invalid gradient stops" );
invalidate(); m_stops.clear();
return;
} }
else
m_stops = stops;
m_isDirty = true;
}
const QskGradientStops& QskGradient::stops() const
{ {
#if 1 m_stops = stops;
/* }
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 m_isDirty = true;
we should check if there is a better solution possible
*/
#endif
return m_stops;
} }
int QskGradient::stopCount() const int QskGradient::stopCount() const
@ -413,7 +416,7 @@ void QskGradient::setAlpha( int alpha )
m_isDirty = true; m_isDirty = true;
} }
bool QskGradient::hasStopAt( qreal value ) const bool QskGradient::hasStopAt( qreal value ) const noexcept
{ {
// better use binary search TODO ... // better use binary search TODO ...
for ( auto& stop : m_stops ) for ( auto& stop : m_stops )
@ -428,20 +431,6 @@ bool QskGradient::hasStopAt( qreal value ) const
return false; 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() void QskGradient::reverse()
{ {
if ( isMonochrome() ) if ( isMonochrome() )
@ -622,25 +611,6 @@ QVariant QskGradient::interpolate(
return QVariant::fromValue( from.interpolated( to, progress ) ); 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( QskGradientStops QskGradient::colorStops(
const QVector< QRgb >& rgb, bool discrete ) const QVector< QRgb >& rgb, bool discrete )
{ {
@ -674,6 +644,30 @@ QGradientStops QskGradient::qtStops() const
return qstops; 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 #ifndef QT_NO_DEBUG_STREAM
#include <qdebug.h> #include <qdebug.h>

View File

@ -38,11 +38,11 @@ class QSK_EXPORT QskGradient
Vertical, Vertical,
Diagonal Diagonal
}; };
Q_ENUM( Orientation ) Q_ENUM( Orientation )
QskGradient(); QskGradient() noexcept = default;
QskGradient( Orientation );
QskGradient( Orientation ) noexcept;
QskGradient( Qt::GlobalColor ); QskGradient( Qt::GlobalColor );
QskGradient( QRgb ); QskGradient( QRgb );
QskGradient( const QColor& ); QskGradient( const QColor& );
@ -58,32 +58,33 @@ class QSK_EXPORT QskGradient
~QskGradient(); ~QskGradient();
void setOrientation( Qt::Orientation ); bool operator==( const QskGradient& ) const noexcept;
void setOrientation( Orientation ); bool operator!=( const QskGradient& ) const noexcept;
Orientation orientation() const;
bool isValid() const; void setOrientation( Qt::Orientation ) noexcept;
Q_INVOKABLE void invalidate(); void setOrientation( Orientation ) noexcept;
Orientation orientation() const noexcept;
bool operator==( const QskGradient& ) const; bool isValid() const noexcept;
bool operator!=( const QskGradient& ) const; bool isMonochrome() const noexcept;
bool isVisible() const noexcept;
void setColor( const QColor& );
void setColors( const QColor&, const QColor& );
Q_INVOKABLE QColor startColor() const;
Q_INVOKABLE QColor endColor() const;
Q_INVOKABLE void setStops( const QVector< QskGradientStop >& ); 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 ); void setAlpha( int alpha );
bool isMonochrome() const;
bool isVisible() const;
void reverse(); void reverse();
QskGradient reversed() const; QskGradient reversed() const;
@ -99,6 +100,7 @@ class QSK_EXPORT QskGradient
Q_INVOKABLE qreal stopAt( int index ) const; Q_INVOKABLE qreal stopAt( int index ) const;
Q_INVOKABLE QColor colorAt( int index ) const; Q_INVOKABLE QColor colorAt( int index ) const;
Q_INVOKABLE int stopCount() const; Q_INVOKABLE int stopCount() const;
QGradientStops qtStops() const; QGradientStops qtStops() const;
@ -109,23 +111,19 @@ class QSK_EXPORT QskGradient
private: private:
void updateStatusBits() const; void updateStatusBits() const;
private:
QVector< QskGradientStop > m_stops; QVector< QskGradientStop > m_stops;
int m_orientation : 4; Orientation m_orientation = Vertical;
mutable bool m_isDirty : 1; mutable bool m_isDirty = false;
mutable bool m_isValid : 1; mutable bool m_isValid = false;
mutable bool m_isMonchrome : 1; mutable bool m_isMonchrome = true;
mutable bool m_isVisible : 1; mutable bool m_isVisible = false;
}; };
Q_DECLARE_METATYPE( QskGradient ) Q_DECLARE_METATYPE( QskGradient )
inline QskGradient::QskGradient()
: QskGradient( Vertical )
{
}
inline QskGradient::QskGradient( Qt::GlobalColor color ) inline QskGradient::QskGradient( Qt::GlobalColor color )
: QskGradient( QColor( color ) ) : QskGradient( QColor( color ) )
{ {
@ -141,31 +139,40 @@ inline QskGradient::QskGradient( QGradient::Preset preset )
{ {
} }
inline QskGradient::Orientation QskGradient::orientation() const inline bool QskGradient::operator!=( const QskGradient& other ) const noexcept
{
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
{ {
return ( !( *this == other ) ); 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 #ifndef QT_NO_DEBUG_STREAM
class QDebug; class QDebug;

View File

@ -84,7 +84,7 @@ void QskBoxNode::setBoxData( const QRectF& rect,
// Renderer is buggy for monochrome gradients with stops. TODO ... // Renderer is buggy for monochrome gradients with stops. TODO ...
if ( fillGradient.stops().count() > 2 && fillGradient.isMonochrome() ) if ( fillGradient.stops().count() > 2 && fillGradient.isMonochrome() )
{ {
fillGradient.setColor( fillGradient.startColor() ); fillGradient.setStops( fillGradient.startColor() );
} }
#endif #endif