diff options
author | Andreas Aardal Hanssen <andreas.aardal.hanssen@nokia.com> | 2009-09-11 07:30:53 (GMT) |
---|---|---|
committer | Andreas Aardal Hanssen <andreas.aardal.hanssen@nokia.com> | 2009-09-15 09:31:02 (GMT) |
commit | a06c7db92ff13a1f8a353851a586ae12755e7c6c (patch) | |
tree | 775c7364e2591045304bfd737f0dd93b88a2bf9a | |
parent | 9b579d7821205250b8d64b06a19d5e4fccd56f31 (diff) | |
download | Qt-a06c7db92ff13a1f8a353851a586ae12755e7c6c.zip Qt-a06c7db92ff13a1f8a353851a586ae12755e7c6c.tar.gz Qt-a06c7db92ff13a1f8a353851a586ae12755e7c6c.tar.bz2 |
Fix bugs in handling of initial focus.
Make sure you can't set focus on an inactive scene, allow items
with subfocus to gain focus even if added to a scene that's not
active, and finally ensure that activating a panel in an active,
but unfocused scene, gives focus to the scene.
Also added a test that checks adding normal vs. panel items to an
active vs. inactive scene, and what happens if you initially say
the item should have focus.
Reviewed-by: TrustMe
-rw-r--r-- | src/gui/graphicsview/qgraphicsscene.cpp | 7 | ||||
-rw-r--r-- | tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp | 58 |
2 files changed, 63 insertions, 2 deletions
diff --git a/src/gui/graphicsview/qgraphicsscene.cpp b/src/gui/graphicsview/qgraphicsscene.cpp index fbd78d9..97bc010 100644 --- a/src/gui/graphicsview/qgraphicsscene.cpp +++ b/src/gui/graphicsview/qgraphicsscene.cpp @@ -581,6 +581,9 @@ void QGraphicsScenePrivate::setActivePanelHelper(QGraphicsItem *item, bool durin return; } + // Ensure the scene has focus when we change panel activation. + q->setFocus(Qt::ActiveWindowFocusReason); + // Find the item's panel. QGraphicsItem *panel = item ? item->panel() : 0; lastActivePanel = panel ? activePanel : 0; @@ -2431,7 +2434,7 @@ void QGraphicsScene::addItem(QGraphicsItem *item) // Ensure that newly added items that have subfocus set, gain // focus automatically if there isn't a focus item already. - if (!d->focusItem && item->focusItem() && item->isActive()) + if (!d->focusItem && item->focusItem()) item->focusItem()->setFocus(); d->updateInputMethodSensitivityInViews(); @@ -2780,7 +2783,7 @@ bool QGraphicsScene::hasFocus() const void QGraphicsScene::setFocus(Qt::FocusReason focusReason) { Q_D(QGraphicsScene); - if (d->hasFocus) + if (d->hasFocus || !isActive()) return; QFocusEvent event(QEvent::FocusIn, focusReason); QCoreApplication::sendEvent(this, &event); diff --git a/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp b/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp index 6998684..07d7cce 100644 --- a/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp +++ b/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp @@ -264,6 +264,8 @@ private slots: void inputMethod_data(); void inputMethod(); void dispatchHoverOnPress(); + void initialFocus_data(); + void initialFocus(); // task specific tests below me void task139710_bspTreeCrash(); @@ -3822,5 +3824,61 @@ void tst_QGraphicsScene::dispatchHoverOnPress() } } +void tst_QGraphicsScene::initialFocus_data() +{ + QTest::addColumn<bool>("activeScene"); + QTest::addColumn<bool>("explicitSetFocus"); + QTest::addColumn<bool>("isPanel"); + QTest::addColumn<bool>("shouldHaveFocus"); + + QTest::newRow("inactive scene, normal item") << false << false << false << false; + QTest::newRow("inactive scene, panel item") << false << false << true << false; + QTest::newRow("inactive scene, normal item, explicit focus") << false << true << false << true; + QTest::newRow("inactive scene, panel, explicit focus") << false << true << true << true; + QTest::newRow("active scene, normal item") << true << false << false << false; + QTest::newRow("active scene, panel item") << true << false << true << false; + QTest::newRow("active scene, normal item, explicit focus") << true << true << false << true; + QTest::newRow("active scene, panel, explicit focus") << true << true << true << true; +} + +void tst_QGraphicsScene::initialFocus() +{ + QFETCH(bool, activeScene); + QFETCH(bool, explicitSetFocus); + QFETCH(bool, isPanel); + QFETCH(bool, shouldHaveFocus); + + QGraphicsRectItem *rect = new QGraphicsRectItem; + rect->setFlag(QGraphicsItem::ItemIsFocusable); + QVERIFY(!rect->hasFocus()); + + if (isPanel) + rect->setFlag(QGraphicsItem::ItemIsPanel); + + // Setting focus on an item before adding to the scene will ensure + // it gets focus when the scene is activated. + if (explicitSetFocus) + rect->setFocus(); + + QGraphicsScene scene; + QVERIFY(!scene.isActive()); + + if (activeScene) { + QEvent windowActivate(QEvent::WindowActivate); + qApp->sendEvent(&scene, &windowActivate); + scene.setFocus(); + } + + scene.addItem(rect); + + if (!activeScene) { + QEvent windowActivate(QEvent::WindowActivate); + qApp->sendEvent(&scene, &windowActivate); + scene.setFocus(); + } + + QCOMPARE(rect->hasFocus(), shouldHaveFocus); +} + QTEST_MAIN(tst_QGraphicsScene) #include "tst_qgraphicsscene.moc" |