diff options
| author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2021-11-19 16:58:36 +0100 |
|---|---|---|
| committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2021-12-04 23:41:45 +0100 |
| commit | 18da655b77de70273e1aad7b9c2c4be74f5de91e (patch) | |
| tree | bf778b8277c8cbd0d094f425f26dd597b3efb8a0 /tests/manual/treeview/sidebyside/testmodel.cpp | |
| parent | fb954b07f2e35842fdd9df3bab39689570d046be (diff) | |
quick: add qquicktreeview
This patch adds TreeView to Qt Quick. It is more or less
a copy from the TreeView in Marketplace, but with some
modifications to make it more equal to the already
existing TableView, ListView, and GridView.
[ChangeLog][Item Views] A new view is added: TreeView
Fixes: QTBUG-61630
Change-Id: Ibb9d22cf9c9df021e77d03287872134c2682682a
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/manual/treeview/sidebyside/testmodel.cpp')
| -rw-r--r-- | tests/manual/treeview/sidebyside/testmodel.cpp | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/tests/manual/treeview/sidebyside/testmodel.cpp b/tests/manual/treeview/sidebyside/testmodel.cpp new file mode 100644 index 0000000000..8afa7db4f4 --- /dev/null +++ b/tests/manual/treeview/sidebyside/testmodel.cpp @@ -0,0 +1,200 @@ +/**************************************************************************** +** +** Copyright (C) 2021 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$ +** +****************************************************************************/ + +#include "testmodel.h" + +TreeItem::TreeItem(TreeItem *parent) + : m_parentItem(parent) +{} + +TreeItem::TreeItem(int rootRow) + : m_parentItem(nullptr) + , rootRow(rootRow) +{} + +TreeItem::~TreeItem() +{ + qDeleteAll(m_childItems); +} + +int TreeItem::row() const +{ + if (!m_parentItem) { + Q_ASSERT(rootRow != -1); + return rootRow; + } + return m_parentItem->m_childItems.indexOf(const_cast<TreeItem *>(this)); +} + +TestModel::TestModel(QObject *parent) + : QAbstractItemModel(parent) +{ + m_rootItems.append(new TreeItem(0)); + m_rootItems.append(new TreeItem(1)); + m_rootItems.append(new TreeItem(2)); + + for (int row = 0; row < m_rootItems.count(); ++row) { + const QString branchTag = QString::number(row); + for (int col = 0; col < m_columnCount; ++col) + m_rootItems[row]->m_entries << QVariant(QString("r:%1, c:%2, d:0, b:%3").arg(row).arg(col).arg(branchTag)); + createTreeRecursive(m_rootItems[row], 4, 1, 5, branchTag); + } +} + +void TestModel::createTreeRecursive(TreeItem *parentItem, int childCount, int currentDepth, int maxDepth, const QString &branchTag) +{ + for (int row = 0; row < childCount; ++row) { + auto childItem = new TreeItem(parentItem); + + if (currentDepth < maxDepth && row == 0) + createTreeRecursive(childItem, childCount, currentDepth + 1, maxDepth, branchTag); + + for (int col = 0; col < m_columnCount; ++col) + childItem->m_entries << QVariant(QString("r:%1, c:%2, d:%3, b:%4").arg(row).arg(col).arg(currentDepth).arg(branchTag)); + parentItem->m_childItems.append(childItem); + } +} + +TreeItem *TestModel::treeItem(const QModelIndex &index) const +{ + if (!index.isValid()) + return nullptr; + return static_cast<TreeItem *>(index.internalPointer()); +} + +int TestModel::rowCount(const QModelIndex &parent) const +{ + if (!parent.isValid()) + return m_rootItems.count(); + return treeItem(parent)->m_childItems.count(); +} + +int TestModel::columnCount(const QModelIndex &) const +{ + return m_columnCount; +} + +QVariant TestModel::data(const QModelIndex &index, int role) const +{ + switch (role) { + case Qt::SizeHintRole: + return QSize(100, 20); + case Qt::DisplayRole: { + if (!index.isValid()) + return QVariant("invalid index"); + TreeItem *item = treeItem(index); + return item->m_entries.at(index.column()); + break; } + default: + break; + } + return QVariant(); +} + +bool TestModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + Q_UNUSED(role) + if (!index.isValid()) + return false; + TreeItem *item = treeItem(index); + Q_ASSERT(item); + item->m_entries[index.column()] = value; + emit dataChanged(index, index); + return true; +} + +QModelIndex TestModel::index(int row, int column, const QModelIndex &parent) const +{ + if (!hasIndex(row, column, parent)) + return QModelIndex(); + if (!parent.isValid()) + return createIndex(row, column, m_rootItems[row]); + return createIndex(row, column, treeItem(parent)->m_childItems.at(row)); +} + +QModelIndex TestModel::parent(const QModelIndex &index) const +{ + if (!index.isValid()) + return QModelIndex(); + + TreeItem *item = treeItem(index); + TreeItem *parentItem = item->m_parentItem; + if (!parentItem) + return QModelIndex(); + + return createIndex(parentItem->row(), 0, parentItem); +} + +bool TestModel::insertRows(int position, int count, const QModelIndex &parent) +{ + if (!parent.isValid()) { + qWarning() << "Cannot insert rows on an invalid parent!"; + return false; + } + + beginInsertRows(parent, position, position + count - 1); + TreeItem *parentItem = treeItem(parent); + + for (int row = 0; row < count; ++row) { + auto newChildItem = new TreeItem(parentItem); + for (int col = 0; col < m_columnCount; ++col) + newChildItem->m_entries << QVariant(QString("(inserted at %1, %2)").arg(position + row).arg(col)); + parentItem->m_childItems.insert(position + row, newChildItem); + } + + endInsertRows(); + return true; +} + +bool TestModel::removeRows(int position, int count, const QModelIndex &parent) +{ + if (!parent.isValid()) { + qWarning() << "Cannot remove rows on an invalid parent!"; + return false; + } + + beginRemoveRows(parent, position, position + count - 1); + + TreeItem *parentItem = treeItem(parent); + parentItem->m_childItems.remove(position, count); + + endRemoveRows(); + return true; +} + |
