diff --git a/plugins/gui/include/gui/context_manager_widget/models/context_tree_model.h b/plugins/gui/include/gui/context_manager_widget/models/context_tree_model.h index e912a088f5e..756fe944257 100644 --- a/plugins/gui/include/gui/context_manager_widget/models/context_tree_model.h +++ b/plugins/gui/include/gui/context_manager_widget/models/context_tree_model.h @@ -112,17 +112,18 @@ namespace hal QVariant data(const QModelIndex& inddex, int role = Qt::DisplayRole) const override; ///@} + BaseTreeItem* getDirectory(u32 directoryId) const; + + BaseTreeItem* getContext(u32 contextId) const; + /** * Adds a directory to the model. * * @param name - The name to the directory. - * @param parent - The Parent of the directory. + * @param parentItem - The parent of the directory. + * @param id - The id of the directory. */ - ContextDirectory* addDirectory(QString name, BaseTreeItem* parent = nullptr, u32 id = 0); - - BaseTreeItem* getDirectory(u32 directoryId) const; - - BaseTreeItem* getContext(u32 contextId) const; + ContextDirectory* addDirectory(QString name, BaseTreeItem* parentItem, u32 id); /** * Adds a given GraphContext to the model. @@ -199,20 +200,31 @@ namespace hal ContextTreeItem* getCurrentDirectory(); /** - * Sets the MinDirectoryId. - * - * @param u32 - id to set MinDirectoryId to. + * Removes a tree item and inserts it under a new parent into a specific row. + * + * @param itemToMove - The tree item to be moved. + * @param newParent - The parent item, under which itemToMove is placed. + * @param row - The row in newParent, where itemToMove is inserted. + * If -1, then itemToMove is instead just appended to newParent. + * @return True, if the operation succeded. False, if not. */ - void setMinDirectoryId(u32 id_) { mMinDirectoryId = id_; } + bool moveItem(ContextTreeItem* itemToMove, BaseTreeItem* newParent, int row = -1); /** - * Get MinDirectoryId. - * - * @return MinDIrectoryId. + * Returns the ids of all direct child directories of a given parent directory. + * + * @param directoyId - The id of the parent directory. + * @return List of IDs of all directories, that are ordered directly under the parent directory. */ - u32 minDirectoryId() const { return mMinDirectoryId; } + std::vector getChildDirectoriesOf(u32 directoryId); - bool moveItem(ContextTreeItem* itemToMove, BaseTreeItem* newParent, int row = -1); + /** + * Returns the ids of all direct child contexts of a given parent directory. + * + * @param directoyId - The id of the parent directory. + * @return List of IDs of all contexts, that are ordered directly under the parent directory. + */ + std::vector getChildContextsOf(u32 directoryId); Q_SIGNALS: void directoryCreatedSignal(ContextTreeItem* item); @@ -223,9 +235,7 @@ namespace hal std::map mContextMap; QVector mContextList; QVector mDirectoryList; - u32 mMinDirectoryId; void dumpRecursion(ContextTreeItem* parent = nullptr, int level = 0) const; - }; } // namespace hal diff --git a/plugins/gui/include/gui/gui_api/gui_api.h b/plugins/gui/include/gui/gui_api/gui_api.h index 062ef464122..1e49a56037f 100644 --- a/plugins/gui/include/gui/gui_api/gui_api.h +++ b/plugins/gui/include/gui/gui_api/gui_api.h @@ -71,6 +71,9 @@ namespace hal static void deleteDirectory(u32 id); static void moveView(u32 viewId, std::optional destinationDirectoryId, std::optional row); static void moveDirectory(u32 directoryId, std::optional destinationDirectoryId, std::optional row); + + static std::optional> getChildDirectories(u32 directoryId); + static std::optional> getChildViews(u32 directoryId); }; } diff --git a/plugins/gui/src/context_manager_widget/models/context_tree_model.cpp b/plugins/gui/src/context_manager_widget/models/context_tree_model.cpp index 46afb5d9c5f..a6c67300c19 100644 --- a/plugins/gui/src/context_manager_widget/models/context_tree_model.cpp +++ b/plugins/gui/src/context_manager_widget/models/context_tree_model.cpp @@ -126,7 +126,7 @@ namespace hal return mContext != nullptr; } - ContextTreeModel::ContextTreeModel(QObject* parent) : BaseTreeModel(parent), mCurrentDirectory(nullptr), mMinDirectoryId(std::numeric_limits::max()) + ContextTreeModel::ContextTreeModel(QObject* parent) : BaseTreeModel(parent), mCurrentDirectory(nullptr) { setHeaderLabels(QStringList() << "View Name" << "ID" << "Timestamp"); } @@ -226,15 +226,8 @@ namespace hal return nullptr; } - ContextDirectory* ContextTreeModel::addDirectory(QString name, BaseTreeItem *parent, u32 id) + ContextDirectory* ContextTreeModel::addDirectory(QString name, BaseTreeItem *parentItem, u32 id) { - if(id == 0) - id = --mMinDirectoryId; - else if (id < mMinDirectoryId) - mMinDirectoryId = id; - - BaseTreeItem* parentItem = parent; - if (!parentItem) parentItem = mCurrentDirectory; if (!parentItem) @@ -455,4 +448,42 @@ namespace hal for (BaseTreeItem* cld : bti->getChildren()) dumpRecursion(dynamic_cast(cld), level+1); } + + std::vector ContextTreeModel::getChildDirectoriesOf(u32 directoryId) + { + BaseTreeItem* directoryItem = getDirectory(directoryId); + if(!directoryItem) + directoryItem = getRootItem(); + + QList children = directoryItem->getChildren(); + std::vector ids; + + for(BaseTreeItem* child : children) + { + ContextTreeItem* cti = dynamic_cast(child); + if(cti->isDirectory()) + ids.push_back(cti->getId()); + } + + return ids; + } + + std::vector ContextTreeModel::getChildContextsOf(u32 directoryId) + { + BaseTreeItem* directoryItem = getDirectory(directoryId); + if(!directoryItem) + directoryItem = getRootItem(); + + QList children = directoryItem->getChildren(); + std::vector ids; + + for(BaseTreeItem* child : children) + { + ContextTreeItem* cti = dynamic_cast(child); + if(cti->isContext()) + ids.push_back(cti->getId()); + } + + return ids; + } } diff --git a/plugins/gui/src/graph_widget/graph_context_manager.cpp b/plugins/gui/src/graph_widget/graph_context_manager.cpp index dba64574e01..2e7c97d34c2 100644 --- a/plugins/gui/src/graph_widget/graph_context_manager.cpp +++ b/plugins/gui/src/graph_widget/graph_context_manager.cpp @@ -60,7 +60,7 @@ namespace hal ContextDirectory* GraphContextManager::createNewDirectory(const QString& name, u32 parentId) { - ContextDirectory* contextDir = mContextTreeModel->addDirectory(name, mContextTreeModel->getDirectory(parentId)); + ContextDirectory* contextDir = mContextTreeModel->addDirectory(name, mContextTreeModel->getDirectory(parentId), ++mMaxContextId); return contextDir; } @@ -658,9 +658,11 @@ namespace hal BaseTreeItem* dirParent = mContextTreeModel->getRootItem(); - if (dirParentId != 0) { + if (dirParentId != 0) dirParent = mContextTreeModel->getDirectory(dirParentId); - } + + if (dirId < 0 || dirId > 0x7FFFFFFF) + dirId = ++mMaxContextId; mContextTreeModel->addDirectory(dirName, dirParent, dirId); } diff --git a/plugins/gui/src/gui_api/gui_api.cpp b/plugins/gui/src/gui_api/gui_api.cpp index a9937851b66..06524c0409f 100644 --- a/plugins/gui/src/gui_api/gui_api.cpp +++ b/plugins/gui/src/gui_api/gui_api.cpp @@ -1014,4 +1014,22 @@ namespace hal act->setObject(uao); act->exec(); } + + std::optional> GuiApiClasses::View::getChildDirectories(u32 directoryId) + { + ContextTreeItem* directoryItem = dynamic_cast(gGraphContextManager->getContextTreeModel()->getDirectory(directoryId)); + // Id 0 is the root item. It does not exist as a directory object, but child items can still be retrieved from it. + if(!directoryItem && directoryId != 0) + return std::nullopt; + return gGraphContextManager->getContextTreeModel()->getChildDirectoriesOf(directoryId); + } + + std::optional> GuiApiClasses::View::getChildViews(u32 directoryId) + { + ContextTreeItem* directoryItem = dynamic_cast(gGraphContextManager->getContextTreeModel()->getDirectory(directoryId)); + // Id 0 is the root item. It does not exist as a directory object, but child items can still be retrieved from it. + if(!directoryItem && directoryId != 0) + return std::nullopt; + return gGraphContextManager->getContextTreeModel()->getChildContextsOf(directoryId); + } } diff --git a/plugins/gui/src/python/python_gui_api_bindings.cpp b/plugins/gui/src/python/python_gui_api_bindings.cpp index 328882b9c78..ee93bf7f38d 100644 --- a/plugins/gui/src/python/python_gui_api_bindings.cpp +++ b/plugins/gui/src/python/python_gui_api_bindings.cpp @@ -264,6 +264,22 @@ PYBIND11_PLUGIN(hal_gui) :param int destinationDirectoryId: ID of the destination directory to which the directory will be moved. If None, the directory is instead moved to the current directory. :param int row: The row index in the parent directory, where the directory will be inserted. +)") + .def_static("getChildDirectories", &GuiApiClasses::View::getChildDirectories, py::arg("directoryId"), R"( + Returns the ids of all direct child directories of a given directory. + + :param int directoryId: ID of the parent directory, whose direct children will be returned + :returns: List of the ids of all direct child directories of the specified directory. + Returns None, if the given directory does not exist. + :rtype: list[int]|None +)") + .def_static("getChildViews", &GuiApiClasses::View::getChildViews, py::arg("directoryId"), R"( + Returns the ids of all direct child views of a given directory. + + :param int directoryId: ID of the parent directory, whose direct children will be returned + :returns: List of the ids of all direct child views of the specified directory. + Returns None, if the given directory does not exist. + :rtype: list[int]|None )");