//when creating the root item).
if ( parent != NULL ) {
this->associateToModel(parent->associatedModel());
- }
+ }
}
TreeItem::~TreeItem()
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);
}
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.
const QVector<QVariant> &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);
QVariant data(int column) const;
bool setData(int column, const QVariant &value);
-
private:
void initialize(const QString &nameId,
const QVector<QVariant> &columnValues,
// 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.
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;
+}
// 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.
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();