X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModel%2FModel_AttributeSelectionList.cpp;h=b44767750922a7ace3dc5ff37bdf1c35ece593ed;hb=061a63480f6840b6d945f7744b3b972e2d4cb25d;hp=24a9efca5308d5684d3030a0b55c6ac5d4fd8f54;hpb=90c499710fd0db7a6b744457f5b7bac32096f4e1;p=modules%2Fshaper.git diff --git a/src/Model/Model_AttributeSelectionList.cpp b/src/Model/Model_AttributeSelectionList.cpp index 24a9efca5..b44767750 100644 --- a/src/Model/Model_AttributeSelectionList.cpp +++ b/src/Model/Model_AttributeSelectionList.cpp @@ -1,6 +1,6 @@ // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -// File: Model_AttributeSelectionList.h +// File: Model_AttributeSelectionList.cpp // Created: 22 Oct 2014 // Author: Mikhail PONIKAROV @@ -10,7 +10,9 @@ #include "Model_Events.h" #include "Model_Data.h" +#include #include +#include #include #include @@ -21,7 +23,8 @@ using namespace std; void Model_AttributeSelectionList::append( - const ResultPtr& theContext, const std::shared_ptr& theSubShape) + const ResultPtr& theContext, const std::shared_ptr& theSubShape, + const bool theTemporarily) { // do not use the degenerated edge as a shape, a list is not incremented in this case if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) { @@ -39,12 +42,18 @@ void Model_AttributeSelectionList::append( if (owner()) { aNewAttr->setObject(owner()); } + aNewAttr->setID(id()); mySize->Set(aNewTag); - aNewAttr->setValue(theContext, theSubShape); + aNewAttr->setValue(theContext, theSubShape, theTemporarily); + if (theTemporarily) + myTmpAttr = aNewAttr; + else + myTmpAttr.reset(); owner()->data()->sendAttributeUpdated(this); } -void Model_AttributeSelectionList::append(std::string theNamingName) +void Model_AttributeSelectionList::append( + const std::string theNamingName, const std::string& theType) { int aNewTag = mySize->Get() + 1; TDF_Label aNewLab = mySize->Label().FindChild(aNewTag); @@ -54,11 +63,79 @@ void Model_AttributeSelectionList::append(std::string theNamingName) if (owner()) { aNewAttr->setObject(owner()); } + aNewAttr->setID(id()); mySize->Set(aNewTag); - aNewAttr->selectSubShape(selectionType(), theNamingName); + aNewAttr->selectSubShape(theType.empty() ? selectionType() : theType, theNamingName); owner()->data()->sendAttributeUpdated(this); } +void Model_AttributeSelectionList::removeLast() +{ + int anOldSize = mySize->Get(); + if (anOldSize != 0) { + mySize->Set(anOldSize - 1); + TDF_Label aLab = mySize->Label().FindChild(anOldSize); + 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(); + owner()->data()->sendAttributeUpdated(this); + } +} + +/// 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() { return mySize->Get(); @@ -77,6 +154,9 @@ void Model_AttributeSelectionList::setSelectionType(const std::string& theType) std::shared_ptr Model_AttributeSelectionList::value(const int theIndex) { + if (myTmpAttr.get() && theIndex == size() - 1) { + return myTmpAttr; + } TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1); // create a new attribute each time, by demand // supporting of old attributes is too slow (synch each time) and buggy on redo @@ -93,14 +173,31 @@ void Model_AttributeSelectionList::clear() { if (mySize->Get() != 0) { mySize->Set(0); + myTmpAttr.reset(); TDF_ChildIterator aSubIter(mySize->Label()); for(; aSubIter.More(); aSubIter.Next()) { - aSubIter.Value().ForgetAllAttributes(Standard_True); + TDF_Label aLab = aSubIter.Value(); + std::shared_ptr aNewAttr = + std::shared_ptr(new Model_AttributeSelection(aLab)); + if (owner()) { + aNewAttr->setObject(owner()); + } + REMOVE_BACK_REF(aNewAttr->context()); + + aLab.ForgetAllAttributes(Standard_True); } owner()->data()->sendAttributeUpdated(this); } } +bool Model_AttributeSelectionList::isInitialized() +{ + if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo + return false; + } + return ModelAPI_AttributeSelectionList::isInitialized(); +} + Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel) { myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;