From 533b45357246725efacf90b330a718d34a0cf3eb Mon Sep 17 00:00:00 2001
From: Caroline Chao <caroline.chao@digia.com>
Date: Mon, 6 May 2013 16:16:13 +0200
Subject: [PATCH] TableView: Fix activated() not emitted

The mousearea in TableView has to accept mouse event in onPressed to
receive upcoming double click event.

Regression introduced by Iff3e5c1.

Instead of not accepting the event in onPressed, set the MouseArea
property propagateComposedEvents to true and does not accept the onClicked
event.

Also uses QStyle::SH_ItemView_ActivateItemOnSingleClick to check if the
table is activated on single or double-clicking. By default, it is activated
on double-clicking.

Change-Id: I744174d7b31efebfae5e8db4db7b99a544ecf50f
Reviewed-by: J-P Nurmi <jpnurmi@digia.com>
---
 src/controls/TableView.qml                    | 23 ++++++--
 src/private/qquickstyleitem.cpp               |  2 +
 src/styles/Desktop/TableViewStyle.qml         |  1 +
 src/styles/TableViewStyle.qml                 |  1 +
 .../data/tableview/table_activated.qml        | 54 +++++++++++++++++++
 .../data/tableview/table_delegate.qml         |  3 ++
 tests/auto/controls/data/tst_tableview.qml    | 30 +++++++++++
 7 files changed, 110 insertions(+), 4 deletions(-)
 create mode 100644 tests/auto/controls/data/tableview/table_activated.qml

diff --git a/src/controls/TableView.qml b/src/controls/TableView.qml
index 9ebf64e9f..430c862d4 100644
--- a/src/controls/TableView.qml
+++ b/src/controls/TableView.qml
@@ -41,6 +41,7 @@
 import QtQuick 2.1
 import QtQuick.Controls 1.0
 import QtQuick.Controls.Private 1.0
+import QtQuick.Controls.Styles 1.0
 
 /*!
    \qmltype TableView
@@ -221,7 +222,8 @@ ScrollView {
     property alias currentRow: listView.currentIndex
 
     /*! \qmlsignal TableView::activated()
-        Emitted when a new row is selected by the user. */
+        Emitted when the user activates an item by single or double-clicking (depending on the platform).
+    */
     signal activated
 
 
@@ -237,6 +239,9 @@ ScrollView {
     __scrollBarTopMargin: Qt.platform.os === "mac" ? headerrow.height : 0
     __viewTopMargin: headerrow.height
 
+    /*! \internal */
+    property bool __activateItemOnSingleClick: __style ? __style.activateItemOnSingleClick : false
+
     /*! \internal */
     function __decrementCurrentIndex() {
         __scroller.blockUpdates = true;
@@ -277,6 +282,7 @@ ScrollView {
             id: mousearea
 
             anchors.fill: listView
+            propagateComposedEvents: true
 
             property bool autoincrement: false
             property bool autodecrement: false
@@ -309,19 +315,28 @@ ScrollView {
                     listView.currentIndex = listView.indexAt(0, y);
             }
 
-            onPressed:  {
+            onClicked: {
+                if (root.__activateItemOnSingleClick)
+                    root.activated()
+                mouse.accepted = false
+            }
+
+            onPressed: {
                 listView.forceActiveFocus()
                 var x = Math.min(flickableItem.contentWidth - 5, Math.max(mouseX + flickableItem.contentX, 0))
                 var y = Math.min(flickableItem.contentHeight - 5, Math.max(mouseY + flickableItem.contentY, 0))
                 listView.currentIndex = listView.indexAt(x, y)
-                mouse.accepted = false
             }
 
-            onDoubleClicked: { root.activated() }
+            onDoubleClicked: {
+                if (!root.__activateItemOnSingleClick)
+                    root.activated()
+            }
 
             // Note:  with boolean preventStealing we are keeping the flickable from
             // eating our mouse press events
             preventStealing: true
+
         }
 
         // Fills extra rows with alternate color
diff --git a/src/private/qquickstyleitem.cpp b/src/private/qquickstyleitem.cpp
index 59c9eaac1..bde25409e 100644
--- a/src/private/qquickstyleitem.cpp
+++ b/src/private/qquickstyleitem.cpp
@@ -946,6 +946,8 @@ QVariant QQuickStyleItem::styleHint(const QString &metric)
         return qApp->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents);
     } else if (metric == "scrollToClickPosition")
         return qApp->style()->styleHint(QStyle::SH_ScrollBar_LeftClickAbsolutePosition);
+    else if (metric == "activateItemOnSingleClick")
+        return qApp->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick);
     else if (metric == "transientScrollBars")
         return qApp->style()->styleHint(QStyle::SH_ScrollBar_Transient, m_styleoption);
     return 0;
diff --git a/src/styles/Desktop/TableViewStyle.qml b/src/styles/Desktop/TableViewStyle.qml
index 266992703..716afa618 100644
--- a/src/styles/Desktop/TableViewStyle.qml
+++ b/src/styles/Desktop/TableViewStyle.qml
@@ -44,6 +44,7 @@ import QtQuick.Controls.Private 1.0
 ScrollViewStyle {
     id: root
 
+    property bool activateItemOnSingleClick: __styleitem.styleHint("activateItemOnSingleClick")
     property color textColor: __styleitem.styleHint("textColor")
     property color highlightedTextColor: __styleitem.styleHint("highlightedTextColor")
 
diff --git a/src/styles/TableViewStyle.qml b/src/styles/TableViewStyle.qml
index 88fc3743f..86678f978 100644
--- a/src/styles/TableViewStyle.qml
+++ b/src/styles/TableViewStyle.qml
@@ -44,6 +44,7 @@ import QtQuick.Controls.Private 1.0
 ScrollViewStyle {
     id: root
 
+    property bool activateItemOnSingleClick: false
     property color textColor: __syspal.text
     property color highlightedTextColor: "white"
 
diff --git a/tests/auto/controls/data/tableview/table_activated.qml b/tests/auto/controls/data/tableview/table_activated.qml
new file mode 100644
index 000000000..52d420c49
--- /dev/null
+++ b/tests/auto/controls/data/tableview/table_activated.qml
@@ -0,0 +1,54 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Tasuku Suzuki <stasuku@gmail.com>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Quick Controls module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+**   * Redistributions of source code must retain the above copyright
+**     notice, this list of conditions and the following disclaimer.
+**   * Redistributions in binary form must reproduce the above copyright
+**     notice, this list of conditions and the following disclaimer in
+**     the documentation and/or other materials provided with the
+**     distribution.
+**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+**     of its contributors may be used to endorse or promote products derived
+**     from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.1
+import QtQuick.Controls 1.0
+
+TableView {
+    height: 70
+    model: 10
+
+    property bool test: false
+    onActivated: test = true
+
+    TableViewColumn {
+        width: 100
+    }
+}
diff --git a/tests/auto/controls/data/tableview/table_delegate.qml b/tests/auto/controls/data/tableview/table_delegate.qml
index 38f4c1ce4..a3ab621d9 100644
--- a/tests/auto/controls/data/tableview/table_delegate.qml
+++ b/tests/auto/controls/data/tableview/table_delegate.qml
@@ -46,6 +46,9 @@ TableView {
     model: [{"text": "text1"}, {"text": "text2"}, {"text": "text3"}]
     property var test: 0
 
+    property bool activatedTest: false
+    onActivated: activatedTest = true
+
     TableViewColumn {
         title: "Text"
         role: "text"
diff --git a/tests/auto/controls/data/tst_tableview.qml b/tests/auto/controls/data/tst_tableview.qml
index 65d7c72cd..bc38ad46f 100644
--- a/tests/auto/controls/data/tst_tableview.qml
+++ b/tests/auto/controls/data/tst_tableview.qml
@@ -180,6 +180,36 @@ TestCase {
         table.destroy()
     }
 
+    function test_activated() {
+        var component = Qt.createComponent("tableview/table_activated.qml")
+        compare(component.status, Component.Ready)
+        var table = component.createObject(container);
+        verify(table !== null, "table created is null")
+        table.forceActiveFocus();
+        compare(table.test, false)
+        if (!table.__activateItemOnSingleClick)
+            mouseDoubleClick(table, 15 , 15, Qt.LeftButton)
+        else
+            mouseClick(table, 15, 15, Qt.LeftButton)
+        compare(table.test, true)
+        table.destroy()
+    }
+
+    function test_activated_withItemDelegate() {
+        var component = Qt.createComponent("tableview/table_delegate.qml")
+        compare(component.status, Component.Ready)
+        var table = component.createObject(container);
+        verify(table !== null, "table created is null")
+        table.forceActiveFocus();
+        compare(table.activatedTest, false)
+        if (!table.__activateItemOnSingleClick)
+            mouseDoubleClick(table, 15 , 50, Qt.LeftButton)
+        else
+            mouseClick(table, 15, 50, Qt.LeftButton)
+        compare(table.activatedTest, true)
+        table.destroy()
+    }
+
     function test_columnCount() {
         var component = Qt.createComponent("tableview/table_multicolumns.qml")
         compare(component.status, Component.Ready)
-- 
GitLab