TreeItem::TreeItem()
{
}
TreeItem
::TreeItem(QVariant name, TreeItem
*parent
) {
parentItem_ = parent;
itemName_ = name;
}
TreeItem::~TreeItem()
{
qDeleteAll(children_);
}
void TreeItem::insertChild(int row, TreeItem *item)
{
item->setParent(this);
children_.insert(row, item);
}
bool TreeItem::removeChildren(int position, int count)
{
if (position < 0 || position + count > children_.size())
return false;
for (int row = 0; row < count; ++row)
delete children_.takeAt(position);
return true;
}
TreeItem *TreeItem::child(int row) const
{
return children_.at(row);
}
TreeItem *TreeItem::parent()
{
return parentItem_;
}
void TreeItem::setParent(TreeItem* parent)
{
parentItem_ = parent;
}
int TreeItem::childrenCount() const
{
return children_.count();
}
int TreeItem::row() const
{
if (parentItem_)
return parentItem_->children_.indexOf(const_cast<TreeItem*>(this));
return 0;
}
void TreeItem
::setItemName(QVariant name
) {
itemName_ = name;
}
{
return itemName_;
}
//////////////////////////////////////////////////////////////////////////
// TreeModel
TreeModel::TreeModel()
{
treeRoot_ = makeShared<TreeItem>();
}
TreeModel::~TreeModel()
{
}
{
if (!hasIndex(row, column, parent))
TreeItem *parentItem = getItem(parent);
TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
}
{
if (!child.isValid())
TreeItem *childItem = static_cast<TreeItem*>(child.internalPointer());
TreeItem *parentItem = childItem->parent();
if (parentItem == treeRoot_.get())
return createIndex(parentItem->row(), 0, parentItem);
}
int TreeModel
::columnCount(const QModelIndex &parent
) const {
TreeItem* parentItem = getItem(parent);
return parentItem->propertyCount();
}
{
if (!index.isValid())
TreeItem* item = getItem(index);
switch (role)
{
case NameRole:
return item->itemName();
}
}
{
bool result = true;
TreeItem* item = getItem(index);
SP_CNRF(item);
switch (role)
{
case NameRole:
item->setItemName(value);
break;
default:
result = false;
break;
}
if (result)
{
emit dataChanged(index, index);
}
return result;
}
int TreeModel
::rowCount(const QModelIndex &parent
) const {
if (parent.column() > 0)
return 0;
TreeItem *parentItem = getItem(parent);
return parentItem->childrenCount();
}
bool TreeModel
::removeRows(int row,
int count,
const QModelIndex &parent
) {
TreeItem *parentItem = getItem(parent);
if (count > 0)
{
beginRemoveRows(parent, row, row + count - 1);
success = parentItem->removeChildren(row, count);
endRemoveRows();
}
return success;
}
void TreeModel
::insertItem(int position, TreeItem
*item,
const QModelIndex &parent
) {
TreeItem *parentItem = getItem(parent);
beginInsertRows(parent, position, position);
parentItem->insertChild(position, item);
endInsertRows();
}
TreeItem
* TreeModel
::getItem(const QModelIndex &index
) const{
if (index.isValid()) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item)
return item;
}
return treeRoot_.get();
}
{
auto treeItem = getItem(index);
if (treeItem)
return removeRows(treeItem->row(), 1, parent(index));
else
return false;
}
void TreeModel::init()
{
if (treeRoot_)
{
beginResetModel();
removeRows
(0, treeRoot_
->childrenCount
(),
QModelIndex());
endResetModel();
}
}
QHash<int, QByteArray> TreeModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
return roles;
}
TreeItem::TreeItem()
{
}
TreeItem::TreeItem(QVariant name, TreeItem *parent)
{
parentItem_ = parent;
itemName_ = name;
}
TreeItem::~TreeItem()
{
qDeleteAll(children_);
}
void TreeItem::insertChild(int row, TreeItem *item)
{
item->setParent(this);
children_.insert(row, item);
}
bool TreeItem::removeChildren(int position, int count)
{
if (position < 0 || position + count > children_.size())
return false;
for (int row = 0; row < count; ++row)
delete children_.takeAt(position);
return true;
}
TreeItem *TreeItem::child(int row) const
{
return children_.at(row);
}
TreeItem *TreeItem::parent()
{
return parentItem_;
}
void TreeItem::setParent(TreeItem* parent)
{
parentItem_ = parent;
}
int TreeItem::childrenCount() const
{
return children_.count();
}
int TreeItem::row() const
{
if (parentItem_)
return parentItem_->children_.indexOf(const_cast<TreeItem*>(this));
return 0;
}
void TreeItem::setItemName(QVariant name)
{
itemName_ = name;
}
QVariant TreeItem::itemName() const
{
return itemName_;
}
//////////////////////////////////////////////////////////////////////////
// TreeModel
TreeModel::TreeModel()
: QAbstractItemModel()
{
treeRoot_ = makeShared<TreeItem>();
}
TreeModel::~TreeModel()
{
}
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
TreeItem *parentItem = getItem(parent);
TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex TreeModel::parent(const QModelIndex &child) const
{
if (!child.isValid())
return QModelIndex();
TreeItem *childItem = static_cast<TreeItem*>(child.internalPointer());
TreeItem *parentItem = childItem->parent();
if (parentItem == treeRoot_.get())
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int TreeModel::columnCount(const QModelIndex &parent) const
{
TreeItem* parentItem = getItem(parent);
return parentItem->propertyCount();
}
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
TreeItem* item = getItem(index);
switch (role)
{
case NameRole:
return item->itemName();
}
return QVariant();
}
bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
bool result = true;
TreeItem* item = getItem(index);
SP_CNRF(item);
switch (role)
{
case NameRole:
item->setItemName(value);
break;
default:
result = false;
break;
}
if (result)
{
emit dataChanged(index, index);
}
return result;
}
int TreeModel::rowCount(const QModelIndex &parent) const
{
if (parent.column() > 0)
return 0;
TreeItem *parentItem = getItem(parent);
return parentItem->childrenCount();
}
bool TreeModel::removeRows(int row, int count, const QModelIndex &parent)
{
TreeItem *parentItem = getItem(parent);
if (count > 0)
{
beginRemoveRows(parent, row, row + count - 1);
success = parentItem->removeChildren(row, count);
endRemoveRows();
}
return success;
}
void TreeModel::insertItem(int position, TreeItem *item, const QModelIndex &parent)
{
TreeItem *parentItem = getItem(parent);
beginInsertRows(parent, position, position);
parentItem->insertChild(position, item);
endInsertRows();
}
TreeItem* TreeModel::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item)
return item;
}
return treeRoot_.get();
}
bool TreeModel::removeItem(const QModelIndex &index)
{
auto treeItem = getItem(index);
if (treeItem)
return removeRows(treeItem->row(), 1, parent(index));
else
return false;
}
void TreeModel::init()
{
if (treeRoot_)
{
beginResetModel();
removeRows(0, treeRoot_->childrenCount(), QModelIndex());
endResetModel();
}
}
QHash<int, QByteArray> TreeModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
return roles;
}
To copy to clipboard, switch view to plain text mode
Bookmarks