From 4bb14de504a07deec611d1f7ca53347ee59c165e Mon Sep 17 00:00:00 2001 From: jrheinlaender Date: Fri, 19 Apr 2013 14:41:40 +0430 Subject: [PATCH] Honour the ordering of the children returned by claimChildren() in the TreeWidget --- src/Gui/Tree.cpp | 51 +++++++++++++-------- src/Mod/PartDesign/Gui/ViewProviderBody.cpp | 15 +++--- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/Gui/Tree.cpp b/src/Gui/Tree.cpp index 44fb60266..6b80f63b1 100644 --- a/src/Gui/Tree.cpp +++ b/src/Gui/Tree.cpp @@ -944,8 +944,10 @@ void DocumentItem::slotChangeObject(const Gui::ViewProviderDocumentObject& view) std::map::iterator it = ObjectMap.find(objectName); if (it != ObjectMap.end()) { // use new grouping style + DocumentObjectItem* parent_of_group = it->second; std::set children; std::vector group = view.claimChildren(); + int group_index = 0; 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 @@ -957,13 +959,20 @@ void DocumentItem::slotChangeObject(const Gui::ViewProviderDocumentObject& view) if (internalName) { std::map::iterator kt = ObjectMap.find(internalName); if (kt != ObjectMap.end()) { - children.insert(kt->second); - QTreeWidgetItem* parent = kt->second->parent(); - if (parent && parent != it->second) { - if (it->second != kt->second) { - int index = parent->indexOfChild(kt->second); - parent->takeChild(index); - it->second->addChild(kt->second); + 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"); @@ -978,21 +987,23 @@ 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 - int count = it->second->childCount(); - for (int i=0; i < count; i++) { - QTreeWidgetItem* child = it->second->child(i); - if (children.find(child) == children.end()) { - it->second->takeChild(i); - this->addChild(child); - } - } - // set the text label - std::string displayName = obj->Label.getValue(); - it->second->setText(0, QString::fromUtf8(displayName.c_str())); + // move all children which are not part of the group anymore to this item + int count = parent_of_group->childCount(); + for (int i=0; i < count; i++) { + QTreeWidgetItem* child = parent_of_group->child(i); + if (children.find(child) == children.end()) { + parent_of_group->takeChild(i); + this->addChild(child); + } + } + + // set the text label + std::string displayName = obj->Label.getValue(); + parent_of_group->setText(0, QString::fromUtf8(displayName.c_str())); } else { Base::Console().Warning("Gui::DocumentItem::slotChangedObject(): Cannot change unknown object.\n"); diff --git a/src/Mod/PartDesign/Gui/ViewProviderBody.cpp b/src/Mod/PartDesign/Gui/ViewProviderBody.cpp index f7629efb5..d92e135a3 100644 --- a/src/Mod/PartDesign/Gui/ViewProviderBody.cpp +++ b/src/Mod/PartDesign/Gui/ViewProviderBody.cpp @@ -120,16 +120,15 @@ std::vector ViewProviderBody::claimChildren(void)const } } - // remove the otherwise handled objects - std::vector Result(Model.size()); - sort (Model.begin(), Model.end()); - std::vector::iterator it = set_difference (Model.begin(), Model.end(), OutSet.begin(),OutSet.end(), Result.begin()); + // remove the otherwise handled objects, preserving their order so the order in the TreeWidget is correct + std::vector Result; + for (std::vector::const_iterator it = Model.begin();it!=Model.end();++it) { + if (OutSet.find(*it) == OutSet.end()) + Result.push_back(*it); + } - //Base::Console().Error("Body claimed children:\n"); - //for (std::vector::const_iterator o = Result.begin(); o != it; o++) - // Base::Console().Error("%s\n", (*o)->getNameInDocument()); // return the rest as claim set of the Body - return std::vector(Result.begin(),it); + return Result; }