struct IndexData{
IndexData *parentIndex;
Entity *parent;
Entity *item;
int row;
};
struct IndexData{
IndexData *parentIndex;
Entity *parent;
Entity *item;
int row;
};
To copy to clipboard, switch view to plain text mode
#include "entitymodel.h"
#include <QDebug>
EntityModel
::EntityModel(Entity
*root,
QObject *parent
) :{
m_root = root;
m_rootIndexData = new IndexData;
m_rootIndexData->parent = 0;
m_rootIndexData->parentIndex = 0;
m_rootIndexData->row = 0;
m_rootIndexData->item = root;
}
{
if (!hasIndex(row, column, parent)) {
}
IndexData *parentData = indexData(parent);
Entity *child = parentData->item->child(row);
if (child)
return createIndex(row, column, createIndexData(parentData, child, row));
}
{
if (!child.isValid())
IndexData *data = indexData(child);
if (data->parent == m_root) {
}
if (data->parentIndex == 0)
return createIndex(data->parentIndex->row, 0, data->parentIndex);
}
int EntityModel
::rowCount(const QModelIndex &parent
) const {
IndexData *data = indexData(parent);
return data->item->childrenCount();
}
int EntityModel
::columnCount(const QModelIndex &parent
) const {
return 2;
}
{
if (!index.isValid())
if (role != Qt::DisplayRole)
Entity *item = indexData(index)->item;
if (index.column() == 0) {
return item->name();
} else if (index.column() == 1) {
return "Entity";
} else {
qCritical() << "Invalid column";
return "";
}
}
EntityModel
::IndexData *EntityModel
::indexData(const QModelIndex &index
) const{
if (index.isValid()) {
IndexData *data = static_cast<IndexData*>(index.internalPointer());
if (data)
return data;
}
return m_rootIndexData;
}
EntityModel::IndexData *EntityModel::createIndexData(IndexData *parent, Entity *item, int row) const
{
if (!m_indexData.contains(quintptr(parent->item))) {
QHash<quintptr, IndexData*>* hash = new QHash<quintptr, IndexData*>();
m_indexData.insert(quintptr(parent->item), hash);
}
if (!m_indexData[quintptr(parent->item)]->contains(quintptr(item))) {
IndexData *data = new IndexData;
data->parent = parent->item;
data->item = item;
data->parentIndex = parent;
data->row = row;
m_indexData[quintptr(parent->item)]->insert(quintptr(item), data);
}
return m_indexData[quintptr(parent->item)]->value(quintptr(item));
}
#include "entitymodel.h"
#include <QDebug>
EntityModel::EntityModel(Entity *root, QObject *parent) :
QAbstractItemModel(parent)
{
m_root = root;
m_rootIndexData = new IndexData;
m_rootIndexData->parent = 0;
m_rootIndexData->parentIndex = 0;
m_rootIndexData->row = 0;
m_rootIndexData->item = root;
}
QModelIndex EntityModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent)) {
return QModelIndex();
}
IndexData *parentData = indexData(parent);
Entity *child = parentData->item->child(row);
if (child)
return createIndex(row, column, createIndexData(parentData, child, row));
return QModelIndex();
}
QModelIndex EntityModel::parent(const QModelIndex &child) const
{
if (!child.isValid())
return QModelIndex();
IndexData *data = indexData(child);
if (data->parent == m_root) {
return QModelIndex();
}
if (data->parentIndex == 0)
return QModelIndex();
return createIndex(data->parentIndex->row, 0, data->parentIndex);
}
int EntityModel::rowCount(const QModelIndex &parent) const
{
IndexData *data = indexData(parent);
return data->item->childrenCount();
}
int EntityModel::columnCount(const QModelIndex &parent) const
{
return 2;
}
QVariant EntityModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
Entity *item = indexData(index)->item;
if (index.column() == 0) {
return item->name();
} else if (index.column() == 1) {
return "Entity";
} else {
qCritical() << "Invalid column";
return "";
}
}
EntityModel::IndexData *EntityModel::indexData(const QModelIndex &index) const
{
if (index.isValid()) {
IndexData *data = static_cast<IndexData*>(index.internalPointer());
if (data)
return data;
}
return m_rootIndexData;
}
EntityModel::IndexData *EntityModel::createIndexData(IndexData *parent, Entity *item, int row) const
{
if (!m_indexData.contains(quintptr(parent->item))) {
QHash<quintptr, IndexData*>* hash = new QHash<quintptr, IndexData*>();
m_indexData.insert(quintptr(parent->item), hash);
}
if (!m_indexData[quintptr(parent->item)]->contains(quintptr(item))) {
IndexData *data = new IndexData;
data->parent = parent->item;
data->item = item;
data->parentIndex = parent;
data->row = row;
m_indexData[quintptr(parent->item)]->insert(quintptr(item), data);
}
return m_indexData[quintptr(parent->item)]->value(quintptr(item));
}
To copy to clipboard, switch view to plain text mode
Bookmarks