diff --git a/src/common/QskGradient.cpp b/src/common/QskGradient.cpp index e4605c9e..064c9f4b 100644 --- a/src/common/QskGradient.cpp +++ b/src/common/QskGradient.cpp @@ -14,7 +14,6 @@ static void qskRegisterGradient() { qRegisterMetaType< QskGradient >(); - qRegisterMetaType< QskGradientStop >(); QMetaType::registerConverter< QColor, QskGradient >( []( const QColor& color ) { return QskGradient( color ); } ); @@ -168,60 +167,6 @@ static inline QVector< QskGradientStop > qskExtractedStops( return extracted; } -void QskGradientStop::setPosition( qreal position ) noexcept -{ - m_position = position; -} - -void QskGradientStop::resetPosition() noexcept -{ - m_position = -1.0; -} - -void QskGradientStop::setColor( const QColor& color ) noexcept -{ - m_color = color; -} - -void QskGradientStop::resetColor() noexcept -{ - m_color = QColor(); -} - -void QskGradientStop::setStop( qreal position, const QColor& color ) noexcept -{ - m_position = position; - m_color = color; -} - -uint QskGradientStop::hash( uint seed ) const noexcept -{ - uint hash = qHashBits( &m_position, sizeof( m_position ), seed ); - return qHashBits( &m_color, sizeof( m_color ), hash ); -} - -QColor QskGradientStop::interpolated( - const QskGradientStop& s1, const QskGradientStop& s2, qreal position ) noexcept -{ - if ( s1.color() == s2.color() ) - return s1.color(); - - auto min = &s1; - auto max = &s2; - - if ( min->position() > max->position() ) - std::swap( min, max ); - - if ( position <= min->position() ) - return min->color(); - - if ( position >= max->position() ) - return max->color(); - - const qreal r = ( position - min->position() ) / ( max->position() - min->position() ); - return QskRgb::interpolated( min->color(), max->color(), r ); -} - QskGradient::QskGradient() : m_orientation( Vertical ) { @@ -423,8 +368,7 @@ bool QskGradient::hasStopAt( qreal value ) const uint QskGradient::hash( uint seed ) const { - const int count = m_stops.size(); - if ( count == 0 ) + if ( m_stops.isEmpty() ) return seed; uint hash = qHashBits( &m_orientation, sizeof( m_orientation ), seed ); @@ -610,12 +554,6 @@ QVariant QskGradient::interpolate( #include -QDebug operator<<( QDebug debug, const QskGradientStop& stop ) -{ - debug << stop.position() << ": " << stop.color(); - return debug; -} - QDebug operator<<( QDebug debug, const QskGradient& gradient ) { debug << "GR:" << gradient.orientation() << gradient.stops().count(); diff --git a/src/common/QskGradient.h b/src/common/QskGradient.h index 4df866f1..cac89f16 100644 --- a/src/common/QskGradient.h +++ b/src/common/QskGradient.h @@ -7,55 +7,14 @@ #define QSK_GRADIENT_H #include "QskGlobal.h" +#include "QskGradientStop.h" #include #include #include -#if QT_VERSION < QT_VERSION_CHECK( 5, 14, 0 ) -/* - since Qt >= 5.14 QColor has constexpr declarations and we could declare - several methods of QskGradientStop being constexpr as well. TODO ... - */ -#endif - -class QDebug; class QVariant; -class QSK_EXPORT QskGradientStop -{ - Q_GADGET - - Q_PROPERTY( qreal position READ position WRITE setPosition RESET resetPosition ) - Q_PROPERTY( QColor color READ color WRITE setColor RESET resetColor ) - - public: - QskGradientStop() noexcept; - QskGradientStop( qreal position, const QColor& color ) noexcept; - - bool operator==( const QskGradientStop& ) const noexcept; - bool operator!=( const QskGradientStop& ) const noexcept; - - void setStop( qreal position, const QColor& color ) noexcept; - - qreal position() const noexcept; - void setPosition( qreal position ) noexcept; - void resetPosition() noexcept; - - const QColor& color() const noexcept; - void setColor( const QColor& color ) noexcept; - void resetColor() noexcept; - - static QColor interpolated( - const QskGradientStop&, const QskGradientStop&, qreal position ) noexcept; - - uint hash( uint seed ) const noexcept; - - private: - qreal m_position; - QColor m_color; -}; - class QSK_EXPORT QskGradient { Q_GADGET @@ -142,6 +101,8 @@ class QSK_EXPORT QskGradient QVector< QskGradientStop > m_stops; }; +Q_DECLARE_METATYPE( QskGradient ) + inline QskGradient::QskGradient( Qt::GlobalColor color ) : QskGradient( QColor( color ) ) { @@ -162,38 +123,6 @@ inline QColor QskGradient::endColor() const return ( m_stops.size() >= 2 ) ? m_stops.last().color() : QColor(); } -inline QskGradientStop::QskGradientStop() noexcept - : m_position( -1.0 ) -{ -} - -inline QskGradientStop::QskGradientStop( - qreal position, const QColor& color ) noexcept - : m_position( position ) - , m_color( color ) -{ -} - -inline qreal QskGradientStop::position() const noexcept -{ - return m_position; -} - -inline const QColor& QskGradientStop::color() const noexcept -{ - return m_color; -} - -inline bool QskGradientStop::operator==( const QskGradientStop& other ) const noexcept -{ - return ( m_position == other.m_position ) && ( m_color == other.m_color ); -} - -inline bool QskGradientStop::operator!=( const QskGradientStop& other ) const noexcept -{ - return ( !( *this == other ) ); -} - inline bool QskGradient::operator==( const QskGradient& other ) const { return ( m_orientation == other.m_orientation ) && ( m_stops == other.m_stops ); @@ -204,14 +133,10 @@ inline bool QskGradient::operator!=( const QskGradient& other ) const return ( !( *this == other ) ); } -Q_DECLARE_TYPEINFO( QskGradientStop, Q_MOVABLE_TYPE ); - -Q_DECLARE_METATYPE( QskGradientStop ) -Q_DECLARE_METATYPE( QskGradient ) - #ifndef QT_NO_DEBUG_STREAM -QSK_EXPORT QDebug operator<<( QDebug, const QskGradientStop& ); +class QDebug; + QSK_EXPORT QDebug operator<<( QDebug, const QskGradient& ); #endif diff --git a/src/common/QskGradientStop.cpp b/src/common/QskGradientStop.cpp new file mode 100644 index 00000000..d982eca3 --- /dev/null +++ b/src/common/QskGradientStop.cpp @@ -0,0 +1,87 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the QSkinny License, Version 1.0 + *****************************************************************************/ + +#include "QskGradient.h" +#include "QskRgbValue.h" + +#include +#include + +#include + +static void qskRegisterGradientStop() +{ + qRegisterMetaType< QskGradientStop >(); +} + +Q_CONSTRUCTOR_FUNCTION( qskRegisterGradientStop ) + +void QskGradientStop::setPosition( qreal position ) noexcept +{ + m_position = position; +} + +void QskGradientStop::resetPosition() noexcept +{ + m_position = -1.0; +} + +void QskGradientStop::setColor( const QColor& color ) noexcept +{ + m_color = color; +} + +void QskGradientStop::resetColor() noexcept +{ + m_color = QColor(); +} + +void QskGradientStop::setStop( qreal position, const QColor& color ) noexcept +{ + m_position = position; + m_color = color; +} + +uint QskGradientStop::hash( uint seed ) const noexcept +{ + uint hash = qHashBits( &m_position, sizeof( m_position ), seed ); + return qHashBits( &m_color, sizeof( m_color ), hash ); +} + +QColor QskGradientStop::interpolated( + const QskGradientStop& s1, const QskGradientStop& s2, qreal position ) noexcept +{ + if ( s1.color() == s2.color() ) + return s1.color(); + + auto min = &s1; + auto max = &s2; + + if ( min->position() > max->position() ) + std::swap( min, max ); + + if ( position <= min->position() ) + return min->color(); + + if ( position >= max->position() ) + return max->color(); + + const qreal r = ( position - min->position() ) / ( max->position() - min->position() ); + return QskRgb::interpolated( min->color(), max->color(), r ); +} + +#ifndef QT_NO_DEBUG_STREAM + +#include + +QDebug operator<<( QDebug debug, const QskGradientStop& stop ) +{ + debug << stop.position() << ": " << stop.color(); + return debug; +} + +#endif + +#include "moc_QskGradientStop.cpp" diff --git a/src/common/QskGradientStop.h b/src/common/QskGradientStop.h new file mode 100644 index 00000000..2371ba77 --- /dev/null +++ b/src/common/QskGradientStop.h @@ -0,0 +1,98 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the QSkinny License, Version 1.0 + *****************************************************************************/ + +#ifndef QSK_GRADIENT_STOP_H +#define QSK_GRADIENT_STOP_H + +#include "QskGlobal.h" + +#include +#include + +#if QT_VERSION < QT_VERSION_CHECK( 5, 14, 0 ) +/* + since Qt >= 5.14 QColor has constexpr declarations and we could declare + several methods of QskGradientStop being constexpr as well. TODO ... + */ +#endif + +class QSK_EXPORT QskGradientStop +{ + Q_GADGET + + Q_PROPERTY( qreal position READ position WRITE setPosition RESET resetPosition ) + Q_PROPERTY( QColor color READ color WRITE setColor RESET resetColor ) + + public: + QskGradientStop() noexcept; + QskGradientStop( qreal position, const QColor& ) noexcept; + + bool operator==( const QskGradientStop& ) const noexcept; + bool operator!=( const QskGradientStop& ) const noexcept; + + void setStop( qreal position, const QColor& ) noexcept; + + qreal position() const noexcept; + void setPosition( qreal position ) noexcept; + void resetPosition() noexcept; + + const QColor& color() const noexcept; + void setColor( const QColor& color ) noexcept; + void resetColor() noexcept; + + static QColor interpolated( + const QskGradientStop&, const QskGradientStop&, qreal position ) noexcept; + + uint hash( uint seed ) const noexcept; + + private: + qreal m_position; + QColor m_color; // using RGBA instead ? +}; + +Q_DECLARE_TYPEINFO( QskGradientStop, Q_MOVABLE_TYPE ); +Q_DECLARE_METATYPE( QskGradientStop ) + +inline QskGradientStop::QskGradientStop() noexcept + : m_position( -1.0 ) +{ +} + +inline QskGradientStop::QskGradientStop( + qreal position, const QColor& color ) noexcept + : m_position( position ) + , m_color( color ) +{ +} + +inline qreal QskGradientStop::position() const noexcept +{ + return m_position; +} + +inline const QColor& QskGradientStop::color() const noexcept +{ + return m_color; +} + +inline bool QskGradientStop::operator==( const QskGradientStop& other ) const noexcept +{ + return ( m_position == other.m_position ) && ( m_color == other.m_color ); +} + +inline bool QskGradientStop::operator!=( const QskGradientStop& other ) const noexcept +{ + return ( !( *this == other ) ); +} + +#ifndef QT_NO_DEBUG_STREAM + +class QDebug; + +QSK_EXPORT QDebug operator<<( QDebug, const QskGradientStop& ); + +#endif + +#endif diff --git a/src/src.pro b/src/src.pro index 8d49744c..00a55da6 100644 --- a/src/src.pro +++ b/src/src.pro @@ -19,6 +19,7 @@ HEADERS += \ common/QskFunctions.h \ common/QskGlobal.h \ common/QskGradient.h \ + common/QskGradientStop.h \ common/QskIntervalF.h \ common/QskMargins.h \ common/QskMetaFunction.h \ @@ -43,6 +44,7 @@ SOURCES += \ common/QskBoxShapeMetrics.cpp \ common/QskFunctions.cpp \ common/QskGradient.cpp \ + common/QskGradientStop.cpp \ common/QskIntervalF.cpp \ common/QskMargins.cpp \ common/QskMetaFunction.cpp \