From 198f6f7c0472432bfa9f1b6223e771f8d6ae8cef Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9dric=20Aguerre?= Date: Thu, 30 Oct 2014 09:46:56 +0100 Subject: [PATCH] [MEDOP] Clean workspace --- src/TreeData/TreeItem.cxx | 49 +++++++++++++++++++++++++++++++++++--- src/TreeData/TreeItem.hxx | 7 +++--- src/TreeData/TreeModel.cxx | 9 ++++++- src/TreeData/TreeModel.hxx | 3 ++- 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/TreeData/TreeItem.cxx b/src/TreeData/TreeItem.cxx index 8e6dbce63..9a0e66b47 100644 --- a/src/TreeData/TreeItem.cxx +++ b/src/TreeData/TreeItem.cxx @@ -47,7 +47,7 @@ void TreeItem::initialize(const QString &nameId, //when creating the root item). if ( parent != NULL ) { this->associateToModel(parent->associatedModel()); - } + } } TreeItem::~TreeItem() @@ -128,13 +128,13 @@ void TreeItem::appendChild(const QString &nameId, folder = new TreeItem(folderNameId, folderColumnValues, this); this->appendChild(folder); } - + // We create the relative path of the next iteration (delete the // first folder path). QStringList nextRelativePath; for (int i = 1; i < relativePath.size(); ++i) nextRelativePath << relativePath[i]; - + folder->appendChild(nameId, columnValues, nextRelativePath); } @@ -154,6 +154,49 @@ void TreeItem::appendChild(TreeItem *item) model->endInsertRows(); } +/*! + * This removes the specified child to this item. This item is the + * direct parent of the specified child. + */ +void TreeItem::removeChild(TreeItem *item) +{ + TreeModel * model = this->associatedModel(); + + int position = this->childCount(); + model->beginRemoveRows(this->modelIndex(), position, position); + _childItems.removeOne(item); + _childItemsMapById.remove(item->nameId()); + _childItemsMapByLabel.remove(item->data(0).toString()); + model->endRemoveRows(); +} + +void TreeItem::removeChild(DataObject * dataObject, const QStringList &relativePath) { + if ( relativePath.isEmpty() ) { + // It is a direct child => just remove it. + QString nameId = QString(dataObject->getNameId().c_str()); + TreeItem * child = this->childById(nameId); + if (child != NULL) + this->removeChild(child); + return; + } + + // The child is embedded in a sub-folder. + // We first check if the sub-folder already exist: + TreeItem * folder = this->childByLabel(relativePath[0]); + if ( folder == NULL ) + return; + + // Go down into subfolder, if any. + QStringList nextRelativePath; + for (int i = 1; i < relativePath.size(); ++i) + nextRelativePath << relativePath[i]; + + folder->removeChild(dataObject, nextRelativePath); + + if (folder->childCount() == 0) + this->removeChild(folder); +} + /*! * The child() function returns the child that corresponds to the * specified row number in the item's list of child items. diff --git a/src/TreeData/TreeItem.hxx b/src/TreeData/TreeItem.hxx index d60e560fe..c0a7d4077 100644 --- a/src/TreeData/TreeItem.hxx +++ b/src/TreeData/TreeItem.hxx @@ -51,8 +51,10 @@ class TREEDATA_EXPORT TreeItem const QVector &columnValues, const QStringList &relativePath=QStringList()); - - + void removeChild(TreeItem * child); + void removeChild(DataObject * dataObject, + const QStringList &relativePath=QStringList()); + TreeItem *child(int row); TreeItem *childById(const QString &nameId); TreeItem *childByLabel(const QString &label); @@ -62,7 +64,6 @@ class TREEDATA_EXPORT TreeItem QVariant data(int column) const; bool setData(int column, const QVariant &value); - private: void initialize(const QString &nameId, const QVector &columnValues, diff --git a/src/TreeData/TreeModel.cxx b/src/TreeData/TreeModel.cxx index 12510fd40..0c5e6422b 100644 --- a/src/TreeData/TreeModel.cxx +++ b/src/TreeData/TreeModel.cxx @@ -186,7 +186,7 @@ bool TreeModel::setHeaderData(int section, Qt::Orientation orientation, // This part is a specific behavior to get a TreeModel that can // organize itself the tree hierarchy using data provided in a // filesystem-like format: -// +// // data="a/b/c" ==> creation/filling of the hierarchy a->b->c // The "folder" categories are unique whereas the leaves may exists // in multiple instances. @@ -208,3 +208,10 @@ bool TreeModel::addData(DataObject * dataObject, const QStringList &path) { rootItem->appendChild(dataObject, path); return true; } + +bool TreeModel::removeData(DataObject * dataObject) { + QStringList path = QString(dataObject->getPath().c_str()).split(DataObject::pathsep.c_str()); + TreeItem * rootItem = this->getItem(); + rootItem->removeChild(dataObject, path); + return true; +} diff --git a/src/TreeData/TreeModel.hxx b/src/TreeData/TreeModel.hxx index 9cb262e08..65bafb687 100644 --- a/src/TreeData/TreeModel.hxx +++ b/src/TreeData/TreeModel.hxx @@ -83,7 +83,7 @@ public: // This part is a specific behavior to get a TreeModel that can // organize itself the tree hierarchy using data provided in a // filesystem-like format: - // + // // data="a/b/c" ==> creation/filling of the hierarchy a->b->c // The "folder" categories are unique whereas the leaves may exists // in multiple instances. @@ -93,6 +93,7 @@ public: bool addData(DataObject * dataObject, const QStringList &path); // TODO: We should implement the delete and the update fucntions + bool removeData(DataObject * dataObject); // This part contains helper functions for general purposes TreeItem * getRootItem(); -- 2.39.2