diff options
author | Ben Boeckel <MathStuf@gmail.com> | 2009-05-21 13:32:32 -0400 |
---|---|---|
committer | Ben Boeckel <MathStuf@gmail.com> | 2009-05-21 13:32:32 -0400 |
commit | a619847c764d510e52bdfd7dc0c25ab3b0c9d77b (patch) | |
tree | b3bf0dd793c1500aec82a3919276f8626f909c96 /sigmodr/tree/TreeModel.cpp | |
parent | 10a70d1e4844338a443a5693d7df16e14144677f (diff) | |
download | sigen-a619847c764d510e52bdfd7dc0c25ab3b0c9d77b.tar.gz sigen-a619847c764d510e52bdfd7dc0c25ab3b0c9d77b.tar.xz sigen-a619847c764d510e52bdfd7dc0c25ab3b0c9d77b.zip |
Implement adding and removing rows from the model
Diffstat (limited to 'sigmodr/tree/TreeModel.cpp')
-rw-r--r-- | sigmodr/tree/TreeModel.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/sigmodr/tree/TreeModel.cpp b/sigmodr/tree/TreeModel.cpp index 024b27e9..1be6d00d 100644 --- a/sigmodr/tree/TreeModel.cpp +++ b/sigmodr/tree/TreeModel.cpp @@ -106,6 +106,42 @@ bool TreeModel::hasChildren(const QModelIndex& parent) const return (0 < static_cast<TreeItem*>(parent.internalPointer())->childCount()); } +bool TreeModel::insertRows(const int row, const int count, const QModelIndex& parent) +{ + if (!parent.isValid()) + return false; + TreeItem* item = static_cast<TreeItem*>(parent.internalPointer()); + if (!item->canAddTo() || (item == m_root) || (row != item->childCount())) + return false; + beginInsertRows(parent, row, row + count - 1); + for (int i = 0; i < count; ++i) + item->addChild(); + endInsertRows(); + emit(layoutAboutToBeChanged()); + emit(layoutChanged()); + return true; +} + +bool TreeModel::removeRows(const int row, const int count, const QModelIndex& parent) +{ + if (!parent.isValid()) + return false; + TreeItem* item = static_cast<TreeItem*>(parent.internalPointer()); + if (item == m_root) + return false; + for (int i = 0; i < count; ++i) + { + TreeItem* child = item->childAt(row + i); + if (!child || !child->canRemove()) + return false; + } + beginRemoveRows(parent, row, row + count - 1); + for (int i = 0; i < count; ++i) + item->removeChild(row + i); + endRemoveRows(); + return true; +} + int TreeModel::rowCount(const QModelIndex& parent) const { if (!parent.isValid()) |