From 005931905af62fb354c012594f9420d0acabbee3 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Thu, 15 Jan 2015 19:46:56 +0100 Subject: Add Q_GADGET wrappers for QModelIndex & Co. The complete list of types is, * QModelIndex * QModelIndexList * QPersistentModelIndex * QItemSelection * QItemSelectionRange These wrapper types follow the QQmlValueType conventions and allow us to expose the wrapped types without introducing meta-type changes. They also allow to customize the string type representation. We also extend QQmlValueTypeFactory to return the meta-object for those types. Finally, we add two-way meta-type conversion between QModelIndex and QPersistentModelIndex to get the same interoperability as in C++ when passing an object of one type to a function requir- ing an object of the other type. Change-Id: Iaa7089ea576c901f12715ffa21e4d94603d53755 Reviewed-by: Caroline Chao --- .../auto/qml/qqmlitemmodels/data/itemselection.qml | 36 +++ .../qml/qqmlitemmodels/data/itemselectionrange.qml | 32 ++ tests/auto/qml/qqmlitemmodels/data/modelindex.qml | 19 ++ .../qqmlitemmodels/data/modelindexconversion.qml | 9 + .../qml/qqmlitemmodels/data/modelindexlist.qml | 21 ++ .../qqmlitemmodels/data/persistentmodelindex.qml | 23 ++ tests/auto/qml/qqmlitemmodels/qqmlitemmodels.pro | 23 ++ tests/auto/qml/qqmlitemmodels/qtestmodel.h | 322 +++++++++++++++++++++ tests/auto/qml/qqmlitemmodels/testtypes.h | 148 ++++++++++ .../auto/qml/qqmlitemmodels/tst_qqmlitemmodels.cpp | 223 ++++++++++++++ 10 files changed, 856 insertions(+) create mode 100644 tests/auto/qml/qqmlitemmodels/data/itemselection.qml create mode 100644 tests/auto/qml/qqmlitemmodels/data/itemselectionrange.qml create mode 100644 tests/auto/qml/qqmlitemmodels/data/modelindex.qml create mode 100644 tests/auto/qml/qqmlitemmodels/data/modelindexconversion.qml create mode 100644 tests/auto/qml/qqmlitemmodels/data/modelindexlist.qml create mode 100644 tests/auto/qml/qqmlitemmodels/data/persistentmodelindex.qml create mode 100644 tests/auto/qml/qqmlitemmodels/qqmlitemmodels.pro create mode 100644 tests/auto/qml/qqmlitemmodels/qtestmodel.h create mode 100644 tests/auto/qml/qqmlitemmodels/testtypes.h create mode 100644 tests/auto/qml/qqmlitemmodels/tst_qqmlitemmodels.cpp (limited to 'tests/auto/qml/qqmlitemmodels') diff --git a/tests/auto/qml/qqmlitemmodels/data/itemselection.qml b/tests/auto/qml/qqmlitemmodels/data/itemselection.qml new file mode 100644 index 0000000000..57cb6436e9 --- /dev/null +++ b/tests/auto/qml/qqmlitemmodels/data/itemselection.qml @@ -0,0 +1,36 @@ +import Test 1.0 + +ItemModelsTest { + property var itemSelection + property int count + property bool contains: false + + function range(top, bottom, left, right, parent) { + if (parent === undefined) + parent = invalidModelIndex() + var topLeft = model.index(top, left, parent) + var bottomRight = model.index(bottom, right, parent) + return createItemSelectionRange(topLeft, bottomRight) + } + + onModelChanged: { + itemSelection = createItemSelection() + itemSelection.prepend(range(0, 0, 0, 5)) + itemSelection.append(range(0, 5, 0, 0)) + for (var i = 0; i < 3; i++) + itemSelection.insert(i, range(i, i + 1, i + 2, i + 3)) + + var itemSelection2 = createItemSelection() + for (i = 3; i < 6; i++) + itemSelection2.select(model.index(i, i + 1), model.index(i + 2, i + 3)) + + itemSelection.merge(itemSelection2, 2 /*ItemSelectionModel.Select*/) + + count = itemSelection.length + contains = itemSelection.contains(model.index(0, 0)) + + itemSelection.removeAt(3) + itemSelection.removeFirst() + itemSelection.removeLast() + } +} diff --git a/tests/auto/qml/qqmlitemmodels/data/itemselectionrange.qml b/tests/auto/qml/qqmlitemmodels/data/itemselectionrange.qml new file mode 100644 index 0000000000..72f732abaf --- /dev/null +++ b/tests/auto/qml/qqmlitemmodels/data/itemselectionrange.qml @@ -0,0 +1,32 @@ +import Test 1.0 + +ItemModelsTest { + property var itemSelectionRange: createItemSelectionRange(invalidModelIndex(), invalidModelIndex()) + property int top: itemSelectionRange.top + property int left: itemSelectionRange.left + property int bottom: itemSelectionRange.bottom + property int right: itemSelectionRange.right + property int width: itemSelectionRange.width + property int height: itemSelectionRange.height + property bool isValid: itemSelectionRange.valid + property bool isEmpty: itemSelectionRange.empty + property var isrModel: itemSelectionRange.model + property bool contains1: false + property bool contains2: false + property bool intersects: false + property var intersected + + onModelChanged: { + if (model) { + var parentIndex = model.index(0, 0) + var index1 = model.index(3, 0, parentIndex) + var index2 = model.index(5, 6, parentIndex) + itemSelectionRange = createItemSelectionRange(index1, index2) + + contains1 = itemSelectionRange.contains(index1) + contains2 = itemSelectionRange.contains(4, 3, parentIndex) + intersects = itemSelectionRange.intersects(createItemSelectionRange(parentIndex, parentIndex)) + intersected = itemSelectionRange.intersected(createItemSelectionRange(parentIndex, parentIndex)) + } + } +} diff --git a/tests/auto/qml/qqmlitemmodels/data/modelindex.qml b/tests/auto/qml/qqmlitemmodels/data/modelindex.qml new file mode 100644 index 0000000000..0d6e3624cb --- /dev/null +++ b/tests/auto/qml/qqmlitemmodels/data/modelindex.qml @@ -0,0 +1,19 @@ +import Test 1.0 + +ItemModelsTest { + property bool isValid: modelIndex.valid + property int row: modelIndex.row + property int column: modelIndex.column + property var parent: modelIndex.parent + property var model: modelIndex.model + property var internalId: modelIndex.internalId + + onSignalWithModelIndex: { + isValid = index.valid + row = index.row + column = index.column + parent = index.parent + model = index.model + internalId = index.internalId + } +} diff --git a/tests/auto/qml/qqmlitemmodels/data/modelindexconversion.qml b/tests/auto/qml/qqmlitemmodels/data/modelindexconversion.qml new file mode 100644 index 0000000000..91ee05eaa9 --- /dev/null +++ b/tests/auto/qml/qqmlitemmodels/data/modelindexconversion.qml @@ -0,0 +1,9 @@ +import Test 1.0 + +ItemModelsTest { + + onModelChanged: { + modelIndex = createPersistentModelIndex(model.index(0, 0)) + persistentModelIndex = model.index(1, 1) + } +} diff --git a/tests/auto/qml/qqmlitemmodels/data/modelindexlist.qml b/tests/auto/qml/qqmlitemmodels/data/modelindexlist.qml new file mode 100644 index 0000000000..44393392d3 --- /dev/null +++ b/tests/auto/qml/qqmlitemmodels/data/modelindexlist.qml @@ -0,0 +1,21 @@ +import Test 1.0 + +ItemModelsTest { + property var modelIndexList + property int count + + onModelChanged: { + modelIndexList = createModelIndexList() + modelIndexList.prepend(model.index(0, 0)) + modelIndexList.append(model.index(1, 1)) + for (var i = 0; i < 3; i++) + modelIndexList.insert(i, model.index(2 + i, 2 + i)) + + count = modelIndexList.length + modelIndex = modelIndexList.at(0) + + modelIndexList.removeAt(3) + modelIndexList.removeFirst() + modelIndexList.removeLast() + } +} diff --git a/tests/auto/qml/qqmlitemmodels/data/persistentmodelindex.qml b/tests/auto/qml/qqmlitemmodels/data/persistentmodelindex.qml new file mode 100644 index 0000000000..13037065a6 --- /dev/null +++ b/tests/auto/qml/qqmlitemmodels/data/persistentmodelindex.qml @@ -0,0 +1,23 @@ +import Test 1.0 + +ItemModelsTest { + property bool isValid: persistentModelIndex.valid + property int row: persistentModelIndex.row + property int column: persistentModelIndex.column + property var parent: persistentModelIndex.parent + property var model: persistentModelIndex.model + property var internalId: persistentModelIndex.internalId + + property var pmi + + onSignalWithPersistentModelIndex: { + isValid = index.valid + row = index.row + column = index.column + parent = index.parent + model = index.model + internalId = index.internalId + + pmi = createPersistentModelIndex(model.index(0, 0)) + } +} diff --git a/tests/auto/qml/qqmlitemmodels/qqmlitemmodels.pro b/tests/auto/qml/qqmlitemmodels/qqmlitemmodels.pro new file mode 100644 index 0000000000..f76c6d0d25 --- /dev/null +++ b/tests/auto/qml/qqmlitemmodels/qqmlitemmodels.pro @@ -0,0 +1,23 @@ +CONFIG += testcase +TARGET = tst_qqmlitemmodels +macx:CONFIG -= app_bundle + +HEADERS = qtestmodel.h testtypes.h +SOURCES += tst_qqmlitemmodels.cpp + +include (../../shared/util.pri) + +TESTDATA = data/* + +CONFIG += parallel_test + +QT += core qml testlib +DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 + +DISTFILES += \ + data/modelindex.qml \ + data/persistentmodelindex.qml \ + data/itemselectionrange.qml \ + data/modelindexlist.qml \ + data/itemselection.qml \ + data/modelindexconversion.qml diff --git a/tests/auto/qml/qqmlitemmodels/qtestmodel.h b/tests/auto/qml/qqmlitemmodels/qtestmodel.h new file mode 100644 index 0000000000..bb0a169652 --- /dev/null +++ b/tests/auto/qml/qqmlitemmodels/qtestmodel.h @@ -0,0 +1,322 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef Q_TEST_MODEL_H +#define Q_TEST_MODEL_H + +#include + +class TestModel: public QAbstractItemModel +{ + Q_OBJECT + +public: + TestModel(QObject *parent = 0): QAbstractItemModel(parent), + fetched(false), rows(10), cols(1), levels(INT_MAX), wrongIndex(false) { init(); } + + TestModel(int _rows, int _cols, QObject *parent = 0): QAbstractItemModel(parent), + fetched(false), rows(_rows), cols(_cols), levels(INT_MAX), wrongIndex(false) { init(); } + + void init() { + decorationsEnabled = false; + alternateChildlessRows = true; + tree = new Node(rows); + } + + inline qint32 level(const QModelIndex &index) const { + Node *n = (Node *)index.internalPointer(); + if (!n) + return -1; + int l = -1; + while (n != tree) { + n = n->parent; + ++l; + } + return l; + } + + void resetModel() + { + beginResetModel(); + fetched = false; + delete tree; + tree = new Node(rows); + endResetModel(); + } + + QString displayData(const QModelIndex &idx) const + { + return QString("[%1,%2,%3,%4]").arg(idx.row()).arg(idx.column()).arg(idx.internalId()).arg(hasChildren(idx)); + } + + bool canFetchMore(const QModelIndex &) const { + return !fetched; + } + + void fetchMore(const QModelIndex &) { + fetched = true; + } + + bool hasChildren(const QModelIndex &parent = QModelIndex()) const { + bool hasFetched = fetched; + fetched = true; + bool r = QAbstractItemModel::hasChildren(parent); + fetched = hasFetched; + return r; + } + + int rowCount(const QModelIndex& parent = QModelIndex()) const { + if (!fetched) + qFatal("%s: rowCount should not be called before fetching", Q_FUNC_INFO); + if ((parent.column() > 0) || (level(parent) > levels) + || (alternateChildlessRows && parent.row() > 0 && (parent.row() & 1))) + return 0; + Node *n = (Node*)parent.internalPointer(); + if (!n) + n = tree; + return n->children.count(); + } + + int columnCount(const QModelIndex& parent = QModelIndex()) const { + if ((parent.column() > 0) || (level(parent) > levels) + || (alternateChildlessRows && parent.row() > 0 && (parent.row() & 1))) + return 0; + return cols; + } + + bool isEditable(const QModelIndex &index) const { + if (index.isValid()) + return true; + return false; + } + + Q_INVOKABLE QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const + { + if (row < 0 || column < 0 || (level(parent) > levels) || column >= cols) + return QModelIndex(); + Node *pn = (Node*)parent.internalPointer(); + if (!pn) + pn = tree; + if (row >= pn->children.count()) + return QModelIndex(); + + Node *n = pn->children.at(row); + if (!n) { + n = new Node(rows, pn); + pn->children[row] = n; + } + return createIndex(row, column, n); + } + + QModelIndex parent(const QModelIndex &index) const + { + Node *n = (Node *)index.internalPointer(); + if (!n || n->parent == tree) + return QModelIndex(); + Q_ASSERT(n->parent->parent); + int parentRow = n->parent->parent->children.indexOf(n->parent); + Q_ASSERT(parentRow != -1); + return createIndex(parentRow, 0, n->parent); + } + + QVariant data(const QModelIndex &idx, int role) const + { + if (!idx.isValid()) + return QVariant(); + + Node *pn = (Node *)idx.internalPointer(); + if (!pn) + pn = tree; + if (pn != tree) + pn = pn->parent; + if (idx.row() < 0 || idx.column() < 0 || idx.column() >= cols + || idx.row() >= pn->children.count()) { + wrongIndex = true; + qWarning("Invalid modelIndex [%d,%d,%p]", idx.row(), idx.column(), + idx.internalPointer()); + return QVariant(); + } + + if (role == Qt::DisplayRole) { + return displayData(idx); + } + + return QVariant(); + } + + bool setData(const QModelIndex &index, const QVariant &value, int role) + { + Q_UNUSED(value); + QVector changedRole(1, role); + emit dataChanged(index, index, changedRole); + return true; + } + + void groupedSetData(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles) + { + emit dataChanged(topLeft, bottomRight, roles); + } + + void changeLayout(const QList &parents = QList()) + { + emit layoutAboutToBeChanged(parents); + emit layoutChanged(parents); + } + + bool removeRows(int row, int count, const QModelIndex &parent) + { + beginRemoveRows(parent, row, row + count - 1); + Node *n = (Node *)parent.internalPointer(); + if (!n) + n = tree; + n->removeRows(row, count); + endRemoveRows(); + return true; + } + + void removeLastColumn() + { + beginRemoveColumns(QModelIndex(), cols - 1, cols - 1); + --cols; + endRemoveColumns(); + } + + void removeAllColumns() + { + beginRemoveColumns(QModelIndex(), 0, cols - 1); + cols = 0; + endRemoveColumns(); + } + + bool insertRows(int row, int count, const QModelIndex &parent) + { + beginInsertRows(parent, row, row + count - 1); + Node *n = (Node *)parent.internalPointer(); + if (!n) + n = tree; + n->addRows(row, count); + endInsertRows(); + return true; + } + + bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) + { + Q_ASSERT_X(sourceRow >= 0 && sourceRow < rowCount(sourceParent) + && count > 0 && sourceRow + count < rowCount(sourceParent) + && destinationChild >= 0 && destinationChild <= rowCount(destinationParent), + Q_FUNC_INFO, "Rows out of range."); + Q_ASSERT_X(!(sourceParent == destinationParent && destinationChild >= sourceRow && destinationChild < sourceRow + count), + Q_FUNC_INFO, "Moving rows onto themselves."); + if (!beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild)) + return false; + Node *src = (Node *)sourceParent.internalPointer(); + if (!src) + src = tree; + Node *dest = (Node *)destinationParent.internalPointer(); + if (!dest) + dest = tree; + QVector buffer = src->children.mid(sourceRow, count); + if (src != dest) { + src->removeRows(sourceRow, count, true /* keep alive */); + dest->addRows(destinationChild, count); + } else { + QVector &c = dest->children; + if (sourceRow < destinationChild) { + memmove(&c[sourceRow], &c[sourceRow + count], sizeof(Node *) * (destinationChild - sourceRow - count)); + destinationChild -= count; + } else { + memmove(&c[destinationChild + count], &c[destinationChild], sizeof(Node *) * (sourceRow - destinationChild)); + } + } + for (int i = 0; i < count; i++) { + Node *n = buffer[i]; + n->parent = dest; + dest->children[i + destinationChild] = n; + } + + endMoveRows(); + return true; + } + + void setDecorationsEnabled(bool enable) + { + decorationsEnabled = enable; + } + + mutable bool fetched; + bool decorationsEnabled; + bool alternateChildlessRows; + int rows, cols; + int levels; + mutable bool wrongIndex; + + struct Node { + Node *parent; + QVector children; + + Node(int rows, Node *p = 0) : parent(p) + { + addRows(0, rows); + } + + ~Node() + { + foreach (Node *n, children) + delete n; + } + + void addRows(int row, int count) + { + if (count > 0) { + children.reserve(children.count() + count); + children.insert(row, count, (Node *)0); + } + } + + void removeRows(int row, int count, bool keepAlive = false) + { + int newCount = qMax(children.count() - count, 0); + int effectiveCountDiff = children.count() - newCount; + if (effectiveCountDiff > 0) { + if (!keepAlive) + for (int i = 0; i < effectiveCountDiff; i++) + delete children[i + row]; + children.remove(row, effectiveCountDiff); + } + } + }; + + Node *tree; +}; + +#endif // Q_TEST_MODEL_H diff --git a/tests/auto/qml/qqmlitemmodels/testtypes.h b/tests/auto/qml/qqmlitemmodels/testtypes.h new file mode 100644 index 0000000000..5345609cd3 --- /dev/null +++ b/tests/auto/qml/qqmlitemmodels/testtypes.h @@ -0,0 +1,148 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TESTTYPES_H +#define TESTTYPES_H + +#include +#include +#include "qdebug.h" + +class ItemModelsTest : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged) + Q_PROPERTY(QModelIndex modelIndex READ modelIndex WRITE setModelIndex NOTIFY changed) + Q_PROPERTY(QPersistentModelIndex persistentModelIndex READ persistentModelIndex WRITE setPersistentModelIndex NOTIFY changed) + +public: + QModelIndex modelIndex() const + { + return m_modelIndex; + } + + QPersistentModelIndex persistentModelIndex() const + { + return m_persistentModelIndex; + } + + void emitChanged() + { + emit changed(); + } + + void emitSignalWithModelIndex(const QModelIndex &index) + { + emit signalWithModelIndex(index); + } + + void emitSignalWithPersistentModelIndex(const QPersistentModelIndex &index) + { + emit signalWithPersistentModelIndex(index); + } + + QAbstractItemModel * model() const + { + return m_model; + } + + Q_INVOKABLE QModelIndex invalidModelIndex() const + { + return QModelIndex(); + } + + Q_INVOKABLE QModelIndexList createModelIndexList() const + { + return QModelIndexList(); + } + + Q_INVOKABLE QItemSelectionRange createItemSelectionRange(const QModelIndex &tl, const QModelIndex &br) const + { + return QItemSelectionRange(tl, br); + } + + Q_INVOKABLE QItemSelection createItemSelection() + { + return QItemSelection(); + } + + Q_INVOKABLE QPersistentModelIndex createPersistentModelIndex(const QModelIndex &index) + { + return QPersistentModelIndex(index); + } + +public slots: + void setModelIndex(const QModelIndex &arg) + { + if (m_modelIndex == arg) + return; + + m_modelIndex = arg; + emit changed(); + } + + void setPersistentModelIndex(const QPersistentModelIndex &arg) + { + if (m_persistentModelIndex == arg) + return; + + m_persistentModelIndex = arg; + emit changed(); + } + + void setModel(QAbstractItemModel *arg) + { + if (m_model == arg) + return; + + m_model = arg; + emit modelChanged(arg); + } + +signals: + void changed(); + + void signalWithModelIndex(QModelIndex index); + void signalWithPersistentModelIndex(QPersistentModelIndex index); + + void modelChanged(QAbstractItemModel * arg); + +private: + QModelIndex m_modelIndex; + QPersistentModelIndex m_persistentModelIndex; + QAbstractItemModel *m_model; +}; + +#endif // TESTTYPES_H + diff --git a/tests/auto/qml/qqmlitemmodels/tst_qqmlitemmodels.cpp b/tests/auto/qml/qqmlitemmodels/tst_qqmlitemmodels.cpp new file mode 100644 index 0000000000..d7e3931376 --- /dev/null +++ b/tests/auto/qml/qqmlitemmodels/tst_qqmlitemmodels.cpp @@ -0,0 +1,223 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include +#include +#include "../../shared/util.h" +#include "testtypes.h" +#include "qtestmodel.h" + +#define INIT_TEST_OBJECT(fileName, object) \ + QQmlComponent component_##object(&engine, testFileUrl(fileName)); \ + QScopedPointerobject(qobject_cast(component_##object.create())); \ + + +class tst_qqmlitemmodels : public QQmlDataTest +{ + Q_OBJECT + +public: + tst_qqmlitemmodels() {} + +private slots: + void initTestCase(); + + void modelIndex(); + void persistentModelIndex(); + void modelIndexConversion(); + void itemSelectionRange(); + void itemSelection(); + void modelIndexList(); + +private: + QQmlEngine engine; +}; + +void tst_qqmlitemmodels::initTestCase() +{ + QQmlDataTest::initTestCase(); + qmlRegisterType("Test", 1, 0, "ItemModelsTest"); +} + +void tst_qqmlitemmodels::modelIndex() +{ + INIT_TEST_OBJECT("modelindex.qml", object); + TestModel model(10, 10); + + QModelIndex index = object->modelIndex(); + for (int i = 0; i < 5; i++) { + QCOMPARE(object->property("isValid").toBool(), index.isValid()); + QCOMPARE(object->property("row").toInt(), index.row()); + QCOMPARE(object->property("column").toInt(), index.column()); + QCOMPARE(object->property("parent").toModelIndex(), index.parent()); + QCOMPARE(object->property("model").value(), index.model()); + QCOMPARE(object->property("internalId").toULongLong(), index.internalId()); + + if (i < 3) { + index = model.index(2 + i, 4 - i, index); + object->setModelIndex(index); + } else if (i < 4) { + index = model.index(2 + i, 4 - i); + object->emitSignalWithModelIndex(index); + } + } +} + +void tst_qqmlitemmodels::persistentModelIndex() +{ + INIT_TEST_OBJECT("persistentmodelindex.qml", object); + TestModel model(10, 10); + + QPersistentModelIndex index = object->persistentModelIndex(); + for (int i = 0; i < 5; i++) { + QCOMPARE(object->property("isValid").toBool(), index.isValid()); + QCOMPARE(object->property("row").toInt(), index.row()); + QCOMPARE(object->property("column").toInt(), index.column()); + QCOMPARE(object->property("parent").toModelIndex(), index.parent()); + QCOMPARE(object->property("model").value(), index.model()); + QCOMPARE(object->property("internalId").toULongLong(), index.internalId()); + + if (i < 2) { + index = model.index(2 + i, 4 - i, index); + object->setPersistentModelIndex(index); + } else if (i < 3) { + model.removeRow(2); + QVERIFY(!index.isValid()); // QPersistentModelIndex should update + object->emitChanged(); // Help QML get the new values as QPMI doesn't emit anything + } else if (i < 4) { + index = model.index(2 + i, 4 - i); + object->emitSignalWithPersistentModelIndex(index); + } + } + + const QVariant &pmiVariant = object->property("pmi"); + QCOMPARE(pmiVariant.type(), QVariant::UserType); + QCOMPARE(pmiVariant.userType(), qMetaTypeId()); + QCOMPARE(pmiVariant.value(), QPersistentModelIndex(model.index(0, 0))); +} + +void tst_qqmlitemmodels::itemSelectionRange() +{ + INIT_TEST_OBJECT("itemselectionrange.qml", object); + TestModel model(10, 10); + + for (int i = 0; i < 2; i++) { + const QVariant &isrVariant = object->property("itemSelectionRange"); + QCOMPARE(isrVariant.type(), QVariant::UserType); + QCOMPARE(isrVariant.userType(), qMetaTypeId()); + const QItemSelectionRange &isr = isrVariant.value(); + if (i > 0) { + QModelIndex parentIndex = model.index(0, 0); + QCOMPARE(QModelIndex(isr.topLeft()), model.index(3, 0, parentIndex)); + QCOMPARE(QModelIndex(isr.bottomRight()), model.index(5, 6, parentIndex)); + } else { + QCOMPARE(QModelIndex(isr.topLeft()), QModelIndex()); + QCOMPARE(QModelIndex(isr.bottomRight()), QModelIndex()); + } + + QCOMPARE(object->property("top").toInt(), isr.top()); + QCOMPARE(object->property("left").toInt(), isr.left()); + QCOMPARE(object->property("bottom").toInt(), isr.bottom()); + QCOMPARE(object->property("right").toInt(), isr.right()); + QCOMPARE(object->property("width").toInt(), isr.width()); + QCOMPARE(object->property("height").toInt(), isr.height()); + QCOMPARE(object->property("isValid").toBool(), isr.isValid()); + QCOMPARE(object->property("isEmpty").toBool(), isr.isEmpty()); + QCOMPARE(object->property("isrModel").value(), isr.model()); + + // Set model for the 2nd iteration and test again + object->setModel(&model); + } + + // Check API function calls + QVERIFY(object->property("contains1").toBool()); + QVERIFY(object->property("contains2").toBool()); + QVERIFY(!object->property("intersects").toBool()); + const QVariant &isrVariant = object->property("intersected"); + QCOMPARE(isrVariant.type(), QVariant::UserType); + QCOMPARE(isrVariant.userType(), qMetaTypeId()); +} + +void tst_qqmlitemmodels::modelIndexConversion() +{ + INIT_TEST_OBJECT("modelindexconversion.qml", object); + TestModel model(10, 10); + object->setModel(&model); + + QCOMPARE(object->modelIndex(), model.index(0, 0)); + QCOMPARE(object->persistentModelIndex(), QPersistentModelIndex(model.index(1, 1))); +} + +void tst_qqmlitemmodels::itemSelection() +{ + INIT_TEST_OBJECT("itemselection.qml", object); + TestModel model(10, 10); + + object->setModel(&model); + QCOMPARE(object->property("count").toInt(), 8); + QCOMPARE(object->property("contains").toBool(), true); + + QVariant milVariant = object->property("itemSelection"); + QCOMPARE(milVariant.type(), QVariant::UserType); + QCOMPARE(milVariant.userType(), qMetaTypeId()); + + const QItemSelection &mil = milVariant.value(); + QCOMPARE(mil.count(), 5); +} + +void tst_qqmlitemmodels::modelIndexList() +{ + INIT_TEST_OBJECT("modelindexlist.qml", object); + TestModel model(10, 10); + + object->setModel(&model); + QCOMPARE(object->property("count").toInt(), 5); + + QVariant milVariant = object->property("modelIndexList"); + QCOMPARE(milVariant.type(), QVariant::UserType); + QCOMPARE(milVariant.userType(), qMetaTypeId()); + + const QModelIndexList &mil = milVariant.value(); + QCOMPARE(mil.count(), 2); + QCOMPARE(mil.at(0), model.index(3, 3)); + QCOMPARE(mil.at(1), model.index(4, 4)); +} + +#undef INIT_TEST_OBJECT + +QTEST_MAIN(tst_qqmlitemmodels) + +#include "tst_qqmlitemmodels.moc" -- cgit v1.2.3