From 9428bca72e472e5a331976e49a68b5484ccc5f88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 24 Oct 2016 18:35:32 +0200 Subject: [PATCH] QWindow: re-order siblings on raise() and lower() Same behavior as QWidget, and allows platform plugins to maintain order of native windows based on the QWindow hierarchy, instead of having to manually keep track of window levels. Change-Id: Iacc7e9ee2527f0737c9da6debc7cec101064f782 Reviewed-by: Lars Knoll Reviewed-by: Paul Olav Tvete --- src/gui/kernel/qwindow.cpp | 30 +++++++++++++++++++ src/gui/kernel/qwindow_p.h | 3 ++ tests/auto/gui/kernel/qwindow/tst_qwindow.cpp | 24 +++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index edb65a2d4c..95e552cf5f 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -343,6 +343,30 @@ void QWindowPrivate::updateVisibility() emit q->visibilityChanged(visibility); } +void QWindowPrivate::updateSiblingPosition(SiblingPosition position) +{ + Q_Q(QWindow); + + if (!q->parent()) + return; + + QObjectList &siblings = q->parent()->d_ptr->children; + + const int siblingCount = siblings.size() - 1; + if (siblingCount == 0) + return; + + const int currentPosition = siblings.indexOf(q); + Q_ASSERT(currentPosition >= 0); + + const int targetPosition = position == PositionTop ? siblingCount : 0; + + if (currentPosition == targetPosition) + return; + + siblings.move(currentPosition, targetPosition); +} + inline bool QWindowPrivate::windowRecreationRequired(QScreen *newScreen) const { Q_Q(const QWindow); @@ -920,6 +944,9 @@ QIcon QWindow::icon() const void QWindow::raise() { Q_D(QWindow); + + d->updateSiblingPosition(QWindowPrivate::PositionTop); + if (d->platformWindow) d->platformWindow->raise(); } @@ -932,6 +959,9 @@ void QWindow::raise() void QWindow::lower() { Q_D(QWindow); + + d->updateSiblingPosition(QWindowPrivate::PositionBottom); + if (d->platformWindow) d->platformWindow->lower(); } diff --git a/src/gui/kernel/qwindow_p.h b/src/gui/kernel/qwindow_p.h index b8a9f5d3de..dd5aa54b4f 100644 --- a/src/gui/kernel/qwindow_p.h +++ b/src/gui/kernel/qwindow_p.h @@ -144,6 +144,9 @@ public: void updateVisibility(); void _q_clearAlert(); + enum SiblingPosition { PositionTop, PositionBottom }; + void updateSiblingPosition(SiblingPosition); + bool windowRecreationRequired(QScreen *newScreen) const; void create(bool recursive); void setTopLevelScreen(QScreen *newScreen, bool recreate); diff --git a/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp b/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp index a41c574454..45be8bf099 100644 --- a/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp +++ b/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp @@ -65,6 +65,7 @@ private slots: void positioningDuringMinimized(); void childWindowPositioning_data(); void childWindowPositioning(); + void childWindowLevel(); void platformSurface(); void isExposed(); void isActive(); @@ -596,6 +597,29 @@ void tst_QWindow::childWindowPositioning() QCOMPARE(childWindowAfter.framePosition(), topLeftOrigin); } +void tst_QWindow::childWindowLevel() +{ + ColoredWindow topLevel(Qt::green); + topLevel.setObjectName("topLevel"); + ColoredWindow yellowChild(Qt::yellow, &topLevel); + yellowChild.setObjectName("yellowChild"); + ColoredWindow redChild(Qt::red, &topLevel); + redChild.setObjectName("redChild"); + ColoredWindow blueChild(Qt::blue, &topLevel); + blueChild.setObjectName("blueChild"); + + const QObjectList &siblings = topLevel.children(); + + QCOMPARE(siblings.constFirst(), &yellowChild); + QCOMPARE(siblings.constLast(), &blueChild); + + yellowChild.raise(); + QCOMPARE(siblings.constLast(), &yellowChild); + + blueChild.lower(); + QCOMPARE(siblings.constFirst(), &blueChild); +} + // QTBUG-49709: Verify that the normal geometry is correctly restored // when executing a sequence of window state changes. So far, Windows // only where state changes have immediate effect.