Salome HOME
Initial version of redesign of working with results
[modules/shaper.git] / src / Model / Model_Data.cpp
index 0bb5a12c82268f7e9075e58f2335eaaf98ad68af..53e34ce9746f399f5771cb8b4fad3c63091981eb 100644 (file)
@@ -5,10 +5,17 @@
 #include <Model_Data.h>
 #include <Model_AttributeDocRef.h>
 #include <Model_AttributeDouble.h>
+#include <Model_AttributeReference.h>
+#include <Model_AttributeRefAttr.h>
+#include <Model_AttributeRefList.h>
+#include <Model_AttributeBoolean.h>
 #include <GeomData_Point.h>
 #include <GeomData_Point2D.h>
 #include <GeomData_Dir.h>
 #include <TDataStd_Name.hxx>
+#include "Model_Events.h"
+#include <Events_Loop.h>
+#include <Events_Error.h>
 
 using namespace std;
 
@@ -21,7 +28,7 @@ void Model_Data::setLabel(TDF_Label& theLab)
   myLab = theLab;
 }
 
-string Model_Data::getName()
+string Model_Data::name()
 {
   Handle(TDataStd_Name) aName;
   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
@@ -31,7 +38,20 @@ string Model_Data::getName()
 
 void Model_Data::setName(string theName)
 {
-  TDataStd_Name::Set(myLab, theName.c_str());
+  bool isModified = false;
+  Handle(TDataStd_Name) aName;
+  if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
+    TDataStd_Name::Set(myLab, theName.c_str());
+    isModified = true;
+  } else {
+    isModified = !aName->Get().IsEqual(theName.c_str());
+    if (isModified)
+      aName->Set(theName.c_str());
+  }
+  if (isModified) {
+    static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
+    ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent, false);
+  }
 }
 
 void Model_Data::addAttribute(string theID, string theAttrType)
@@ -42,17 +62,28 @@ void Model_Data::addAttribute(string theID, string theAttrType)
     anAttr = new Model_AttributeDocRef(anAttrLab);
   else if (theAttrType == ModelAPI_AttributeDouble::type())
     anAttr = new Model_AttributeDouble(anAttrLab);
+  else if (theAttrType == ModelAPI_AttributeReference::type())
+    anAttr = new Model_AttributeReference(anAttrLab);
+  else if (theAttrType == ModelAPI_AttributeRefAttr::type())
+    anAttr = new Model_AttributeRefAttr(anAttrLab);
+  else if (theAttrType == ModelAPI_AttributeRefList::type())
+    anAttr = new Model_AttributeRefList(anAttrLab);
   else if (theAttrType == GeomData_Point::type())
     anAttr = new GeomData_Point(anAttrLab);
   else if (theAttrType == GeomData_Dir::type())
     anAttr = new GeomData_Dir(anAttrLab);
   else if (theAttrType == GeomData_Point2D::type())
     anAttr = new GeomData_Point2D(anAttrLab);
+  else if (theAttrType == Model_AttributeBoolean::type())
+    anAttr = new Model_AttributeBoolean(anAttrLab);
 
-  if (anAttr)
+  if (anAttr) {
     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
-  else
-    ; // TODO: generate error on unknown attribute request and/or add mechanism for customization
+    anAttr->setObject(myObject);
+  }
+  else {
+    Events_Error::send("Can not create unknown type of attribute " + theAttrType);
+  }
 }
 
 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string theID)
@@ -85,6 +116,66 @@ boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string theID)
   return aRes;
 }
 
+boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string theID)
+{
+  map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
+  if (aFound == myAttrs.end()) {
+    // TODO: generate error on unknown attribute request and/or add mechanism for customization
+    return boost::shared_ptr<ModelAPI_AttributeBoolean>();
+  }
+  boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = 
+    boost::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aFound->second);
+  if (!aRes) {
+    // TODO: generate error on invalid attribute type request
+  }
+  return aRes;
+}
+
+boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const string theID)
+{
+  map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
+  if (aFound == myAttrs.end()) {
+    // TODO: generate error on unknown attribute request and/or add mechanism for customization
+    return boost::shared_ptr<ModelAPI_AttributeReference>();
+  }
+  boost::shared_ptr<ModelAPI_AttributeReference> aRes = 
+    boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aFound->second);
+  if (!aRes) {
+    // TODO: generate error on invalid attribute type request
+  }
+  return aRes;
+}
+
+boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const string theID)
+{
+  map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
+  if (aFound == myAttrs.end()) {
+    // TODO: generate error on unknown attribute request and/or add mechanism for customization
+    return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
+  }
+  boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = 
+    boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aFound->second);
+  if (!aRes) {
+    // TODO: generate error on invalid attribute type request
+  }
+  return aRes;
+}
+
+boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const string theID)
+{
+  map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
+  if (aFound == myAttrs.end()) {
+    // TODO: generate error on unknown attribute request and/or add mechanism for customization
+    return boost::shared_ptr<ModelAPI_AttributeRefList>();
+  }
+  boost::shared_ptr<ModelAPI_AttributeRefList> aRes = 
+    boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aFound->second);
+  if (!aRes) {
+    // TODO: generate error on invalid attribute type request
+  }
+  return aRes;
+}
+
 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string theID)
 {
   boost::shared_ptr<ModelAPI_Attribute> aResult;
@@ -92,3 +183,46 @@ boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string th
     return aResult;
   return myAttrs[theID];
 }
+
+const string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute> theAttr)
+{
+  map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
+  for(; anAttr != myAttrs.end(); anAttr++) {
+    if (anAttr->second == theAttr) return anAttr->first;
+  }
+  // not found
+  static string anEmpty;
+  return anEmpty;
+}
+
+bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data> theData)
+{
+  boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
+  if (aData)
+    return myLab.IsEqual(aData->myLab) == Standard_True;
+  return false;
+}
+
+bool Model_Data::isValid()
+{
+  return !myLab.IsNull() && myLab.HasAttribute();
+}
+
+list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const string theType)
+{
+  list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
+  map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = myAttrs.begin();
+  for(; anAttrsIter != myAttrs.end(); anAttrsIter++) {
+    if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
+      aResult.push_back(anAttrsIter->second);
+    }
+  }
+  return aResult;
+}
+
+void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
+{
+  theAttr->setInitialized();
+  static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
+  ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
+}