From d07e5fcf3fdb59fea67a644fad6d8dd38672124c Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Wed, 26 May 2021 13:06:50 +0200 Subject: [PATCH] qskFuzzyFllor/qskFuzzyCeil added --- src/common/QskFunctions.cpp | 25 +++++++++++++++++++++++++ src/common/QskFunctions.h | 19 +++++++++++-------- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/common/QskFunctions.cpp b/src/common/QskFunctions.cpp index 6917b108..8187adf7 100644 --- a/src/common/QskFunctions.cpp +++ b/src/common/QskFunctions.cpp @@ -22,6 +22,8 @@ QSK_QT_PRIVATE_END #include #include +#include + template< class Rect, class Value > static inline Rect qskAlignedRect( const Rect& outerRect, Value width, Value height, Qt::Alignment alignment ) @@ -193,3 +195,26 @@ QRect qskPlatformScreenGeometry( const QScreen* screen ) return screen->handle()->geometry(); } + +/* + Due to the nature of floating point arithmetic + we might floor a value, that is already "aligned" + F.e static_cast< int >( 0.29 / 0.01 ) -> 28 + */ +qreal qskFuzzyFloor( qreal value, qreal stepSize ) +{ + qreal v = std::floor( value / stepSize ) * stepSize; + if ( qFuzzyCompare( value - v, stepSize ) ) + v = value; + + return v; +} + +qreal qskFuzzyCeil( qreal value, qreal stepSize ) +{ + qreal v = std::ceil( value / stepSize ) * stepSize; + if ( qFuzzyCompare( v - value, stepSize ) ) + v = value; + + return v; +} diff --git a/src/common/QskFunctions.h b/src/common/QskFunctions.h index ea8ba764..b8a95b1d 100644 --- a/src/common/QskFunctions.h +++ b/src/common/QskFunctions.h @@ -36,6 +36,15 @@ QSK_EXPORT QRectF qskValidOrEmptyInnerRect( QSK_EXPORT qreal qskHorizontalAdvance( const QFont&, const QString& ); QSK_EXPORT qreal qskHorizontalAdvance( const QFontMetricsF&, const QString& ); +inline QMarginsF qskMargins( const QRectF& rect, const QRectF& innerRect ) +{ + return QMarginsF( + innerRect.left() - rect.left(), + innerRect.top() - rect.top(), + rect.right() - innerRect.right(), + rect.bottom() - innerRect.bottom() ); +} + inline bool qskFuzzyCompare( qreal value1, qreal value2 ) { if ( qFuzzyIsNull( value1 ) ) @@ -47,13 +56,7 @@ inline bool qskFuzzyCompare( qreal value1, qreal value2 ) return qFuzzyCompare( value1, value2 ); } -inline QMarginsF qskMargins( const QRectF& rect, const QRectF& innerRect ) -{ - return QMarginsF( - innerRect.left() - rect.left(), - innerRect.top() - rect.top(), - rect.right() - innerRect.right(), - rect.bottom() - innerRect.bottom() ); -} +QSK_EXPORT qreal qskFuzzyFloor( qreal value, qreal stepSize ); +QSK_EXPORT qreal qskFuzzyCeil( qreal value, qreal stepSize ); #endif