Skip to content

Commit

Permalink
1. Changed directory IDs to be positive. IDs of new directories do no…
Browse files Browse the repository at this point in the history
…t overlap with view-IDs.

2. Implemented bindings for getting the child views and directories of a given directory.
  • Loading branch information
Skaleee committed Jun 15, 2024
1 parent ac4c8a6 commit 873d674
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<u32> 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<u32> getChildContextsOf(u32 directoryId);

Q_SIGNALS:
void directoryCreatedSignal(ContextTreeItem* item);
Expand All @@ -223,9 +235,7 @@ namespace hal
std::map<GraphContext *, ContextTreeItem *> mContextMap;
QVector<GraphContext*> mContextList;
QVector<ContextDirectory*> mDirectoryList;
u32 mMinDirectoryId;

void dumpRecursion(ContextTreeItem* parent = nullptr, int level = 0) const;

};
} // namespace hal
3 changes: 3 additions & 0 deletions plugins/gui/include/gui/gui_api/gui_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ namespace hal
static void deleteDirectory(u32 id);
static void moveView(u32 viewId, std::optional<u32> destinationDirectoryId, std::optional<int> row);
static void moveDirectory(u32 directoryId, std::optional<u32> destinationDirectoryId, std::optional<int> row);

static std::optional<std::vector<u32>> getChildDirectories(u32 directoryId);
static std::optional<std::vector<u32>> getChildViews(u32 directoryId);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace hal
return mContext != nullptr;
}

ContextTreeModel::ContextTreeModel(QObject* parent) : BaseTreeModel(parent), mCurrentDirectory(nullptr), mMinDirectoryId(std::numeric_limits<u32>::max())
ContextTreeModel::ContextTreeModel(QObject* parent) : BaseTreeModel(parent), mCurrentDirectory(nullptr)
{
setHeaderLabels(QStringList() << "View Name" << "ID" << "Timestamp");
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -455,4 +448,42 @@ namespace hal
for (BaseTreeItem* cld : bti->getChildren())
dumpRecursion(dynamic_cast<ContextTreeItem*>(cld), level+1);
}

std::vector<u32> ContextTreeModel::getChildDirectoriesOf(u32 directoryId)
{
BaseTreeItem* directoryItem = getDirectory(directoryId);
if(!directoryItem)
directoryItem = getRootItem();

QList<BaseTreeItem*> children = directoryItem->getChildren();
std::vector<u32> ids;

for(BaseTreeItem* child : children)
{
ContextTreeItem* cti = dynamic_cast<ContextTreeItem*>(child);
if(cti->isDirectory())
ids.push_back(cti->getId());
}

return ids;
}

std::vector<u32> ContextTreeModel::getChildContextsOf(u32 directoryId)
{
BaseTreeItem* directoryItem = getDirectory(directoryId);
if(!directoryItem)
directoryItem = getRootItem();

QList<BaseTreeItem*> children = directoryItem->getChildren();
std::vector<u32> ids;

for(BaseTreeItem* child : children)
{
ContextTreeItem* cti = dynamic_cast<ContextTreeItem*>(child);
if(cti->isContext())
ids.push_back(cti->getId());
}

return ids;
}
}
8 changes: 5 additions & 3 deletions plugins/gui/src/graph_widget/graph_context_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down
18 changes: 18 additions & 0 deletions plugins/gui/src/gui_api/gui_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,4 +1014,22 @@ namespace hal
act->setObject(uao);
act->exec();
}

std::optional<std::vector<u32>> GuiApiClasses::View::getChildDirectories(u32 directoryId)
{
ContextTreeItem* directoryItem = dynamic_cast<ContextTreeItem*>(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<std::vector<u32>> GuiApiClasses::View::getChildViews(u32 directoryId)
{
ContextTreeItem* directoryItem = dynamic_cast<ContextTreeItem*>(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);
}
}
16 changes: 16 additions & 0 deletions plugins/gui/src/python/python_gui_api_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
)");


Expand Down

0 comments on commit 873d674

Please sign in to comment.