reimplement QWindow::focusObject() on QQuickCanvas
authorPekka Vuorela <pekka.ta.vuorela@nokia.com>
Fri, 20 Jan 2012 13:38:51 +0000 (15:38 +0200)
committerQt by Nokia <qt-info@nokia.com>
Wed, 25 Jan 2012 20:40:44 +0000 (21:40 +0100)
Change-Id: I935a303d0baa9500ab47f749eb3b3f9a11a70946
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
Reviewed-by: Joona Petrell <joona.t.petrell@nokia.com>
src/quick/items/qquickcanvas.cpp
src/quick/items/qquickcanvas.h
tests/auto/qtquick2/qquickcanvas/data/focus.qml [new file with mode: 0644]
tests/auto/qtquick2/qquickcanvas/tst_qquickcanvas.cpp

index fa7931a..626c3a1 100644 (file)
@@ -671,6 +671,16 @@ QQuickItem *QQuickCanvas::activeFocusItem() const
     return d->activeFocusItem;
 }
 
+QObject *QQuickCanvas::focusObject() const
+{
+    Q_D(const QQuickCanvas);
+
+    if (d->activeFocusItem)
+        return d->activeFocusItem;
+    return const_cast<QQuickCanvas*>(this);
+}
+
+
 /*!
   Returns the item which currently has the mouse grab.
 */
index 46d2702..5e6bb3a 100644 (file)
@@ -83,6 +83,7 @@ public:
 
     QQuickItem *rootItem() const;
     QQuickItem *activeFocusItem() const;
+    QObject *focusObject() const;
 
     QQuickItem *mouseGrabberItem() const;
 
diff --git a/tests/auto/qtquick2/qquickcanvas/data/focus.qml b/tests/auto/qtquick2/qquickcanvas/data/focus.qml
new file mode 100644 (file)
index 0000000..901f2fc
--- /dev/null
@@ -0,0 +1,11 @@
+import QtQuick 2.0
+import QtQuick.Window 2.0 as Window
+
+Window.Window {
+     Item {
+          objectName: "item1"
+     }
+     Item {
+          objectName: "item2"
+     }
+}
index 78bf989..307968d 100644 (file)
@@ -208,6 +208,8 @@ private slots:
 
     void animationsWhileHidden();
 
+    void focusObject();
+
 private:
     QTouchDevice *touchDevice;
 };
@@ -668,6 +670,30 @@ void tst_qquickcanvas::headless()
 
 }
 
+void tst_qquickcanvas::focusObject()
+{
+    QDeclarativeEngine engine;
+    QDeclarativeComponent component(&engine);
+    component.loadUrl(testFileUrl("focus.qml"));
+    QObject *created = component.create();
+    QVERIFY(created);
+
+    QQuickCanvas *canvas = qobject_cast<QQuickCanvas*>(created);
+    QVERIFY(canvas);
+
+    QQuickItem *item1 = canvas->findChild<QQuickItem*>("item1");
+    QVERIFY(item1);
+    item1->setFocus(true);
+    QCOMPARE(item1, canvas->focusObject());
+
+    QQuickItem *item2 = canvas->findChild<QQuickItem*>("item2");
+    QVERIFY(item2);
+    item2->setFocus(true);
+    QCOMPARE(item2, canvas->focusObject());
+
+    delete canvas;
+}
+
 QTEST_MAIN(tst_qquickcanvas)
 
 #include "tst_qquickcanvas.moc"