diff options
author | Oliver Eftevaag <[email protected]> | 2024-10-17 15:43:42 +0200 |
---|---|---|
committer | Oliver Eftevaag <[email protected]> | 2024-11-19 22:59:08 +0100 |
commit | 55c3b94035787ea265c5ff6e3c271d7154186def (patch) | |
tree | c5748dd31b82937a43d7a8447eb296b7437f96e4 | |
parent | 4af42404be163a89c0fd62a9cbf7c2544189aa4a (diff) |
Move DelegateChooser from Qt.labs.models to QtQml.Models
I assume the intention was to eventually move it out of Qt.labs.models,
once we've had a second look at the API, and are happy enough with it.
The DelegateChooser uses the adaptor model's `value` function, to fetch
data from the model, which it then uses for finding a suitable
DelegateChoice to use. (Based on the DelegateChoice::roleValue
property).
Since the adaptor model's `value` function takes a QString, as the role
to read data from, it won't make sense to change the datatype of
DelegateChooser::role.
The rest of the API makes sense too, in my opinion, and I see no reason
to change it.
Add a simple test in tst_qqmldelegatemodel, since QQmlDelegateModel is
using the DelegateChooser in QQmlDelegateModelPrivate::resolveDelegate.
[ChangeLog][QtQmlModels][Important Behavior Change] DelegateChooser has
been moved from Qt.labs.models to QtQml.Models.
Fixes: QTBUG-98101
Task-number: QTBUG-100069
Change-Id: I76ea58f57475b6a069517b5030e2ba3ac47007d2
Reviewed-by: Mitch Curtis <[email protected]>
15 files changed, 145 insertions, 31 deletions
diff --git a/src/labs/models/CMakeLists.txt b/src/labs/models/CMakeLists.txt index 0ae2d7d16e..fc6625de54 100644 --- a/src/labs/models/CMakeLists.txt +++ b/src/labs/models/CMakeLists.txt @@ -25,5 +25,5 @@ qt_internal_extend_target(LabsQmlModels CONDITION QT_FEATURE_qml_table_model qt_internal_extend_target(LabsQmlModels CONDITION QT_FEATURE_qml_delegate_model SOURCES - qqmldelegatecomponent.cpp qqmldelegatecomponent_p.h + qqmldelegatechooser.cpp qqmldelegatechooser_p.h ) diff --git a/src/labs/models/qqmldelegatechooser.cpp b/src/labs/models/qqmldelegatechooser.cpp new file mode 100644 index 0000000000..658b632e41 --- /dev/null +++ b/src/labs/models/qqmldelegatechooser.cpp @@ -0,0 +1,12 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include "qqmldelegatechooser_p.h" + +QT_BEGIN_NAMESPACE + +QT_END_NAMESPACE + +#if QT_CONFIG(qml_delegate_model) +#include "moc_qqmldelegatechooser_p.cpp" +#endif diff --git a/src/labs/models/qqmldelegatechooser_p.h b/src/labs/models/qqmldelegatechooser_p.h new file mode 100644 index 0000000000..d99f3fec4c --- /dev/null +++ b/src/labs/models/qqmldelegatechooser_p.h @@ -0,0 +1,38 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QQMLDELEGATECHOOSER_P_H +#define QQMLDELEGATECHOOSER_P_H + +#include "qqmlmodelsglobal_p.h" + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtQml/qqml.h> +#include <QtQmlModels/private/qtqmlmodelsglobal_p.h> +#include <QtQmlModels/private/qqmldelegatecomponent_p.h> + +QT_REQUIRE_CONFIG(qml_delegate_model); + +QT_BEGIN_NAMESPACE + +struct Q_LABSQMLMODELS_EXPORT QQmlDelegateChooserForeign +{ + Q_GADGET + QML_FOREIGN(QQmlDelegateChooser) + QML_NAMED_ELEMENT(DelegateChooser) + QML_ADDED_IN_VERSION(1, 0) +}; + +QT_END_NAMESPACE + +#endif // QQMLDELEGATECHOOSER_P_H diff --git a/src/qmlmodels/CMakeLists.txt b/src/qmlmodels/CMakeLists.txt index 510143bb74..dd3c3dec0e 100644 --- a/src/qmlmodels/CMakeLists.txt +++ b/src/qmlmodels/CMakeLists.txt @@ -62,6 +62,7 @@ qt_internal_extend_target(QmlModels CONDITION QT_FEATURE_qml_delegate_model qqmlabstractdelegatecomponent.cpp qqmlabstractdelegatecomponent_p.h qqmladaptormodel.cpp qqmladaptormodel_p.h qqmladaptormodelenginedata.cpp qqmladaptormodelenginedata_p.h + qqmldelegatecomponent.cpp qqmldelegatecomponent_p.h qqmldelegatemodel.cpp qqmldelegatemodel_p.h qqmldelegatemodel_p_p.h qqmldmabstractitemmodeldata.cpp qqmldmabstractitemmodeldata_p.h @@ -71,6 +72,7 @@ qt_internal_extend_target(QmlModels CONDITION QT_FEATURE_qml_delegate_model qqmllistcompositor.cpp qqmllistcompositor_p.h qquickpackage.cpp qquickpackage_p.h ) + qt_internal_add_docs(QmlModels doc/qtqmlmodels.qdocconf ) diff --git a/src/labs/models/qqmldelegatecomponent.cpp b/src/qmlmodels/qqmldelegatecomponent.cpp index e13d9603b9..53cbde5f7e 100644 --- a/src/labs/models/qqmldelegatecomponent.cpp +++ b/src/qmlmodels/qqmldelegatecomponent.cpp @@ -9,7 +9,8 @@ QT_BEGIN_NAMESPACE /*! \qmltype DelegateChoice //! \nativetype QQmlDelegateChoice - \inqmlmodule Qt.labs.qmlmodels + \inqmlmodule QtQml.Models + \since 6.9 \brief Encapsulates a delegate and when to use it. The DelegateChoice type wraps a delegate and defines the circumstances @@ -21,7 +22,7 @@ QT_BEGIN_NAMESPACE */ /*! - \qmlproperty variant Qt.labs.qmlmodels::DelegateChoice::roleValue + \qmlproperty variant QtQml.Models::DelegateChoice::roleValue This property holds the value used to match the role data for the role provided by \l DelegateChooser::role. */ QVariant QQmlDelegateChoice::roleValue() const @@ -39,7 +40,7 @@ void QQmlDelegateChoice::setRoleValue(const QVariant &value) } /*! - \qmlproperty int Qt.labs.qmlmodels::DelegateChoice::row + \qmlproperty int QtQml.Models::DelegateChoice::row This property holds the value used to match the row value of model elements. With models that have only the index property (and thus only one column), this property should be intended as an index, and set to the desired index value. @@ -51,7 +52,7 @@ void QQmlDelegateChoice::setRoleValue(const QVariant &value) */ /*! - \qmlproperty int Qt.labs.qmlmodels::DelegateChoice::index + \qmlproperty int QtQml.Models::DelegateChoice::index This property holds the value used to match the index value of model elements. This is effectively an alias for \l row. @@ -73,7 +74,7 @@ void QQmlDelegateChoice::setRow(int r) } /*! - \qmlproperty int Qt.labs.qmlmodels::DelegateChoice::column + \qmlproperty int QtQml.Models::DelegateChoice::column This property holds the value used to match the column value of model elements. */ int QQmlDelegateChoice::column() const @@ -96,7 +97,7 @@ QQmlComponent *QQmlDelegateChoice::delegate() const } /*! - \qmlproperty Component Qt.labs.qmlmodels::DelegateChoice::delegate + \qmlproperty Component QtQml.Models::DelegateChoice::delegate This property holds the delegate to use if this choice matches the model item. */ void QQmlDelegateChoice::setDelegate(QQmlComponent *delegate) @@ -138,7 +139,8 @@ bool QQmlDelegateChoice::match(int row, int column, const QVariant &value) const /*! \qmltype DelegateChooser //! \nativetype QQmlDelegateChooser - \inqmlmodule Qt.labs.qmlmodels + \inqmlmodule QtQml.Models + \since 6.9 \brief Allows a view to use different delegates for different types of items in the model. The DelegateChooser is a special \l Component type intended for those scenarios where a @@ -154,9 +156,9 @@ bool QQmlDelegateChoice::match(int row, int column, const QVariant &value) const delegate with each setting: \qml + import QtQml.Models import QtQuick import QtQuick.Controls - import Qt.labs.qmlmodels ListView { width: 200; height: 400 @@ -190,7 +192,7 @@ bool QQmlDelegateChoice::match(int row, int column, const QVariant &value) const */ /*! - \qmlproperty string Qt.labs.qmlmodels::DelegateChooser::role + \qmlproperty string QtQml.Models::DelegateChooser::role This property holds the role or the property name used to determine the delegate for a given model item. \note For \l{QAbstractItemModel} based models, including \l{ListModel}, the DelegateChooser will @@ -208,7 +210,7 @@ void QQmlDelegateChooser::setRole(const QString &role) } /*! - \qmlproperty list<DelegateChoice> Qt.labs.qmlmodels::DelegateChooser::choices + \qmlproperty list<DelegateChoice> QtQml.Models::DelegateChooser::choices \qmldefault The list of DelegateChoices for the chooser. diff --git a/src/labs/models/qqmldelegatecomponent_p.h b/src/qmlmodels/qqmldelegatecomponent_p.h index 2929094fd6..8659b4af4a 100644 --- a/src/labs/models/qqmldelegatecomponent_p.h +++ b/src/qmlmodels/qqmldelegatecomponent_p.h @@ -4,7 +4,7 @@ #ifndef QQMLDELEGATECOMPONENT_P_H #define QQMLDELEGATECOMPONENT_P_H -#include "qqmlmodelsglobal_p.h" +#include <private/qtqmlmodelsglobal_p.h> // // W A R N I N G @@ -25,7 +25,7 @@ QT_REQUIRE_CONFIG(qml_delegate_model); QT_BEGIN_NAMESPACE -class Q_LABSQMLMODELS_EXPORT QQmlDelegateChoice : public QObject +class Q_QMLMODELS_EXPORT QQmlDelegateChoice : public QObject { Q_OBJECT Q_PROPERTY(QVariant roleValue READ roleValue WRITE setRoleValue NOTIFY roleValueChanged FINAL) @@ -35,7 +35,7 @@ class Q_LABSQMLMODELS_EXPORT QQmlDelegateChoice : public QObject Q_PROPERTY(QQmlComponent* delegate READ delegate WRITE setDelegate NOTIFY delegateChanged FINAL) Q_CLASSINFO("DefaultProperty", "delegate") QML_NAMED_ELEMENT(DelegateChoice) - QML_ADDED_IN_VERSION(1, 0) + QML_ADDED_IN_VERSION(6, 9) public: QVariant roleValue() const; @@ -67,14 +67,14 @@ private: QQmlComponent *m_delegate = nullptr; }; -class Q_LABSQMLMODELS_EXPORT QQmlDelegateChooser : public QQmlAbstractDelegateComponent +class Q_QMLMODELS_EXPORT QQmlDelegateChooser : public QQmlAbstractDelegateComponent { Q_OBJECT Q_PROPERTY(QString role READ role WRITE setRole NOTIFY roleChanged FINAL) Q_PROPERTY(QQmlListProperty<QQmlDelegateChoice> choices READ choices CONSTANT FINAL) Q_CLASSINFO("DefaultProperty", "choices") QML_NAMED_ELEMENT(DelegateChooser) - QML_ADDED_IN_VERSION(1, 0) + QML_ADDED_IN_VERSION(6, 9) public: QString role() const final { return m_role; } diff --git a/tests/auto/qml/qqmldelegatemodel/data/delegatechooser.qml b/tests/auto/qml/qqmldelegatemodel/data/delegatechooser.qml new file mode 100644 index 0000000000..ffeba941a1 --- /dev/null +++ b/tests/auto/qml/qqmldelegatemodel/data/delegatechooser.qml @@ -0,0 +1,34 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +import QtQuick + +Window { + width: 640 + height: 480 + title: qsTr("Hello World") + + ListView { + width: 200; height: 400 + + model: ListModel { + ListElement { c: "red" } + ListElement { c: "green" } + ListElement { c: "blue" } + ListElement { c: "purple" } + ListElement { c: "orange" } + ListElement { c: "red" } + ListElement { c: "brown"} + } + + delegate: DelegateChooser { + role: "c" + DelegateChoice { roleValue: "red"; Rectangle { color: modelData; width: 200; height: 50 } } + DelegateChoice { roleValue: "green"; Rectangle { color: modelData; width: 200; height: 50 } } + DelegateChoice { roleValue: "blue"; Rectangle { color: modelData; width: 200; height: 50 } } + DelegateChoice { roleValue: "purple"; Rectangle { color: modelData; width: 200; height: 50 } } + DelegateChoice { roleValue: "orange"; Rectangle { color: modelData; width: 200; height: 50 } } + DelegateChoice { roleValue: "brown"; Rectangle { color: modelData; width: 200; height: 50 } } + } + } +} diff --git a/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp b/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp index 3f08d8fc85..86e9bbef69 100644 --- a/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp +++ b/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp @@ -10,11 +10,13 @@ #include <QtQml/qqmlcomponent.h> #include <QtQml/qqmlapplicationengine.h> #include <QtQmlModels/private/qqmldelegatemodel_p.h> +#include <QtQmlModels/private/qqmldelegatemodel_p_p.h> #include <QtQmlModels/private/qqmllistmodel_p.h> #include <QtQuick/qquickview.h> #include <QtQuick/qquickitem.h> #include <QtQuick/private/qquickitemview_p_p.h> #include <QtQuick/private/qquicklistview_p.h> +#include <QtQuick/private/qquickrectangle_p.h> #include <QtQuickTest/quicktest.h> #include <QtQuickTestUtils/private/qmlutils_p.h> #include <QtQuickTestUtils/private/visualtestutils_p.h> @@ -53,6 +55,7 @@ private slots: void clearCacheDuringInsertion(); void viewUpdatedOnDelegateChoiceAffectingRoleChange(); void proxyModelWithDelayedSourceModelInListView(); + void delegateChooser(); }; class BaseAbstractItemModel : public QAbstractItemModel @@ -806,6 +809,33 @@ void tst_QQmlDelegateModel::proxyModelWithDelayedSourceModelInListView() QTRY_COMPARE(listView->count(), 1); } +void tst_QQmlDelegateModel::delegateChooser() +{ + QQuickApplicationHelper helper(this, "delegatechooser.qml"); + QVERIFY2(helper.ready, helper.failureMessage()); + + QQuickWindow *window = helper.window; + window->show(); + QVERIFY(QTest::qWaitForWindowExposed(window)); + + QQuickListView *lv = window->findChild<QQuickListView *>(); + QVERIFY(lv); + + QQmlDelegateModel *model = qobject_cast<QQmlDelegateModel *>(QQuickItemViewPrivate::get(lv)->model); + QVERIFY(model); + QQmlAdaptorModel *adaptorModel = &QQmlDelegateModelPrivate::get(model)->m_adaptorModel; + QVERIFY(adaptorModel); + + const int lvCount = lv->count(); + QCOMPARE(lvCount, model->count()); + + for (int i = 0; i < lvCount; ++i) { + QQuickRectangle *rectangle = qobject_cast<QQuickRectangle *>(QQuickVisualTestUtils::findViewDelegateItem(lv, i)); + QVERIFY(rectangle); + QCOMPARE(rectangle->color(), QColor::fromString(adaptorModel->value(adaptorModel->indexAt(i, 0), "modelData").toString())); + } +} + QTEST_MAIN(tst_QQmlDelegateModel) #include "tst_qqmldelegatemodel.moc" diff --git a/tests/auto/qmltest/listview/data/MultiDelegate.qml b/tests/auto/qmltest/listview/data/MultiDelegate.qml index 491171d8e8..5cc9a2930a 100644 --- a/tests/auto/qmltest/listview/data/MultiDelegate.qml +++ b/tests/auto/qmltest/listview/data/MultiDelegate.qml @@ -1,9 +1,8 @@ // Copyright (C) 2018 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only -import QtQuick 2.12 -import QtQml.Models 2.12 -import Qt.labs.qmlmodels 1.0 +import QtQuick +import QtQml.Models ListView { width: 400 diff --git a/tests/auto/qmltest/listview/data/MultiDelegate2.qml b/tests/auto/qmltest/listview/data/MultiDelegate2.qml index 9ff7b44458..1ab720a394 100644 --- a/tests/auto/qmltest/listview/data/MultiDelegate2.qml +++ b/tests/auto/qmltest/listview/data/MultiDelegate2.qml @@ -1,9 +1,8 @@ // Copyright (C) 2018 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only -import QtQuick 2.12 -import QtQml.Models 2.12 -import Qt.labs.qmlmodels 1.0 +import QtQuick +import QtQml.Models ListView { width: 400 diff --git a/tests/auto/qmltest/listview/data/MultiDelegate3.qml b/tests/auto/qmltest/listview/data/MultiDelegate3.qml index 2e13364c66..1407ef217b 100644 --- a/tests/auto/qmltest/listview/data/MultiDelegate3.qml +++ b/tests/auto/qmltest/listview/data/MultiDelegate3.qml @@ -1,9 +1,8 @@ // Copyright (C) 2020 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only -import QtQuick 2.12 -import QtQml.Models 2.12 -import Qt.labs.qmlmodels 1.0 +import QtQuick +import QtQml.Models ListView { width: 400 diff --git a/tests/auto/quick/qquicklistview/data/usechooserwithoutdefault.qml b/tests/auto/quick/qquicklistview/data/usechooserwithoutdefault.qml index cba0ad5720..310bf6f46e 100644 --- a/tests/auto/quick/qquicklistview/data/usechooserwithoutdefault.qml +++ b/tests/auto/quick/qquicklistview/data/usechooserwithoutdefault.qml @@ -3,7 +3,7 @@ import QtQuick 2.12 import QtQuick.Window 2.3 -import Qt.labs.qmlmodels 1.0 +import QtQml.Models Item { width: 640 diff --git a/tests/auto/quick/qquicktableview/data/checkrowandcolumnnotchanged.qml b/tests/auto/quick/qquicktableview/data/checkrowandcolumnnotchanged.qml index fa15f2f54b..1cd2480786 100644 --- a/tests/auto/quick/qquicktableview/data/checkrowandcolumnnotchanged.qml +++ b/tests/auto/quick/qquicktableview/data/checkrowandcolumnnotchanged.qml @@ -3,7 +3,7 @@ import QtQuick 2.12 import QtQuick.Window 2.3 -import Qt.labs.qmlmodels 1.0 +import QtQml.Models Item { width: 640 diff --git a/tests/auto/quick/qquicktableview/data/usechooserwithoutdefault.qml b/tests/auto/quick/qquicktableview/data/usechooserwithoutdefault.qml index 4ff2a7401f..6c0d042aeb 100644 --- a/tests/auto/quick/qquicktableview/data/usechooserwithoutdefault.qml +++ b/tests/auto/quick/qquicktableview/data/usechooserwithoutdefault.qml @@ -3,7 +3,7 @@ import QtQuick 2.12 import QtQuick.Window 2.3 -import Qt.labs.qmlmodels 1.0 +import QtQml.Models Item { width: 640 diff --git a/tests/auto/quick/qquickvisualdatamodel/data/filterGroupForDelegate.qml b/tests/auto/quick/qquickvisualdatamodel/data/filterGroupForDelegate.qml index d72ca51d7f..857893c6f5 100644 --- a/tests/auto/quick/qquickvisualdatamodel/data/filterGroupForDelegate.qml +++ b/tests/auto/quick/qquickvisualdatamodel/data/filterGroupForDelegate.qml @@ -1,6 +1,5 @@ -import QtQml.Models 2.12 -import Qt.labs.qmlmodels 1.0 -import QtQuick 2.12 +import QtQml.Models +import QtQuick Item { id: root |