commit 22abccea8e22d65c74f2d40e95a384b69ed38fde Author: Marcel Gotsch Date: Thu Jan 12 18:16:29 2023 +0100 add scale API to QWindow diff --git a/qtbase/src/gui/kernel/qhighdpiscaling.cpp b/qtbase/src/gui/kernel/qhighdpiscaling.cpp index 89a7a3afc3..ee1193f6ae 100644 --- a/qtbase/src/gui/kernel/qhighdpiscaling.cpp +++ b/qtbase/src/gui/kernel/qhighdpiscaling.cpp @@ -758,14 +758,15 @@ QHighDpiScaling::ScaleAndOrigin QHighDpiScaling::scaleAndOrigin(const QScreen *s QHighDpiScaling::ScaleAndOrigin QHighDpiScaling::scaleAndOrigin(const QWindow *window, QHighDpiScaling::Point position) { if (!m_active) - return { qreal(1), QPoint() }; + return { window->scale(), QPoint() }; // Determine correct screen; use the screen which contains the given // position if a valid position is passed. QScreen *screen = window ? window->screen() : QGuiApplication::primaryScreen(); QScreen *overrideScreen = QHighDpiScaling::screenForPosition(position, screen); QScreen *targetScreen = overrideScreen ? overrideScreen : screen; - return scaleAndOrigin(targetScreen, position); + auto so = scaleAndOrigin(targetScreen, position); + return { so.factor*window->scale(), so.origin }; } #else diff --git a/qtbase/src/gui/kernel/qplatformwindow.cpp b/qtbase/src/gui/kernel/qplatformwindow.cpp index 633a262d56..dbe9102590 100644 --- a/qtbase/src/gui/kernel/qplatformwindow.cpp +++ b/qtbase/src/gui/kernel/qplatformwindow.cpp @@ -751,7 +751,7 @@ QRect QPlatformWindow::initialGeometry(const QWindow *w, const QRect &initialGeo } } } - return QHighDpi::toNativePixels(deviceIndependentRect, screen); + return QHighDpi::toNativePixels(deviceIndependentRect, w); } /*! diff --git a/qtbase/src/gui/kernel/qwindow.cpp b/qtbase/src/gui/kernel/qwindow.cpp index d4031b3f54..5a1d15ea53 100644 --- a/qtbase/src/gui/kernel/qwindow.cpp +++ b/qtbase/src/gui/kernel/qwindow.cpp @@ -1334,7 +1334,7 @@ qreal QWindow::devicePixelRatio() const // which typically is the primary screen and will be correct for single-display // systems (a very common case). if (!d->platformWindow) - return screen()->devicePixelRatio(); + return d->scale*screen()->devicePixelRatio(); return d->platformWindow->devicePixelRatio() * QHighDpiScaling::factor(this); } @@ -1727,6 +1727,31 @@ void QWindow::setSizeIncrement(const QSize &size) d->platformWindow->propagateSizeHints(); } +qreal QWindow::scale() const +{ + Q_D(const QWindow); + return d->scale; +} + +void QWindow::setScale(qreal scale) +{ + Q_D(QWindow); + + if (qFuzzyCompare(d->scale, scale)) + { + return; + } + + auto oldGeometry = geometry(); + d->scale = scale; + + if (d->platformWindow) { + d->platformWindow->setGeometry(QHighDpi::toNativeWindowGeometry(oldGeometry, this)); + } + + emit scaleChanged(); +} + /*! Sets the geometry of the window, excluding its window frame, to a rectangle constructed from \a posx, \a posy, \a w and \a h. diff --git a/qtbase/src/gui/kernel/qwindow.h b/qtbase/src/gui/kernel/qwindow.h index 102a324366..b3593e2c79 100644 --- a/qtbase/src/gui/kernel/qwindow.h +++ b/qtbase/src/gui/kernel/qwindow.h @@ -123,6 +123,7 @@ class Q_GUI_EXPORT QWindow : public QObject, public QSurface Q_PROPERTY(int maximumWidth READ maximumWidth WRITE setMaximumWidth NOTIFY maximumWidthChanged) Q_PROPERTY(int maximumHeight READ maximumHeight WRITE setMaximumHeight NOTIFY maximumHeightChanged) + Q_PROPERTY(qreal scale READ scale WRITE setScale NOTIFY scaleChanged REVISION(6, 3)) Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged) Q_PROPERTY(bool active READ isActive NOTIFY activeChanged REVISION(2, 1)) Q_PROPERTY(Visibility visibility READ visibility WRITE setVisibility NOTIFY visibilityChanged @@ -230,6 +231,9 @@ public: void setBaseSize(const QSize &size); void setSizeIncrement(const QSize &size); + qreal scale() const; + void setScale(qreal scale); + QRect geometry() const; QMargins frameMargins() const; @@ -345,6 +349,8 @@ Q_SIGNALS: void maximumWidthChanged(int arg); void maximumHeightChanged(int arg); + Q_REVISION(6, 3) void scaleChanged(); + void visibleChanged(bool arg); Q_REVISION(2, 1) void visibilityChanged(QWindow::Visibility visibility); Q_REVISION(2, 1) void activeChanged(); diff --git a/qtbase/src/gui/kernel/qwindow_p.h b/qtbase/src/gui/kernel/qwindow_p.h index dafd09b1e2..bfb4cc672e 100644 --- a/qtbase/src/gui/kernel/qwindow_p.h +++ b/qtbase/src/gui/kernel/qwindow_p.h @@ -152,6 +152,7 @@ public: bool resizeAutomatic = true; Qt::ScreenOrientation contentOrientation = Qt::PrimaryOrientation; qreal opacity= 1; + qreal scale = 1; QRegion mask; QSize minimumSize = {0, 0}; diff --git a/qtdeclarative/src/quick/items/qquickwindow.cpp b/qtdeclarative/src/quick/items/qquickwindow.cpp index 8738aa50d3..8e39f1d2f0 100644 --- a/qtdeclarative/src/quick/items/qquickwindow.cpp +++ b/qtdeclarative/src/quick/items/qquickwindow.cpp @@ -428,7 +428,7 @@ static void updatePixelRatioHelper(QQuickItem *item, float pixelRatio) void QQuickWindow::physicalDpiChanged() { Q_D(QQuickWindow); - const qreal newPixelRatio = screen()->devicePixelRatio(); + const qreal newPixelRatio = QWindow::devicePixelRatio(); if (qFuzzyCompare(newPixelRatio, d->devicePixelRatio)) return; d->devicePixelRatio = newPixelRatio; @@ -453,6 +453,13 @@ void QQuickWindow::handleScreenChanged(QScreen *screen) d->forcePolish(); } +void QQuickWindow::handleScaleChanged() +{ + Q_D(QQuickWindow); + physicalDpiChanged(); + d->forcePolish(); +} + void forcePolishHelper(QQuickItem *item) { if (item->flags() & QQuickItem::ItemHasContents) { @@ -770,7 +777,7 @@ void QQuickWindowPrivate::init(QQuickWindow *c, QQuickRenderControl *control) Q_ASSERT(windowManager || renderControl); if (QScreen *screen = q->screen()) { - devicePixelRatio = screen->devicePixelRatio(); + devicePixelRatio = scale*screen->devicePixelRatio(); // if the screen changes, then QQuickWindow::handleScreenChanged disconnects // and connects to the new screen physicalDpiChangedConnection = QObject::connect(screen, &QScreen::physicalDotsPerInchChanged, @@ -803,6 +810,7 @@ void QQuickWindowPrivate::init(QQuickWindow *c, QQuickRenderControl *control) QObject::connect(q, SIGNAL(focusObjectChanged(QObject*)), q, SIGNAL(activeFocusItemChanged())); QObject::connect(q, SIGNAL(screenChanged(QScreen*)), q, SLOT(handleScreenChanged(QScreen*))); + QObject::connect(q, SIGNAL(scaleChanged()), q, SLOT(handleScaleChanged())); QObject::connect(qApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)), q, SLOT(handleApplicationStateChanged(Qt::ApplicationState))); QObject::connect(q, SIGNAL(frameSwapped()), q, SLOT(runJobsAfterSwap()), Qt::DirectConnection); diff --git a/qtdeclarative/src/quick/items/qquickwindow.h b/qtdeclarative/src/quick/items/qquickwindow.h index 48878b057d..6016d43854 100644 --- a/qtdeclarative/src/quick/items/qquickwindow.h +++ b/qtdeclarative/src/quick/items/qquickwindow.h @@ -256,6 +256,7 @@ private Q_SLOTS: void cleanupSceneGraph(); void physicalDpiChanged(); void handleScreenChanged(QScreen *screen); + void handleScaleChanged(); void setTransientParent_helper(QQuickWindow *window); void runJobsAfterSwap(); void handleApplicationStateChanged(Qt::ApplicationState state);