X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FTreeData%2FTreeItem.cxx;h=6459cfa52c333136ed2e87cd71f121a611d89ca4;hb=d0b0c10e5649e0338079df4a53cf95044792d989;hp=26394f61f92ac00ae0985c18920e3b24239816d5;hpb=6f30a39cf0e334b55338328e63c320413f332dba;p=modules%2Fgui.git diff --git a/src/TreeData/TreeItem.cxx b/src/TreeData/TreeItem.cxx index 26394f61f..6459cfa52 100644 --- a/src/TreeData/TreeItem.cxx +++ b/src/TreeData/TreeItem.cxx @@ -1,9 +1,9 @@ -// Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE +// Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either -// version 2.1 of the License. +// version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -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.