summaryrefslogtreecommitdiffstats
path: root/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
diff options
context:
space:
mode:
authorAndreas Aardal Hanssen <andreas.aardal.hanssen@nokia.com>2009-07-24 03:40:51 +0200
committerAndreas Aardal Hanssen <andreas.aardal.hanssen@nokia.com>2009-07-27 07:46:39 +0200
commitf68fed388dcdba6ab6dad3af4933bcd3aa123cf8 (patch)
treeb0e386b94a444db6bbf9debd93c9fd9b89473114 /tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
parent092b004126f82545b4237e43507f21920d06ac58 (diff)
Add QGraphicsItem::ItemAutoDetectsFocusProxy and improve subfocus support.
If you set this flag on an item, and descendant item that gains input focus will become this item's focus proxy. This simplifies how focus proxy items are assigned from QML; instead of binding the possible focusProxy property to a named child widget, this assignment happens automatically as you set the focus property of a descendant to true. As part of this change, QGraphicsWidget::focusWidget behavior has been improved and moved into QGraphicsItem. For example, if you set focus on an item that it's part of a scene, it can gain focus once the parent has been assigned (which is how object trees are built in QML). Autotests are included. Reviewed-by: Michael Brasser
Diffstat (limited to 'tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp')
-rw-r--r--tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp107
1 files changed, 107 insertions, 0 deletions
diff --git a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
index 011e480b09..9f1693dc87 100644
--- a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
+++ b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
@@ -275,6 +275,9 @@ private slots:
void itemHasNoContents();
void hitTestUntransformableItem();
void focusProxy();
+ void autoDetectFocusProxy();
+ void subFocus();
+ void reverseCreateAutoFocusProxy();
// task specific tests below me
void task141694_textItemEnsureVisible();
@@ -7464,5 +7467,109 @@ void tst_QGraphicsItem::focusProxy()
QCOMPARE(item3->focusProxy(), (QGraphicsItem *)0);
}
+void tst_QGraphicsItem::autoDetectFocusProxy()
+{
+ QGraphicsScene scene;
+ QGraphicsItem *item = scene.addRect(0, 0, 10, 10);
+ item->setFlag(QGraphicsItem::ItemIsFocusable);
+
+ QGraphicsItem *item2 = scene.addRect(0, 0, 10, 10);
+ item2->setParentItem(item);
+ item2->setFlag(QGraphicsItem::ItemIsFocusable);
+
+ QGraphicsItem *item3 = scene.addRect(0, 0, 10, 10);
+ item3->setParentItem(item2);
+ item3->setFlag(QGraphicsItem::ItemIsFocusable);
+
+ item->setFlag(QGraphicsItem::ItemAutoDetectsFocusProxy);
+ QCOMPARE(item->focusProxy(), (QGraphicsItem *)0);
+
+ item2->setFocus();
+ QCOMPARE(item->focusProxy(), item2);
+ item3->setFocus();
+ QCOMPARE(item->focusProxy(), item3);
+ item3->clearFocus();
+ QCOMPARE(item->focusProxy(), item3);
+}
+
+void tst_QGraphicsItem::subFocus()
+{
+ // Construct a text item that's not part of a scene (yet)
+ // and has no parent. Setting focus on it will not make
+ // the item gain input focus; that requires a scene. But
+ // it does set subfocus, indicating that the item wishes
+ // to gain focus later.
+ QGraphicsTextItem *text = new QGraphicsTextItem("Hello");
+ text->setTextInteractionFlags(Qt::TextEditorInteraction);
+ QVERIFY(!text->hasFocus());
+ text->setFocus();
+ QVERIFY(!text->hasFocus());
+ QCOMPARE(text->focusItem(), (QGraphicsItem *)text);
+
+ // Add a sibling.
+ QGraphicsTextItem *text2 = new QGraphicsTextItem("Hi");
+ text2->setTextInteractionFlags(Qt::TextEditorInteraction);
+ text2->setPos(30, 30);
+
+ // Add both items to a scene and check that it's text that
+ // got input focus.
+ QGraphicsScene scene;
+ scene.addItem(text);
+ scene.addItem(text2);
+ QVERIFY(text->hasFocus());
+
+ text->setData(0, "text");
+ text2->setData(0, "text2");
+
+ // Remove text2 and set subfocus on it. Then readd. Reparent it onto the
+ // other item and see that although it becomes text's focus
+ // item, it does not immediately gain input focus.
+ scene.removeItem(text2);
+ text2->setFocus();
+ scene.addItem(text2);
+ QCOMPARE(text2->focusItem(), (QGraphicsItem *)text2);
+ text2->setParentItem(text);
+ qDebug() << text->data(0).toString() << (void*)(QGraphicsItem *)text;
+ qDebug() << text2->data(0).toString() << (void*)(QGraphicsItem *)text2;
+ QCOMPARE(text->focusItem(), (QGraphicsItem *)text2);
+ QVERIFY(text->hasFocus());
+ QVERIFY(!text2->hasFocus());
+
+ // Remove both items from the scene, restore subfocus and
+ // readd them. Now the subfocus should kick in and give
+ // text2 focus.
+ scene.removeItem(text);
+ QCOMPARE(text->focusItem(), (QGraphicsItem *)0);
+ QCOMPARE(text2->focusItem(), (QGraphicsItem *)0);
+ text2->setFocus();
+ QCOMPARE(text->focusItem(), (QGraphicsItem *)text2);
+ QCOMPARE(text2->focusItem(), (QGraphicsItem *)text2);
+ scene.addItem(text);
+
+ // Hiding and showing text should pass focus to text2.
+ QCOMPARE(text->focusItem(), (QGraphicsItem *)text2);
+ QVERIFY(text2->hasFocus());
+}
+
+void tst_QGraphicsItem::reverseCreateAutoFocusProxy()
+{
+ QGraphicsTextItem *text = new QGraphicsTextItem;
+ text->setTextInteractionFlags(Qt::TextEditorInteraction);
+ text->setFlag(QGraphicsItem::ItemAutoDetectsFocusProxy);
+
+ QGraphicsTextItem *text2 = new QGraphicsTextItem;
+ text2->setTextInteractionFlags(Qt::TextEditorInteraction);
+ text2->setFocus();
+ QVERIFY(!text2->hasFocus());
+ QCOMPARE(text->focusProxy(), (QGraphicsItem *)0);
+ text2->setParentItem(text);
+ QCOMPARE(text->focusProxy(), (QGraphicsItem *)text2);
+ QCOMPARE(text->focusItem(), (QGraphicsItem *)text2);
+
+ QGraphicsScene scene;
+ scene.addItem(text);
+ QVERIFY(text2->hasFocus());
+}
+
QTEST_MAIN(tst_QGraphicsItem)
#include "tst_qgraphicsitem.moc"