Salome HOME
Implementation of remove of elements from the list by indexes
[modules/shaper.git] / src / Model / Model_AttributeSelectionList.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_AttributeSelectionList.cpp
4 // Created:     22 Oct 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "Model_AttributeSelectionList.h"
8 #include "Model_AttributeSelection.h"
9 #include "Model_Application.h"
10 #include "Model_Events.h"
11 #include "Model_Data.h"
12
13 #include <TDF_AttributeIterator.hxx>
14 #include <TDF_ChildIterator.hxx>
15 #include <TDF_RelocationTable.hxx>
16 #include <TopAbs_ShapeEnum.hxx>
17
18 #include <TopoDS.hxx>
19 #include <TopoDS_Shape.hxx>
20 #include <TopoDS_Edge.hxx>
21 #include <BRep_Tool.hxx>
22
23 using namespace std;
24
25 void Model_AttributeSelectionList::append(
26     const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
27     const bool theTemporarily)
28 {
29   // do not use the degenerated edge as a shape, a list is not incremented in this case
30   if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
31     const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
32     if (aSubShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
33       return;
34     }
35   }
36
37   int aNewTag = mySize->Get() + 1;
38   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
39
40   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
41     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
42   if (owner()) {
43     aNewAttr->setObject(owner());
44   }
45   aNewAttr->setID(id());
46   mySize->Set(aNewTag);
47   aNewAttr->setValue(theContext, theSubShape, theTemporarily);
48   if (theTemporarily)
49     myTmpAttr = aNewAttr;
50   else 
51     myTmpAttr.reset();
52   owner()->data()->sendAttributeUpdated(this);
53 }
54
55 void Model_AttributeSelectionList::append(
56   const std::string theNamingName, const std::string& theType)
57 {
58   int aNewTag = mySize->Get() + 1;
59   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
60
61   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
62     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
63   if (owner()) {
64     aNewAttr->setObject(owner());
65   }
66   aNewAttr->setID(id());
67   mySize->Set(aNewTag);
68   aNewAttr->selectSubShape(theType.empty() ? selectionType() : theType, theNamingName);
69   owner()->data()->sendAttributeUpdated(this);
70 }
71
72 void Model_AttributeSelectionList::removeLast() 
73 {
74   int anOldSize = mySize->Get();
75   if (anOldSize != 0) {
76     mySize->Set(anOldSize - 1);
77     TDF_Label aLab = mySize->Label().FindChild(anOldSize);
78     std::shared_ptr<Model_AttributeSelection> aOldAttr = 
79       std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
80     aOldAttr->setObject(owner());
81     REMOVE_BACK_REF(aOldAttr->context());
82     aLab.ForgetAllAttributes(Standard_True);
83     myTmpAttr.reset();
84     owner()->data()->sendAttributeUpdated(this);
85   }
86 }
87
88 /// makes copy of all attributes on the given label and all sub-labels
89 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
90   TDF_AttributeIterator anAttrIter(theSource);
91   for(; anAttrIter.More(); anAttrIter.Next()) {
92     Handle(TDF_Attribute) aTargetAttr;
93     if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
94       // create a new attribute if not yet exists in the destination
95             aTargetAttr = anAttrIter.Value()->NewEmpty();
96       theDestination.AddAttribute(aTargetAttr);
97     }
98     Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
99     anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
100   }
101   // copy the sub-labels content
102   TDF_ChildIterator aSubLabsIter(theSource);
103   for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
104     copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
105   }
106 }
107
108 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
109 {
110   int anOldSize = mySize->Get();
111   int aRemoved = 0;
112   // iterate one by one and shifting the removed indicies
113   for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
114     if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
115       if (aRemoved) { // but must be shifted to the removed position
116         TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
117         TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
118         copyAttrs(aSource, aDest);
119         // remove the moved source
120         aSource.ForgetAllAttributes(Standard_True);
121       }
122     } else { // this is removed, so remove everything from this label
123       TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
124       std::shared_ptr<Model_AttributeSelection> aOldAttr = 
125         std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
126       aOldAttr->setObject(owner());
127       REMOVE_BACK_REF(aOldAttr->context());
128       aLab.ForgetAllAttributes(Standard_True);
129       myTmpAttr.reset();
130       aRemoved++;
131     }
132   }
133   if (aRemoved) { // remove was performed, so, update the size and this attribute
134     mySize->Set(anOldSize - aRemoved);
135     owner()->data()->sendAttributeUpdated(this);
136   }
137 }
138
139 int Model_AttributeSelectionList::size()
140 {
141   return mySize->Get();
142 }
143
144 const std::string Model_AttributeSelectionList::selectionType() const
145 {
146   return TCollection_AsciiString(mySelectionType->Get()).ToCString();
147 }
148
149 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
150 {
151   mySelectionType->Set(theType.c_str());
152 }
153
154 std::shared_ptr<ModelAPI_AttributeSelection> 
155   Model_AttributeSelectionList::value(const int theIndex)
156 {
157   if (myTmpAttr.get() && theIndex == size() - 1) {
158     return myTmpAttr;
159   }
160   TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
161   // create a new attribute each time, by demand
162   // supporting of old attributes is too slow (synch each time) and buggy on redo
163   // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
164   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
165     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
166   if (owner()) {
167     aNewAttr->setObject(owner());
168   }
169   return aNewAttr;
170 }
171
172 void Model_AttributeSelectionList::clear()
173 {
174   if (mySize->Get() != 0) {
175     mySize->Set(0);
176     myTmpAttr.reset();
177     TDF_ChildIterator aSubIter(mySize->Label());
178     for(; aSubIter.More(); aSubIter.Next()) {
179       TDF_Label aLab = aSubIter.Value();
180       std::shared_ptr<Model_AttributeSelection> aNewAttr = 
181         std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
182       if (owner()) {
183         aNewAttr->setObject(owner());
184       }
185       REMOVE_BACK_REF(aNewAttr->context());
186
187       aLab.ForgetAllAttributes(Standard_True);
188     }
189     owner()->data()->sendAttributeUpdated(this);
190   }
191 }
192
193 bool Model_AttributeSelectionList::isInitialized()
194 {
195   if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
196     return false;
197   }
198   return ModelAPI_AttributeSelectionList::isInitialized();
199 }
200
201 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
202 {
203   myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
204   if (!myIsInitialized) {
205     mySize = TDataStd_Integer::Set(theLabel, 0);
206     mySelectionType = TDataStd_Comment::Set(theLabel, "");
207   } else { // recollect mySubs
208     theLabel.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
209   }
210 }