From 2fc5d6c818bf2a465702a1a456bfa4ef2f066f78 Mon Sep 17 00:00:00 2001 From: Alexander Golubev Date: Tue, 30 Jun 2015 23:32:25 +0300 Subject: [PATCH] Gui/Tree: make tree view correctly handle child order than moving them --- src/Gui/Tree.cpp | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/src/Gui/Tree.cpp b/src/Gui/Tree.cpp index d266a995b..c68bf9bf3 100644 --- a/src/Gui/Tree.cpp +++ b/src/Gui/Tree.cpp @@ -966,7 +966,7 @@ void DocumentItem::slotChangeObject(const Gui::ViewProviderDocumentObject& view) DocumentObjectItem* parent_of_group = it->second; std::set children; std::vector group = view.claimChildren(); - int group_index = 0; + int group_index = 0; // counter of children inserted to the tree for (std::vector::iterator jt = group.begin(); jt != group.end(); ++jt) { if ((*jt) && view.getObject()->getDocument()->isIn(*jt)){ // Note: It is possible that we receive an invalid pointer from claimChildren(), e.g. if multiple properties @@ -981,21 +981,34 @@ void DocumentItem::slotChangeObject(const Gui::ViewProviderDocumentObject& view) DocumentObjectItem* child_of_group = kt->second; children.insert(child_of_group); QTreeWidgetItem* parent_of_child = child_of_group->parent(); - if (parent_of_child && parent_of_child != parent_of_group) { - if (parent_of_group != child_of_group) { - // This child's parent must be adjusted - int index = parent_of_child->indexOfChild(child_of_group); - parent_of_child->takeChild(index); - // Insert the child at the correct position according to the order of the children returned - // by claimChildren - if (group_index <= parent_of_group->childCount()) - parent_of_group->insertChild(group_index, child_of_group); - else - parent_of_group->addChild(child_of_group); - } - else { - Base::Console().Warning("Gui::DocumentItem::slotChangedObject(): Object references to itself.\n"); + + if (parent_of_child) { + if (parent_of_child != parent_of_group) { + if (parent_of_group != child_of_group) { + // This child's parent must be adjusted + parent_of_child->removeChild(child_of_group); + // Insert the child at the correct position according to the order of the children returned + // by claimChildren + if (group_index <= parent_of_group->childCount()) + parent_of_group->insertChild(group_index, child_of_group); + else + parent_of_group->addChild(child_of_group); + group_index++; + } else { + Base::Console().Warning("Gui::DocumentItem::slotChangedObject(): Object references to itself.\n"); + } + } else { + // The child already in the right group, but we may need to ajust it's index to follow the order of claimChildren + int index=parent_of_group->indexOfChild (child_of_group); + if (index>group_index) { + parent_of_group->takeChild (index); + parent_of_group->insertChild (group_index, child_of_group); + } + group_index++; } + } else { + Base::Console().Warning("Gui::DocumentItem::slotChangedObject(): " + "'%s' claimed a top level object '%s' to be it's child.\n", objectName.c_str(), internalName); } } } @@ -1006,8 +1019,6 @@ void DocumentItem::slotChangeObject(const Gui::ViewProviderDocumentObject& view) else { Base::Console().Warning("Gui::DocumentItem::slotChangedObject(): Group references unknown object.\n"); } - - group_index++; } // move all children which are not part of the group anymore to this item