From eb8c7d19c98d547f89caa1b5866fd2b5e43b32bf Mon Sep 17 00:00:00 2001
From: Michael Brasser <michael.brasser@nokia.com>
Date: Thu, 5 Aug 2010 14:36:57 +1000
Subject: Make sure onFocusChanged is correctly emitted for items in a
 FocusScope.

Task-number: QTBUG-12649
Reviewed-by: Martin Jones
---
 src/declarative/graphicsitems/qdeclarativeitem.cpp | 13 +----
 src/gui/graphicsview/qgraphicsitem.cpp             |  5 ++
 .../tst_qdeclarativefocusscope.cpp                 | 56 ++++++++++++++++++++++
 3 files changed, 62 insertions(+), 12 deletions(-)

diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp
index 50998eb..5b74129 100644
--- a/src/declarative/graphicsitems/qdeclarativeitem.cpp
+++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp
@@ -2406,18 +2406,7 @@ void QDeclarativeItemPrivate::focusChanged(bool flag)
     Q_Q(QDeclarativeItem);
     if (!(flags & QGraphicsItem::ItemIsFocusScope) && parent)
         emit q->activeFocusChanged(flag);   //see also QDeclarativeItemPrivate::subFocusItemChange()
-
-    bool inScope = false;
-    QGraphicsItem *p = parent;
-    while (p) {
-        if (p->flags() & QGraphicsItem::ItemIsFocusScope) {
-            inScope = true;
-            break;
-        }
-        p = p->parentItem();
-    }
-    if (!inScope)
-        emit q->focusChanged(flag);
+    emit q->focusChanged(flag);
 }
 
 /*! \internal */
diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp
index 1626d83..ff3dc1f 100644
--- a/src/gui/graphicsview/qgraphicsitem.cpp
+++ b/src/gui/graphicsview/qgraphicsitem.cpp
@@ -3259,8 +3259,12 @@ void QGraphicsItemPrivate::setFocusHelper(Qt::FocusReason focusReason, bool clim
     QGraphicsItem *p = parent;
     while (p) {
         if (p->flags() & QGraphicsItem::ItemIsFocusScope) {
+            QGraphicsItem *oldFocusScopeItem = p->d_ptr->focusScopeItem;
             p->d_ptr->focusScopeItem = q_ptr;
             if (!p->focusItem() && !focusFromShow) {
+                if (oldFocusScopeItem)
+                    oldFocusScopeItem->d_ptr->focusScopeItemChange(false);
+                focusScopeItemChange(true);
                 // If you call setFocus on a child of a focus scope that
                 // doesn't currently have a focus item, then stop.
                 return;
@@ -5595,6 +5599,7 @@ void QGraphicsItemPrivate::subFocusItemChange()
 */
 void QGraphicsItemPrivate::focusScopeItemChange(bool isSubFocusItem)
 {
+    Q_UNUSED(isSubFocusItem);
 }
 
 /*!
diff --git a/tests/auto/declarative/qdeclarativefocusscope/tst_qdeclarativefocusscope.cpp b/tests/auto/declarative/qdeclarativefocusscope/tst_qdeclarativefocusscope.cpp
index b0c9c03..b138f61 100644
--- a/tests/auto/declarative/qdeclarativefocusscope/tst_qdeclarativefocusscope.cpp
+++ b/tests/auto/declarative/qdeclarativefocusscope/tst_qdeclarativefocusscope.cpp
@@ -69,6 +69,7 @@ private slots:
     void textEdit();
     void forceFocus();
     void noParentFocus();
+    void signalEmission();
 };
 
 /*
@@ -344,6 +345,61 @@ void tst_qdeclarativefocusscope::noParentFocus()
     delete view;
 }
 
+void tst_qdeclarativefocusscope::signalEmission()
+{
+    QDeclarativeView *view = new QDeclarativeView;
+    view->setSource(QUrl::fromLocalFile(SRCDIR "/data/signalEmission.qml"));
+
+    QDeclarativeRectangle *item1 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item1"));
+    QDeclarativeRectangle *item2 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item2"));
+    QDeclarativeRectangle *item3 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item3"));
+    QDeclarativeRectangle *item4 = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("item4"));
+    QVERIFY(item1 != 0);
+    QVERIFY(item2 != 0);
+    QVERIFY(item3 != 0);
+    QVERIFY(item4 != 0);
+
+    view->show();
+    qApp->setActiveWindow(view);
+    qApp->processEvents();
+
+#ifdef Q_WS_X11
+    // to be safe and avoid failing setFocus with window managers
+    qt_x11_wait_for_window_manager(view);
+#endif
+
+    QVariant blue(QColor("blue"));
+    QVariant red(QColor("red"));
+
+    QVERIFY(view->hasFocus());
+    QVERIFY(view->scene()->hasFocus());
+    item1->setFocus(true);
+    QCOMPARE(item1->property("color"), red);
+    QCOMPARE(item2->property("color"), blue);
+    QCOMPARE(item3->property("color"), blue);
+    QCOMPARE(item4->property("color"), blue);
+
+    item2->setFocus(true);
+    QCOMPARE(item1->property("color"), blue);
+    QCOMPARE(item2->property("color"), red);
+    QCOMPARE(item3->property("color"), blue);
+    QCOMPARE(item4->property("color"), blue);
+
+    item3->setFocus(true);
+    QCOMPARE(item1->property("color"), blue);
+    QCOMPARE(item2->property("color"), red);
+    QCOMPARE(item3->property("color"), red);
+    QCOMPARE(item4->property("color"), blue);
+
+    item4->setFocus(true);
+    QCOMPARE(item1->property("color"), blue);
+    QCOMPARE(item2->property("color"), red);
+    QCOMPARE(item3->property("color"), blue);
+    QCOMPARE(item4->property("color"), red);
+
+    delete view;
+}
+
 QTEST_MAIN(tst_qdeclarativefocusscope)
 
 #include "tst_qdeclarativefocusscope.moc"
-- 
cgit v0.12