Salome HOME
Merge branch 'Dev_0.7.1' of newgeom:newgeom.git into Dev_0.7.1
[modules/shaper.git] / src / Model / Model_Session.cpp
index 159b7a2f678b19e60ac6fac4862c1881100dbeca..816b87f750d1e391f44a92ef939a0981d5214289 100644 (file)
 #include <Model_Application.h>
 #include <Model_Events.h>
 #include <Model_Validator.h>
+#include <ModelAPI_Events.h>
 #include <Events_Loop.h>
 #include <Events_Error.h>
 #include <Config_FeatureMessage.h>
 #include <Config_AttributeMessage.h>
 #include <Config_ValidatorMessage.h>
 #include <Config_ModuleReader.h>
+#include <ModelAPI_ResultPart.h>
 
 #include <TDF_CopyTool.hxx>
 #include <TDF_DataSet.hxx>
@@ -33,7 +35,8 @@ static Model_Session* myImpl = new Model_Session();
 
 bool Model_Session::load(const char* theFileName)
 {
-  return ROOT_DOC->load(theFileName);
+  bool aRes = ROOT_DOC->load(theFileName);
+  return aRes;
 }
 
 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
@@ -60,12 +63,16 @@ void Model_Session::startOperation()
 
 void Model_Session::finishOperation()
 {
+  setCheckTransactions(false);
   ROOT_DOC->finishOperation();
+  setCheckTransactions(true);
 }
 
 void Model_Session::abortOperation()
 {
+  setCheckTransactions(false);
   ROOT_DOC->abortOperation();
+  setCheckTransactions(true);
   // here the update mechanism may work after abort, so, supress the warnings about
   // modifications outside of the transactions
   bool aWasCheck = myCheckTransactions;
@@ -94,7 +101,9 @@ bool Model_Session::canUndo()
 
 void Model_Session::undo()
 {
+  setCheckTransactions(false);
   ROOT_DOC->undo();
+  setCheckTransactions(true);
 }
 
 bool Model_Session::canRedo()
@@ -104,7 +113,9 @@ bool Model_Session::canRedo()
 
 void Model_Session::redo()
 {
+  setCheckTransactions(false);
   ROOT_DOC->redo();
+  setCheckTransactions(true);
 }
 
 FeaturePtr Model_Session::createFeature(string theFeatureID)
@@ -176,8 +187,18 @@ void Model_Session::setActiveDocument(
 {
   if (myCurrentDoc != theDoc) {
     myCurrentDoc = theDoc;
-    if (theSendSignal) {
-      static std::shared_ptr<Events_Message> aMsg(new Events_Message(Events_Loop::eventByName("CurrentDocumentChanged")));
+    if (theDoc.get() && theSendSignal) {
+      // syncronize the document: it may be just opened or opened but removed before
+      std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theDoc);
+      if (aDoc.get()) {
+        bool aWasChecked = myCheckTransactions;
+        setCheckTransactions(false);
+        aDoc->synchronizeFeatures(false, true);
+        if (aWasChecked)
+            setCheckTransactions(true);
+      }
+      static std::shared_ptr<Events_Message> aMsg(
+          new Events_Message(Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED)));
       Events_Loop::loop()->send(aMsg);
     }
   }
@@ -233,7 +254,8 @@ Model_Session::Model_Session()
   ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
   // register the configuration reading listener
   Events_Loop* aLoop = Events_Loop::loop();
-  static const Events_ID kFeatureEvent = Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
+  static const Events_ID kFeatureEvent = 
+    Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
   aLoop->registerListener(this, kFeatureEvent);
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
@@ -243,7 +265,8 @@ Model_Session::Model_Session()
 
 void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessage)
 {
-  static const Events_ID kFeatureEvent = Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
+  static const Events_ID kFeatureEvent = 
+    Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
   static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
   if (theMessage->eventID() == kFeatureEvent) {
     const std::shared_ptr<Config_FeatureMessage> aMsg = 
@@ -283,6 +306,17 @@ void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessa
   } else {  // create/update/delete
     if (myCheckTransactions && !isOperation())
       Events_Error::send("Modification of data structure outside of the transaction");
+    // if part is deleted, make the root as the current document (on undo of Parts creations)
+    static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
+    if (theMessage->eventID() == kDeletedEvent) {
+      std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
+        std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
+      if (aDeleted && 
+          aDeleted->groups().find(ModelAPI_ResultPart::group()) != aDeleted->groups().end()) 
+      {
+        setActiveDocument(moduleDocument());
+      }
+    }
   }
 }