Salome HOME
Add checking that object is used before the delete operation
[modules/shaper.git] / src / Model / Model_Update.cpp
index 9df4d10f0769a687844a6b8c8c1bd8a9fe58b63b..34637a08cbec2f3b3211c7331991a50bc2ae0572 100644 (file)
 #include <ModelAPI_Events.h>
 #include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_AttributeRefList.h>
+#include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_Result.h>
 #include <ModelAPI_Validator.h>
 #include <Events_Loop.h>
 #include <Events_LongOp.h>
 #include <Events_Error.h>
+#include <Config_PropManager.h>
 
 using namespace std;
 
@@ -22,27 +24,78 @@ Model_Update MY_INSTANCE;  /// the only one instance initialized on load of the
 
 Model_Update::Model_Update()
 {
-  Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
-  Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+  Events_Loop* aLoop = Events_Loop::loop();
+  static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
+  aLoop->registerListener(this, kChangedEvent);
+  static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
+  aLoop->registerListener(this, kRebuildEvent);
+  static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
+  aLoop->registerListener(this, kCreatedEvent);
+  static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
+  aLoop->registerListener(this, kUpdatedEvent);
+  static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
+  aLoop->registerListener(this, kOpFinishEvent);
+  static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
+  aLoop->registerListener(this, kOpAbortEvent);
+
+  Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild automatically",
+                                   Config_Prop::Bool, "false");
+  isAutomatic = Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
 }
 
-void Model_Update::processEvent(const Events_Message* theMessage)
+void Model_Update::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
 {
+  static Events_Loop* aLoop = Events_Loop::loop();
+  static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
+  static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
+  static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
+  static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
+  static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
+  static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
+  bool isAutomaticChanged = false;
+  if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
+    isAutomatic = 
+      Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
+  } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
+    if (isAutomatic == false) {
+      isAutomaticChanged = true;
+      isAutomatic = true;
+    }
+  } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent) {
+    boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
+        boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
+    const std::set<ObjectPtr>& anObjs = aMsg->objects();
+    std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
+    for(; anObjIter != anObjs.cend(); anObjIter++)
+      myJustCreatedOrUpdated.insert(*anObjIter);
+  } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
+    myJustCreatedOrUpdated.clear();
+    return;
+  }
+
   if (isExecuted)
     return;  // nothing to do: it is executed now
+
   //Events_LongOp::start(this);
   isExecuted = true;
-  const ModelAPI_ObjectUpdatedMessage* aMsg =
-      dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
-  myInitial = aMsg->objects();
+  list<boost::shared_ptr<ModelAPI_Document> > aDocs;
+  boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
+      boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
+  if (aMsg) myInitial = aMsg->objects();
+  else {
+    myInitial.clear();
+    // on change flag all documents must be updated
+    if (isAutomatic) {
+      aDocs = ModelAPI_Session::get()->allOpenedDocuments();
+    }
+  }
   // collect all documents involved into the update
-  set<boost::shared_ptr<ModelAPI_Document> > aDocs;
   set<boost::shared_ptr<ModelAPI_Object> >::iterator aFIter = myInitial.begin();
   for (; aFIter != myInitial.end(); aFIter++) {
-    aDocs.insert((*aFIter)->document());
+    aDocs.push_back((*aFIter)->document());
   }
   // iterate all features of features-documents to update them (including hidden)
-  set<boost::shared_ptr<ModelAPI_Document> >::iterator aDIter = aDocs.begin();
+  list<boost::shared_ptr<ModelAPI_Document> >::iterator aDIter = aDocs.begin();
   for (; aDIter != aDocs.end(); aDIter++) {
     int aNbFeatures = (*aDIter)->size(ModelAPI_Feature::group(), true);
     for (int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
@@ -54,9 +107,10 @@ void Model_Update::processEvent(const Events_Message* theMessage)
   }
   myUpdated.clear();
   // flush
-  static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
-  Events_Loop::loop()->flush(EVENT_DISP);
+  static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
+  aLoop->flush(EVENT_DISP);
   //Events_LongOp::end(this);
+  if (isAutomaticChanged) isAutomatic = false;
   isExecuted = false;
 }
 
@@ -68,6 +122,7 @@ bool Model_Update::updateFeature(FeaturePtr theFeature)
   // check all features this feature depended on (recursive call of updateFeature)
   bool aMustbeUpdated = myInitial.find(theFeature) != myInitial.end();
   if (theFeature) {  // only real feature contains references to other objects
+    if (theFeature->data()->mustBeUpdated()) aMustbeUpdated = true;
     // references
     list<boost::shared_ptr<ModelAPI_Attribute> > aRefs = theFeature->data()->attributes(
         ModelAPI_AttributeReference::type());
@@ -79,6 +134,25 @@ bool Model_Update::updateFeature(FeaturePtr theFeature)
         aMustbeUpdated = true;
       }
     }
+    // reference to attribute or object
+    list<boost::shared_ptr<ModelAPI_Attribute> > aRefAttrs = theFeature->data()->attributes(
+        ModelAPI_AttributeRefAttr::type());
+    for (aRefsIter = aRefAttrs.begin(); aRefsIter != aRefAttrs.end(); aRefsIter++) {
+      boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
+        boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefsIter);
+      if (!aRef) continue;
+      if (aRef->isObject()) {
+        boost::shared_ptr<ModelAPI_Object> aSub = aRef->object();
+        if (updateObject(aSub)) {
+          aMustbeUpdated = true;
+        }
+      } else if (aRef->attr()) { // reference to the attribute
+        boost::shared_ptr<ModelAPI_Object> aSub = aRef->attr()->owner();
+        if (updateObject(aSub)) {
+          aMustbeUpdated = true;
+        }
+      }
+    }
     // lists of references
     aRefs = theFeature->data()->attributes(ModelAPI_AttributeRefList::type());
     for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
@@ -97,14 +171,33 @@ bool Model_Update::updateFeature(FeaturePtr theFeature)
 
       if (boost::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
           !theFeature->isPersistentResult()) {
-        ModelAPI_ValidatorsFactory* aFactory = ModelAPI_PluginManager::get()->validators();
+        ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
         if (aFactory->validate(theFeature)) {
-          try {
-            theFeature->execute();
-          } catch(...) {
-            Events_Error::send(
-              "Feature " + theFeature->getKind() + " has failed during the execution");
-            theFeature->eraseResults();
+          if (isAutomatic || (myJustCreatedOrUpdated.find(theFeature) != myJustCreatedOrUpdated.end()) ||
+            !theFeature->isPersistentResult() /* execute quick, not persistent results */) {
+            try {
+              theFeature->execute();
+            } catch(...) {
+              Events_Error::send(
+                "Feature " + theFeature->getKind() + " has failed during the execution");
+              theFeature->eraseResults();
+            }
+            theFeature->data()->mustBeUpdated(false);
+            const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
+            std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
+            for (; aRIter != aResults.cend(); aRIter++) {
+              boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
+              aRes->data()->mustBeUpdated(false);
+            }
+          } else {
+            theFeature->data()->mustBeUpdated(true);
+            const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
+            std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
+            for (; aRIter != aResults.cend(); aRIter++) {
+              boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
+              aRes->data()->mustBeUpdated(true);
+            }
+            aMustbeUpdated = false;
           }
         } else {
           theFeature->eraseResults();