diff --git a/src/common/QskBoxShapeMetrics.cpp b/src/common/QskBoxShapeMetrics.cpp index e1528c06..014bfc8b 100644 --- a/src/common/QskBoxShapeMetrics.cpp +++ b/src/common/QskBoxShapeMetrics.cpp @@ -90,61 +90,58 @@ QskBoxShapeMetrics QskBoxShapeMetrics::toAbsolute( const QSizeF& size ) const no return *this; QskBoxShapeMetrics absoluted = *this; + absoluted.m_sizeMode = Qt::AbsoluteSize; if ( size.isEmpty() ) { for ( int i = 0; i < 4; i++ ) absoluted.m_radii[ i ] = QSizeF( 0.0, 0.0 ); + + return absoluted; } - else + + if ( m_scalingMode == RoundedRectangle ) { for ( int i = 0; i < 4; i++ ) { auto& radius = absoluted.m_radii[ i ]; - qreal rx = qskAbsoluted( size.width(), radius.width() ); - qreal ry = qskAbsoluted( size.height(), radius.height() ); + const qreal rx = qskAbsoluted( size.width(), radius.width() ); + const qreal ry = qskAbsoluted( size.height(), radius.height() ); + + radius.rheight() = radius.rwidth() = std::min( rx, ry ); + } + } + else + { + const auto ratio = size.height() / size.width(); + + for ( int i = 0; i < 4; i++ ) + { + auto& radius = absoluted.m_radii[ i ]; + + const qreal rx = qskAbsoluted( size.width(), radius.width() ); + const qreal ry = qskAbsoluted( size.height(), radius.height() ); if ( rx <= 0.0 || ry <= 0.0 ) { radius.rwidth() = radius.rheight() = 0.0; + continue; + } + + if ( ratio >= 1.0 ) + { + radius.rwidth() = ry / ratio; + radius.rheight() = ry; } else { - if ( m_aspectRatioMode != Qt::IgnoreAspectRatio ) - { -#if 0 - if ( ( m_aspectRatioMode == Qt::KeepAspectRatioByExpanding ) && - ( radius.width() >= radius.height() ) ) - { - rx = ry * radius.width() / radius.height(); - } - else - { - ry = rx * radius.height() / radius.width(); - } - -#else - if ( m_aspectRatioMode == Qt::KeepAspectRatio ) - { - rx = std::min( rx, ry ); - ry = std::min( rx, ry ); - } - else - { - rx = std::max( rx, ry ); - ry = std::max( rx, ry ); - } -#endif - } radius.rwidth() = rx; - radius.rheight() = ry; + radius.rheight() = rx * ratio; } } } - absoluted.m_sizeMode = Qt::AbsoluteSize; - return absoluted; } @@ -161,7 +158,7 @@ QskBoxShapeMetrics QskBoxShapeMetrics::interpolated( qskInterpolatedSize( m_radii[ 1 ], to.m_radii[ 1 ], ratio ), qskInterpolatedSize( m_radii[ 2 ], to.m_radii[ 2 ], ratio ), qskInterpolatedSize( m_radii[ 3 ], to.m_radii[ 3 ], ratio ), - to.m_sizeMode, to.m_aspectRatioMode ); + to.m_sizeMode, to.m_scalingMode ); } QVariant QskBoxShapeMetrics::interpolate( diff --git a/src/common/QskBoxShapeMetrics.h b/src/common/QskBoxShapeMetrics.h index 637400fd..0433c8e6 100644 --- a/src/common/QskBoxShapeMetrics.h +++ b/src/common/QskBoxShapeMetrics.h @@ -26,10 +26,17 @@ class QSK_EXPORT QskBoxShapeMetrics Q_PROPERTY( qreal radius READ radiusX WRITE setRadius ) Q_PROPERTY( Qt::SizeMode sizeMode READ sizeMode WRITE setSizeMode ) - Q_PROPERTY( Qt::AspectRatioMode aspectRatioMode - READ aspectRatioMode WRITE setAspectRatioMode ) + Q_PROPERTY( ScalingMode scalingMode READ scalingMode WRITE setScalingMode ) public: + enum ScalingMode + { + // How to scale, when translating to Qt::AbsoluteSize + RoundedRectangle, + Ellipsoid + }; + Q_ENUM( ScalingMode ); + constexpr QskBoxShapeMetrics() noexcept; constexpr QskBoxShapeMetrics( qreal topLeft, qreal topRight, @@ -83,8 +90,8 @@ class QSK_EXPORT QskBoxShapeMetrics void setSizeMode( Qt::SizeMode ) noexcept; constexpr Qt::SizeMode sizeMode() const noexcept; - void setAspectRatioMode( Qt::AspectRatioMode ) noexcept; - constexpr Qt::AspectRatioMode aspectRatioMode() const noexcept; + void setScalingMode( ScalingMode ) noexcept; + constexpr ScalingMode scalingMode() const noexcept; QskBoxShapeMetrics interpolated( const QskBoxShapeMetrics&, qreal value ) const noexcept; @@ -105,22 +112,22 @@ class QSK_EXPORT QskBoxShapeMetrics inline constexpr QskBoxShapeMetrics( const QSizeF& topLeft, const QSizeF& topRight, const QSizeF& bottomLeft, const QSizeF& bottomRight, - Qt::SizeMode sizeMode, Qt::AspectRatioMode aspectRatioMode ) noexcept + Qt::SizeMode sizeMode, ScalingMode scalingMode ) noexcept : m_radii{ topLeft, topRight, bottomLeft, bottomRight } , m_sizeMode( sizeMode ) - , m_aspectRatioMode( aspectRatioMode ) + , m_scalingMode( scalingMode ) { } QSizeF m_radii[ 4 ]; Qt::SizeMode m_sizeMode : 2; - Qt::AspectRatioMode m_aspectRatioMode : 2; + ScalingMode m_scalingMode : 1; }; inline constexpr QskBoxShapeMetrics::QskBoxShapeMetrics() noexcept : m_radii{ { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 } } , m_sizeMode( Qt::AbsoluteSize ) - , m_aspectRatioMode( Qt::KeepAspectRatio ) + , m_scalingMode( RoundedRectangle ) { } @@ -135,7 +142,7 @@ inline constexpr QskBoxShapeMetrics::QskBoxShapeMetrics( : m_radii{ { radiusX, radiusY }, { radiusX, radiusY }, { radiusX, radiusY }, { radiusX, radiusY } } , m_sizeMode( sizeMode ) - , m_aspectRatioMode( Qt::KeepAspectRatio ) + , m_scalingMode( RoundedRectangle ) { } @@ -144,7 +151,7 @@ inline constexpr QskBoxShapeMetrics::QskBoxShapeMetrics( qreal topLeft, qreal to : m_radii{ { topLeft, topLeft }, { topRight, topRight }, { bottomLeft, bottomLeft }, { bottomRight, bottomRight } } , m_sizeMode( sizeMode ) - , m_aspectRatioMode( Qt::KeepAspectRatio ) + , m_scalingMode( RoundedRectangle ) { } @@ -152,7 +159,7 @@ inline constexpr bool QskBoxShapeMetrics::operator==( const QskBoxShapeMetrics& other ) const noexcept { return ( m_sizeMode == other.m_sizeMode ) - && ( m_aspectRatioMode == other.m_aspectRatioMode ) + && ( m_scalingMode == other.m_scalingMode ) && ( m_radii[ 0 ] == other.m_radii[ 0 ] ) && ( m_radii[ 1 ] == other.m_radii[ 1 ] ) && ( m_radii[ 2 ] == other.m_radii[ 2 ] ) @@ -250,15 +257,15 @@ inline constexpr Qt::SizeMode QskBoxShapeMetrics::sizeMode() const noexcept return m_sizeMode; } -inline void QskBoxShapeMetrics::setAspectRatioMode( - Qt::AspectRatioMode aspectRatioMode ) noexcept +inline void QskBoxShapeMetrics::setScalingMode( ScalingMode scalingMode ) noexcept { - m_aspectRatioMode = aspectRatioMode; + m_scalingMode = scalingMode; } -inline constexpr Qt::AspectRatioMode QskBoxShapeMetrics::aspectRatioMode() const noexcept +inline constexpr QskBoxShapeMetrics::ScalingMode + QskBoxShapeMetrics::scalingMode() const noexcept { - return m_aspectRatioMode; + return m_scalingMode; } inline constexpr bool QskBoxShapeMetrics::isRectellipse() const noexcept @@ -281,7 +288,7 @@ inline constexpr QskBoxShapeMetrics QskBoxShapeMetrics::transposed() const noexc return QskBoxShapeMetrics( m_radii[ 0 ].transposed(), m_radii[ 1 ].transposed(), m_radii[ 2 ].transposed(), m_radii[ 3 ].transposed(), - m_sizeMode, m_aspectRatioMode ); + m_sizeMode, m_scalingMode ); } #ifndef QT_NO_DEBUG_STREAM