Salome HOME
Issue #1305: Make different gray color for selectable and non-selectable items
[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 #include <TNaming_Builder.hxx>
89 #include <TNaming_Iterator.hxx>
90
91 // copies named shapes: we need the implementation of this 
92 static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
93   const Handle(TDF_Attribute)& theTo)
94
95   TDF_Label aLab = theTo->Label();
96   TNaming_Builder B(aLab);
97
98   TNaming_Iterator It (theFrom);
99   for ( ;It.More() ; It.Next()) {
100     const TopoDS_Shape& OS     = It.OldShape();
101     const TopoDS_Shape& NS     = It.NewShape();
102     TNaming_Evolution   Status = It.Evolution();
103
104     switch (Status) {
105     case TNaming_PRIMITIVE :
106       B.Generated(NS);
107       break;
108     case TNaming_GENERATED :
109       B.Generated(OS, NS);
110       break;
111     case TNaming_MODIFY : 
112       B.Modify(OS, NS);
113       break;
114     case TNaming_DELETE : 
115       B.Delete (OS);
116       break;
117     case TNaming_SELECTED :
118       B.Select(NS, OS);
119       break;
120     default:
121       break;
122     }
123   }
124 }
125
126 /// makes copy of all attributes on the given label and all sub-labels
127 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
128   TDF_AttributeIterator anAttrIter(theSource);
129   for(; anAttrIter.More(); anAttrIter.Next()) {
130     Handle(TDF_Attribute) aTargetAttr;
131     if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
132       // create a new attribute if not yet exists in the destination
133             aTargetAttr = anAttrIter.Value()->NewEmpty();
134       theDestination.AddAttribute(aTargetAttr);
135     }
136     // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
137     Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
138     if (aNS.IsNull()) {
139       Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
140       anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
141     } else {
142       CopyNS(aNS, aTargetAttr);
143     }
144   }
145   // copy the sub-labels content
146   TDF_ChildIterator aSubLabsIter(theSource);
147   for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
148     copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
149   }
150 }
151
152 void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
153 {
154   int anOldSize = mySize->Get();
155   int aRemoved = 0;
156   // iterate one by one and shifting the removed indicies
157   for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
158     if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
159       if (aRemoved) { // but must be shifted to the removed position
160         TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
161         TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
162         copyAttrs(aSource, aDest);
163         // remove the moved source
164         aSource.ForgetAllAttributes(Standard_True);
165       }
166     } else { // this is removed, so remove everything from this label
167       TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
168       std::shared_ptr<Model_AttributeSelection> aOldAttr = 
169         std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
170       aOldAttr->setObject(owner());
171       REMOVE_BACK_REF(aOldAttr->context());
172       aLab.ForgetAllAttributes(Standard_True);
173       myTmpAttr.reset();
174       aRemoved++;
175     }
176   }
177   if (aRemoved) { // remove was performed, so, update the size and this attribute
178     mySize->Set(anOldSize - aRemoved);
179     owner()->data()->sendAttributeUpdated(this);
180   }
181 }
182
183 int Model_AttributeSelectionList::size()
184 {
185   return mySize->Get();
186 }
187
188 bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext,
189                                             const std::shared_ptr<GeomAPI_Shape>& theSubShape,
190                                             const bool theTemporarily)
191 {
192   for(int anIndex = size() - 1; anIndex >= 0; anIndex--) {
193     AttributeSelectionPtr anAttr = value(anIndex);
194     if (anAttr.get()) {
195       if (anAttr->context() == theContext) { // contexts are equal, so, check that values are also
196         std::shared_ptr<GeomAPI_Shape> aValue = anAttr->value();
197         if (!aValue.get()) {
198           if (!theSubShape.get()) { // both are null
199             return true;
200           }
201         } else {
202           if (aValue->isEqual(theSubShape)) // shapes are equal
203             return true;
204         }
205       }
206     }
207   }
208   return false;
209 }
210
211 const std::string Model_AttributeSelectionList::selectionType() const
212 {
213   return TCollection_AsciiString(mySelectionType->Get()).ToCString();
214 }
215
216 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
217 {
218   mySelectionType->Set(theType.c_str());
219 }
220
221 std::shared_ptr<ModelAPI_AttributeSelection> 
222   Model_AttributeSelectionList::value(const int theIndex)
223 {
224   if (myTmpAttr.get() && theIndex == size() - 1) {
225     return myTmpAttr;
226   }
227   TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
228   // create a new attribute each time, by demand
229   // supporting of old attributes is too slow (synch each time) and buggy on redo
230   // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
231   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
232     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
233   if (owner()) {
234     aNewAttr->setObject(owner());
235   }
236   return aNewAttr;
237 }
238
239 void Model_AttributeSelectionList::clear()
240 {
241   if (mySize->Get() != 0) {
242     mySize->Set(0);
243     myTmpAttr.reset();
244     TDF_ChildIterator aSubIter(mySize->Label());
245     for(; aSubIter.More(); aSubIter.Next()) {
246       TDF_Label aLab = aSubIter.Value();
247       std::shared_ptr<Model_AttributeSelection> aNewAttr = 
248         std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
249       if (owner()) {
250         aNewAttr->setObject(owner());
251       }
252       REMOVE_BACK_REF(aNewAttr->context());
253
254       aLab.ForgetAllAttributes(Standard_True);
255     }
256     owner()->data()->sendAttributeUpdated(this);
257   }
258 }
259
260 bool Model_AttributeSelectionList::isInitialized()
261 {
262   if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
263     return false;
264   }
265   return ModelAPI_AttributeSelectionList::isInitialized();
266 }
267
268 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
269 {
270   myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
271   if (!myIsInitialized) {
272     mySize = TDataStd_Integer::Set(theLabel, 0);
273     mySelectionType = TDataStd_Comment::Set(theLabel, "");
274   } else { // recollect mySubs
275     theLabel.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
276   }
277 }