Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / Model / Model_Document.cxx
index 9eebe15d103f5f84e9dab52c49ae2b72ba55cd18..22641f4b3d7665376060fb4f3221448119503efd 100644 (file)
@@ -4,20 +4,23 @@
 
 #include <Model_Document.h>
 #include <ModelAPI_Feature.h>
-#include <Model_Object.h>
+#include <Model_Data.h>
 #include <Model_Application.h>
 #include <Model_PluginManager.h>
 #include <Model_Iterator.h>
+#include <Model_Events.h>
 #include <Event_Loop.h>
 
 #include <TDataStd_Integer.hxx>
 #include <TDataStd_Comment.hxx>
 #include <TDF_ChildIDIterator.hxx>
 
+#include <climits>
+
 static const int UNDO_LIMIT = 10; // number of possible undo operations
 
 static const int TAG_GENERAL = 1; // general properties tag
-static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (Root for Model_ObjectsMgr)
+static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (Root for Model_DatasMgr)
 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for Model_History)
 
 using namespace std;
@@ -219,9 +222,10 @@ void Model_Document::addFeature(const std::shared_ptr<ModelAPI_Feature> theFeatu
   const std::string& aGroup = theFeature->getGroup();
   TDF_Label aGroupLab = groupLabel(aGroup);
   TDF_Label anObjLab = aGroupLab.NewChild();
-  std::shared_ptr<Model_Object> aData(new Model_Object);
+  std::shared_ptr<Model_Data> aData(new Model_Data);
   aData->setLabel(anObjLab);
-  aData->setDocument(Model_Application::getApplication()->getDocument(myID));
+  shared_ptr<ModelAPI_Document> aThis = Model_Application::getApplication()->getDocument(myID);
+  aData->setDocument(aThis);
   theFeature->setData(aData);
   setUniqueName(theFeature);
   theFeature->initAttributes();
@@ -231,9 +235,9 @@ void Model_Document::addFeature(const std::shared_ptr<ModelAPI_Feature> theFeatu
   TDataStd_Integer::Set(anObjLab, myFeatures[aGroup].size());
   myFeatures[aGroup].push_back(theFeature);
 
-  // event: model is updated
-  static Event_ID anEvent = Event_Loop::eventByName(EVENT_FEATURE_UPDATED);
-  ModelAPI_FeatureUpdatedMessage aMsg(theFeature);
+  // event: feature is added
+  static Event_ID anEvent = Event_Loop::eventByName(EVENT_FEATURE_CREATED);
+  ModelAPI_FeatureUpdatedMessage aMsg(aThis, theFeature, anEvent);
   Event_Loop::loop()->send(aMsg);
 }
 
@@ -255,7 +259,7 @@ int Model_Document::featureIndex(shared_ptr<ModelAPI_Feature> theFeature)
   if (theFeature->data()->document().get() != this) {
     return theFeature->data()->document()->featureIndex(theFeature);
   }
-  shared_ptr<Model_Object> aData = dynamic_pointer_cast<Model_Object>(theFeature->data());
+  shared_ptr<Model_Data> aData = dynamic_pointer_cast<Model_Data>(theFeature->data());
   Handle(TDataStd_Integer) aFeatureIndex;
   if (aData->label().FindAttribute(TDataStd_Integer::GetID(), aFeatureIndex)) {
     return aFeatureIndex->Get();
@@ -348,6 +352,7 @@ void Model_Document::setUniqueName(
 
 void Model_Document::synchronizeFeatures()
 {
+  shared_ptr<ModelAPI_Document> aThis = Model_Application::getApplication()->getDocument(myID);
   // iterate groups labels
   TDF_ChildIDIterator aGroupsIter(myDoc->Main().FindChild(TAG_OBJECTS),
     TDataStd_Comment::GetID(), Standard_False);
@@ -361,9 +366,13 @@ void Model_Document::synchronizeFeatures()
   }
   // delete all groups left after the data model groups iteration
   while(aGroupNamesIter != myGroupsNames.end()) {
-    myFeatures.erase(*aGroupNamesIter);
-    myGroups.erase(*aGroupNamesIter);
+    string aGroupName = *aGroupNamesIter;
+    myFeatures.erase(aGroupName);
+    myGroups.erase(aGroupName);
     aGroupNamesIter = myGroupsNames.erase(aGroupNamesIter);
+    // say that features were deleted from group
+    ModelAPI_FeatureDeletedMessage aMsg(aThis, aGroupName);
+    Event_Loop::loop()->send(aMsg);
   }
   // create new groups basing on the following data model update
   for(; aGroupsIter.More(); aGroupsIter.Next()) {
@@ -386,10 +395,10 @@ void Model_Document::synchronizeFeatures()
     TDF_ChildIDIterator aFLabIter(
       aGroupsIter.Value()->Label(), TDataStd_Comment::GetID(), Standard_False);
     while(aFIter != aFeatures.end() || aFLabIter.More()) {
-      static const int INFINITE_TAG = MAXINT; // no label means that it exists somwhere in infinite
+      static const int INFINITE_TAG = INT_MAX; // no label means that it exists somwhere in infinite
       int aFeatureTag = INFINITE_TAG; 
       if (aFIter != aFeatures.end()) { // existing tag for feature
-        shared_ptr<Model_Object> aData = dynamic_pointer_cast<Model_Object>((*aFIter)->data());
+        shared_ptr<Model_Data> aData = dynamic_pointer_cast<Model_Data>((*aFIter)->data());
         aFeatureTag = aData->label().Tag();
       }
       int aDSTag = INFINITE_TAG; 
@@ -398,21 +407,24 @@ void Model_Document::synchronizeFeatures()
       }
       if (aDSTag > aFeatureTag) { // feature is removed
         aFIter = aFeatures.erase(aFIter);
+        // event: model is updated
+        ModelAPI_FeatureDeletedMessage aMsg(aThis, aGroupName);
+        Event_Loop::loop()->send(aMsg);
       } else if (aDSTag < aFeatureTag) { // a new feature is inserted
         // create a feature
         shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_PluginManager::get()->createFeature(
           TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(
           aFLabIter.Value())->Get()).ToCString());
 
-        std::shared_ptr<Model_Object> aData(new Model_Object);
+        std::shared_ptr<Model_Data> aData(new Model_Data);
         TDF_Label aLab = aFLabIter.Value()->Label();
         aData->setLabel(aLab);
         aData->setDocument(Model_Application::getApplication()->getDocument(myID));
         aFeature->setData(aData);
         aFeature->initAttributes();
         // event: model is updated
-        static Event_ID anEvent = Event_Loop::eventByName(EVENT_FEATURE_UPDATED);
-        ModelAPI_FeatureUpdatedMessage aMsg(aFeature);
+        static Event_ID anEvent = Event_Loop::eventByName(EVENT_FEATURE_CREATED);
+        ModelAPI_FeatureUpdatedMessage aMsg(aThis, aFeature, anEvent);
         Event_Loop::loop()->send(aMsg);
 
         if (aFIter == aFeatures.end()) {
@@ -431,19 +443,3 @@ void Model_Document::synchronizeFeatures()
     }
   }
 }
-
-ModelAPI_FeatureUpdatedMessage::ModelAPI_FeatureUpdatedMessage(
-  shared_ptr<ModelAPI_Feature> theFeature)
-  : Event_Message(messageId(), 0), myFeature(theFeature)
-{}
-
-const Event_ID ModelAPI_FeatureUpdatedMessage::messageId()
-{
-  static Event_ID MY_ID = Event_Loop::eventByName("FeatureUpdated");
-  return MY_ID;
-}
-
-shared_ptr<ModelAPI_Feature> ModelAPI_FeatureUpdatedMessage::feature()
-{
-  return myFeature;
-}