2017-07-21 18:21:34 +02:00
|
|
|
#include "QskFunctions.h"
|
|
|
|
|
2018-07-19 14:10:48 +02:00
|
|
|
#include <qguiapplication.h>
|
|
|
|
#include <qmath.h>
|
2018-08-03 08:15:28 +02:00
|
|
|
#include <qscreen.h>
|
2020-03-13 13:32:22 +01:00
|
|
|
#include <qfont.h>
|
|
|
|
#include <qfontmetrics.h>
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2020-03-10 10:17:44 +01:00
|
|
|
QSK_QT_PRIVATE_BEGIN
|
|
|
|
#include <private/qguiapplication_p.h>
|
|
|
|
QSK_QT_PRIVATE_END
|
|
|
|
|
|
|
|
#include <qpa/qplatformintegration.h>
|
|
|
|
#include <qpa/qplatformscreen.h>
|
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
template< class Rect, class Value >
|
|
|
|
static inline Rect qskAlignedRect( const Rect& outerRect,
|
|
|
|
Value width, Value height, Qt::Alignment alignment )
|
|
|
|
{
|
2019-05-16 08:23:10 +02:00
|
|
|
Value x = outerRect.x();
|
|
|
|
Value y = outerRect.y();
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2018-08-03 08:15:28 +02:00
|
|
|
switch ( alignment & Qt::AlignHorizontal_Mask )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2019-05-16 08:23:10 +02:00
|
|
|
case Qt::AlignHCenter:
|
|
|
|
x += ( outerRect.width() - width ) / 2;
|
2017-07-21 18:21:34 +02:00
|
|
|
break;
|
2019-05-16 08:23:10 +02:00
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
case Qt::AlignRight:
|
2019-05-16 08:23:10 +02:00
|
|
|
x += outerRect.width() - width;
|
2017-07-21 18:21:34 +02:00
|
|
|
break;
|
2019-05-16 08:23:10 +02:00
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
default:
|
2019-05-16 08:23:10 +02:00
|
|
|
break;
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 08:15:28 +02:00
|
|
|
switch ( alignment & Qt::AlignVertical_Mask )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2019-05-16 08:23:10 +02:00
|
|
|
case Qt::AlignVCenter:
|
|
|
|
y += ( outerRect.height() - height ) / 2;
|
2017-07-21 18:21:34 +02:00
|
|
|
break;
|
2019-05-16 08:23:10 +02:00
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
case Qt::AlignBottom:
|
2019-05-16 08:23:10 +02:00
|
|
|
y += outerRect.height() - height;
|
2017-07-21 18:21:34 +02:00
|
|
|
break;
|
2019-05-16 08:23:10 +02:00
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
default:
|
2019-05-16 08:23:10 +02:00
|
|
|
break;
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:23:10 +02:00
|
|
|
return Rect( x, y, width, height );
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QRect qskAlignedRect( const QRect& outerRect,
|
|
|
|
int width, int height, Qt::Alignment alignment )
|
|
|
|
{
|
|
|
|
return qskAlignedRect< QRect, int >( outerRect, width, height, alignment );
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF qskAlignedRectF( const QRectF& outerRect,
|
|
|
|
qreal width, qreal height, Qt::Alignment alignment )
|
|
|
|
{
|
|
|
|
return qskAlignedRect< QRectF, qreal >( outerRect, width, height, alignment );
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect qskInnerRect( const QRectF& rect )
|
|
|
|
{
|
|
|
|
const int left = qCeil( rect.left() );
|
|
|
|
const int top = qCeil( rect.top() );
|
|
|
|
const int right = qFloor( rect.right() );
|
|
|
|
const int bottom = qFloor( rect.bottom() );
|
|
|
|
|
|
|
|
return QRect( left, top, right - left, bottom - top );
|
|
|
|
}
|
|
|
|
|
2019-04-10 19:37:59 +02:00
|
|
|
QRectF qskInnerRectF( const QRectF& rect )
|
|
|
|
{
|
|
|
|
const qreal left = qCeil( rect.left() );
|
|
|
|
const qreal top = qCeil( rect.top() );
|
|
|
|
const qreal right = qFloor( rect.right() );
|
|
|
|
const qreal bottom = qFloor( rect.bottom() );
|
|
|
|
|
|
|
|
return QRectF( left, top, right - left, bottom - top );
|
|
|
|
}
|
|
|
|
|
2017-10-17 17:34:00 +02:00
|
|
|
QRectF qskValidOrEmptyInnerRect( const QRectF& rect, const QMarginsF& margins )
|
|
|
|
{
|
|
|
|
qreal x, y, h, w;
|
|
|
|
|
2019-04-24 08:39:13 +02:00
|
|
|
if ( rect.width() > 0.0 )
|
2017-10-17 17:34:00 +02:00
|
|
|
{
|
2019-04-24 08:39:13 +02:00
|
|
|
const qreal marginsWidth = margins.left() + margins.right();
|
|
|
|
|
|
|
|
if ( marginsWidth > rect.width() )
|
|
|
|
{
|
|
|
|
x = rect.x() + rect.width() * ( margins.left() / marginsWidth );
|
|
|
|
w = 0.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x = rect.x() + margins.left();
|
|
|
|
w = rect.width() - marginsWidth;
|
|
|
|
}
|
2018-08-03 08:15:28 +02:00
|
|
|
}
|
2017-10-17 17:34:00 +02:00
|
|
|
else
|
|
|
|
{
|
2019-04-24 08:39:13 +02:00
|
|
|
x = rect.x();
|
|
|
|
w = 0.0;
|
2018-08-03 08:15:28 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 08:39:13 +02:00
|
|
|
if ( rect.height() > 0.0 )
|
2017-10-17 17:34:00 +02:00
|
|
|
{
|
2019-04-24 08:39:13 +02:00
|
|
|
const qreal marginsHeight = margins.top() + margins.bottom();
|
|
|
|
if ( marginsHeight > rect.height() )
|
|
|
|
{
|
|
|
|
y = rect.y() + rect.height() * ( margins.top() / marginsHeight );
|
|
|
|
h = 0.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
y = rect.y() + margins.top();
|
|
|
|
h = rect.height() - marginsHeight;
|
|
|
|
}
|
2018-08-03 08:15:28 +02:00
|
|
|
}
|
2017-10-17 17:34:00 +02:00
|
|
|
else
|
|
|
|
{
|
2019-04-24 08:39:13 +02:00
|
|
|
y = rect.y();
|
|
|
|
h = 0.0;
|
2018-08-03 08:15:28 +02:00
|
|
|
}
|
2017-10-17 17:34:00 +02:00
|
|
|
|
|
|
|
return QRectF( x, y, w, h );
|
2018-08-03 08:15:28 +02:00
|
|
|
}
|
2017-10-17 17:34:00 +02:00
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
qreal qskDpiScaled( qreal value )
|
|
|
|
{
|
|
|
|
static qreal factor = 0.0;
|
|
|
|
|
|
|
|
if ( factor <= 0.0 )
|
|
|
|
{
|
|
|
|
if ( const QScreen* screen = QGuiApplication::primaryScreen() )
|
|
|
|
factor = screen->logicalDotsPerInchX();
|
|
|
|
else
|
|
|
|
factor = 100.0;
|
|
|
|
|
|
|
|
factor /= 96.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value * factor;
|
|
|
|
}
|
2020-03-10 10:17:44 +01:00
|
|
|
|
2020-03-13 13:32:22 +01:00
|
|
|
qreal qskHorizontalAdvance( const QFont& font, const QString& text )
|
|
|
|
{
|
|
|
|
return qskHorizontalAdvance( QFontMetricsF( font ), text );
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal qskHorizontalAdvance( const QFontMetricsF& fontMetrics, const QString& text )
|
|
|
|
{
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK( 5, 11, 0 )
|
|
|
|
return fontMetrics.horizontalAdvance( text );
|
|
|
|
#else
|
|
|
|
return fontMetrics.width( text );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-03-10 10:17:44 +01:00
|
|
|
bool qskHasPlatformWindowManagement()
|
|
|
|
{
|
|
|
|
if ( auto platform = QGuiApplicationPrivate::platformIntegration() )
|
|
|
|
return platform->hasCapability( QPlatformIntegration::WindowManagement );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect qskPlatformScreenGeometry( const QScreen* screen )
|
|
|
|
{
|
|
|
|
if ( screen == nullptr )
|
|
|
|
return QRect();
|
|
|
|
|
|
|
|
return screen->handle()->geometry();
|
|
|
|
}
|
2020-03-12 09:54:54 +01:00
|
|
|
|
2020-03-13 15:05:01 +01:00
|
|
|
#if 0
|
|
|
|
|
|
|
|
// does not compile with Qt 5.6 TODO ...
|
|
|
|
|
|
|
|
QSK_QT_PRIVATE_BEGIN
|
|
|
|
#include <private/qhighdpiscaling_p.h>
|
|
|
|
QSK_QT_PRIVATE_END
|
|
|
|
|
2020-03-12 09:54:54 +01:00
|
|
|
qreal qskGlobalScaleFactor()
|
|
|
|
{
|
|
|
|
// The value of QT_SCALE_FACTOR
|
|
|
|
const QScreen* noScreen = nullptr;
|
|
|
|
return QHighDpiScaling::factor( noScreen );
|
|
|
|
}
|
2020-03-13 15:05:01 +01:00
|
|
|
|
|
|
|
#endif
|