2017-07-21 18:21:34 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* QSkinny - Copyright (C) 2016 Uwe Rathmann
|
|
|
|
* This file may be used under the terms of the QSkinny License, Version 1.0
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef QSK_GRADIENT_H
|
|
|
|
#define QSK_GRADIENT_H
|
|
|
|
|
2021-09-16 10:01:53 +02:00
|
|
|
#include "QskGradientStop.h"
|
2017-10-17 17:34:00 +02:00
|
|
|
|
2022-03-18 16:50:34 +01:00
|
|
|
#include <qbrush.h>
|
2018-08-03 08:15:28 +02:00
|
|
|
#include <qmetatype.h>
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2022-11-13 17:22:09 +01:00
|
|
|
class QskLinearDirection;
|
|
|
|
class QskRadialDirection;
|
|
|
|
class QskConicDirection;
|
2022-10-31 14:42:08 +01:00
|
|
|
|
2017-10-17 17:34:00 +02:00
|
|
|
class QVariant;
|
2022-12-01 13:03:57 +01:00
|
|
|
class QGradient;
|
2017-07-21 18:21:34 +02:00
|
|
|
|
|
|
|
class QSK_EXPORT QskGradient
|
|
|
|
{
|
|
|
|
Q_GADGET
|
|
|
|
|
2022-11-13 10:09:18 +01:00
|
|
|
Q_PROPERTY( Type type READ type )
|
|
|
|
|
2022-11-30 17:05:37 +01:00
|
|
|
Q_PROPERTY( QskLinearDirection linear READ linearDirection WRITE setLinearDirection )
|
|
|
|
Q_PROPERTY( QskConicDirection conic READ conicDirection WRITE setConicDirection )
|
|
|
|
Q_PROPERTY( QskRadialDirection radial READ radialDirection WRITE setRadialDirection )
|
|
|
|
|
2022-12-01 12:03:36 +01:00
|
|
|
Q_PROPERTY( QskGradientStops stops READ stops WRITE setStops )
|
|
|
|
|
|
|
|
Q_PROPERTY( Spread spread READ spread WRITE setSpread )
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2019-05-10 08:09:48 +02:00
|
|
|
Q_PROPERTY( bool valid READ isValid )
|
|
|
|
Q_PROPERTY( bool visible READ isVisible )
|
|
|
|
Q_PROPERTY( bool monochrome READ isMonochrome )
|
|
|
|
|
2022-11-04 17:16:38 +01:00
|
|
|
Q_CLASSINFO( "DefaultProperty", "stops" )
|
|
|
|
|
2018-08-03 08:15:28 +02:00
|
|
|
public:
|
2022-10-31 14:42:08 +01:00
|
|
|
enum Type
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2022-10-31 14:42:08 +01:00
|
|
|
Stops,
|
|
|
|
|
|
|
|
Linear,
|
|
|
|
Radial,
|
|
|
|
Conic
|
2017-07-21 18:21:34 +02:00
|
|
|
};
|
2022-10-31 14:42:08 +01:00
|
|
|
Q_ENUM( Type )
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2022-12-01 12:03:36 +01:00
|
|
|
enum Spread
|
|
|
|
{
|
|
|
|
PadSpread,
|
|
|
|
ReflectSpread,
|
|
|
|
RepeatSpread
|
|
|
|
};
|
|
|
|
Q_ENUM( Spread )
|
|
|
|
|
2022-10-24 16:02:46 +02:00
|
|
|
QskGradient() noexcept = default;
|
|
|
|
|
2017-10-17 17:34:00 +02:00
|
|
|
QskGradient( Qt::GlobalColor );
|
|
|
|
QskGradient( QRgb );
|
2017-07-21 18:21:34 +02:00
|
|
|
QskGradient( const QColor& );
|
2022-10-31 14:42:08 +01:00
|
|
|
QskGradient( const QColor&, const QColor& );
|
2022-03-18 16:50:34 +01:00
|
|
|
QskGradient( QGradient::Preset );
|
2022-12-01 12:03:36 +01:00
|
|
|
QskGradient( const QskGradientStops& );
|
2017-10-17 17:34:00 +02:00
|
|
|
|
2022-12-09 11:23:32 +01:00
|
|
|
QskGradient( const QGradient& );
|
2022-12-01 13:03:57 +01:00
|
|
|
|
2022-10-31 14:42:08 +01:00
|
|
|
QskGradient( const QskGradient& ) noexcept;
|
2017-07-21 18:21:34 +02:00
|
|
|
|
|
|
|
~QskGradient();
|
|
|
|
|
2022-10-31 14:42:08 +01:00
|
|
|
QskGradient& operator=( const QskGradient& ) noexcept;
|
|
|
|
|
2022-10-24 16:02:46 +02:00
|
|
|
bool operator==( const QskGradient& ) const noexcept;
|
|
|
|
bool operator!=( const QskGradient& ) const noexcept;
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2022-10-31 14:42:08 +01:00
|
|
|
QskGradient::Type type() const noexcept;
|
2022-10-25 18:17:21 +02:00
|
|
|
|
2022-11-13 17:22:09 +01:00
|
|
|
void setLinearDirection( const QskLinearDirection& );
|
|
|
|
void setLinearDirection( qreal, qreal, qreal, qreal );
|
|
|
|
void setLinearDirection( Qt::Orientation );
|
|
|
|
QskLinearDirection linearDirection() const;
|
|
|
|
|
|
|
|
void setRadialDirection( const QskRadialDirection& );
|
|
|
|
void setRadialDirection( const qreal x, qreal y, qreal radius );
|
|
|
|
QskRadialDirection radialDirection() const;
|
|
|
|
|
|
|
|
void setConicDirection( qreal, qreal );
|
|
|
|
void setConicDirection( qreal, qreal, qreal, qreal = 360.0 );
|
|
|
|
void setConicDirection( const QskConicDirection& );
|
|
|
|
QskConicDirection conicDirection() const;
|
|
|
|
|
2022-11-29 10:59:09 +01:00
|
|
|
void setDirection( Type );
|
2022-11-13 17:22:09 +01:00
|
|
|
void resetDirection();
|
|
|
|
|
2022-10-24 16:02:46 +02:00
|
|
|
bool isValid() const noexcept;
|
|
|
|
bool isMonochrome() const noexcept;
|
|
|
|
bool isVisible() const noexcept;
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2022-12-01 12:03:36 +01:00
|
|
|
void setStops( const QskGradientStops& );
|
|
|
|
const QskGradientStops& stops() const noexcept;
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2022-11-16 16:19:19 +01:00
|
|
|
void setStops( const QRgb );
|
|
|
|
void setStops( Qt::GlobalColor );
|
2022-10-24 16:02:46 +02:00
|
|
|
void setStops( const QColor& );
|
2022-11-16 16:19:19 +01:00
|
|
|
|
2022-10-24 16:02:46 +02:00
|
|
|
void setStops( const QColor&, const QColor& );
|
|
|
|
void setStops( QGradient::Preset );
|
2017-10-17 17:34:00 +02:00
|
|
|
|
2022-10-24 16:02:46 +02:00
|
|
|
void clearStops();
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2022-10-24 16:02:46 +02:00
|
|
|
Q_INVOKABLE bool hasStopAt( qreal value ) const noexcept;
|
2017-10-17 17:34:00 +02:00
|
|
|
|
2022-10-24 16:02:46 +02:00
|
|
|
Q_INVOKABLE QColor startColor() const noexcept;
|
|
|
|
Q_INVOKABLE QColor endColor() const noexcept;
|
2017-10-17 17:34:00 +02:00
|
|
|
|
2022-11-16 13:46:37 +01:00
|
|
|
QRgb rgbStart() const;
|
|
|
|
QRgb rgbEnd() const;
|
|
|
|
|
2022-10-24 16:02:46 +02:00
|
|
|
void setAlpha( int alpha );
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2022-12-01 12:03:36 +01:00
|
|
|
void setSpread( Spread );
|
|
|
|
Spread spread() const noexcept;
|
2022-10-31 14:42:08 +01:00
|
|
|
|
2020-07-31 12:42:36 +02:00
|
|
|
void reverse();
|
|
|
|
QskGradient reversed() const;
|
|
|
|
|
2017-10-17 17:34:00 +02:00
|
|
|
QskGradient interpolated( const QskGradient&, qreal value ) const;
|
|
|
|
|
|
|
|
static QVariant interpolate( const QskGradient&,
|
|
|
|
const QskGradient&, qreal progress );
|
|
|
|
|
2022-10-24 17:08:48 +02:00
|
|
|
// all stops between [from, to] with positions streched into [0,1]
|
|
|
|
QskGradient extracted( qreal from, qreal start ) const;
|
|
|
|
|
2022-03-25 10:28:06 +01:00
|
|
|
QskHashValue hash( QskHashValue seed ) const;
|
2017-10-17 17:34:00 +02:00
|
|
|
|
2022-10-24 16:40:47 +02:00
|
|
|
Q_INVOKABLE qreal stopAt( int index ) const noexcept;
|
|
|
|
Q_INVOKABLE QColor colorAt( int index ) const noexcept;
|
2022-10-24 16:02:46 +02:00
|
|
|
|
2022-10-24 17:29:39 +02:00
|
|
|
int stepCount() const noexcept;
|
2022-06-25 16:14:08 +02:00
|
|
|
|
2022-12-09 11:23:32 +01:00
|
|
|
QGradient toQGradient() const;
|
2022-12-01 13:03:57 +01:00
|
|
|
|
2019-04-18 16:11:05 +02:00
|
|
|
private:
|
2022-01-21 07:29:07 +01:00
|
|
|
void updateStatusBits() const;
|
|
|
|
|
2022-10-24 16:02:46 +02:00
|
|
|
private:
|
2022-12-01 12:03:36 +01:00
|
|
|
QskGradientStops m_stops;
|
2022-01-21 07:29:07 +01:00
|
|
|
|
2022-10-31 14:42:08 +01:00
|
|
|
/*
|
|
|
|
Linear: x1, y1, x2, y2
|
|
|
|
Radial: centerX, centerY, radius, n/a
|
|
|
|
Conic: centerX, centerY, startAngle, spanAngle
|
|
|
|
*/
|
|
|
|
qreal m_values[4] = {};
|
|
|
|
|
|
|
|
Type m_type = Stops;
|
2022-12-01 12:03:36 +01:00
|
|
|
Spread m_spread = PadSpread;
|
2022-01-21 07:29:07 +01:00
|
|
|
|
2022-10-24 16:02:46 +02:00
|
|
|
mutable bool m_isDirty = false;
|
|
|
|
mutable bool m_isValid = false;
|
|
|
|
mutable bool m_isMonchrome = true;
|
|
|
|
mutable bool m_isVisible = false;
|
2017-07-21 18:21:34 +02:00
|
|
|
};
|
|
|
|
|
2021-09-16 10:01:53 +02:00
|
|
|
Q_DECLARE_METATYPE( QskGradient )
|
|
|
|
|
2022-10-31 14:42:08 +01:00
|
|
|
inline QskGradient::QskGradient( Qt::GlobalColor color )
|
|
|
|
: QskGradient( QColor( color ) )
|
2017-10-17 17:34:00 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-10-31 14:42:08 +01:00
|
|
|
inline QskGradient::QskGradient( QRgb rgb )
|
|
|
|
: QskGradient( QColor::fromRgba( rgb ) )
|
2022-10-25 18:17:21 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-10-31 14:42:08 +01:00
|
|
|
inline bool QskGradient::operator!=( const QskGradient& other ) const noexcept
|
2022-10-25 18:17:21 +02:00
|
|
|
{
|
2022-10-31 14:42:08 +01:00
|
|
|
return !( *this == other );
|
2022-10-25 18:17:21 +02:00
|
|
|
}
|
|
|
|
|
2022-10-31 14:42:08 +01:00
|
|
|
inline QskGradient::Type QskGradient::type() const noexcept
|
2022-10-25 18:17:21 +02:00
|
|
|
{
|
2022-10-31 14:42:08 +01:00
|
|
|
return m_type;
|
2022-10-25 18:17:21 +02:00
|
|
|
}
|
|
|
|
|
2022-10-24 16:02:46 +02:00
|
|
|
inline const QskGradientStops& QskGradient::stops() const noexcept
|
2017-10-17 17:34:00 +02:00
|
|
|
{
|
2022-10-24 16:02:46 +02:00
|
|
|
#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;
|
2017-10-17 17:34:00 +02:00
|
|
|
}
|
|
|
|
|
2022-11-16 16:19:19 +01:00
|
|
|
inline void QskGradient::setStops( QRgb rgb )
|
|
|
|
{
|
|
|
|
setStops( QColor::fromRgba( rgb ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void QskGradient::setStops( Qt::GlobalColor color )
|
|
|
|
{
|
|
|
|
setStops( QColor( color ) );
|
|
|
|
}
|
|
|
|
|
2022-10-24 16:02:46 +02:00
|
|
|
inline QColor QskGradient::startColor() const noexcept
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2022-10-24 16:02:46 +02:00
|
|
|
return m_stops.isEmpty() ? QColor() : m_stops.first().color();
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
2022-10-24 16:02:46 +02:00
|
|
|
inline QColor QskGradient::endColor() const noexcept
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2022-10-24 16:02:46 +02:00
|
|
|
return m_stops.isEmpty() ? QColor() : m_stops.last().color();
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
2022-11-16 13:46:37 +01:00
|
|
|
inline QRgb QskGradient::rgbStart() const
|
|
|
|
{
|
|
|
|
return m_stops.isEmpty() ? qRgba( 0, 0, 0, 255 ) : m_stops.first().rgb();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QRgb QskGradient::rgbEnd() const
|
|
|
|
{
|
|
|
|
return m_stops.isEmpty() ? qRgba( 0, 0, 0, 255 ) : m_stops.last().rgb();
|
|
|
|
}
|
|
|
|
|
2022-12-01 12:03:36 +01:00
|
|
|
inline QskGradient::Spread QskGradient::spread() const noexcept
|
2022-10-31 14:42:08 +01:00
|
|
|
{
|
|
|
|
return m_spread;
|
|
|
|
}
|
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
#ifndef QT_NO_DEBUG_STREAM
|
|
|
|
|
2021-09-16 10:01:53 +02:00
|
|
|
class QDebug;
|
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
QSK_EXPORT QDebug operator<<( QDebug, const QskGradient& );
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|