From 2d34c9dcdc5f88e7c46a29df4f3d4ce73f3f8a8b Mon Sep 17 00:00:00 2001 From: mpv Date: Mon, 11 Jan 2016 20:04:21 +0300 Subject: [PATCH] Implementation of remove of elements from the list by indexes --- src/Model/Model_AttributeRefList.cpp | 22 +++++++++ src/Model/Model_AttributeSelectionList.cpp | 49 +++++++++++++++++++ .../ModuleBase_WidgetMultiSelector.cpp | 10 +--- 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/src/Model/Model_AttributeRefList.cpp b/src/Model/Model_AttributeRefList.cpp index 200807984..a1e0aeb66 100644 --- a/src/Model/Model_AttributeRefList.cpp +++ b/src/Model/Model_AttributeRefList.cpp @@ -214,6 +214,28 @@ void Model_AttributeRefList::removeLast() void Model_AttributeRefList::remove(const std::set& theIndices) { + std::shared_ptr aDoc = std::dynamic_pointer_cast( + owner()->document()); + if (aDoc && !myRef->IsEmpty()) { + // collet labels that will be removed + TDF_LabelList aLabelsToRemove; + TDF_ListIteratorOfLabelList aLabIter(myRef->List()); + for(int aCurrent = 0; aLabIter.More(); aLabIter.Next(), aCurrent++) { + if (theIndices.find(aCurrent) != theIndices.end()) + aLabelsToRemove.Append(aLabIter.Value()); + } + // remove labels + for(aLabIter.Initialize(aLabelsToRemove); aLabIter.More(); aLabIter.Next()) { + ObjectPtr anObj = aDoc->objects()->object(aLabIter.Value()); + if (anObj.get()) { + myRef->Remove(aLabIter.Value()); + REMOVE_BACK_REF(anObj); + } + } + if (!aLabelsToRemove.IsEmpty()) { + owner()->data()->sendAttributeUpdated(this); + } + } } Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel) diff --git a/src/Model/Model_AttributeSelectionList.cpp b/src/Model/Model_AttributeSelectionList.cpp index 677d14d78..b44767750 100644 --- a/src/Model/Model_AttributeSelectionList.cpp +++ b/src/Model/Model_AttributeSelectionList.cpp @@ -10,7 +10,9 @@ #include "Model_Events.h" #include "Model_Data.h" +#include #include +#include #include #include @@ -83,8 +85,55 @@ void Model_AttributeSelectionList::removeLast() } } +/// makes copy of all attributes on the given label and all sub-labels +static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) { + TDF_AttributeIterator anAttrIter(theSource); + for(; anAttrIter.More(); anAttrIter.Next()) { + Handle(TDF_Attribute) aTargetAttr; + if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) { + // create a new attribute if not yet exists in the destination + aTargetAttr = anAttrIter.Value()->NewEmpty(); + theDestination.AddAttribute(aTargetAttr); + } + Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map + anAttrIter.Value()->Paste(aTargetAttr, aRelocTable); + } + // copy the sub-labels content + TDF_ChildIterator aSubLabsIter(theSource); + for(; aSubLabsIter.More(); aSubLabsIter.Next()) { + copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag())); + } +} + void Model_AttributeSelectionList::remove(const std::set& theIndices) { + int anOldSize = mySize->Get(); + int aRemoved = 0; + // iterate one by one and shifting the removed indicies + for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) { + if (theIndices.find(aCurrent) == theIndices.end()) { // not removed + if (aRemoved) { // but must be shifted to the removed position + TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1); + TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved); + copyAttrs(aSource, aDest); + // remove the moved source + aSource.ForgetAllAttributes(Standard_True); + } + } else { // this is removed, so remove everything from this label + TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1); + std::shared_ptr aOldAttr = + std::shared_ptr(new Model_AttributeSelection(aLab)); + aOldAttr->setObject(owner()); + REMOVE_BACK_REF(aOldAttr->context()); + aLab.ForgetAllAttributes(Standard_True); + myTmpAttr.reset(); + aRemoved++; + } + } + if (aRemoved) { // remove was performed, so, update the size and this attribute + mySize->Set(anOldSize - aRemoved); + owner()->data()->sendAttributeUpdated(this); + } } int Model_AttributeSelectionList::size() diff --git a/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp b/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp index 73ee5e462..6328648a3 100755 --- a/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp @@ -506,19 +506,13 @@ void ModuleBase_WidgetMultiSelector::onDeleteItem() AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID()); if (aSelectionListAttr.get()) { aDone = !anAttributeIds.empty(); - //aSelectionListAttr->remove(anAttributeIds); - std::set::const_iterator anIt = anAttributeIds.begin(), aLast = anAttributeIds.end(); - for (; anIt != aLast; anIt++) - aSelectionListAttr->removeLast(); + aSelectionListAttr->remove(anAttributeIds); } else { AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID()); if (aRefListAttr.get()) { aDone = !anAttributeIds.empty(); - //aRefListAttr->remove(anAttributeIds); - std::set::const_iterator anIt = anAttributeIds.begin(), aLast = anAttributeIds.end(); - for (; anIt != aLast; anIt++) - aRefListAttr->removeLast(); + aRefListAttr->remove(anAttributeIds); } } if (aDone) { -- 2.39.2