diff options
author | Andreas Aardal Hanssen <andrhans@cisco.com> | 2012-11-23 21:52:41 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-12-07 09:11:59 +0100 |
commit | 4a9c3b2433d4552bdabb832f71204aca95e68897 (patch) | |
tree | 4531a83a1c5ca5e5c70d95a7ece64f269f0834ce /tests/auto/widgets/graphicsview/qgraphicswidget | |
parent | 9c02a285529945bf1c3b8fb6d6faf7b382724f72 (diff) |
Repair QGraphicsWidget focus chain when used with ItemIsPanel.
Add handling of the focus chain to QGraphicsItem::setFlags(), so that
the focus chain is repaired (panels pop out of the chain and non-panels
merge back in) when the ItemIsPanel flag is toggled. Add handling focus
chain to QGraphicsWidgetPrivate::fixFocusChainBeforeReparenting for
panels.
Before this fix, you must enable the ItemIsPanel flag before adding
the item as a child to a parent panel, and you lose focus when using
the tab key to focus around a panel after it has been reparented into
another panel.
Task-number: QTBUG-28187
Change-Id: I1d0d81a90697eaf715a8a337c8bf6c2159329e68
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'tests/auto/widgets/graphicsview/qgraphicswidget')
-rw-r--r-- | tests/auto/widgets/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp | 103 |
1 files changed, 102 insertions, 1 deletions
diff --git a/tests/auto/widgets/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp b/tests/auto/widgets/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp index 901bb7c38a..ad8fa3f241 100644 --- a/tests/auto/widgets/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp @@ -172,12 +172,12 @@ private slots: void initialShow2(); void itemChangeEvents(); void itemSendGeometryPosChangesDeactivated(); - void fontPropagatesResolveToChildren(); void fontPropagatesResolveToGrandChildren(); void fontPropagatesResolveInParentChange(); void fontPropagatesResolveViaNonWidget(); void fontPropagatesResolveFromScene(); + void tabFocus(); // Task fixes void task236127_bspTreeIndexFails(); @@ -3303,6 +3303,107 @@ void tst_QGraphicsWidget::itemSendGeometryPosChangesDeactivated() QCOMPARE(item->geometry(), QRectF(10, 10, 60, 60)); } +class TabFocusWidget : public QGraphicsWidget +{ + Q_OBJECT +public: + TabFocusWidget(const QString &name, QGraphicsItem *parent = 0) + : QGraphicsWidget(parent) + { setFocusPolicy(Qt::TabFocus); setData(0, name); } +}; + +void verifyTabFocus(QGraphicsScene *scene, const QList<QGraphicsWidget *> &chain, bool wrapsAround) +{ + QKeyEvent tabEvent(QEvent::KeyPress, Qt::Key_Tab, 0); + QKeyEvent backtabEvent(QEvent::KeyPress, Qt::Key_Backtab, 0); + + for (int i = 0; i < chain.size(); ++i) + chain.at(i)->clearFocus(); + + int n = chain.size() * (wrapsAround ? 3 : 1); + for (int i = 0; i < n; ++i) + { + if (i == 0) + chain.at(0)->setFocus(); + else + qApp->sendEvent(scene, &tabEvent); + QVERIFY(chain.at(i % chain.size())->hasFocus()); + QCOMPARE(scene->focusItem(), chain.at(i % chain.size())); + } + for (int i = n - 2; i >= 0; --i) + { + qApp->sendEvent(scene, &backtabEvent); + QVERIFY(chain.at(i % chain.size())->hasFocus()); + QCOMPARE(scene->focusItem(), chain.at(i % chain.size())); + } +} + +void tst_QGraphicsWidget::tabFocus() +{ + QGraphicsScene scene; + scene.setFocus(); + + QEvent activate(QEvent::WindowActivate); + qApp->sendEvent(&scene, &activate); + + TabFocusWidget *widget = new TabFocusWidget("1"); + scene.addItem(widget); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget, false); + + TabFocusWidget *widget2 = new TabFocusWidget("2"); + scene.addItem(widget2); + scene.setFocusItem(0); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget << widget2, false); + + TabFocusWidget *widget3 = new TabFocusWidget("3"); + widget3->setFlag(QGraphicsItem::ItemIsPanel); + scene.addItem(widget3); + QCOMPARE(scene.activePanel(), (QGraphicsItem *)widget3); + scene.setActivePanel(0); + scene.setFocusItem(0); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget << widget2, false); + + scene.setActivePanel(widget3); + widget3->setFocus(); + QCOMPARE(scene.focusItem(), (QGraphicsItem *)widget3); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget3, true); + + TabFocusWidget *widget4 = new TabFocusWidget("4"); + widget4->setParentItem(widget3); + QVERIFY(widget3->hasFocus()); + widget3->clearFocus(); + QVERIFY(!widget3->focusItem()); + QCOMPARE(scene.activePanel(), (QGraphicsItem *)widget3); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget3 << widget4, true); + + QGraphicsWidget *widget5 = new QGraphicsWidget; widget5->setData(0, QLatin1String("5")); + widget5->setParentItem(widget3); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget3 << widget4, true); + + widget5->setFocusPolicy(Qt::TabFocus); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget3 << widget4 << widget5, true); + + TabFocusWidget *widget6 = new TabFocusWidget("6"); + widget6->setParentItem(widget4); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget3 << widget4 << widget6 << widget5, true); + + TabFocusWidget *widget7 = new TabFocusWidget("7", widget6); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget3 << widget4 << widget6 << widget7 << widget5, true); + + TabFocusWidget *widget8 = new TabFocusWidget("8", widget6); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget3 << widget4 << widget6 << widget7 << widget8 << widget5, true); + widget6->setFlag(QGraphicsItem::ItemIsPanel); + widget6->setActive(true); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget6 << widget7 << widget8, true); + widget3->setActive(true); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget3 << widget4 << widget5, true); + widget6->setFlag(QGraphicsItem::ItemIsPanel, false); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget3 << widget4 << widget6 << widget7 << widget8 << widget5, true); + scene.removeItem(widget6); + verifyTabFocus(&scene, QList<QGraphicsWidget *>() << widget3 << widget4 << widget5, true); + delete widget6; +} + void tst_QGraphicsWidget::QT_BUG_6544_tabFocusFirstUnsetWhenRemovingItems() { QGraphicsScene scene; |