From 33850881a074f93622869cc4dc6bc77fbbc1fa0c Mon Sep 17 00:00:00 2001 From: Jens Bache-Wiig <jens.bache-wiig@digia.com> Date: Fri, 31 May 2013 13:10:02 +0200 Subject: [PATCH] TableView: Expand to single column and prevent multiple use This fix ensures that we can only add a TableViewColumn exactly once. It also automatically expands the column with to the viewport when only one column is in use. In addition I have disabled dragging when columnCount == 1 as it was pointless. Change-Id: Ief6011c3e58166907836bf55b0fa6643698192d2 Reviewed-by: Caroline Chao <caroline.chao@digia.com> --- src/controls/TableView.qml | 14 ++++++++++---- src/controls/TableViewColumn.qml | 10 ++++++++-- tests/auto/controls/data/tst_tableview.qml | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/controls/TableView.qml b/src/controls/TableView.qml index d0417bfa9..88a897e10 100644 --- a/src/controls/TableView.qml +++ b/src/controls/TableView.qml @@ -303,7 +303,12 @@ ScrollView { if (typeof column['createObject'] === 'function') object = column.createObject(root) + else if (object.__view) { + console.warn("TableView::insertColumn(): you cannot add a column to multiple views") + return null + } if (index >= 0 && index <= columnCount && object.Accessible.role === Accessible.ColumnHeader) { + object.__view = root columnModel.insert(index, {columnItem: object}) return object } @@ -616,7 +621,7 @@ ScrollView { delegate: Item { z:-index - width: modelData.width + width: columnCount == 1 ? viewport.width + __verticalScrollBar.width : modelData.width visible: modelData.visible height: headerVisible ? headerStyle.height : 0 @@ -655,7 +660,7 @@ ScrollView { // NOTE: the direction is different from the master branch // so this indicates that I am using an invalid assumption on item ordering onPositionChanged: { - if (pressed) { // only do this while dragging + if (pressed && columnCount > 1) { // only do this while dragging for (var h = columnCount-1 ; h >= 0 ; --h) { if (drag.target.x > headerrow.children[h].x) { repeater.targetIndex = h @@ -680,7 +685,7 @@ ScrollView { } drag.maximumX: 1000 drag.minimumX: -1000 - drag.target: draghandle + drag.target: columnCount > 1 ? draghandle : null } Loader { @@ -708,6 +713,7 @@ ScrollView { anchors.rightMargin: -width/2 width: 16 ; height: parent.height anchors.right: parent.right + enabled: columnCount > 1 onPositionChanged: { var newHeaderWidth = modelData.width + (mouseX - offset) modelData.width = Math.max(minimumSize, newHeaderWidth) @@ -728,7 +734,7 @@ ScrollView { modelData.width = minWidth } onPressedChanged: if (pressed) offset=mouseX - cursorShape: Qt.SplitHCursor + cursorShape: enabled ? Qt.SplitHCursor : Qt.ArrowCursor } } } diff --git a/src/controls/TableViewColumn.qml b/src/controls/TableViewColumn.qml index 2c629dfbd..f1abc5532 100644 --- a/src/controls/TableViewColumn.qml +++ b/src/controls/TableViewColumn.qml @@ -49,6 +49,10 @@ import QtQuick 2.1 */ QtObject { + + /*! \internal */ + property Item __view: null + /*! The title text of the column. */ property string title @@ -56,8 +60,10 @@ QtObject { property string role /*! The current width of the column - The default value depends on platform. */ - property int width: 160 + The default value depends on platform. If only one + column is defined, the width expands to the viewport. + */ + property int width: (__view && __view.columnCount === 1) ? __view.viewport.width : 160 /*! The visible status of the column. */ property bool visible: true diff --git a/tests/auto/controls/data/tst_tableview.qml b/tests/auto/controls/data/tst_tableview.qml index d092ae9d5..8763900ef 100644 --- a/tests/auto/controls/data/tst_tableview.qml +++ b/tests/auto/controls/data/tst_tableview.qml @@ -240,6 +240,23 @@ TestCase { table.destroy() } + function test_columnWidth() { + var tableView = Qt.createQmlObject('import QtQuick 2.1; import QtQuick.Controls 1.0; TableView { }', testCase, ''); + compare(tableView.columnCount, 0) + var column = newColumn.createObject(testCase, {title: "title 1"}); + verify(column.__view === null) + compare(column.width, 160) + compare(column.title, "title 1") + tableView.addColumn(column) + compare(column.__view, tableView) + compare(column.width, tableView.viewport.width) + var tableView2 = Qt.createQmlObject('import QtQuick 2.1; import QtQuick.Controls 1.0; TableView { }', testCase, ''); + tableView2.addColumn(column) // should not work + compare(column.__view, tableView) //same as before + tableView2.destroy() + tableView.destroy() + } + function test_dynamicColumns() { var component = Qt.createComponent("tableview/table_dynamiccolumns.qml") compare(component.status, Component.Ready) -- GitLab