Salome HOME
Fix for the issue #2819 : Fatal error when undo
[modules/shaper.git] / src / Model / Model_AttributeRefList.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "Model_AttributeRefList.h"
22 #include "Model_Application.h"
23 #include "Model_Data.h"
24 #include "Model_Objects.h"
25 #include <ModelAPI_Feature.h>
26 #include <TDF_ListIteratorOfLabelList.hxx>
27 #include <TDF_Tool.hxx>
28 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
29
30 void Model_AttributeRefList::append(ObjectPtr theObject)
31 {
32   if (owner()->document() == theObject->document()) {
33     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
34     myRef->Append(aData->label().Father());  // store label of the object
35   } else if (theObject.get() && theObject->data()->isValid()) { // reference to the other document
36     myRef->Append(myRef->Label());
37     // if these attributes exist, the link is external: keep reference to access the label
38     std::ostringstream anIdString; // string with document Id
39     anIdString<<theObject->document()->id();
40     myExtDocRef->Append(anIdString.str().c_str());
41     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
42     TCollection_AsciiString anEntry;
43     TDF_Tool::Entry(aData->label().Father(), anEntry);
44     myExtDocRef->Append(anEntry);
45   } else return; // something is wrong
46
47   if (myHashUsed) {
48     myHashObjects.insert(theObject);
49     myHashIndex[myRef->Extent() - 1] = theObject;
50     myHashIndexNoEmpty[int(myHashIndexNoEmpty.size())] = theObject;
51   }
52
53   // do it before the transaction finish to make just created/removed objects know dependencies
54   // and reference from composite feature is removed automatically
55   ADD_BACK_REF(theObject);
56   owner()->data()->sendAttributeUpdated(this);
57 }
58
59 void Model_AttributeRefList::remove(ObjectPtr theObject)
60 {
61   eraseHash();
62   if (theObject.get() != NULL) {
63     if (owner()->document() == theObject->document()) {
64       std::shared_ptr<Model_Data> aData;
65       aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
66       myRef->Remove(aData->label().Father());
67       REMOVE_BACK_REF(theObject);
68       owner()->data()->sendAttributeUpdated(this);
69     } else {
70       // LCOV_EXCL_START
71       // create new lists because for the current moment remove one of the duplicated elements
72       // from the list is buggy
73       TDF_LabelList anOldList = myRef->List();
74       myRef->Clear();
75       TDataStd_ListOfExtendedString anOldExts = myExtDocRef->List();
76       myExtDocRef->Clear();
77
78       std::ostringstream anIdString; // string with document Id
79       anIdString<<theObject->document()->id();
80       std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
81       TCollection_AsciiString anEntry;
82       TDF_Tool::Entry(aData->label().Father(), anEntry);
83       bool aFound = false;
84       TDataStd_ListIteratorOfListOfExtendedString anExtIter(anOldExts);
85       for (TDF_ListIteratorOfLabelList aLIter(anOldList); aLIter.More(); aLIter.Next()) {
86         if (aLIter.Value() == myRef->Label()) {
87           if (anExtIter.Value() == anIdString.str().c_str()) {
88             TDataStd_ListIteratorOfListOfExtendedString anExtIter2 = anExtIter;
89             anExtIter2.Next();
90             if (anExtIter2.Value() == anEntry) { // fully matches, so, remove(don't copy)
91               aFound = true;
92               continue;
93             }
94           }
95           myExtDocRef->Append(anExtIter.Value());
96           anExtIter.Next();
97           myExtDocRef->Append(anExtIter.Value());
98           anExtIter.Next();
99         }
100         myRef->Append(aLIter.Value());
101       }
102       if (aFound) {
103         REMOVE_BACK_REF(theObject);
104         owner()->data()->sendAttributeUpdated(this);
105       }
106       // LCOV_EXCL_STOP
107     }
108   }
109   else { // in case of empty object remove, the first empty object is removed from the list
110     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
111         owner()->document());
112     if (aDoc) {
113       const TDF_LabelList& aList = myRef->List();
114       for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
115         ObjectPtr anObj = aDoc->objects()->object(aLIter.Value());
116         if (anObj.get() == NULL) {
117           myRef->Remove(aLIter.Value());
118           REMOVE_BACK_REF(theObject);
119           owner()->data()->sendAttributeUpdated(this);
120           break;
121         }
122       }
123     }
124   }
125 }
126
127 void Model_AttributeRefList::clear()
128 {
129   std::list<ObjectPtr> anOldList = list();
130   myRef->Clear();
131   std::list<ObjectPtr>::iterator anOldIter = anOldList.begin();
132   for(; anOldIter != anOldList.end(); anOldIter++) {
133     REMOVE_BACK_REF((*anOldIter));
134   }
135   myExtDocRef->Clear();
136   eraseHash();
137   owner()->data()->sendAttributeUpdated(this);
138 }
139
140 int Model_AttributeRefList::size(const bool theWithEmpty) const
141 {
142   if (theWithEmpty)
143     return myRef->Extent();
144
145   if (myHashUsed)
146     return int(myHashIndexNoEmpty.size());
147
148   int aResult = 0;
149   const TDF_LabelList& aList = myRef->List();
150   for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
151     if (!aLIter.Value().IsNull() && !aLIter.Value().IsRoot()) aResult++;
152   }
153   return aResult;
154 }
155
156 bool Model_AttributeRefList::isInitialized()
157 {
158   if (size(false) == 0) {
159     // empty list is not initialized list: sketch will be not valid after add/undo
160     return false;
161   }
162   return ModelAPI_AttributeRefList::isInitialized();
163 }
164
165 ObjectPtr Model_AttributeRefList::iteratedObject(TDF_ListIteratorOfLabelList& theLIter,
166     TDataStd_ListIteratorOfListOfExtendedString& theExtIter,
167     std::shared_ptr<Model_Document> theDoc) const
168 {
169   ObjectPtr anObj;
170   if (!theLIter.Value().IsNull() && !theLIter.Value().IsRoot()) {
171     if (theLIter.Value() == myRef->Label()) { // external document object
172       int anID = atoi(TCollection_AsciiString(theExtIter.Value()).ToCString());
173       theExtIter.Next();
174       DocumentPtr aRefDoc = Model_Application::getApplication()->document(anID);
175       if (aRefDoc.get()) {
176         std::shared_ptr<Model_Document> aDR = std::dynamic_pointer_cast<Model_Document>(aRefDoc);
177         TDF_Label aRefLab;
178         TDF_Tool::Label(aDR->objects()->featuresLabel().Data(),
179           TCollection_AsciiString(theExtIter.Value()).ToCString(), aRefLab);
180         if (!aRefLab.IsNull()) {
181           anObj = aDR->objects()->object(aRefLab);
182         }
183       }
184       theExtIter.Next();
185     } else { // internal document object
186       anObj = theDoc->objects()->object(theLIter.Value());
187     }
188   }
189   return anObj;
190 }
191
192 std::list<ObjectPtr> Model_AttributeRefList::list()
193 {
194   createHash();
195   std::list<ObjectPtr> aResult;
196   std::map<int, ObjectPtr>::iterator aHashIter = myHashIndex.begin();
197   for(; aHashIter != myHashIndex.end(); aHashIter++) {
198     aResult.push_back(aHashIter->second);
199   }
200   return aResult;
201 }
202
203 bool Model_AttributeRefList::isInList(const ObjectPtr& theObj)
204 {
205   if(!theObj.get()) {
206     return false;
207   }
208   createHash();
209   return myHashObjects.count(theObj) != 0;
210 }
211
212 ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty)
213 {
214   createHash();
215   std::map<int, ObjectPtr>::iterator aFind;
216   if (theWithEmpty) {
217     aFind = myHashIndex.find(theIndex);
218     if (aFind == myHashIndex.end())
219       return ObjectPtr();
220   } else {
221     aFind = myHashIndexNoEmpty.find(theIndex);
222     if (aFind == myHashIndexNoEmpty.end())
223       return ObjectPtr();
224   }
225   return aFind->second;
226 }
227
228 void Model_AttributeRefList::substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew)
229 {
230   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
231       owner()->document());
232   if (aDoc) {
233     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theCurrent->data());
234     if (aData.get() && aData->isValid()) {
235       TDF_Label aCurrentLab = aData->label().Father();
236       TDF_Label aNewLab;
237       if (theNew.get() && theNew->data()->isValid()) { // the new may be null
238         std::shared_ptr<Model_Data> aNewData =
239           std::dynamic_pointer_cast<Model_Data>(theNew->data());
240         aNewLab = aNewData->label().Father();
241       } else {
242         aNewLab = aCurrentLab.Root(); // root means null object
243       }
244       // do the substitution
245       eraseHash();
246       ADD_BACK_REF(theNew);
247       if (myRef->InsertAfter(aNewLab, aCurrentLab)) {
248         myRef->Remove(aCurrentLab);
249         REMOVE_BACK_REF(theCurrent);
250       }
251       owner()->data()->sendAttributeUpdated(this);
252     }
253   }
254 }
255
256 void Model_AttributeRefList::removeLast()
257 {
258   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
259       owner()->document());
260   if (aDoc && !myRef->IsEmpty()) {
261     ObjectPtr anObj = aDoc->objects()->object(myRef->Last());
262     if (anObj.get()) {
263       eraseHash();
264       myRef->Remove(myRef->Last());
265       REMOVE_BACK_REF(anObj);
266       owner()->data()->sendAttributeUpdated(this);
267     }
268   }
269 }
270
271 void Model_AttributeRefList::remove(const std::set<int>& theIndices)
272 {
273   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
274       owner()->document());
275   if (aDoc && !myRef->IsEmpty()) {
276     // collect labels that will be removed
277     TDF_LabelList aLabelsToRemove;
278     TDF_ListIteratorOfLabelList aLabIter(myRef->List());
279     for(int aCurrent = 0; aLabIter.More(); aLabIter.Next(), aCurrent++) {
280       if (theIndices.find(aCurrent) != theIndices.end())
281         aLabelsToRemove.Append(aLabIter.Value());
282     }
283     // remove labels
284     for(aLabIter.Initialize(aLabelsToRemove); aLabIter.More(); aLabIter.Next()) {
285       ObjectPtr anObj = aDoc->objects()->object(aLabIter.Value());
286       if (anObj.get()) {
287         myRef->Remove(aLabIter.Value());
288         REMOVE_BACK_REF(anObj);
289       }
290     }
291     if (!aLabelsToRemove.IsEmpty()) {
292       eraseHash();
293       owner()->data()->sendAttributeUpdated(this);
294     }
295   }
296 }
297
298 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
299 {
300   myLab = theLabel;
301   reinit();
302 }
303
304 void Model_AttributeRefList::reinit()
305 {
306   myIsInitialized = myLab.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
307   if (!myIsInitialized) {
308     myRef = TDataStd_ReferenceList::Set(myLab);
309   }
310   if (!myLab.FindAttribute(TDataStd_ExtStringList::GetID(), myExtDocRef)) {
311     myExtDocRef = TDataStd_ExtStringList::Set(myLab);
312   }
313   eraseHash();
314 }
315
316 void Model_AttributeRefList::createHash()
317 {
318   if (myHashUsed) {
319     return;
320   }
321   eraseHash();
322   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
323     owner()->document());
324   if (aDoc) {
325     const TDF_LabelList& aList = myRef->List();
326     TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
327     for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
328       ObjectPtr anObj = iteratedObject(aLIter, anExtIter, aDoc);
329       myHashIndex[int(myHashIndex.size())] = anObj;
330       if (anObj.get()) {
331         myHashIndexNoEmpty[int(myHashIndexNoEmpty.size())] = anObj;
332         myHashObjects.insert(anObj);
333       }
334     }
335   }
336 }
337
338
339 void Model_AttributeRefList::eraseHash()
340 {
341   myHashObjects.clear();
342   myHashIndex.clear();
343   myHashIndexNoEmpty.clear();
344   myHashUsed = false;
345 }