Salome HOME
Correctly close the document on opening the new one
authormpv <mikhail.ponikarov@opencascade.com>
Tue, 11 Nov 2014 12:34:06 +0000 (15:34 +0300)
committermpv <mikhail.ponikarov@opencascade.com>
Tue, 11 Nov 2014 12:34:06 +0000 (15:34 +0300)
src/Model/Model_Application.cpp
src/Model/Model_Application.h
src/Model/Model_Document.cpp
src/Model/Model_Document.h
src/Model/Model_Session.cpp
src/Model/Model_Session.h
src/ModelAPI/ModelAPI_Document.h
src/ModelAPI/ModelAPI_Session.h
src/XGUI/XGUI_PartDataModel.cpp
src/XGUI/XGUI_Workshop.cpp

index 23ecf05ae7f2109317f435566ad88d2b17a63c2a..d53dc665c679dcd14f69eca5b2881a10655b910b 100644 (file)
@@ -40,7 +40,17 @@ const boost::shared_ptr<Model_Document>& Model_Application::getDocument(string t
 
 void Model_Application::deleteDocument(string theDocID)
 {
-  myDocs.erase(theDocID);
+  if (myDocs.find(theDocID) != myDocs.end()) {
+    myDocs[theDocID]->close(true);
+    myDocs.erase(theDocID);
+  }
+  myLoadedByDemand.clear();
+}
+
+void Model_Application::deleteAllDocuments()
+{
+  myDocs.clear();
+  myLoadedByDemand.clear();
 }
 
 //=======================================================================
index ae68024eec9443ce54dd8f0d12ccf87887f4752e..d9eb4bb200cc3b1ece948bda72524aed6d5707d7 100644 (file)
@@ -36,6 +36,8 @@ class Model_Application : public TDocStd_Application
   MODEL_EXPORT bool hasDocument(std::string theDocID);
   //! Deletes the document from the application
   MODEL_EXPORT void deleteDocument(std::string theDocID);
+  //! Deletes all documents existing in the application
+  MODEL_EXPORT void deleteAllDocuments();
 
   //! Set path for the loaded by demand documents
   void setLoadPath(std::string thePath);
index a224cc99f153f3bdae7f967e0d7b7f5dabeecbff..06ec246f36628794ecb0fccc6cf4b68aa5836418 100644 (file)
@@ -202,7 +202,7 @@ bool Model_Document::save(const char* theFileName, std::list<std::string>& theRe
   return isDone;
 }
 
-void Model_Document::close()
+void Model_Document::close(const bool theForever)
 {
   boost::shared_ptr<ModelAPI_Session> aPM = Model_Session::get();
   if (this != aPM->moduleDocument().get() && this == aPM->activeDocument().get()) {
@@ -211,14 +211,36 @@ void Model_Document::close()
   // close all subs
   std::set<std::string>::iterator aSubIter = mySubs.begin();
   for (; aSubIter != mySubs.end(); aSubIter++)
-    subDoc(*aSubIter)->close();
+    subDoc(*aSubIter)->close(theForever);
   mySubs.clear();
-  // close this only if it is module document, otherwise it can be undoed
-  if (this == aPM->moduleDocument().get()) {
+
+  // close for thid document needs no transaction in this document
+  boost::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(false);
+
+  // delete all features of this document
+  boost::shared_ptr<ModelAPI_Document> aThis = 
+    Model_Application::getApplication()->getDocument(myID);
+  Events_Loop* aLoop = Events_Loop::loop();
+  NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myObjs);
+  for(; aFeaturesIter.More(); aFeaturesIter.Next()) {
+    FeaturePtr aFeature = aFeaturesIter.Value();
+    static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
+    ModelAPI_EventCreator::get()->sendDeleted(aThis, ModelAPI_Feature::group());
+    ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
+    aFeature->eraseResults();
+    aFeature->erase();
+  }
+  myObjs.Clear();
+  aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
+  aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
+
+  // close all only if it is really asked, otherwise it can be undoed/redoed
+  if (theForever) {
     if (myDoc->CanClose() == CDM_CCS_OK)
       myDoc->Close();
-    Model_Application::getApplication()->deleteDocument(myID);
   }
+
+  boost::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(true);
 }
 
 void Model_Document::startOperation()
@@ -629,6 +651,8 @@ int Model_Document::size(const std::string& theGroupID, const bool theHidden)
     for (; aLabIter.More(); aLabIter.Next()) {
       TDF_Label aFLabel = aLabIter.Value()->Label();
       FeaturePtr aFeature = feature(aFLabel);
+      if (!aFeature) // may be on close
+        continue;
       const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
       for (; aRIter != aResults.cend(); aRIter++) {
index d8743f0994d03ffe707df3d26605251531c81f6f..05d7d76eb39afc65b01e095272fa50258e97e9fb 100644 (file)
@@ -49,7 +49,8 @@ class Model_Document : public ModelAPI_Document
   MODEL_EXPORT virtual bool save(const char* theFileName, std::list<std::string>& theResults);
 
   //! Removes document data
-  MODEL_EXPORT virtual void close();
+  //! \param theForever if it is false, document is just hiden (to keep possibility make it back on Undo/Redo)
+  MODEL_EXPORT virtual void close(const bool theForever = false);
 
   //! Starts a new operation (opens a tansaction)
   MODEL_EXPORT virtual void startOperation();
index 23e7a2621633170b0ecf97ec6034c8a62dfc54a1..2c9a5abad0de823444d7842d3701867fa3e319dd 100644 (file)
@@ -39,6 +39,12 @@ bool Model_Session::save(const char* theFileName, std::list<std::string>& theRes
   return ROOT_DOC->save(theFileName, theResults);
 }
 
+void Model_Session::closeAll()
+{
+  ROOT_DOC->close(true);
+  Model_Application::getApplication()->deleteAllDocuments();
+}
+
 void Model_Session::startOperation()
 {
   ROOT_DOC->startOperation();
index fd4f9c0b5ac3e35a1800082dbba8b67bf2036521..5186a2497f7da6ad3ccc9da9ea1b707089db7f44 100644 (file)
@@ -43,6 +43,9 @@ class Model_Session : public ModelAPI_Session, public Events_Listener
   //! \returns true if file was stored successfully
   MODEL_EXPORT virtual bool save(const char* theFileName, std::list<std::string>& theResults);
 
+  //! Closes all documents
+  MODEL_EXPORT virtual void closeAll();
+
   //! Starts a new operation (opens a tansaction)
   MODEL_EXPORT virtual void startOperation();
   //! Finishes the previously started operation (closes the transaction)
index 703ab0f881cebeb997bbf62d9fe3b5d4747ec1f8..0b67c44f1b7e526ac6297b1c62850362cdbdf3a1 100644 (file)
@@ -35,7 +35,8 @@ public:
   virtual const std::string& kind() const = 0;
 
   //! Removes document data
-  virtual void close() = 0;
+  //! \param theForever if it is false, document is just hiden (to keep possibility make it back on Undo/Redo)
+  virtual void close(const bool theForever = false) = 0;
 
   //! Adds to the document the new feature of the given feature id
   //! \param creates feature and puts it in the document (if it is not action)
index c97105421512fd83b16efbab28bdd012501b3666..95199bbabb42494f2921d08e7c342d8a08e989d9 100644 (file)
@@ -40,6 +40,9 @@ class MODELAPI_EXPORT ModelAPI_Session
   //! \returns true if file was stored successfully
   virtual bool save(const char* theFileName, std::list<std::string>& theResults) = 0;
 
+  //! Closes all documents
+  virtual void closeAll() = 0;
+
   //! Starts a new operation (opens a tansaction)
   virtual void startOperation() = 0;
   //! Finishes the previously started operation (closes the transaction)
index 42d22cce9ed799b35889a657a8a086d5c7055677..75b57303445d5a89c67ab9e1d1ba8c8066ac7dc4 100644 (file)
@@ -448,7 +448,9 @@ DocumentPtr XGUI_PartDataModel::partDocument() const
   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
   ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), myId);
   ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
-  return aPart->partDoc();
+  if (aPart)
+    return aPart->partDoc();
+  return DocumentPtr(); // null if not found
 }
 
 ObjectPtr XGUI_PartDataModel::object(const QModelIndex& theIndex) const
index ad21e62aa9685731d12564fe68596ff707de9b4f..12e6bb7b37eea54deb7d68da9449a2a487e27c58 100644 (file)
@@ -726,7 +726,7 @@ void XGUI_Workshop::onOpen()
     } else if (anAnswer == QMessageBox::Cancel) {
       return;
     }
-    aSession->moduleDocument()->close();
+    aSession->closeAll();
     myCurrentDir = "";
   }
 
@@ -1327,6 +1327,6 @@ void XGUI_Workshop::closeDocument()
   objectBrowser()->clearContent();
 
   SessionPtr aMgr = ModelAPI_Session::get();
-  aMgr->moduleDocument()->close();
+  aMgr->closeAll();
   objectBrowser()->clearContent();
 }