Salome HOME
Make automatic/manual rebuild property and connect the "Rebuild" button
[modules/shaper.git] / src / Model / Model_Update.cpp
index 848f38b80e2d008f29d8e3563159665726dd51f3..70d97733047d062ec3e411032b61fae96c7fba0a 100644 (file)
 #include <Events_Loop.h>
 #include <Events_LongOp.h>
 #include <Events_Error.h>
+#include <Config_PropManager.h>
 
 using namespace std;
 
 Model_Update MY_INSTANCE;  /// the only one instance initialized on load of the library
 
-Model_Update::Model_Update()
+Model_Update::Model_Update() : isCreated(false)
 {
+  static const Events_ID kChangedEvent = Events_Loop::loop()->eventByName("PreferenceChanged");
+  Events_Loop::loop()->registerListener(this, kChangedEvent);
+  static const Events_ID kRebuildEvent = Events_Loop::loop()->eventByName("Rebuild");
+  Events_Loop::loop()->registerListener(this, kRebuildEvent);
   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+  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 boost::shared_ptr<Events_Message>& theMessage)
 {
+  static const Events_ID kChangedEvent = Events_Loop::loop()->eventByName("PreferenceChanged");
+  static const Events_ID kRebuildEvent = Events_Loop::loop()->eventByName("Rebuild");
+  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;
+    }
+  }
+
   if (isExecuted)
     return;  // nothing to do: it is executed now
+  // execute just created features anyway
+  isCreated = theMessage->eventID() == Events_Loop::eventByName(EVENT_OBJECT_CREATED);
+
   //Events_LongOp::start(this);
   isExecuted = true;
+  list<boost::shared_ptr<ModelAPI_Document> > aDocs;
   boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
       boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
-  myInitial = aMsg->objects();
+  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++) {
@@ -58,6 +89,8 @@ void Model_Update::processEvent(const boost::shared_ptr<Events_Message>& theMess
   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
   Events_Loop::loop()->flush(EVENT_DISP);
   //Events_LongOp::end(this);
+  if (isAutomaticChanged) isAutomatic = false;
+  isCreated = false;
   isExecuted = false;
 }
 
@@ -69,6 +102,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());
@@ -119,12 +153,19 @@ bool Model_Update::updateFeature(FeaturePtr theFeature)
           !theFeature->isPersistentResult()) {
         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 || (isCreated && myInitial.find(theFeature) != myInitial.end()) ||
+            !theFeature->isPersistentResult() /* execute quick, not persistent results */) {
+            try {
+              theFeature->execute();
+              theFeature->data()->mustBeUpdated(false);
+            } catch(...) {
+              Events_Error::send(
+                "Feature " + theFeature->getKind() + " has failed during the execution");
+              theFeature->eraseResults();
+            }
+          } else {
+            theFeature->data()->mustBeUpdated(true);
+            aMustbeUpdated = false;
           }
         } else {
           theFeature->eraseResults();