Salome HOME
Replace of Events_Error by Events_InfoMessage. Provide storing of non translated...
[modules/shaper.git] / src / Model / Model_Session.cpp
index 04229f2c2257af2b46404547d8a463d4c23fe384..fe35df17d218e244740d6a3b28e6e43407b282e4 100644 (file)
 #include <Model_Validator.h>
 #include <ModelAPI_Events.h>
 #include <Events_Loop.h>
-#include <Events_Error.h>
+#include <Events_InfoMessage.h>
 #include <Config_FeatureMessage.h>
 #include <Config_AttributeMessage.h>
 #include <Config_ValidatorMessage.h>
 #include <Config_ModuleReader.h>
 #include <Config_ValidatorReader.h>
 #include <ModelAPI_ResultPart.h>
+#include <ModelAPI_Tools.h>
 
 #include <TDF_CopyTool.hxx>
 #include <TDF_DataSet.hxx>
@@ -37,13 +38,13 @@ static Model_Session* myImpl = new Model_Session();
 
 bool Model_Session::load(const char* theFileName)
 {
-  bool aRes = ROOT_DOC->load(theFileName, ROOT_DOC);
+  bool aRes = ROOT_DOC->load(theFileName, "root", ROOT_DOC);
   return aRes;
 }
 
 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
 {
-  return ROOT_DOC->save(theFileName, theResults);
+  return ROOT_DOC->save(theFileName, "root", theResults);
 }
 
 void Model_Session::closeAll()
@@ -51,22 +52,30 @@ void Model_Session::closeAll()
   Model_Application::getApplication()->deleteAllDocuments();
 }
 
-void Model_Session::startOperation(const std::string& theId)
+void Model_Session::startOperation(const std::string& theId, const bool theAttachedToNested)
 {
+  myOperationAttachedToNext = theAttachedToNested;
   ROOT_DOC->startOperation();
   ROOT_DOC->operationId(theId);
   static std::shared_ptr<Events_Message> aStartedMsg
     (new Events_Message(Events_Loop::eventByName("StartOperation")));
   Events_Loop::loop()->send(aStartedMsg);
   // remove all useless documents that has been closed: on start of operation undo/redo is cleared
-  std::list<std::shared_ptr<ModelAPI_Document> > aUsedDocs = allOpenedDocuments();
-  Model_Application::getApplication()->removeUselessDocuments(aUsedDocs);
+  // MPV: this code is dangerous now because it may close the document that is activated right now
+  // but not in the list of the opened documents yet (create, delete, undo, activate Part)
+  // later this must be updated by correct usage of uniques IDs of documents, not names of results
+  //std::list<std::shared_ptr<ModelAPI_Document> > aUsedDocs = allOpenedDocuments();
+  //Model_Application::getApplication()->removeUselessDocuments(aUsedDocs);
 }
 
 void Model_Session::finishOperation()
 {
   setCheckTransactions(false);
   ROOT_DOC->finishOperation();
+  if (myOperationAttachedToNext) { // twice, with nested
+    ROOT_DOC->finishOperation();
+    myOperationAttachedToNext = false;
+  }
   setCheckTransactions(true);
 }
 
@@ -74,6 +83,10 @@ void Model_Session::abortOperation()
 {
   setCheckTransactions(false);
   ROOT_DOC->abortOperation();
+  if (myOperationAttachedToNext) { // twice, with nested
+    ROOT_DOC->abortOperation();
+    myOperationAttachedToNext = false;
+  }
   setCheckTransactions(true);
   // here the update mechanism may work after abort, so, supress the warnings about
   // modifications outside of the transactions
@@ -143,9 +156,9 @@ FeaturePtr Model_Session::createFeature(string theFeatureID, Model_Document* the
   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
     std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
     if (!aPlugin.second.empty() && aPlugin.second != theDocOwner->kind()) {
-      Events_Error::send(
-          string("Feature '") + theFeatureID + "' can be created only in document '"
-              + aPlugin.second + "' by the XML definition");
+      Events_InfoMessage("Model_Session",
+          "Feature '%1' can be created only in document '%2' by the XML definition")
+          .arg(theFeatureID).arg(aPlugin.second).send();
       return FeaturePtr();
     }
     myCurrentPluginName = aPlugin.first;
@@ -156,16 +169,16 @@ FeaturePtr Model_Session::createFeature(string theFeatureID, Model_Document* the
     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
       FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
       if (!aCreated) {
-        Events_Error::send(
-            string("Can not initialize feature '") + theFeatureID + "' in plugin '"
-                + myCurrentPluginName + "'");
+        Events_InfoMessage("Model_Session",
+            "Can not initialize feature '%1' in plugin '%2'")
+            .arg(theFeatureID).arg(myCurrentPluginName).send();
       }
       return aCreated;
     } else {
-      Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
+      Events_InfoMessage("Model_Session","Can not load plugin '%1'").arg(myCurrentPluginName).send();
     }
   } else {
-    Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
+    Events_InfoMessage("Model_Session","Feature '%1' not found in any plugin").arg(theFeatureID).send();
   }
 
   return FeaturePtr();  // return nothing
@@ -173,29 +186,29 @@ FeaturePtr Model_Session::createFeature(string theFeatureID, Model_Document* the
 
 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
 {
-  bool aFirstCall = !Model_Application::getApplication()->hasDocument("root");
+  Handle(Model_Application) anApp = Model_Application::getApplication();
+  bool aFirstCall = !anApp->hasRoot();
   if (aFirstCall) {
+    // to be sure that plugins are loaded, even before the first "createFeature" call (in unit tests)
+    LoadPluginsInfo();
     // creation of the root document is always outside of the transaction, so, avoid checking it
     setCheckTransactions(false);
-  }
-  std::shared_ptr<ModelAPI_Document> aDoc = std::shared_ptr<ModelAPI_Document>(
-      Model_Application::getApplication()->getDocument("root"));
-  if (aFirstCall) {
+    anApp->createDocument(0); // 0 is a root ID
     // creation of the root document is always outside of the transaction, so, avoid checking it
     setCheckTransactions(true);
   }
-  return aDoc;
+  return anApp->rootDocument();
 }
 
-std::shared_ptr<ModelAPI_Document> Model_Session::document(std::string theDocID)
+std::shared_ptr<ModelAPI_Document> Model_Session::document(int theDocID)
 {
   return std::shared_ptr<ModelAPI_Document>(
-      Model_Application::getApplication()->getDocument(theDocID));
+      Model_Application::getApplication()->document(theDocID));
 }
 
 bool Model_Session::hasModuleDocument()
 {
-  return Model_Application::getApplication()->hasDocument("root");
+  return Model_Application::getApplication()->hasRoot();
 }
 
 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
@@ -205,18 +218,54 @@ std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
   return myCurrentDoc;
 }
 
+/// makes the last feature in the document as the current
+static void makeCurrentLast(std::shared_ptr<ModelAPI_Document> theDoc) {
+  if (theDoc.get()) {
+    FeaturePtr aLast = std::dynamic_pointer_cast<Model_Document>(theDoc)->lastFeature();
+    // if last is nested into something else, make this something else as last:
+    // otherwise it will look like edition of sub-element, so, the main will be disabled
+    if (aLast.get()) {
+      CompositeFeaturePtr aMain = ModelAPI_Tools::compositeOwner(aLast);
+      while(aMain.get()) {
+        aLast = aMain;
+        aMain = ModelAPI_Tools::compositeOwner(aLast);
+      }
+    }
+    theDoc->setCurrentFeature(aLast, false);
+  }
+}
+
 void Model_Session::setActiveDocument(
   std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
 {
   if (myCurrentDoc != theDoc) {
+    if (myCurrentDoc.get())
+      myCurrentDoc->setActive(false);
+    if (theDoc.get())
+      theDoc->setActive(true);
+
+    std::shared_ptr<ModelAPI_Document> aPrevious = myCurrentDoc;
     myCurrentDoc = theDoc;
     if (theDoc.get() && theSendSignal) {
+      // this must be before the synchronisation call because features in PartSet lower than this
+      // part feature must be disabled and don't recomputed anymore (issue 1156,
+      // translation feature is failed on activation of Part 2)
+      if (isOperation()) { // do it only in transaction, not on opening of document
+        DocumentPtr aRoot = moduleDocument();
+        if (myCurrentDoc != aRoot) {
+          FeaturePtr aPartFeat = ModelAPI_Tools::findPartFeature(aRoot, myCurrentDoc);
+          if (aPartFeat.get()) {
+            aRoot->setCurrentFeature(aPartFeat, false);
+          }
+        }
+      }
       // 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->objects()->synchronizeFeatures(false, true, true);
+        TDF_LabelList anEmptyUpdated;
+        aDoc->objects()->synchronizeFeatures(anEmptyUpdated, true, true, true);
         if (aWasChecked)
             setCheckTransactions(true);
       }
@@ -224,6 +273,17 @@ void Model_Session::setActiveDocument(
           new Events_Message(Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED)));
       Events_Loop::loop()->send(aMsg);
     }
+    // make the current state correct and synchronised in the module and sub-documents
+    if (isOperation()) { // do it only in transaction, not on opening of document
+      if (myCurrentDoc == moduleDocument()) {
+        // make the current feature the latest in root, in previous root current become also last
+        makeCurrentLast(aPrevious);
+        makeCurrentLast(myCurrentDoc);
+      } else {
+        // make the current feature the latest in sub, root current feature becomes this sub
+        makeCurrentLast(myCurrentDoc);
+      }
+    }
   }
 }
 
@@ -237,12 +297,10 @@ std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments
     DocumentPtr anAPIDoc = *aDoc;
     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
     if (aDoc) {
-      const std::set<std::string> aSubs = aDoc->subDocuments(true);
-      std::set<std::string>::const_iterator aSubIter = aSubs.cbegin();
+      const std::set<int> aSubs = aDoc->subDocuments();
+      std::set<int>::const_iterator aSubIter = aSubs.cbegin();
       for(; aSubIter != aSubs.cend(); aSubIter++) {
-        if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
-          aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
-        }
+        aResult.push_back(Model_Application::getApplication()->document(*aSubIter));
       }
     }
   }
@@ -255,11 +313,9 @@ bool Model_Session::isLoadByDemand(const std::string theDocID)
 }
 
 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
-    std::shared_ptr<ModelAPI_Document> theSource, std::string theID)
+    std::shared_ptr<ModelAPI_Document> theSource, const int theDestID)
 {
-  // create a new document
-  std::shared_ptr<Model_Document> aNew = std::dynamic_pointer_cast<Model_Document>(
-      Model_Application::getApplication()->getDocument(theID));
+  std::shared_ptr<Model_Document> aNew = Model_Application::getApplication()->document(theDestID);
   // make a copy of all labels
   TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
       .Father();
@@ -271,7 +327,8 @@ std::shared_ptr<ModelAPI_Document> Model_Session::copy(
   aRT->SetRelocation(aSourceRoot, aTargetRoot);
   TDF_CopyTool::Copy(aDS, aRT);
 
-  aNew->objects()->synchronizeFeatures(false, true, true);
+  TDF_LabelList anEmptyUpdated;
+  aNew->objects()->synchronizeFeatures(anEmptyUpdated, true, true, true);
   return aNew;
 }
 
@@ -279,6 +336,7 @@ Model_Session::Model_Session()
 {
   myPluginsInfoLoaded = false;
   myCheckTransactions = true;
+  myOperationAttachedToNext = false;
   ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
   // register the configuration reading listener
   Events_Loop* aLoop = Events_Loop::loop();
@@ -300,7 +358,7 @@ void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessa
     const std::shared_ptr<Config_FeatureMessage> aMsg = 
       std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
     if (aMsg) {
-      // proccess the plugin info, load plugin
+      // process the plugin info, load plugin
       if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
         myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
           aMsg->pluginLibrary(), aMsg->documentKind());
@@ -336,16 +394,32 @@ 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");
+      Events_InfoMessage("Model_Session", "Modification of data structure outside of the transaction").send();
     // 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()) 
+          aDeleted->groups().find(ModelAPI_ResultPart::group()) != aDeleted->groups().end())
       {
-        setActiveDocument(moduleDocument());
+         // check that the current feature of the session is still the active Part (even disabled)
+        bool aFound = false;
+        FeaturePtr aCurrentPart = moduleDocument()->currentFeature(true);
+        if (aCurrentPart.get()) {
+          const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aCurrentPart->results();
+          std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
+          for(; !aFound && aRes != aResList.end(); aRes++) {
+            ResultPartPtr aPRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
+            if (aPRes.get() && aPRes->isActivated() && aPRes->partDoc() == activeDocument()) {
+              aFound = true;
+
+            }
+          }
+        }
+        if (!aFound) { // if not, the part was removed, so activate the module document
+          setActiveDocument(moduleDocument());
+        }
       }
     }
   }
@@ -389,3 +463,8 @@ ModelAPI_ValidatorsFactory* Model_Session::validators()
   static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
   return aFactory;
 }
+
+int Model_Session::transactionID()
+{
+  return ROOT_DOC->transactionID();
+}