6#include "QskFunctions.h"
9#include <qfontmetrics.h>
12template<
class Rect,
class Value >
13static inline Rect qskAlignedRect(
const Rect& outerRect,
14 Value width, Value height, Qt::Alignment alignment )
16 Value x = outerRect.x();
17 Value y = outerRect.y();
19 switch ( alignment & Qt::AlignHorizontal_Mask )
21 case Qt::AlignHCenter:
22 x += ( outerRect.width() - width ) / 2;
26 x += outerRect.width() - width;
33 switch ( alignment & Qt::AlignVertical_Mask )
35 case Qt::AlignVCenter:
36 y += ( outerRect.height() - height ) / 2;
40 y += outerRect.height() - height;
47 return Rect( x, y, width, height );
50QRect qskAlignedRect(
const QRect& outerRect,
51 int width,
int height, Qt::Alignment alignment )
53 return qskAlignedRect< QRect, int >( outerRect, width, height, alignment );
56QRectF qskAlignedRectF(
const QRectF& outerRect,
57 qreal width, qreal height, Qt::Alignment alignment )
59 return qskAlignedRect< QRectF, qreal >( outerRect, width, height, alignment );
62QRect qskInnerRect(
const QRectF& rect )
64 const int left = qCeil( rect.left() );
65 const int top = qCeil( rect.top() );
66 const int right = qFloor( rect.right() );
67 const int bottom = qFloor( rect.bottom() );
69 return QRect( left, top, right - left, bottom - top );
72QRectF qskInnerRectF(
const QRectF& rect )
74 const qreal left = qCeil( rect.left() );
75 const qreal top = qCeil( rect.top() );
76 const qreal right = qFloor( rect.right() );
77 const qreal bottom = qFloor( rect.bottom() );
79 return QRectF( left, top, right - left, bottom - top );
82QRectF qskValidOrEmptyInnerRect(
const QRectF& rect,
const QMarginsF& margins )
86 if ( rect.width() > 0.0 )
88 const qreal marginsWidth = margins.left() + margins.right();
90 if ( marginsWidth > rect.width() )
92 x = rect.x() + rect.width() * ( margins.left() / marginsWidth );
97 x = rect.x() + margins.left();
98 w = rect.width() - marginsWidth;
107 if ( rect.height() > 0.0 )
109 const qreal marginsHeight = margins.top() + margins.bottom();
110 if ( marginsHeight > rect.height() )
112 y = rect.y() + rect.height() * ( margins.top() / marginsHeight );
117 y = rect.y() + margins.top();
118 h = rect.height() - marginsHeight;
127 return QRectF( x, y, w, h );
130QRectF qskInterpolatedRect(
const QRectF& from,
const QRectF& to, qreal progress )
132 if ( progress <= 0.0 )
135 if ( progress >= 1.0 )
138 const auto x = from.x() + progress * ( to.x() - from.x() );
139 const auto y = from.y() + progress * ( to.y() - from.y() );
140 const auto w = from.width() + progress * ( to.width() - from.width() );
141 const auto h = from.height() + progress * ( to.height() - from.height() );
143 return QRectF( x, y, w, h );
146QSizeF qskInterpolatedSize(
const QSizeF& from,
const QSizeF& to, qreal progress )
148 if ( progress <= 0.0 )
151 if ( progress >= 1.0 )
154 const auto w = from.width() + progress * ( to.width() - from.width() );
155 const auto h = from.height() + progress * ( to.height() - from.height() );
157 return QSizeF( w, h );
160qreal qskHorizontalAdvance(
const QFont& font,
const QString& text )
162 return qskHorizontalAdvance( QFontMetricsF( font ), text );
165qreal qskHorizontalAdvance(
const QFontMetricsF& fontMetrics,
const QString& text )
167 return fontMetrics.horizontalAdvance( text );
170QSizeF qskTextRenderSize(
const QFontMetricsF& fontMetrics,
const QString& text )
172 if ( text.isEmpty() )
173 return QSizeF( 0.0, 0.0 );
175 QRectF r( 0.0, 0.0, 10e6, 10e6 );
176 r = fontMetrics.boundingRect( r, 0, text );
181qreal qskFuzzyFloor( qreal value, qreal stepSize )
183 const double eps = 1.0e-6 * stepSize;
185 value = ( value + eps ) / stepSize;
186 return std::floor( value ) * stepSize;
189qreal qskFuzzyCeil( qreal value, qreal stepSize )
191 const double eps = 1.0e-6 * stepSize;
193 value = ( value - eps ) / stepSize;
194 return std::ceil( value ) * stepSize;
197double qskConstrainedDegrees(
double degrees )
199 degrees = fmod( degrees, 360.0 );
206float qskConstrainedDegrees(
float degrees )
208 degrees = fmodf( degrees, 360.0f );
209 if ( degrees < 0.0f )
215double qskConstrainedRadians(
double radians )
217 constexpr double pi2 = 2.0 * M_PI;
219 radians = fmod( radians, pi2 );
226float qskConstrainedRadians(
float radians )
228 constexpr float pi2 = 2.0f * M_PI;
230 radians = fmodf( radians, pi2 );
231 if ( radians < 0.0f )
238bool qskHasEnvironment(
const char* env )
242 const int value = qEnvironmentVariableIntValue( env, &ok );
247 auto result = qgetenv( env );
248 return !result.isEmpty() && result !=
"false";