Salome HOME
Issue #2052 : really remove results if during the feature execution number of results...
[modules/shaper.git] / src / Model / Model_Application.cpp
index 128f6ee4dffa76228158d713d4a5967ffabb6d8b..e6170af5c12bee586e13fab20b10fce79a011b5a 100644 (file)
@@ -7,10 +7,9 @@
 #include <Model_Application.h>
 #include <Model_Document.h>
 
-IMPLEMENT_STANDARD_HANDLE(Model_Application, TDocStd_Application)
-IMPLEMENT_STANDARD_RTTIEXT(Model_Application, TDocStd_Application)
+#include <ModelAPI_Events.h>
 
-using namespace std;
+IMPLEMENT_STANDARD_RTTIEXT(Model_Application, TDocStd_Application)
 
 static Handle_Model_Application TheApplication = new Model_Application;
 
@@ -21,26 +20,51 @@ Handle(Model_Application) Model_Application::getApplication()
 }
 
 //=======================================================================
-const std::shared_ptr<Model_Document>& Model_Application::getDocument(string theDocID)
+std::shared_ptr<Model_Document> Model_Application::document(const int theDocID)
 {
   if (myDocs.find(theDocID) != myDocs.end())
     return myDocs[theDocID];
+  return std::shared_ptr<Model_Document>(); // not loaded, so return null
+}
 
+//=======================================================================
+void Model_Application::createDocument(const int theDocID)
+{
   static const std::string thePartSetKind("PartSet");
   static const std::string thePartKind("Part");
   std::shared_ptr<Model_Document> aNew(
-    new Model_Document(theDocID, theDocID == "root" ? thePartSetKind : thePartKind));
+    new Model_Document(theDocID, theDocID == 0 ? thePartSetKind : thePartKind));
+  myDocs[theDocID] = aNew;
+
+  aNew->setThis(aNew);
+  static Events_ID anId = ModelAPI_DocumentCreatedMessage::eventId();
+  std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::shared_ptr
+    <ModelAPI_DocumentCreatedMessage>(new ModelAPI_DocumentCreatedMessage(anId, this));
+  aMessage->setDocument(aNew);
+  Events_Loop::loop()->send(aMessage);
+}
+
+//=======================================================================
+bool Model_Application::loadDocument(const std::string theDocName, const int theDocID)
+{
+  static const std::string thePartKind("Part"); // root document is never loaded here
+  std::shared_ptr<Model_Document> aNew(new Model_Document(theDocID, thePartKind));
   myDocs[theDocID] = aNew;
+
+  bool aRes = true;
   // load it if it must be loaded by demand
-  if (myLoadedByDemand.find(theDocID) != myLoadedByDemand.end() && !myPath.empty()) {
-    aNew->load(myPath.c_str());
-    myLoadedByDemand.erase(theDocID);  // done, don't do it anymore
+  if (myLoadedByDemand.find(theDocName) != myLoadedByDemand.end() && !myPath.empty()) {
+    aRes = aNew->load(myPath.c_str(), theDocName.c_str(), aNew);
+    myLoadedByDemand.erase(theDocName);  // done, don't do it anymore
+  } else { // error
+    aRes = false;
   }
 
-  return myDocs[theDocID];
+  return aRes;
 }
 
-void Model_Application::deleteDocument(string theDocID)
+//=======================================================================
+void Model_Application::deleteDocument(const int theDocID)
 {
   if (myDocs.find(theDocID) != myDocs.end()) {
     myDocs[theDocID]->close(true);
@@ -49,18 +73,36 @@ void Model_Application::deleteDocument(string theDocID)
   myLoadedByDemand.clear();
 }
 
+//=======================================================================
 void Model_Application::deleteAllDocuments()
 {
+  std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
+  for(; aDoc != myDocs.end(); aDoc++) {
+    if (aDoc->second->isOpened()) // here is main document was closed before subs and closed subs
+      aDoc->second->close(true);
+  }
   myDocs.clear();
   myLoadedByDemand.clear();
 }
 
 //=======================================================================
-bool Model_Application::hasDocument(std::string theDocID)
+bool Model_Application::hasDocument(const int theDocID)
 {
   return myDocs.find(theDocID) != myDocs.end();
 }
 
+//=======================================================================
+bool Model_Application::hasRoot()
+{
+  return !myDocs.empty();
+}
+
+//=======================================================================
+std::shared_ptr<Model_Document> Model_Application::rootDocument()
+{
+  return myDocs[0];
+}
+
 //=======================================================================
 void Model_Application::setLoadPath(std::string thePath)
 {
@@ -74,9 +116,9 @@ const std::string& Model_Application::loadPath() const
 }
 
 //=======================================================================
-void Model_Application::setLoadByDemand(std::string theID)
+void Model_Application::setLoadByDemand(std::string theID, const int theDocID)
 {
-  myLoadedByDemand.insert(theID);
+  myLoadedByDemand[theID] = theDocID;
 }
 
 //=======================================================================
@@ -89,7 +131,7 @@ bool Model_Application::isLoadByDemand(std::string theID)
 void Model_Application::removeUselessDocuments(
   std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
 {
-  std::map<std::string, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
+  std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
   while(aDoc != myDocs.end()) {
     bool aFound = false;
     std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
@@ -106,6 +148,26 @@ void Model_Application::removeUselessDocuments(
   }
 }
 
+int Model_Application::generateDocumentId()
+{
+  int aResult;
+  // count until the result id is unique
+  for(aResult = int(myDocs.size()); true; aResult++) {
+    if (myDocs.find(aResult) == myDocs.end()) {
+      bool aFound = false;
+      std::map<std::string, int>::iterator aLBDIter = myLoadedByDemand.begin();
+      for(; aLBDIter != myLoadedByDemand.end(); aLBDIter++) {
+        if (aLBDIter->second == aResult) {
+          aFound = true;
+          break;
+        }
+      }
+      if (!aFound) break;
+    }
+  }
+  return aResult;
+}
+
 //=======================================================================
 Model_Application::Model_Application()
 {