From e5068aa21e803a7ddb8eec9fadfc00dcd30728ab Mon Sep 17 00:00:00 2001
From: Liang Qi <liang.qi@digia.com>
Date: Tue, 21 May 2013 12:45:58 +0200
Subject: [PATCH] Fix a wrong behavior when mouse click tab in TabView

When nextItemInFocusChain is out of the TabView, the tabitem in TabBar
will get focused.

Autotest is included.

Change-Id: If5701afa1aeed16383b8d96483342a4629293997
Reviewed-by: J-P Nurmi <jpnurmi@digia.com>
---
 src/private/TabBar.qml                   | 20 ++++++++-
 tests/auto/controls/data/tst_tabview.qml | 55 ++++++++++++++++++++++--
 2 files changed, 71 insertions(+), 4 deletions(-)

diff --git a/src/private/TabBar.qml b/src/private/TabBar.qml
index ebaef02a3..826a38116 100644
--- a/src/private/TabBar.qml
+++ b/src/private/TabBar.qml
@@ -86,6 +86,20 @@ FocusScope {
         return null;
     }
 
+    /*! \internal */
+    function __isAncestorOf(item, child) {
+        //TODO: maybe removed from 5.2 if the function was merged in qtdeclarative
+        if (child === item)
+            return false;
+
+        while (child) {
+            child = child.parent;
+            if (child === item)
+                return true;
+        }
+        return false;
+    }
+
     ListView {
         id: tabrow
         objectName: "tabrow"
@@ -152,7 +166,11 @@ FocusScope {
 
             onPressed: {
                 tabView.currentIndex = index;
-                tabbar.nextItemInFocusChain(true).forceActiveFocus();
+                var next = tabbar.nextItemInFocusChain(true);
+                if (__isAncestorOf(tabView.tabAt(currentIndex), next))
+                    next.forceActiveFocus();
+                else
+                    tabitem.forceActiveFocus();
             }
 
             Loader {
diff --git a/tests/auto/controls/data/tst_tabview.qml b/tests/auto/controls/data/tst_tabview.qml
index 64e2fc445..992baf221 100644
--- a/tests/auto/controls/data/tst_tabview.qml
+++ b/tests/auto/controls/data/tst_tabview.qml
@@ -226,10 +226,15 @@ TestCase {
     function test_mousePressOnTabBar() {
         var test_tabView = 'import QtQuick 2.1;             \
         import QtQuick.Controls 1.0;                        \
+        Column {                                            \
+            property alias tabview: _tabview;               \
+            property alias textfield: _textfield;           \
         TabView {                                           \
+            id: _tabview;                                   \
             width: 200; height: 100;                        \
             property alias tab1: _tab1;                     \
             property alias tab2: _tab2;                     \
+            property alias tab3: _tab3;                     \
             Tab {                                           \
                 id: _tab1;                                  \
                 title: "Tab1";                              \
@@ -268,18 +273,53 @@ TestCase {
                     }                                       \
                 }                                           \
             }                                               \
+            Tab {                                           \
+                id: _tab3;                                  \
+                title: "Tab3";                              \
+                active: true;                               \
+                Column {                                    \
+                    objectName: "column3";                  \
+                    property alias child5: _child5;         \
+                    property alias child6: _child6;         \
+                    anchors.fill: parent;                   \
+                    Button {                                \
+                        id: _child5;                        \
+                        activeFocusOnTab: false;            \
+                        text: "button 1 in Tab3";           \
+                    }                                       \
+                    Button {                                \
+                        id: _child6;                        \
+                        activeFocusOnTab: false;            \
+                        text: "button 2 in Tab3";           \
+                    }                                       \
+                }                                           \
+            }                                               \
+        }                                                   \
+        TextField {                                         \
+            id: _textfield;                                 \
+            text: "textfile outside of tabview";            \
+        }                                                   \
         }                                                   '
 
-        var tabView = Qt.createQmlObject(test_tabView, container, '')
-        compare(tabView.count, 2)
+        var item = Qt.createQmlObject(test_tabView, container, '')
+
+        var textField = item.textfield
+        verify(textField !== null)
+
+        var tabView = item.tabview
+        verify(tabView !== null)
+        compare(tabView.count, 3)
         verify(tabView.tab1.status === Loader.Ready)
         verify(tabView.tab2.status === Loader.Ready)
+        verify(tabView.tab3.status === Loader.Ready)
         waitForRendering(tabView)
 
         var column1 = getColumnItem(tabView.tab1, "column1")
         verify(column1 !== null)
         var column2 = getColumnItem(tabView.tab2, "column2")
         verify(column2 !== null)
+        var column3 = getColumnItem(tabView.tab3, "column3")
+        verify(column3 !== null)
 
         var child1 = column1.child1
         verify(child1 !== null)
@@ -293,7 +333,7 @@ TestCase {
         verify(tabrowItem !== null)
 
         var mouseareas = populateMouseAreaItems(tabrowItem)
-        verify(mouseareas.length, 2)
+        verify(mouseareas.length, 3)
 
         var tab1 = mouseareas[0]
         verify(tab1 !== null)
@@ -319,6 +359,15 @@ TestCase {
         mouseClick(tab2, tab2.width/2, tab2.height/2)
         verify(child3.activeFocus)
 
+        var tab3 = mouseareas[2]
+        verify(tab3 !== null)
+        //printGeometry(tab3)
+
+        waitForRendering(tab3)
+        mouseClick(tab3, tab3.width/2, tab3.height/2)
+        verify(tab3.activeFocus)
+        verify(!textField.activeFocus)
+
         tabView.destroy()
     }
 
-- 
GitLab