6#include "QskStippleMetrics.h"
8#include <qhashfunctions.h>
12static void qskRegisterStippleMetrics()
14 qRegisterMetaType< QskStippleMetrics >();
16#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
17 QMetaType::registerEqualsComparator< QskStippleMetrics >();
20 QMetaType::registerConverter< QPen, QskStippleMetrics >(
23 QMetaType::registerConverter< Qt::PenStyle, QskStippleMetrics >(
27Q_CONSTRUCTOR_FUNCTION( qskRegisterStippleMetrics )
29QVector< qreal > qskDashPattern( Qt::PenStyle style )
31 static const QVector< qreal > pattern[] =
33 {}, { 1 }, { 4, 2 }, { 1, 2 },
34 { 4, 2, 1, 2 }, { 4, 2, 1, 2, 1, 2 }, {}
37 return pattern[ style ];
40static inline qreal qskInterpolated( qreal from, qreal to, qreal ratio )
42 return from + ( to - from ) * ratio;
45static inline QVector< qreal > qskInterpolatedSpaces(
46 const QVector< qreal >& pattern, qreal progress )
48 QVector< qreal > interpolated;
49 interpolated.reserve( pattern.count() );
51 for (
int i = 1; i < pattern.count(); i += 2 )
52 interpolated[i] = progress * pattern[i];
57QskStippleMetrics::QskStippleMetrics( Qt::PenStyle penStyle )
58 : m_pattern( qskDashPattern( penStyle ) )
62QskStippleMetrics::QskStippleMetrics(
const QPen& pen )
65 if ( pen.style() == Qt::CustomDashLine )
67 m_offset = pen.dashOffset();
68 m_pattern = pen.dashPattern();
72void QskStippleMetrics::setPattern(
const QVector< qreal >& pattern )
77void QskStippleMetrics::setOffset( qreal offset )
noexcept
88 const auto offset = qskInterpolated( m_offset, to.m_offset, progress );
90 QVector< qreal > pattern;
94 pattern = qskInterpolatedSpaces( to.m_pattern, progress );
96 else if ( to.isSolid() )
98 pattern = qskInterpolatedSpaces( m_pattern, 1.0 - progress );
102 const auto count = qMax( m_pattern.count(), to.m_pattern.count() );
103 pattern.reserve( count );
105 for (
int i = 0; i < count; i++ )
107 const auto v1 = m_pattern.value( i, 0.0 );
108 const auto v2 = to.m_pattern.value( i, 0.0 );
110 pattern += qskInterpolated( v1, v2, progress );
117QVariant QskStippleMetrics::interpolate(
120 return QVariant::fromValue( from.interpolated( to, progress ) );
123QskHashValue QskStippleMetrics::hash( QskHashValue seed )
const noexcept
125 auto hash = qHash( m_offset, seed );
126 return qHash( m_pattern, hash );
129#ifndef QT_NO_DEBUG_STREAM
135 QDebugStateSaver saver( debug );
138 debug <<
"QskStippleMetrics" <<
'(';
139 debug << metrics.offset() <<
',' << metrics.pattern();
147#include "moc_QskStippleMetrics.cpp"