1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "testmodel.h"
TreeItem::TreeItem(TreeItem *parent)
: m_parentItem(parent)
{}
TreeItem::~TreeItem()
{
qDeleteAll(m_childItems);
}
int TreeItem::row() const
{
if (!m_parentItem)
return 0;
return m_parentItem->m_childItems.indexOf(const_cast<TreeItem *>(this));
}
TestModel::TestModel(QObject *parent)
: QAbstractItemModel(parent)
{
m_rootItem.reset(new TreeItem());
for (int col = 0; col < m_columnCount; ++col)
m_rootItem.data()->m_entries << QVariant(QString("0, %1").arg(col));
createTreeRecursive(m_rootItem.data(), 4, 0, 4);
}
void TestModel::createTreeRecursive(TreeItem *item, int childCount, int currentDepth, int maxDepth)
{
for (int row = 0; row < childCount; ++row) {
auto childItem = new TreeItem(item);
for (int col = 0; col < m_columnCount; ++col)
childItem->m_entries << QVariant(QString("%1, %2").arg(row).arg(col));
item->m_childItems.append(childItem);
if (currentDepth < maxDepth && row == childCount - 1)
createTreeRecursive(childItem, childCount, currentDepth + 1, maxDepth);
}
}
TreeItem *TestModel::treeItem(const QModelIndex &index) const
{
if (!index.isValid())
return m_rootItem.data();
return static_cast<TreeItem *>(index.internalPointer());
}
int TestModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return 1; // root of the tree
if (parent.column() == 0)
return treeItem(parent)->m_childItems.size();
return 0;
}
int TestModel::columnCount(const QModelIndex &parent) const
{
return parent.column() == 0 ? m_columnCount : 0;
}
QVariant TestModel::data(const QModelIndex &index, int role) const
{
Q_UNUSED(role)
if (!index.isValid())
return QVariant();
TreeItem *item = treeItem(TestModel::index(index.row(), 0, index.parent()));
return item->m_entries.at(index.column());
}
bool TestModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
Q_UNUSED(role)
if (!index.isValid())
return false;
TreeItem *item = treeItem(TestModel::index(index.row(), 0, index.parent()));
item->m_entries[index.column()] = value;
emit dataChanged(index, index);
return true;
}
QModelIndex TestModel::index(int row, int column, const QModelIndex &parent) const
{
Q_UNUSED(column)
if (!hasIndex(row, column, parent))
return QModelIndex();
if (column != 0)
return createIndex(row, column);
if (!parent.isValid())
return createIndex(0, 0, m_rootItem.data());
return createIndex(row, 0, treeItem(parent)->m_childItems.at(row));
}
QModelIndex TestModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
if (index.column() != 0)
return QModelIndex();
TreeItem *parentItem = treeItem(index)->m_parentItem;
if (!parentItem) {
qWarning() << "failed to resolve parent item for:" << index;
return QModelIndex();
}
return createIndex(parentItem->row(), 0, parentItem);
}
bool TestModel::insertRows(int position, int rows, const QModelIndex &parent)
{
if (!parent.isValid()) {
qWarning() << "Cannot insert rows on an invalid parent!";
return false;
}
beginInsertRows(parent, position, position + rows - 1);
TreeItem *parentItem = treeItem(parent);
for (int row = 0; row < rows; ++row) {
auto newChildItem = new TreeItem(parentItem);
for (int col = 0; col < m_columnCount; ++col)
newChildItem->m_entries << QVariant(QString("%1, %2 (inserted)").arg(position + row).arg(col));
parentItem->m_childItems.insert(position + row, newChildItem);
}
endInsertRows();
return true;
}
void insertColumnsRecursive(TreeItem *item, int cols)
{
int pos = item->m_entries.size();
for (int col = 0; col < cols; col++)
item->m_entries << QVariant(QString("%1, %2 (inserted)").arg(pos + col).arg(col));
for (auto child : item->m_childItems)
insertColumnsRecursive(child, cols);
}
bool TestModel::insertColumns(int position, int cols, const QModelIndex &parent)
{
if (!parent.isValid()) {
qWarning() << "Cannot insert columns on an invalid parent!";
return false;
}
beginInsertColumns(parent, position, position + cols - 1);
TreeItem *parentItem = treeItem(parent);
TreeItem *item = m_rootItem.data();
insertColumnsRecursive(item, cols);
m_columnCount += cols;
endInsertColumns();
return true;
}
|