From 533105cadfde6d0113ea25481b9723e2b06de8e4 Mon Sep 17 00:00:00 2001 From: Andreas Aardal Hanssen Date: Tue, 11 Dec 2012 21:04:12 +0100 Subject: [PATCH] Deactivating an inactive panel no longer causes unwanted deactivation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QGraphicsItem::setActive() is by design not guarded against calls that do not change the current activation state of the item (e.g., calling setActive(true) on an active item or calling setActive(false) on an inactive item). This is to ensure that it's possible to set explicit activation state on items, either before they are added to a scene, or while the scene itself is inactive. Before this fix, calling setActive(false) on a panel item that is not currently active would by accident clear activation from any other panel that might have focus. After this fix, activation is only cleared if the item setActive() was called on itself is the active panel, or is the panel that will regain activation once the scene is reactivated. Task-number: QTBUG-28544 Change-Id: Ic4752f1e4400f9a0660bc968834747610212bb52 Reviewed-by: Bjørn Erik Nilsen Reviewed-by: Andreas Aardal Hanssen Reviewed-by: Yoann Lopes --- src/widgets/graphicsview/qgraphicsitem.cpp | 24 +++++++------ .../qgraphicsitem/tst_qgraphicsitem.cpp | 35 +++++++++++++++++++ 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/widgets/graphicsview/qgraphicsitem.cpp b/src/widgets/graphicsview/qgraphicsitem.cpp index cd379e1b05a..48430e60db4 100644 --- a/src/widgets/graphicsview/qgraphicsitem.cpp +++ b/src/widgets/graphicsview/qgraphicsitem.cpp @@ -3202,16 +3202,20 @@ void QGraphicsItem::setActive(bool active) // Activate this item. d_ptr->scene->setActivePanel(this); } else { - // Deactivate this item, and reactivate the parent panel, - // or the last active panel (if any). - QGraphicsItem *nextToActivate = 0; - if (d_ptr->parent) - nextToActivate = d_ptr->parent->panel(); - if (!nextToActivate) - nextToActivate = d_ptr->scene->d_func()->lastActivePanel; - if (nextToActivate == this || isAncestorOf(nextToActivate)) - nextToActivate = 0; - d_ptr->scene->setActivePanel(nextToActivate); + QGraphicsItem *activePanel = d_ptr->scene->activePanel(); + QGraphicsItem *thisPanel = panel(); + if (!activePanel || activePanel == thisPanel) { + // Deactivate this item, and reactivate the parent panel, + // or the last active panel (if any). + QGraphicsItem *nextToActivate = 0; + if (d_ptr->parent) + nextToActivate = d_ptr->parent->panel(); + if (!nextToActivate) + nextToActivate = d_ptr->scene->d_func()->lastActivePanel; + if (nextToActivate == this || isAncestorOf(nextToActivate)) + nextToActivate = 0; + d_ptr->scene->setActivePanel(nextToActivate); + } } } } diff --git a/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp index d5a40d07431..cd246ac25d5 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp @@ -428,6 +428,7 @@ private slots: void activate(); void setActivePanelOnInactiveScene(); void activationOnShowHide(); + void deactivateInactivePanel(); void moveWhileDeleting(); void ensureDirtySceneTransform(); void focusScope(); @@ -9030,6 +9031,40 @@ public: } }; +void tst_QGraphicsItem::deactivateInactivePanel() +{ + QGraphicsScene scene; + QGraphicsItem *panel1 = scene.addRect(QRectF(0, 0, 10, 10)); + panel1->setFlag(QGraphicsItem::ItemIsPanel); + + QGraphicsItem *panel2 = scene.addRect(QRectF(0, 0, 10, 10)); + panel2->setFlag(QGraphicsItem::ItemIsPanel); + + QEvent event(QEvent::WindowActivate); + qApp->sendEvent(&scene, &event); + + panel1->setActive(true); + QVERIFY(scene.isActive()); + QVERIFY(panel1->isActive()); + QVERIFY(!panel2->isActive()); + QCOMPARE(scene.activePanel(), panel1); + + panel2->setActive(true); + QVERIFY(panel2->isActive()); + QVERIFY(!panel1->isActive()); + QCOMPARE(scene.activePanel(), panel2); + + panel2->setActive(false); + QVERIFY(panel1->isActive()); + QVERIFY(!panel2->isActive()); + QCOMPARE(scene.activePanel(), panel1); + + panel2->setActive(false); + QVERIFY(panel1->isActive()); + QVERIFY(!panel2->isActive()); + QCOMPARE(scene.activePanel(), panel1); +} + void tst_QGraphicsItem::moveWhileDeleting() { QGraphicsScene scene;