From 4f244c8f76ed7d676ce6e1e2b7342f4cfbf5a379 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Sun, 5 Mar 2023 11:52:42 +0100 Subject: [PATCH] pixel scaling algo moved from M3 to QskPlatform.h --- playground/dials/SkinFactory.cpp | 2 +- skins/material3/QskMaterial3Skin.cpp | 53 ++-------------------------- skins/squiek/QskSquiekSkin.cpp | 50 +++++++++++++++----------- src/common/QskPlatform.cpp | 48 ++++++++++++++++--------- src/common/QskPlatform.h | 20 ++++++++++- src/controls/QskSkin.cpp | 2 +- src/controls/QskSubWindow.cpp | 4 +-- src/controls/QskSubWindowArea.cpp | 2 +- 8 files changed, 88 insertions(+), 93 deletions(-) diff --git a/playground/dials/SkinFactory.cpp b/playground/dials/SkinFactory.cpp index dc7300e8..926facfd 100644 --- a/playground/dials/SkinFactory.cpp +++ b/playground/dials/SkinFactory.cpp @@ -20,7 +20,7 @@ namespace inline QFont qskFont( qreal pointSize ) { QFont font( "Roboto" ); - font.setPointSizeF( pointSize / qskDpiScaled( 1.0 ) ); + font.setPointSizeF( pointSize / qskDpToPixels( 1.0 ) ); return font; } diff --git a/skins/material3/QskMaterial3Skin.cpp b/skins/material3/QskMaterial3Skin.cpp index 6ca894e7..1fa55aa2 100644 --- a/skins/material3/QskMaterial3Skin.cpp +++ b/skins/material3/QskMaterial3Skin.cpp @@ -59,61 +59,14 @@ static const int qskDuration = 150; namespace { - // see: https://en.wikipedia.org/wiki/Device-independent_pixel - inline qreal dpToPixels( long double value ) - { - static qreal factor = -1.0; - - if ( factor < 0.0 ) - { - factor = 1.0; - - if ( const auto screen = QGuiApplication::primaryScreen() ) - { - const qreal pdpi = screen->physicalDotsPerInch(); - qreal dpi; - - // calculate buckets according to https://developer.android.com/training/multiscreen/screendensities - if( pdpi <= 140.0 ) - { - dpi = 120.0; // ldpi - } - else if( pdpi <= 200.0 ) - { - dpi = 160.0; // mdpi - } - else if( pdpi <= 280.0 ) - { - dpi = 240.0; // hdpi - } - else if( pdpi <= 400.0 ) - { - dpi = 320.0; // xhdpi - } - else if( pdpi <= 560.0 ) - { - dpi = 480.0; // xxhdpi - } - else - { - dpi = 640.0; // xxxhdpi - } - - factor = dpi / 160.0; - } - } - - return value * factor; - } - inline double operator ""_dp( long double value ) { - return dpToPixels( value ); + return qskDpToPixels( static_cast< qreal >( value ) ); } - inline double operator ""_dp( unsigned long long int value ) + inline double operator ""_dp( unsigned long long value ) { - return dpToPixels( value ); + return qskDpToPixels( value ); } class Editor : private QskSkinHintTableEditor diff --git a/skins/squiek/QskSquiekSkin.cpp b/skins/squiek/QskSquiekSkin.cpp index 8c92d590..4487a0eb 100644 --- a/skins/squiek/QskSquiekSkin.cpp +++ b/skins/squiek/QskSquiekSkin.cpp @@ -52,6 +52,16 @@ static const int qskDuration = 200; namespace { + inline double operator ""_dp( long double value ) + { + return qskDpToPixels( static_cast< qreal >( value ) ); + } + + inline double operator ""_dp( unsigned long long value ) + { + return qskDpToPixels( value ); + } + class ColorPalette { public: @@ -309,15 +319,15 @@ void Editor::setupCheckBox() using A = QskAspect; using Q = QskCheckBox; - const qreal size = qskDpiScaled( 26 ); + const qreal size = 26_dp; - setSpacing( Q::Panel, qskDpiScaled( 5 ) ); + setSpacing( Q::Panel, 5_dp ); setStrutSize( Q::Box, size, size ); - setPadding( Q::Box, qskDpiScaled( 5 ) ); - setBoxShape( Q::Box, qskDpiScaled( 3 ) ); - setBoxBorderMetrics( Q::Box, qskDpiScaled( 1 ) ); + setPadding( Q::Box, 5_dp ); + setBoxShape( Q::Box, 3_dp ); + setBoxBorderMetrics( Q::Box, 1_dp ); setBoxBorderColors( Q::Box, m_pal.darker125 ); setGradient( Q::Box, m_pal.lighter135 ); @@ -362,8 +372,8 @@ void Editor::setupMenu() setHint( Q::Overlay | A::Style, true ); setGradient( Q::Overlay, QColor( 220, 220, 220, 100 ) ); - setBoxShape( Q::Panel, qskDpiScaled( 4 ) ); - setBoxBorderMetrics( Q::Panel, qskDpiScaled( 1 ) ); + setBoxShape( Q::Panel, 4_dp ); + setBoxBorderMetrics( Q::Panel, 1_dp ); setBoxBorderColors( Q::Panel, m_pal.darker125 ); setGradient( Q::Panel, m_pal.lighter110 ); @@ -371,7 +381,7 @@ void Editor::setupMenu() const bool isCascading = qskMaybeDesktopPlatform(); setHint( Q::Panel | A::Style, isCascading ); - setMetric( Q::Separator | A::Size, qskDpiScaled( 2 ) ); + setMetric( Q::Separator | A::Size, 2_dp ); setSeparator( Q::Separator ); setPadding( Q::Segment, QskMargins( 2, 10, 2, 10 ) ); @@ -520,7 +530,7 @@ void Editor::setupSegmentedBar() setBoxBorderColors( Q::Panel, borderColors ); - const QSize strutSize( qskDpiScaled( 100 ), qskDpiScaled( 50 ) ); + const QSize strutSize( 100_dp, 50_dp ); setStrutSize( Q::Panel | A::Horizontal, strutSize ); setStrutSize( Q::Panel | A::Vertical, strutSize.transposed() ); @@ -576,7 +586,7 @@ void Editor::setupPageIndicator() { using Q = QskPageIndicator; - const auto extent = qskDpiScaled( 8 ); + const auto extent = 8_dp; setStrutSize( Q::Bullet, extent, extent ); // circles, without border @@ -584,7 +594,7 @@ void Editor::setupPageIndicator() setBoxBorderMetrics( Q::Bullet, 0 ); setGradient( Q::Bullet, m_pal.darker150 ); - setMargin( Q::Bullet, qskDpiScaled( 1 ) ); + setMargin( Q::Bullet, 1_dp ); setGradient( Q::Bullet | Q::Selected, m_pal.lighter150 ); setMargin( Q::Bullet | Q::Selected, 0 ); @@ -601,7 +611,7 @@ void Editor::setupPushButton() // Panel setHint( Q::Panel | QskAspect::Direction, Qsk::TopToBottom ); - setStrutSize( Q::Panel, qskDpiScaled( 75.0 ), qskDpiScaled( 23.0 ) ); + setStrutSize( Q::Panel, 75_dp, 23_dp ); setMargin( Q::Panel, 0 ); setPadding( Q::Panel, 10 ); @@ -641,18 +651,18 @@ void Editor::setupRadioBox() { using Q = QskRadioBox; - setSpacing( Q::Panel, qskDpiScaled( 10 ) ); - setStrutSize( Q::Button, qskDpiScaled( 20 ), qskDpiScaled( 20 ) ); + setSpacing( Q::Panel, 10_dp ); + setStrutSize( Q::Button, 20_dp, 20_dp ); for ( auto subControl : { Q::Button, Q::Indicator, Q::Ripple } ) setBoxShape( subControl, 100, Qt::RelativeSize ); // circular - setBoxBorderMetrics( Q::Button, qskDpiScaled( 1 ) ); + setBoxBorderMetrics( Q::Button, 1_dp ); setBoxBorderColors( Q::Button, m_pal.darker125 ); setBoxBorderColors( Q::Button | Q::Disabled, m_pal.theme ); - setPadding( Q::Button, qskDpiScaled( 5 ) ); + setPadding( Q::Button, 5_dp ); setColor( Q::Text, m_pal.themeForeground ); setColor( Q::Text | Q::Disabled, m_pal.darker200 ); @@ -897,7 +907,7 @@ void Editor::setupInputPredictionBar() setButton( Q::ButtonPanel, Flat ); setButton( Q::ButtonPanel | QskPushButton::Pressed, Sunken ); - setStrutSize( Q::ButtonPanel, qskDpiScaled( 30.0 ), qskDpiScaled( 23.0 ) ); + setStrutSize( Q::ButtonPanel, 30_dp, 23_dp ); setColor( Q::ButtonText, m_pal.themeForeground ); setColor( Q::ButtonText | QskPushButton::Disabled, m_pal.darker200 ); @@ -944,7 +954,7 @@ void Editor::setupScrollView() // scroll bars for ( auto subControl : { Q::HorizontalScrollBar, Q::VerticalScrollBar } ) { - setMetric( subControl | A::Size, qskDpiScaled( 12 ) ); + setMetric( subControl | A::Size, 12_dp ); setPadding( subControl, 0 ); setMargin( subControl, 0 ); } @@ -956,7 +966,7 @@ void Editor::setupScrollView() setButton( subControl, Raised, bw ); - const auto extent = qskDpiScaled( 40.0 ); + const auto extent = 40_dp; if ( subControl == Q::HorizontalScrollHandle ) { @@ -1092,7 +1102,7 @@ void Editor::setupSwitchButton() using A = QskAspect; using Q = QskSwitchButton; - const qreal radius = qskDpiScaled( 12 ); + const qreal radius = 15_dp; const qreal handleSize = 2 * ( radius - 2 ); setBoxShape( Q::Groove, 100, Qt::RelativeSize ); diff --git a/src/common/QskPlatform.cpp b/src/common/QskPlatform.cpp index e94f9b57..c42bbaf3 100644 --- a/src/common/QskPlatform.cpp +++ b/src/common/QskPlatform.cpp @@ -11,23 +11,6 @@ QSK_QT_PRIVATE_END #include #include -qreal qskDpiScaled( qreal value ) -{ - static qreal factor = 0.0; - - if ( factor <= 0.0 ) - { - if ( const auto screen = QGuiApplication::primaryScreen() ) - factor = screen->logicalDotsPerInchX(); - else - factor = 100.0; - - factor /= 96.0; - } - - return value * factor; -} - qreal qskGlobalScaleFactor() { // The value of QT_SCALE_FACTOR @@ -58,4 +41,35 @@ QRect qskPlatformScreenGeometry( const QScreen* screen ) return screen->handle()->geometry(); } +static inline qreal qskRoundedDpi( qreal dpi ) +{ + // see https://developer.android.com/training/multiscreen/screendensities + + if( dpi <= 140.0 ) + return 120.0; // ldpi + + if( dpi <= 200.0 ) + return 160.0; // mdpi + + if( dpi <= 280.0 ) + return 240.0; // hdpi + + if( dpi <= 400.0 ) + return 320.0; // xhdpi + + if( dpi <= 560.0 ) + return 480.0; // xxhdpi + + return 640.0; // xxxhdpi +} +qreal qskDpToPixelsFactor() +{ + if ( const auto screen = QGuiApplication::primaryScreen() ) + { + // see: https://en.wikipedia.org/wiki/Device-independent_pixel + return qskRoundedDpi( screen->physicalDotsPerInch() ) / 160.0; + } + + return 1.0; +} diff --git a/src/common/QskPlatform.h b/src/common/QskPlatform.h index 9184df0c..d2301e3b 100644 --- a/src/common/QskPlatform.h +++ b/src/common/QskPlatform.h @@ -12,11 +12,29 @@ class QScreen; class QPlatformIntegration; class QRect; -QSK_EXPORT qreal qskDpiScaled( qreal value ); QSK_EXPORT qreal qskGlobalScaleFactor(); QSK_EXPORT bool qskMaybeDesktopPlatform(); QSK_EXPORT const QPlatformIntegration* qskPlatformIntegration(); QSK_EXPORT QRect qskPlatformScreenGeometry( const QScreen* ); +/* + One dp is a virtual pixel unit that's roughly equal to one pixel + on a medium-density screen ( 160 dpi ). + + see: https://en.wikipedia.org/wiki/Device-independent_pixel + */ + +QSK_EXPORT qreal qskDpToPixelsFactor(); + +inline qreal qskDpToPixels( qreal value ) +{ + static qreal factor = -1.0; + + if ( factor < 0.0 ) + factor = qskDpToPixelsFactor(); + + return value * factor; +} + #endif diff --git a/src/controls/QskSkin.cpp b/src/controls/QskSkin.cpp index 5bc38b4a..8015fe6e 100644 --- a/src/controls/QskSkin.cpp +++ b/src/controls/QskSkin.cpp @@ -251,7 +251,7 @@ void QskSkin::setupFonts( const QString& family, int weight, bool italic ) for ( int i = TinyFont; i <= HugeFont; i++ ) { - font.setPixelSize( qskDpiScaled( sizes[i - 1] ) ); + font.setPixelSize( qskDpToPixels( sizes[i - 1] ) ); m_data->fonts[ i ] = font; } diff --git a/src/controls/QskSubWindow.cpp b/src/controls/QskSubWindow.cpp index 94115c93..f68b4e2e 100644 --- a/src/controls/QskSubWindow.cpp +++ b/src/controls/QskSubWindow.cpp @@ -222,10 +222,10 @@ QSizeF QskSubWindow::layoutSizeHint( { // should be Minimum Width/Height from the skin hints if ( hint.width() < 0.0 ) - hint.setWidth( qskDpiScaled( 100 ) ); + hint.setWidth( qskDpToPixels( 100 ) ); if ( hint.height() < 0.0 ) - hint.setHeight( qskDpiScaled( 80 ) ); + hint.setHeight( qskDpToPixels( 80 ) ); } return hint; diff --git a/src/controls/QskSubWindowArea.cpp b/src/controls/QskSubWindowArea.cpp index f50e2394..7c9942c0 100644 --- a/src/controls/QskSubWindowArea.cpp +++ b/src/controls/QskSubWindowArea.cpp @@ -26,7 +26,7 @@ static void qskUpdateEventFilter( QskSubWindowArea* area ) static Qt::Edges qskSelectedEdges( const QRectF& rect, const QPointF& pos ) { - const qreal tolerance = qskDpiScaled( 10.0 ); + const qreal tolerance = qskDpToPixels( 10.0 ); Qt::Edges edges; if ( pos.x() <= rect.left() + tolerance )