From ac4c8a622ee36dfc67f325036314cc72c7e94a15 Mon Sep 17 00:00:00 2001 From: joern274 Date: Thu, 13 Jun 2024 17:46:55 +0200 Subject: [PATCH] disallow drop of directory into one of its (grand-)child directories --- .../models/context_proxy_model.cpp | 44 +++++++++++-------- .../models/context_tree_model.cpp | 1 - 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/plugins/gui/src/context_manager_widget/models/context_proxy_model.cpp b/plugins/gui/src/context_manager_widget/models/context_proxy_model.cpp index f6343c7fd57..a61a803cf83 100644 --- a/plugins/gui/src/context_manager_widget/models/context_proxy_model.cpp +++ b/plugins/gui/src/context_manager_widget/models/context_proxy_model.cpp @@ -1,4 +1,4 @@ -#include "gui/context_manager_widget/models/context_proxy_model.h" +#include "gui/context_manager_widget/models/context_proxy_model.h" #include "gui/gui_utils/sort.h" #include "gui/basic_tree_model/base_tree_model.h" @@ -92,22 +92,22 @@ namespace hal } QByteArray encodedData; QDataStream stream(&encodedData, QIODevice::WriteOnly); - QString type; + QString moveType; int id; if (item->isDirectory()) { id = item->directory()->id(); - type = "dir"; + moveType = "dir"; } else if (item->isContext()) { id = item->context()->id(); - type = "view"; + moveType = "view"; } else Q_ASSERT (1==0); - stream << type << id << row << (quintptr) parentItem; - retval->setText(type); + stream << moveType << id << row << (quintptr) parentItem; + retval->setText(moveType); retval->setData("contexttreemodel/item", encodedData); return retval; @@ -121,14 +121,14 @@ namespace hal BaseTreeModel* model = static_cast(sourceModel()); if (!model) return false; - QString type; - int id; + QString moveType; + int moveId; int sourceRow = -1; quintptr sourceParent = 0; auto encItem = mimeData->data("contexttreemodel/item"); QDataStream dataStream(&encItem, QIODevice::ReadOnly); - dataStream >> type >> id >> sourceRow >> sourceParent; + dataStream >> moveType >> moveId >> sourceRow >> sourceParent; ContextTreeItem* sourceParentItem = dynamic_cast((BaseTreeItem*) sourceParent); u32 sourceParentId = sourceParentItem ? sourceParentItem->getId() : 0; @@ -199,21 +199,27 @@ namespace hal qDebug() << "drop no parent " << row << column; } */ - BaseTreeItem* targetParentItem = model->getItemFromIndex(mapToSource(parent)); - if (targetParentItem == model->getRootItem()) return true; - ContextTreeItem* parentItem = dynamic_cast(targetParentItem); - if (!parentItem || parentItem->isContext()) return false; + BaseTreeItem* tpar = model->getItemFromIndex(mapToSource(parent)); + if (tpar == model->getRootItem()) return true; + ContextTreeItem* targetParentItem = dynamic_cast(tpar); + if (!targetParentItem || targetParentItem->isContext()) return false; - QString type; - int id; + QString moveType; + int moveId; auto encItem = mimeData->data("contexttreemodel/item"); QDataStream dataStream(&encItem, QIODevice::ReadOnly); - dataStream >> type >> id >> moveRow >> moveParent; + dataStream >> moveType >> moveId >> moveRow >> moveParent; - if (type == "dir") + if (moveType == "dir") { - if (parentItem->isDirectory() && (int) parentItem->directory()->id() == id) - return false; + // target must not be the item itself or any (grand-)child of the item + ContextTreeItem* targetAnchestorItem = targetParentItem; + while (targetAnchestorItem) + { + if (targetAnchestorItem->isDirectory() && (int) targetAnchestorItem->directory()->id() == moveId) + return false; + targetAnchestorItem = dynamic_cast(targetAnchestorItem->getParent()); + } } return true; 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 27c1ec2f252..46afb5d9c5f 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 @@ -228,7 +228,6 @@ namespace hal ContextDirectory* ContextTreeModel::addDirectory(QString name, BaseTreeItem *parent, u32 id) { - if(id == 0) id = --mMinDirectoryId; else if (id < mMinDirectoryId)