From a619847c764d510e52bdfd7dc0c25ab3b0c9d77b Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Thu, 21 May 2009 13:32:32 -0400 Subject: Implement adding and removing rows from the model --- sigmodr/tree/TreeModel.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'sigmodr/tree/TreeModel.cpp') 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(parent.internalPointer())->childCount()); } +bool TreeModel::insertRows(const int row, const int count, const QModelIndex& parent) +{ + if (!parent.isValid()) + return false; + TreeItem* item = static_cast(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(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()) -- cgit