diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2018-08-21 12:44:12 +0200 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2018-09-27 06:45:00 +0000 |
commit | 34948d35f6bf4fa4acb46e60833b3b3f9d3ace46 (patch) | |
tree | afabecffdfee2694152a0a47b3ab208054117e93 /tests | |
parent | 1f758eb7d14a69fa39d6e9463b5176db5ab504c0 (diff) |
Manual test, TableView, abstracttablemodel: add more logic to the test
Add buttons for adding and removing rows and columns
Change-Id: I8d391db4f61ff4ec4273adc7134bd30ffc73fa3c
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'tests')
4 files changed, 238 insertions, 18 deletions
diff --git a/tests/manual/tableview/abstracttablemodel/Button.qml b/tests/manual/tableview/abstracttablemodel/Button.qml new file mode 100644 index 0000000000..2d4dcde80d --- /dev/null +++ b/tests/manual/tableview/abstracttablemodel/Button.qml @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.12 + +Rectangle { + width: 100 + height: 40 + border.width: 1 + color: "lightgreen" + + property string text + signal clicked + + Text { + anchors.centerIn: parent + text: parent.text + } + + MouseArea { + anchors.fill: parent + onClicked: parent.clicked() + } +} diff --git a/tests/manual/tableview/abstracttablemodel/abstracttablemodel.pro b/tests/manual/tableview/abstracttablemodel/abstracttablemodel.pro index ade49c33a2..fee8edc6c8 100644 --- a/tests/manual/tableview/abstracttablemodel/abstracttablemodel.pro +++ b/tests/manual/tableview/abstracttablemodel/abstracttablemodel.pro @@ -2,7 +2,7 @@ TEMPLATE = app TARGET = tableview_abstracttablemodel QT += qml quick SOURCES += main.cpp -RESOURCES += main.qml +RESOURCES += main.qml Button.qml # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin diff --git a/tests/manual/tableview/abstracttablemodel/main.cpp b/tests/manual/tableview/abstracttablemodel/main.cpp index 6e300184be..4fba938ee9 100644 --- a/tests/manual/tableview/abstracttablemodel/main.cpp +++ b/tests/manual/tableview/abstracttablemodel/main.cpp @@ -41,6 +41,9 @@ #include <QQmlApplicationEngine> #include <QAbstractTableModel> #include <QSet> +#include <QDebug> + +typedef QPair<QString, bool> CellData; class TestTableModel : public QAbstractTableModel { @@ -52,16 +55,38 @@ public: TestTableModel(QObject *parent = nullptr) : QAbstractTableModel(parent) { } int rowCount(const QModelIndex & = QModelIndex()) const override { return m_rows; } - void setRowCount(int count) { beginResetModel(); m_rows = count; emit rowCountChanged(); endResetModel(); } + void setRowCount(int count) { + beginResetModel(); + m_rows = count; + rebuildModel(); + emit rowCountChanged(); + endResetModel(); + } int columnCount(const QModelIndex & = QModelIndex()) const override { return m_cols; } - void setColumnCount(int count) { beginResetModel(); m_cols = count; emit columnCountChanged(); endResetModel(); } + void setColumnCount(int count) { + beginResetModel(); + m_cols = count; + rebuildModel(); + emit columnCountChanged(); + endResetModel(); + } int indexValue(const QModelIndex &index) const { return index.row() + (index.column() * rowCount()); } + void rebuildModel() + { + m_modelData = QVector<QVector<CellData>>(m_cols); + for (int x = 0; x < m_cols; ++x) { + m_modelData[x] = QVector<CellData>(m_rows); + for (int y = 0; y < m_rows; ++y) + m_modelData[x][y] = qMakePair(QStringLiteral("white"), false); + } + } + QVariant data(const QModelIndex &index, int role) const override { if (!index.isValid()) @@ -69,9 +94,9 @@ public: switch (role) { case Qt::DisplayRole: - return QString("%1, %2").arg(index.column()).arg(index.row()); + return m_modelData[index.column()][index.row()].first; case Qt::CheckStateRole: - return m_checkedCells.contains(indexValue(index)); + return m_modelData[index.column()][index.row()].second; default: return QVariant(); } @@ -85,15 +110,12 @@ public: if (role != Qt::CheckStateRole) return false; - int i = indexValue(index); bool checked = value.toBool(); - if (checked == m_checkedCells.contains(i)) + auto &cellData = m_modelData[index.column()][index.row()]; + if (checked == cellData.second) return false; - if (checked) - m_checkedCells.insert(i); - else - m_checkedCells.remove(i); + cellData.second = checked; emit dataChanged(index, index, {role}); return true; @@ -107,6 +129,106 @@ public: }; } + Q_INVOKABLE void insertRows(int row, int count) + { + insertRows(row, count, QModelIndex()); + } + + Q_INVOKABLE void removeRows(int row, int count) + { + removeRows(row, count, QModelIndex()); + } + + Q_INVOKABLE void insertColumns(int column, int count) + { + insertColumns(column, count, QModelIndex()); + } + + Q_INVOKABLE void removeColumns(int column, int count) + { + removeColumns(column, count, QModelIndex()); + } + + bool insertRows(int row, int count, const QModelIndex &) override + { + if (row > m_rows) + return false; + + beginInsertRows(QModelIndex(), row, row + count - 1); + + m_rows += count; + + for (int y = 0; y < count; ++y) { + for (int x = 0; x < m_cols; ++x) + m_modelData[x].insert(row, qMakePair(QStringLiteral("lightgreen"), false)); + } + + endInsertRows(); + + return true; + } + + bool removeRows(int row, int count, const QModelIndex &) override + { + if (row + count > m_rows) + count = m_rows - row; + if (count < 1) + return false; + + beginRemoveRows(QModelIndex(), row, row + count - 1); + + m_rows -= count; + + for (int y = 0; y < count; ++y) { + for (int x = 0; x < m_cols; ++x) + m_modelData[x].remove(row); + } + + endRemoveRows(); + + return true; + } + + bool insertColumns(int column, int count, const QModelIndex &) override + { + if (column > m_cols) + return false; + + beginInsertColumns(QModelIndex(), column, column + count - 1); + + m_cols += count; + + for (int x = 0; x < count; ++x) { + const int c = column + x; + m_modelData.insert(c, QVector<CellData>(m_rows)); + for (int y = 0; y < m_rows; ++y) + m_modelData[c][y] = qMakePair(QStringLiteral("lightblue"), false); + } + + endInsertColumns(); + + return true; + } + + bool removeColumns(int column, int count, const QModelIndex &) override + { + if (column + count > m_cols) + count = m_cols - column; + if (count < 1) + return false; + + beginRemoveColumns(QModelIndex(), column, column + count - 1); + + m_cols -= count; + + for (int x = 0; x < count; ++x) + m_modelData.remove(column + x); + + endRemoveColumns(); + + return true; + } + signals: void rowCountChanged(); void columnCountChanged(); @@ -115,7 +237,7 @@ private: int m_rows = 0; int m_cols = 0; - QSet<int> m_checkedCells; + QVector<QVector<CellData>> m_modelData; }; int main(int argc, char *argv[]) diff --git a/tests/manual/tableview/abstracttablemodel/main.qml b/tests/manual/tableview/abstracttablemodel/main.qml index 87e5ee0aa8..52967a1a0d 100644 --- a/tests/manual/tableview/abstracttablemodel/main.qml +++ b/tests/manual/tableview/abstracttablemodel/main.qml @@ -48,10 +48,13 @@ Window { height: 480 visible: true + property int selectedX: -1 + property int selectedY: -1 + TestTableModel { id: tableModel rowCount: 200 - columnCount: 200 + columnCount: 5000 } Rectangle { @@ -59,10 +62,42 @@ Window { anchors.margins: 10 color: "darkgray" + Row { + id: menu + x: 2 + y: 2 + spacing: 1 + Button { + text: "Add row" + onClicked: tableModel.insertRows(selectedY, 1) + } + Button { + text: "Remove row" + onClicked: tableModel.removeRows(selectedY, 1) + } + Button { + text: "Add column" + onClicked: tableModel.insertColumns(selectedX, 1) + } + Button { + text: "Remove column" + onClicked: tableModel.removeColumns(selectedX, 1) + } + } + Text { + anchors.right: parent.right + anchors.top: parent.top + anchors.margins: 2 + text: "x:" + selectedX + ", y:" + selectedY + } + TableView { id: tableView - anchors.fill: parent - anchors.margins: 1 + anchors.left: parent.left + anchors.right: parent.right + anchors.top: menu.bottom + anchors.bottom: parent.bottom + anchors.margins: 2 clip: true model: tableModel @@ -77,16 +112,19 @@ Window { id: delegate implicitWidth: 100 implicitHeight: 50 - color: checked ? "lightblue" : "white" + color: display + border.width: row === selectedY && column == selectedX ? 2 : 0 + border.color: "darkgreen" Text { anchors.fill: parent - text: display + text: column + ", " + row MouseArea { anchors.fill: parent onClicked: { - checked = !checked + selectedX = column + selectedY = row } } } |