diff --git a/src/common/QskBorderMetrics.cpp b/src/common/QskBorderMetrics.cpp index 228cf8d8..b5e448dd 100644 --- a/src/common/QskBorderMetrics.cpp +++ b/src/common/QskBorderMetrics.cpp @@ -20,6 +20,14 @@ static inline QSizeF qskInterpolatedSize( return from + ( to - from ) * ratio; } +static inline qreal qskAbsoluted( qreal length, qreal percentage ) +{ + // 100% means -> 0.5 of length + percentage = qBound( 0.0, percentage, 100.0 ); + return percentage / 100.0 * 0.5 * length; +} + + QskBorderMetrics::QskBorderMetrics( qreal width, qreal radiusX, qreal radiusY ): m_widths( ( width >= 0.0 ) ? width : 0.0 ), m_widthIsRelative( false ), @@ -98,6 +106,52 @@ void QskBorderMetrics::setRadius( Qt::Corner corner, qreal radiusX, qreal radius } } +QskBorderMetrics QskBorderMetrics::toAbsolute( const QSizeF& size ) const +{ + if ( !( m_radiusIsRelative || m_widthIsRelative ) ) + return *this; + + QskBorderMetrics absoluted = *this; + + if ( m_radiusIsRelative ) + { + if ( size.isEmpty() ) + { + for ( int i = 0; i < 4; i++ ) + absoluted.m_radii[ i ] = QSizeF( 0.0, 0.0 ); + } + else + { + for ( int i = 0; i < 4; i++ ) + { + auto& radii = absoluted.m_radii[ i ]; + + radii.setWidth( qskAbsoluted( size.width(), radii.width() ) ); + radii.setHeight( qskAbsoluted( size.height(), radii.height() ) ); + } + } + } + + if ( m_widthIsRelative ) + { + auto& w = absoluted.m_widths; + + if ( size.isEmpty() ) + { + w = QskMargins(); + } + else + { + w.setLeft( qskAbsoluted( size.width(), w.left() ) ); + w.setTop( qskAbsoluted( size.height(), w.top() ) ); + w.setRight( qskAbsoluted( size.width(), w.right() ) ); + w.setBottom( qskAbsoluted( size.height(), w.bottom() ) ); + } + } + + return absoluted; +} + QskBorderMetrics QskBorderMetrics::interpolated( const QskBorderMetrics& to, qreal ratio ) const { diff --git a/src/common/QskBorderMetrics.h b/src/common/QskBorderMetrics.h index a6c9dce5..2fc9d8b6 100644 --- a/src/common/QskBorderMetrics.h +++ b/src/common/QskBorderMetrics.h @@ -64,6 +64,7 @@ public: Qt::SizeMode widthSizeMode() const; QskBorderMetrics interpolated( const QskBorderMetrics&, qreal value ) const; + QskBorderMetrics toAbsolute ( const QSizeF& ) const; static QVariant interpolate( const QskBorderMetrics&, const QskBorderMetrics&, qreal progress ); @@ -86,6 +87,7 @@ private: }; inline QskBorderMetrics::QskBorderMetrics(): + m_radii( { { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 } } ), m_widthIsRelative( false ), m_radiusIsRelative( false ) {