code moved to QskGradientStop.[h|cpp]

This commit is contained in:
Uwe Rathmann 2021-09-16 10:01:53 +02:00
parent 71da4dcdff
commit 2219a6fe8e
5 changed files with 193 additions and 143 deletions

View File

@ -14,7 +14,6 @@
static void qskRegisterGradient() static void qskRegisterGradient()
{ {
qRegisterMetaType< QskGradient >(); qRegisterMetaType< QskGradient >();
qRegisterMetaType< QskGradientStop >();
QMetaType::registerConverter< QColor, QskGradient >( QMetaType::registerConverter< QColor, QskGradient >(
[]( const QColor& color ) { return QskGradient( color ); } ); []( const QColor& color ) { return QskGradient( color ); } );
@ -168,60 +167,6 @@ static inline QVector< QskGradientStop > qskExtractedStops(
return extracted; 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() QskGradient::QskGradient()
: m_orientation( Vertical ) : m_orientation( Vertical )
{ {
@ -423,8 +368,7 @@ bool QskGradient::hasStopAt( qreal value ) const
uint QskGradient::hash( uint seed ) const uint QskGradient::hash( uint seed ) const
{ {
const int count = m_stops.size(); if ( m_stops.isEmpty() )
if ( count == 0 )
return seed; return seed;
uint hash = qHashBits( &m_orientation, sizeof( m_orientation ), seed ); uint hash = qHashBits( &m_orientation, sizeof( m_orientation ), seed );
@ -610,12 +554,6 @@ QVariant QskGradient::interpolate(
#include <qdebug.h> #include <qdebug.h>
QDebug operator<<( QDebug debug, const QskGradientStop& stop )
{
debug << stop.position() << ": " << stop.color();
return debug;
}
QDebug operator<<( QDebug debug, const QskGradient& gradient ) QDebug operator<<( QDebug debug, const QskGradient& gradient )
{ {
debug << "GR:" << gradient.orientation() << gradient.stops().count(); debug << "GR:" << gradient.orientation() << gradient.stops().count();

View File

@ -7,55 +7,14 @@
#define QSK_GRADIENT_H #define QSK_GRADIENT_H
#include "QskGlobal.h" #include "QskGlobal.h"
#include "QskGradientStop.h"
#include <qcolor.h> #include <qcolor.h>
#include <qmetatype.h> #include <qmetatype.h>
#include <qvector.h> #include <qvector.h>
#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 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 class QSK_EXPORT QskGradient
{ {
Q_GADGET Q_GADGET
@ -142,6 +101,8 @@ class QSK_EXPORT QskGradient
QVector< QskGradientStop > m_stops; QVector< QskGradientStop > m_stops;
}; };
Q_DECLARE_METATYPE( QskGradient )
inline QskGradient::QskGradient( Qt::GlobalColor color ) inline QskGradient::QskGradient( Qt::GlobalColor color )
: QskGradient( QColor( color ) ) : QskGradient( QColor( color ) )
{ {
@ -162,38 +123,6 @@ inline QColor QskGradient::endColor() const
return ( m_stops.size() >= 2 ) ? m_stops.last().color() : QColor(); 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 inline bool QskGradient::operator==( const QskGradient& other ) const
{ {
return ( m_orientation == other.m_orientation ) && ( m_stops == other.m_stops ); 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 ) ); return ( !( *this == other ) );
} }
Q_DECLARE_TYPEINFO( QskGradientStop, Q_MOVABLE_TYPE );
Q_DECLARE_METATYPE( QskGradientStop )
Q_DECLARE_METATYPE( QskGradient )
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QSK_EXPORT QDebug operator<<( QDebug, const QskGradientStop& ); class QDebug;
QSK_EXPORT QDebug operator<<( QDebug, const QskGradient& ); QSK_EXPORT QDebug operator<<( QDebug, const QskGradient& );
#endif #endif

View File

@ -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 <qhashfunctions.h>
#include <qvariant.h>
#include <algorithm>
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.h>
QDebug operator<<( QDebug debug, const QskGradientStop& stop )
{
debug << stop.position() << ": " << stop.color();
return debug;
}
#endif
#include "moc_QskGradientStop.cpp"

View File

@ -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 <qcolor.h>
#include <qmetatype.h>
#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

View File

@ -19,6 +19,7 @@ HEADERS += \
common/QskFunctions.h \ common/QskFunctions.h \
common/QskGlobal.h \ common/QskGlobal.h \
common/QskGradient.h \ common/QskGradient.h \
common/QskGradientStop.h \
common/QskIntervalF.h \ common/QskIntervalF.h \
common/QskMargins.h \ common/QskMargins.h \
common/QskMetaFunction.h \ common/QskMetaFunction.h \
@ -43,6 +44,7 @@ SOURCES += \
common/QskBoxShapeMetrics.cpp \ common/QskBoxShapeMetrics.cpp \
common/QskFunctions.cpp \ common/QskFunctions.cpp \
common/QskGradient.cpp \ common/QskGradient.cpp \
common/QskGradientStop.cpp \
common/QskIntervalF.cpp \ common/QskIntervalF.cpp \
common/QskMargins.cpp \ common/QskMargins.cpp \
common/QskMetaFunction.cpp \ common/QskMetaFunction.cpp \