]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_AttributeRefList.cpp
Salome HOME
Debug corrections for: deselect objects when multi-selection control is active.
[modules/shaper.git] / src / Model / Model_AttributeRefList.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_AttributeRefList.cxx
4 // Created:     8 May 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "Model_AttributeRefList.h"
8 #include "Model_Application.h"
9 #include "Model_Data.h"
10 #include "Model_Objects.h"
11 #include <ModelAPI_Feature.h>
12 #include <TDF_ListIteratorOfLabelList.hxx>
13
14 using namespace std;
15
16 void Model_AttributeRefList::append(ObjectPtr theObject)
17 {
18   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
19   myRef->Append(aData->label().Father());  // store label of the object
20   // do it before the transaction finish to make just created/removed objects know dependencies
21   // and reference from composite feature is removed automatically
22   ADD_BACK_REF(theObject);
23
24   owner()->data()->sendAttributeUpdated(this);
25 }
26
27 void Model_AttributeRefList::remove(ObjectPtr theObject)
28 {
29   std::shared_ptr<Model_Data> aData;
30   if (theObject.get() != NULL) {
31     aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
32     myRef->Remove(aData->label().Father());
33     REMOVE_BACK_REF(theObject);
34   }
35   else { // in case of empty object remove, the first empty object is removed from the list
36     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
37         owner()->document());
38     if (aDoc) {
39       const TDF_LabelList& aList = myRef->List();
40       for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
41         ObjectPtr anObj = aDoc->objects()->object(aLIter.Value());
42         if (anObj.get() == NULL) {
43           myRef->Remove(aLIter.Value());
44           REMOVE_BACK_REF(theObject);
45           break;
46         }
47       }
48     }
49   }
50   owner()->data()->sendAttributeUpdated(this);
51 }
52
53 void Model_AttributeRefList::clear()
54 {
55   std::list<ObjectPtr> anOldList = list();
56   myRef->Clear();
57   std::list<ObjectPtr>::iterator anOldIter = anOldList.begin();
58   for(; anOldIter != anOldList.end(); anOldIter++) {
59     REMOVE_BACK_REF((*anOldIter));
60   }
61   owner()->data()->sendAttributeUpdated(this);
62 }
63
64 int Model_AttributeRefList::size(const bool theWithEmpty) const
65 {
66   if (theWithEmpty)
67     return myRef->Extent();
68   int aResult = 0;
69   const TDF_LabelList& aList = myRef->List();
70   for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
71     if (!aLIter.Value().IsNull()) aResult++;
72   }
73   return aResult;
74 }
75
76 bool Model_AttributeRefList::isInitialized()
77 {
78   if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo
79     return false;
80   }
81   return ModelAPI_AttributeRefList::isInitialized();
82 }
83
84 list<ObjectPtr> Model_AttributeRefList::list()
85 {
86   std::list<ObjectPtr> aResult;
87   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
88       owner()->document());
89   if (aDoc) {
90     const TDF_LabelList& aList = myRef->List();
91     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
92       ObjectPtr anObj;
93       if (!aLIter.Value().IsNull())
94         anObj = aDoc->objects()->object(aLIter.Value());
95       aResult.push_back(anObj);
96     }
97   }
98   return aResult;
99 }
100
101 bool Model_AttributeRefList::isInList(const ObjectPtr& theObj)
102 {
103   std::list<ObjectPtr> aResult;
104   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
105       owner()->document());
106   if (aDoc) {
107     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObj->data());
108     if (aData.get() && aData->isValid()) {
109       TDF_Label anObjLab = aData->label().Father();
110       const TDF_LabelList& aList = myRef->List();
111       for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
112         if (aLIter.Value().IsEqual(anObjLab)) {
113           return true;
114         }
115       }
116     }
117   }
118   return false;
119 }
120
121 ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty) const
122 {
123   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
124       owner()->document());
125   if (aDoc) {
126     const TDF_LabelList& aList = myRef->List();
127     int anIndex = -1;
128     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
129       if (theWithEmpty || !aLIter.Value().IsNull())
130         anIndex++;
131       if (anIndex == theIndex)
132         return aDoc->objects()->object(aLIter.Value());
133     }
134   }
135   return ObjectPtr();
136 }
137
138 void Model_AttributeRefList::substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew)
139 {
140   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
141       owner()->document());
142   if (aDoc) {
143     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theCurrent->data());
144     if (aData.get() && aData->isValid()) {
145       TDF_Label aCurrentLab = aData->label().Father();
146       TDF_Label aNewLab;
147       if (theNew.get() && theNew->data()->isValid()) { // the new may be null
148         std::shared_ptr<Model_Data> aNewData = 
149           std::dynamic_pointer_cast<Model_Data>(theNew->data());
150         aNewLab = aNewData->label().Father();
151       }
152       // do the substitution
153       ADD_BACK_REF(theNew);
154       if (myRef->InsertAfter(aNewLab, aCurrentLab)) {
155         myRef->Remove(aCurrentLab);
156         REMOVE_BACK_REF(theCurrent);
157       }
158       owner()->data()->sendAttributeUpdated(this);
159     }
160   }
161 }
162
163 void Model_AttributeRefList::removeLast()
164 {
165   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
166       owner()->document());
167   if (aDoc && !myRef->IsEmpty()) {
168     ObjectPtr anObj = aDoc->objects()->object(myRef->Last());
169     if (anObj.get()) {
170       myRef->Remove(myRef->Last());
171       REMOVE_BACK_REF(anObj);
172       owner()->data()->sendAttributeUpdated(this);
173     }
174   }
175 }
176
177 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
178 {
179   myIsInitialized = theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
180   if (!myIsInitialized) {
181     myRef = TDataStd_ReferenceList::Set(theLabel);
182   }
183 }