Salome HOME
Make correct management of concealed on history position changes (on edit, manual...
[modules/shaper.git] / src / Model / Model_Data.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Data.hxx
4 // Created:     21 Mar 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_Data.h>
8 #include <Model_AttributeDocRef.h>
9 #include <Model_AttributeInteger.h>
10 #include <Model_AttributeDouble.h>
11 #include <Model_AttributeReference.h>
12 #include <Model_AttributeRefAttr.h>
13 #include <Model_AttributeRefList.h>
14 #include <Model_AttributeBoolean.h>
15 #include <Model_AttributeString.h>
16 #include <Model_AttributeSelection.h>
17 #include <Model_AttributeSelectionList.h>
18 #include <Model_AttributeIntArray.h>
19 #include <Model_Events.h>
20 #include <ModelAPI_Feature.h>
21 #include <ModelAPI_Result.h>
22 #include <ModelAPI_Validator.h>
23 #include <ModelAPI_Session.h>
24 #include <ModelAPI_ResultPart.h>
25
26 #include <GeomData_Point.h>
27 #include <GeomData_Point2D.h>
28 #include <GeomData_Dir.h>
29 #include <Events_Loop.h>
30 #include <Events_Error.h>
31
32 #include <TDataStd_Name.hxx>
33 #include <TDataStd_AsciiString.hxx>
34 #include <TDataStd_IntegerArray.hxx>
35 #include <TDF_AttributeIterator.hxx>
36 #include <TDF_ChildIterator.hxx>
37 #include <TDF_RelocationTable.hxx>
38
39 #include <string>
40
41 // myLab contains:
42 // TDataStd_Name - name of the object
43 // TDataStd_IntegerArray - state of the object execution, transaction ID of update
44 // TDataStd_BooleanArray - array of flags of this data:
45 //                             0 - is in history or not
46 static const int kFlagInHistory = 0;
47 //                             1 - is displayed or not
48 static const int kFlagDisplayed = 1;
49
50 // invalid data
51 const static std::shared_ptr<ModelAPI_Data> kInvalid(new Model_Data());
52
53 Model_Data::Model_Data() : mySendAttributeUpdated(true)
54 {
55 }
56
57 void Model_Data::setLabel(TDF_Label theLab)
58 {
59   myLab = theLab;
60   // set or get the default flags
61   if (!myLab.FindAttribute(TDataStd_BooleanArray::GetID(), myFlags)) {
62     // set default values if not found
63     myFlags = TDataStd_BooleanArray::Set(myLab, 0, 1);
64     myFlags->SetValue(kFlagInHistory, Standard_True); // is in history by default is true
65     myFlags->SetValue(kFlagDisplayed, Standard_True); // is displayed by default is true
66   }
67 }
68
69 std::string Model_Data::name()
70 {
71   Handle(TDataStd_Name) aName;
72   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
73     return std::string(TCollection_AsciiString(aName->Get()).ToCString());
74   return "";  // not defined
75 }
76
77 void Model_Data::setName(const std::string& theName)
78 {
79   bool isModified = false;
80   Handle(TDataStd_Name) aName;
81   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
82     TDataStd_Name::Set(myLab, theName.c_str());
83     isModified = true;
84   } else {
85     isModified = !aName->Get().IsEqual(theName.c_str());
86     if (isModified)
87       aName->Set(theName.c_str());
88   }
89 }
90
91 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
92 {
93   AttributePtr aResult;
94   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
95   ModelAPI_Attribute* anAttr = 0;
96   if (theAttrType == ModelAPI_AttributeDocRef::typeId()) {
97     anAttr = new Model_AttributeDocRef(anAttrLab);
98   } else if (theAttrType == Model_AttributeInteger::typeId()) {
99     anAttr = new Model_AttributeInteger(anAttrLab);
100   } else if (theAttrType == ModelAPI_AttributeDouble::typeId()) {
101     anAttr = new Model_AttributeDouble(anAttrLab);
102   } else if (theAttrType == Model_AttributeBoolean::typeId()) {
103     anAttr = new Model_AttributeBoolean(anAttrLab);
104   } else if (theAttrType == Model_AttributeString::typeId()) {
105     anAttr = new Model_AttributeString(anAttrLab);
106   } else if (theAttrType == ModelAPI_AttributeReference::typeId()) {
107     anAttr = new Model_AttributeReference(anAttrLab);
108   } else if (theAttrType == ModelAPI_AttributeSelection::typeId()) {
109     anAttr = new Model_AttributeSelection(anAttrLab);
110   } else if (theAttrType == ModelAPI_AttributeSelectionList::typeId()) {
111     anAttr = new Model_AttributeSelectionList(anAttrLab);
112   } else if (theAttrType == ModelAPI_AttributeRefAttr::typeId()) {
113     anAttr = new Model_AttributeRefAttr(anAttrLab);
114   } else if (theAttrType == ModelAPI_AttributeRefList::typeId()) {
115     anAttr = new Model_AttributeRefList(anAttrLab);
116   } else if (theAttrType == ModelAPI_AttributeIntArray::typeId()) {
117     anAttr = new Model_AttributeIntArray(anAttrLab);
118   } 
119   // create also GeomData attributes here because only here the OCAF strucure is known
120   else if (theAttrType == GeomData_Point::typeId()) {
121     anAttr = new GeomData_Point(anAttrLab);
122   } else if (theAttrType == GeomData_Dir::typeId()) {
123     anAttr = new GeomData_Dir(anAttrLab);
124   } else if (theAttrType == GeomData_Point2D::typeId()) {
125     anAttr = new GeomData_Point2D(anAttrLab);
126   }
127   if (anAttr) {
128     aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
129     myAttrs[theID] = aResult;
130     anAttr->setObject(myObject);
131     anAttr->setID(theID);
132   } else {
133     Events_Error::send("Can not create unknown type of attribute " + theAttrType);
134   }
135   return aResult;
136 }
137
138 // macro for gthe generic returning of the attribute by the ID
139 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
140   std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
141     std::shared_ptr<ATTR_TYPE> aRes; \
142     std::map<std::string, AttributePtr >::iterator aFound = \
143       myAttrs.find(theID); \
144     if (aFound != myAttrs.end()) { \
145       aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second); \
146     } \
147     return aRes; \
148   }
149 // implement nice getting methods for all ModelAPI attributes
150 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
151 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
152 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
153 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
154 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
155 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
156 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
157 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
158 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
159 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
160 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray);
161
162 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
163 {
164   std::shared_ptr<ModelAPI_Attribute> aResult;
165   if (myAttrs.find(theID) == myAttrs.end())  // no such attribute
166     return aResult;
167   return myAttrs[theID];
168 }
169
170 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
171 {
172   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = 
173     myAttrs.begin();
174   for (; anAttr != myAttrs.end(); anAttr++) {
175     if (anAttr->second == theAttr)
176       return anAttr->first;
177   }
178   // not found
179   static std::string anEmpty;
180   return anEmpty;
181 }
182
183 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
184 {
185   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
186   if (aData)
187     return myLab.IsEqual(aData->myLab) == Standard_True ;
188   return false;
189 }
190
191 bool Model_Data::isValid()
192 {
193   return !myLab.IsNull() && myLab.HasAttribute();
194 }
195
196 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
197 {
198   std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
199   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
200     myAttrs.begin();
201   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
202     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
203       aResult.push_back(anAttrsIter->second);
204     }
205   }
206   return aResult;
207 }
208
209 std::list<std::string> Model_Data::attributesIDs(const std::string& theType) 
210 {
211   std::list<std::string> aResult;
212   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
213     myAttrs.begin();
214   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
215     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
216       aResult.push_back(anAttrsIter->first);
217     }
218   }
219   return aResult;
220 }
221
222 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
223 {
224   theAttr->setInitialized();
225   if (theAttr->isArgument()) {
226     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
227     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
228     if (mySendAttributeUpdated && myObject) {
229       myObject->attributeChanged(theAttr->id());
230     }
231   }
232 }
233
234 void Model_Data::blockSendAttributeUpdated(const bool theBlock)
235 {
236   mySendAttributeUpdated = !theBlock;
237 }
238
239 void Model_Data::erase()
240 {
241   if (!myLab.IsNull())
242     myLab.ForgetAllAttributes();
243 }
244
245 // indexes in the state array
246 enum StatesIndexes {
247   STATE_INDEX_STATE = 1, // the state type itself
248   STATE_INDEX_TRANSACTION = 2, // transaction ID
249 };
250
251 /// Returns the label array, initialises it by default values if not exists
252 static Handle(TDataStd_IntegerArray) stateArray(TDF_Label& theLab)
253 {
254   Handle(TDataStd_IntegerArray) aStateArray;
255   if (!theLab.FindAttribute(TDataStd_IntegerArray::GetID(), aStateArray)) {
256     aStateArray = TDataStd_IntegerArray::Set(theLab, 1, 2);
257     aStateArray->SetValue(STATE_INDEX_STATE, ModelAPI_StateMustBeUpdated); // default state
258     aStateArray->SetValue(STATE_INDEX_TRANSACTION, 0); // default transaction ID (not existing)
259   }
260   return aStateArray;
261 }
262
263 void Model_Data::execState(const ModelAPI_ExecState theState)
264 {
265   if (theState != ModelAPI_StateNothing) {
266     stateArray(myLab)->SetValue(STATE_INDEX_STATE, (int)theState);
267   }
268 }
269
270 ModelAPI_ExecState Model_Data::execState()
271 {
272   return ModelAPI_ExecState(stateArray(myLab)->Value(STATE_INDEX_STATE));
273 }
274
275 int Model_Data::updateID()
276 {
277   return stateArray(myLab)->Value(STATE_INDEX_TRANSACTION);
278 }
279
280 void Model_Data::setUpdateID(const int theID)
281 {
282   stateArray(myLab)->SetValue(STATE_INDEX_TRANSACTION, theID);
283 }
284
285 void Model_Data::setError(const std::string& theError, bool theSend)
286 {
287   execState(ModelAPI_StateExecFailed);
288   if (theSend) {
289     Events_Error::send(theError);
290   }
291   TDataStd_AsciiString::Set(myLab, theError.c_str());
292 }
293
294 std::string Model_Data::error() const
295 {
296   Handle(TDataStd_AsciiString) anErrorAttr;
297   if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
298     return std::string(anErrorAttr->Get().ToCString());
299   }
300   return std::string();
301 }
302
303 int Model_Data::featureId() const
304 {
305   return myLab.Father().Tag(); // tag of the feature label
306 }
307
308 void Model_Data::eraseBackReferences()
309 {
310   myRefsToMe.clear();
311   std::shared_ptr<ModelAPI_Result> aRes = 
312     std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
313   if (aRes)
314     aRes->setIsConcealed(false);
315 }
316
317 void Model_Data::removeBackReference(FeaturePtr theFeature, std::string theAttrID)
318 {
319   AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
320   if (myRefsToMe.find(anAttribute) == myRefsToMe.end())
321     return;
322
323   myRefsToMe.erase(anAttribute);
324
325   // remove concealment immideately: on deselection it must be posible to reselect in GUI the same
326   if (ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
327     updateConcealmentFlag();
328   }
329 }
330
331 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID, 
332    const bool theApplyConcealment)
333 {
334   // do not add the same attribute twice
335   AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
336   if (myRefsToMe.find(anAttribute) != myRefsToMe.end())
337     return;
338
339   myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
340   if (theApplyConcealment && 
341       ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
342     std::shared_ptr<ModelAPI_Result> aRes = 
343       std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
344     // the second condition is for history upper than concealment causer, so the feature result may
345     // be displayed and previewed; also for avoiding of quick show/hide on history
346     // moving deep down
347     if (aRes && !theFeature->isDisabled()) {
348       aRes->setIsConcealed(true);
349     }
350   }
351 }
352
353 void Model_Data::updateConcealmentFlag()
354 {
355   std::set<AttributePtr>::iterator aRefsIter = myRefsToMe.begin();
356   for(; aRefsIter != myRefsToMe.end(); aRefsIter++) {
357     if (aRefsIter->get()) {
358       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefsIter)->owner());
359       if (aFeature.get() && !aFeature->isDisabled()) {
360         if (ModelAPI_Session::get()->validators()->isConcealed(
361               aFeature->getKind(), (*aRefsIter)->id())) {
362           return; // it is still concealed, nothing to do
363         }
364       }
365     }
366   }
367   // thus, no concealment references anymore => make not-concealed
368   std::shared_ptr<ModelAPI_Result> aRes = 
369     std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
370   if (aRes.get()) {
371     aRes->setIsConcealed(false);
372     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
373     ModelAPI_EventCreator::get()->sendUpdated(aRes, anEvent);
374     Events_Loop::loop()->flush(anEvent);
375   }
376 }
377
378 void Model_Data::referencesToObjects(
379   std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
380 {
381   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
382   std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
383   for(; anAttr != myAttrs.end(); anAttr++) {
384     std::string aType = anAttr->second->attributeType();
385     if (aType == ModelAPI_AttributeReference::typeId()) { // reference to object
386       std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
387           ModelAPI_AttributeReference>(anAttr->second);
388       aReferenced.push_back(aRef->value());
389     } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { // reference to attribute or object
390       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
391           ModelAPI_AttributeRefAttr>(anAttr->second);
392       aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
393     } else if (aType == ModelAPI_AttributeRefList::typeId()) { // list of references
394       aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
395     } else if (aType == ModelAPI_AttributeSelection::typeId()) { // selection attribute
396       std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
397           ModelAPI_AttributeSelection>(anAttr->second);
398       aReferenced.push_back(aRef->context());
399     } else if (aType == ModelAPI_AttributeSelectionList::typeId()) { // list of selection attributes
400       std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
401           ModelAPI_AttributeSelectionList>(anAttr->second);
402       for(int a = aRef->size() - 1; a >= 0; a--) {
403         aReferenced.push_back(aRef->value(a)->context());
404       }
405     } else
406       continue; // nothing to do, not reference
407
408     if (!aReferenced.empty()) {
409       theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
410       aReferenced.clear();
411     }
412   }
413 }
414
415 /// makes copy of all attributes on the given label and all sub-labels
416 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
417   TDF_AttributeIterator anAttrIter(theSource);
418   for(; anAttrIter.More(); anAttrIter.Next()) {
419     Handle(TDF_Attribute) aTargetAttr;
420     if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
421       // create a new attribute if not yet exists in the destination
422             aTargetAttr = anAttrIter.Value()->NewEmpty();
423       theDestination.AddAttribute(aTargetAttr);
424     }
425     Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
426     anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
427   }
428   // copy the sub-labels content
429   TDF_ChildIterator aSubLabsIter(theSource);
430   for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
431     copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
432   }
433 }
434
435 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
436 {
437   TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
438   copyAttrs(myLab, aTargetRoot);
439   // make initialized the initialized attributes
440   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator aMyIter = myAttrs.begin();
441   for(; aMyIter != myAttrs.end(); aMyIter++) {
442     if (aMyIter->second->isInitialized()) {
443       theTarget->attribute(aMyIter->first)->setInitialized();
444     }
445   }
446 }
447
448 bool Model_Data::isInHistory()
449 {
450   return myFlags->Value(kFlagInHistory) == Standard_True;
451 }
452
453 void Model_Data::setIsInHistory(const bool theFlag)
454 {
455   return myFlags->SetValue(kFlagInHistory, theFlag);
456 }
457
458 bool Model_Data::isDisplayed()
459 {
460   if (!myObject.get() || !myObject->document().get() || // object is in valid
461       myFlags->Value(kFlagDisplayed) != Standard_True) // or it was not displayed before
462     return false;
463   if (myObject->document()->isActive()) // for active documents it must be ok anyway
464     return true;
465   // any object from the root document except part result may be displayed
466   if (myObject->document() == ModelAPI_Session::get()->moduleDocument() &&
467       myObject->groupName() != ModelAPI_ResultPart::group())
468     return true;
469   return false;
470 }
471
472 void Model_Data::setDisplayed(const bool theDisplay)
473 {
474   if (theDisplay != isDisplayed()) {
475     myFlags->SetValue(kFlagDisplayed, theDisplay);
476     static Events_Loop* aLoop = Events_Loop::loop();
477     static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
478     static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
479     aECreator->sendUpdated(myObject, EVENT_DISP);
480   }
481 }
482
483 std::shared_ptr<ModelAPI_Data> Model_Data::invalidPtr()
484 {
485   return kInvalid;
486 }
487
488 std::shared_ptr<ModelAPI_Data> Model_Data::invalidData()
489 {
490   return kInvalid;
491 }