Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / Model / Model_AttributeRefAttr.cpp
index bedda92d5d92a704c0465cd42609a00dd1e52018..24bcddec1a7e63378c7ed4e5fd3f24462f323295 100644 (file)
@@ -4,56 +4,75 @@
 
 #include "Model_AttributeRefAttr.h"
 #include "Model_Application.h"
-#include "Model_Events.h"
 #include "Model_Data.h"
 #include <ModelAPI_Feature.h>
-#include <Events_Loop.h>
 
 using namespace std;
 
-void Model_AttributeRefAttr::setValue(boost::shared_ptr<ModelAPI_Attribute> theAttr)
+bool Model_AttributeRefAttr::isFeature()
 {
-  if (value() != theAttr) {
+  return myID->Get().Length() == 0;
+}
+
+void Model_AttributeRefAttr::setAttr(boost::shared_ptr<ModelAPI_Attribute> theAttr)
+{
+  boost::shared_ptr<Model_Data> aData = 
+    boost::dynamic_pointer_cast<Model_Data>(theAttr->owner()->data());
+  string anID = aData->id(theAttr);
+  if (myIsInitialized && feature() == theAttr->owner() && myID->Get().IsEqual(anID.c_str()))
+    return; // nothing is changed
+
+  myRef->Set(aData->label());
+  myID->Set(aData->id(theAttr).c_str());
+  owner()->data()->sendAttributeUpdated(this);
+}
+
+boost::shared_ptr<ModelAPI_Attribute> Model_AttributeRefAttr::attr()
+{
+  FeaturePtr aFeature = feature();
+  if (aFeature) {
     boost::shared_ptr<Model_Data> aData = 
-      boost::dynamic_pointer_cast<Model_Data>(theAttr->feature()->data());
-    if (myRef.IsNull()) {
-      boost::shared_ptr<Model_Data> aMyData = 
-        boost::dynamic_pointer_cast<Model_Data>(feature()->data());
-      TDF_Reference::Set(aMyData->label(), aData->label());
-    } else {
-      myRef->Set(aData->label());
-    }
-    myID->Set(aData->id(theAttr).c_str());
+      boost::dynamic_pointer_cast<Model_Data>(aFeature->data());
+    return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString());
+  }
+  // not initialized
+  return boost::shared_ptr<ModelAPI_Attribute>();
+}
 
-    static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
-    Model_FeatureUpdatedMessage aMsg(feature(), anEvent);
-    Events_Loop::loop()->send(aMsg);
+void Model_AttributeRefAttr::setFeature(FeaturePtr theFeature)
+{
+  if (!myIsInitialized || myID->Get().Length() != 0 || feature() != theFeature) {
+    boost::shared_ptr<Model_Data> aData = 
+      boost::dynamic_pointer_cast<Model_Data>(theFeature->data());
+    myRef->Set(aData->label());
+    myID->Set(""); // feature is identified by the empty ID
+    owner()->data()->sendAttributeUpdated(this);
   }
 }
 
-boost::shared_ptr<ModelAPI_Attribute> Model_AttributeRefAttr::value()
+FeaturePtr Model_AttributeRefAttr::feature()
 {
-  if (!myRef.IsNull()) {
+  if (myRef->Get() != myRef->Label()) { // initialized
     boost::shared_ptr<Model_Document> aDoc = 
-      boost::dynamic_pointer_cast<Model_Document>(feature()->document());
+      boost::dynamic_pointer_cast<Model_Document>(owner()->document());
     if (aDoc) {
       TDF_Label aRefLab = myRef->Get();
       TDF_Label aFeatureLab = aRefLab.Father();
-      boost::shared_ptr<Model_Data> aData = 
-        boost::dynamic_pointer_cast<Model_Data>(aDoc->feature(aRefLab)->data());
-      return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString());
+      return aDoc->feature(aRefLab);
     }
   }
   // not initialized
-  return boost::shared_ptr<ModelAPI_Attribute>();
+  return FeaturePtr();
 }
 
 Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel)
 {
-  // check the attribute could be already presented in this doc (after load document)
-  if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myID)) {
+  myIsInitialized = theLabel.FindAttribute(TDataStd_Comment::GetID(), myID) == Standard_True;
+  if (!myIsInitialized) {
     // create attribute: not initialized by value yet
-    TDataStd_Comment::Set(theLabel, "");
-    // reference attribute is not set to the label!
+    myID = TDataStd_Comment::Set(theLabel, "");
+    myRef = TDF_Reference::Set(theLabel, theLabel); // not initialized: reference to itself
+  } else {
+    theLabel.FindAttribute(TDF_Reference::GetID(), myRef);
   }
 }