From fe3b3c707e2bc41f2e98d315ba72f50d4929e064 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Tue, 3 Mar 2020 10:48:27 +0100 Subject: HeaderView: Add support for list based models Change-Id: I411136bd2b9a277d84a7c68c55bb1c317b6cc9d2 Reviewed-by: Mitch Curtis --- src/imports/controls/HorizontalHeaderView.qml | 4 +- src/imports/controls/VerticalHeaderView.qml | 4 +- .../controls/fusion/HorizontalHeaderView.qml | 4 +- src/imports/controls/fusion/VerticalHeaderView.qml | 4 +- .../controls/imagine/HorizontalHeaderView.qml | 4 +- .../controls/imagine/VerticalHeaderView.qml | 4 +- .../controls/material/HorizontalHeaderView.qml | 4 +- .../controls/material/VerticalHeaderView.qml | 4 +- .../controls/universal/HorizontalHeaderView.qml | 4 +- .../controls/universal/VerticalHeaderView.qml | 4 +- src/quicktemplates2/qquickheaderview.cpp | 24 ++++- src/quicktemplates2/qquickheaderview_p_p.h | 2 +- tests/auto/qquickheaderview/data/ListModel.qml | 107 +++++++++++++++++++++ .../auto/qquickheaderview/tst_qquickheaderview.cpp | 34 +++++++ 14 files changed, 194 insertions(+), 13 deletions(-) create mode 100644 tests/auto/qquickheaderview/data/ListModel.qml diff --git a/src/imports/controls/HorizontalHeaderView.qml b/src/imports/controls/HorizontalHeaderView.qml index 7f0454da..ec91af25 100644 --- a/src/imports/controls/HorizontalHeaderView.qml +++ b/src/imports/controls/HorizontalHeaderView.qml @@ -55,7 +55,9 @@ T.HorizontalHeaderView { Text { id: text - text: model[control.textRole] + text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] + : model[control.textRole]) + : modelData width: parent.width height: parent.height horizontalAlignment: Text.AlignHCenter diff --git a/src/imports/controls/VerticalHeaderView.qml b/src/imports/controls/VerticalHeaderView.qml index 5fbfaedf..3fc9ca5a 100644 --- a/src/imports/controls/VerticalHeaderView.qml +++ b/src/imports/controls/VerticalHeaderView.qml @@ -55,7 +55,9 @@ T.VerticalHeaderView { Text { id: text - text: model[control.textRole] + text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] + : model[control.textRole]) + : modelData width: parent.width height: parent.height horizontalAlignment: Text.AlignHCenter diff --git a/src/imports/controls/fusion/HorizontalHeaderView.qml b/src/imports/controls/fusion/HorizontalHeaderView.qml index 3d0f247b..bbd9dc3f 100644 --- a/src/imports/controls/fusion/HorizontalHeaderView.qml +++ b/src/imports/controls/fusion/HorizontalHeaderView.qml @@ -65,7 +65,9 @@ T.HorizontalHeaderView { Text { id: text - text: model[control.textRole] + text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] + : model[control.textRole]) + : modelData width: parent.width height: parent.height horizontalAlignment: Text.AlignHCenter diff --git a/src/imports/controls/fusion/VerticalHeaderView.qml b/src/imports/controls/fusion/VerticalHeaderView.qml index 5ad0db8f..b220cdf2 100644 --- a/src/imports/controls/fusion/VerticalHeaderView.qml +++ b/src/imports/controls/fusion/VerticalHeaderView.qml @@ -65,7 +65,9 @@ T.VerticalHeaderView { Text { id: text - text: model[control.textRole] + text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] + : model[control.textRole]) + : modelData width: parent.width height: parent.height horizontalAlignment: Text.AlignHCenter diff --git a/src/imports/controls/imagine/HorizontalHeaderView.qml b/src/imports/controls/imagine/HorizontalHeaderView.qml index 7f0454da..ec91af25 100644 --- a/src/imports/controls/imagine/HorizontalHeaderView.qml +++ b/src/imports/controls/imagine/HorizontalHeaderView.qml @@ -55,7 +55,9 @@ T.HorizontalHeaderView { Text { id: text - text: model[control.textRole] + text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] + : model[control.textRole]) + : modelData width: parent.width height: parent.height horizontalAlignment: Text.AlignHCenter diff --git a/src/imports/controls/imagine/VerticalHeaderView.qml b/src/imports/controls/imagine/VerticalHeaderView.qml index 5fbfaedf..3fc9ca5a 100644 --- a/src/imports/controls/imagine/VerticalHeaderView.qml +++ b/src/imports/controls/imagine/VerticalHeaderView.qml @@ -55,7 +55,9 @@ T.VerticalHeaderView { Text { id: text - text: model[control.textRole] + text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] + : model[control.textRole]) + : modelData width: parent.width height: parent.height horizontalAlignment: Text.AlignHCenter diff --git a/src/imports/controls/material/HorizontalHeaderView.qml b/src/imports/controls/material/HorizontalHeaderView.qml index 504579cc..fd672f34 100644 --- a/src/imports/controls/material/HorizontalHeaderView.qml +++ b/src/imports/controls/material/HorizontalHeaderView.qml @@ -56,7 +56,9 @@ T.HorizontalHeaderView { Text { id: text - text: model[control.textRole] + text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] + : model[control.textRole]) + : modelData width: parent.width height: parent.height horizontalAlignment: Text.AlignHCenter diff --git a/src/imports/controls/material/VerticalHeaderView.qml b/src/imports/controls/material/VerticalHeaderView.qml index 7f8ecc92..5fc5aebe 100644 --- a/src/imports/controls/material/VerticalHeaderView.qml +++ b/src/imports/controls/material/VerticalHeaderView.qml @@ -56,7 +56,9 @@ T.VerticalHeaderView { Text { id: text - text: model[control.textRole] + text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] + : model[control.textRole]) + : modelData width: parent.width height: parent.height horizontalAlignment: Text.AlignHCenter diff --git a/src/imports/controls/universal/HorizontalHeaderView.qml b/src/imports/controls/universal/HorizontalHeaderView.qml index 8cdfda35..47daa8ce 100644 --- a/src/imports/controls/universal/HorizontalHeaderView.qml +++ b/src/imports/controls/universal/HorizontalHeaderView.qml @@ -57,7 +57,9 @@ T.HorizontalHeaderView { Text { id: text - text: model[control.textRole] + text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] + : model[control.textRole]) + : modelData width: parent.width height: parent.height horizontalAlignment: Text.AlignHCenter diff --git a/src/imports/controls/universal/VerticalHeaderView.qml b/src/imports/controls/universal/VerticalHeaderView.qml index 7c7544af..04408d68 100644 --- a/src/imports/controls/universal/VerticalHeaderView.qml +++ b/src/imports/controls/universal/VerticalHeaderView.qml @@ -57,7 +57,9 @@ T.VerticalHeaderView { Text { id: text - text: model[control.textRole] + text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] + : model[control.textRole]) + : modelData width: parent.width height: parent.height horizontalAlignment: Text.AlignHCenter diff --git a/src/quicktemplates2/qquickheaderview.cpp b/src/quicktemplates2/qquickheaderview.cpp index 49463944..5c465cc7 100644 --- a/src/quicktemplates2/qquickheaderview.cpp +++ b/src/quicktemplates2/qquickheaderview.cpp @@ -151,7 +151,11 @@ This property holds the model role used to display text in each header cell. - The default value is the \c "display" role. + When the model has multiple roles, textRole can be set to determine which + role should be displayed. + + If model is a QAbstractItemModel then it will default to "display"; otherwise + it is empty. \sa QAbstractItemModel::roleNames() */ @@ -161,7 +165,11 @@ This property holds the model role used to display text in each header cell. - The default value is the \c "display" role. + When the model has multiple roles, textRole can be set to determine which + role should be displayed. + + If model is a QAbstractItemModel then it will default to "display"; otherwise + it is empty. \sa QAbstractItemModel::roleNames() */ @@ -241,6 +249,18 @@ void QQuickHeaderViewBasePrivate::syncModel() } QQuickTableViewPrivate::syncModel(); + + isTransposed = false; + const auto aim = model->abstractItemModel(); + if (orientation() == Qt::Horizontal) { + // For models that are just a list or a number, and especially not a + // table, we transpose the view when the orientation is horizontal. + // The model (list) will then be laid out horizontally rather than + // vertically, which is the otherwise the default. + isTransposed = !aim || aim->columnCount() == 1; + } + if (m_textRole.isEmpty() && aim) + m_textRole = QLatin1String("display"); } void QQuickHeaderViewBasePrivate::syncSyncView() diff --git a/src/quicktemplates2/qquickheaderview_p_p.h b/src/quicktemplates2/qquickheaderview_p_p.h index 961c554b..655c2a58 100644 --- a/src/quicktemplates2/qquickheaderview_p_p.h +++ b/src/quicktemplates2/qquickheaderview_p_p.h @@ -112,7 +112,7 @@ protected: }; QStack m_hiddenSectionSizes; bool m_modelExplicitlySetByUser = false; - QString m_textRole = QStringLiteral("display"); + QString m_textRole; }; class QQuickHorizontalHeaderViewPrivate : public QQuickHeaderViewBasePrivate diff --git a/tests/auto/qquickheaderview/data/ListModel.qml b/tests/auto/qquickheaderview/data/ListModel.qml new file mode 100644 index 00000000..59021458 --- /dev/null +++ b/tests/auto/qquickheaderview/data/ListModel.qml @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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 The Qt Company Ltd 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.15 +import QtQuick.Window 2.15 +import QtQuick.Controls 2.15 +import TestTableModel 0.1 + +Window { + objectName: "window" + width: 400 + height: 400 + visible: true + + Component { + id: cellDelegate + Rectangle { + implicitHeight: 25 + implicitWidth: 50 + color: "red" + Text { + text: row + "," + column + } + } + } + + HorizontalHeaderView { + id: hhv + objectName: "horizontalHeader" + model: ["AAA", "BBB", "CCC", "DDD", "EEE"] + syncView: tv + anchors.top: parent.top + x: vhv.width + } + + VerticalHeaderView { + id: vhv + objectName: "verticalHeader" + model: ["111", "222", "333", "444", "555"] + syncView: tv + anchors.left: parent.left + y: hhv.height + } + + TableView { + id: tv + objectName: "tableView" + model: TestTableModel { + id: tm + objectName: "tableModel" + rowCount: 5 + columnCount: 5 + } + delegate: cellDelegate + anchors.top: hhv.bottom + anchors.left: vhv.right + anchors.right: parent.right + anchors.bottom: parent.bottom + } +} diff --git a/tests/auto/qquickheaderview/tst_qquickheaderview.cpp b/tests/auto/qquickheaderview/tst_qquickheaderview.cpp index 5ce5439d..64f769e8 100644 --- a/tests/auto/qquickheaderview/tst_qquickheaderview.cpp +++ b/tests/auto/qquickheaderview/tst_qquickheaderview.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -218,6 +219,7 @@ private slots: void testHeaderDataProxyModel(); void testOrientation(); void testModel(); + void listModel(); private: QQmlEngine *engine; @@ -349,6 +351,38 @@ void tst_QQuickHeaderView::testModel() QCOMPARE(modelChangedSpy.count(), 2); } +void tst_QQuickHeaderView::listModel() +{ + QQmlComponent component(engine); + component.loadUrl(testFileUrl("ListModel.qml")); + + QScopedPointer root(component.create()); + QVERIFY2(root, qPrintable(component.errorString())); + + auto hhv = root->findChild("horizontalHeader"); + QVERIFY(hhv); + auto vhv = root->findChild("verticalHeader"); + QVERIFY(vhv); + + auto hhvCell1 = hhv->childAt(0, 0)->childAt(0, 0)->findChild(); + QVERIFY(hhvCell1); + QCOMPARE(hhvCell1->property("text"), "AAA"); + + auto hhvCell2 = hhv->childAt(hhvCell1->width() + 5, 0)-> + childAt(hhvCell1->width() + 5, 0)->findChild(); + QVERIFY(hhvCell2); + QCOMPARE(hhvCell2->property("text"), "BBB"); + + auto vhvCell1 = vhv->childAt(0, 0)->childAt(0, 0)->findChild(); + QVERIFY(vhvCell1); + QCOMPARE(vhvCell1->property("text"), "111"); + + auto vhvCell2 = vhv->childAt(0, vhvCell1->height() + 5)-> + childAt(0, vhvCell1->height() + 5)->findChild(); + QVERIFY(vhvCell2); + QCOMPARE(vhvCell2->property("text"), "222"); +} + QTEST_MAIN(tst_QQuickHeaderView) #include "tst_qquickheaderview.moc" -- cgit v1.2.3