Salome HOME
Implementation of open/save documents functionality
authormpv <mikhail.ponikarov@opencascade.com>
Thu, 15 May 2014 05:04:31 +0000 (09:04 +0400)
committermpv <mikhail.ponikarov@opencascade.com>
Thu, 15 May 2014 05:04:31 +0000 (09:04 +0400)
src/Model/Model_Application.cpp
src/Model/Model_Application.h
src/Model/Model_AttributeDocRef.cpp
src/Model/Model_Document.cpp
src/XGUI/XGUI_Workshop.cpp

index 166b4e1a173f3812013eabdce23d4aec14c8f8f2..df32ef977d7c599acfbb7dbadc23c99492533fb7 100644 (file)
@@ -26,6 +26,12 @@ const boost::shared_ptr<Model_Document>& Model_Application::getDocument(string t
 
   boost::shared_ptr<Model_Document> aNew(new Model_Document(theDocID));
   myDocs[theDocID] = aNew;
+  // 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
+  }
+
   return myDocs[theDocID];
 }
 
@@ -40,6 +46,18 @@ bool Model_Application::hasDocument(std::string theDocID)
   return myDocs.find(theDocID) != myDocs.end();
 }
 
+//=======================================================================
+void Model_Application::setLoadPath(std::string thePath)
+{
+  myPath = thePath;
+}
+
+//=======================================================================
+void Model_Application::setLoadByDemand(std::string theID)
+{
+  myLoadedByDemand.insert(theID);
+}
+
 //=======================================================================
 Model_Application::Model_Application()
 {
index ddd3155bd0ed9e599b34ebca545df95da6eca10c..9281f9ce6e4773565072d1c697508f315e09e63c 100644 (file)
@@ -36,6 +36,11 @@ public:
   //! Deletes the document from the application
   MODEL_EXPORT void deleteDocument(std::string theDocID);
 
+  //! Set path for the loaded by demand documents
+  void setLoadPath(std::string thePath);
+  //! Defines that specified document must be loaded by demand
+  void setLoadByDemand(std::string theID);
+
 public:
   // Redefined OCAF methods
   //! Return name of resource (i.e. "Standard")
@@ -51,6 +56,10 @@ public:
 private:
   /// Map from string identifiers to created documents of an application
   std::map<std::string, boost::shared_ptr<Model_Document> > myDocs;
+  /// Path for the loaded by demand documents
+  std::string myPath;
+  /// Path for the loaded by demand documents
+  std::set<std::string> myLoadedByDemand;
 };
 
 #endif
index 93c8af71346ce82569f5cea6cf867f8b0d8ac55b..dfb307f4dbfe0981cc8915fc838cea6a798540a4 100644 (file)
@@ -36,5 +36,11 @@ Model_AttributeDocRef::Model_AttributeDocRef(TDF_Label& theLabel)
   if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myComment)) {
     // create attribute: not initialized by value yet, just empty string
     myComment = TDataStd_Comment::Set(theLabel, "");
+  } else { // document was already referenced: try to set it as loaded by demand
+    Handle(Model_Application) anApp = Model_Application::getApplication();
+    string anID(TCollection_AsciiString(myComment->Get()).ToCString());
+    if (!anApp->hasDocument(anID)) {
+      anApp->setLoadByDemand(anID);
+    }
   }
 }
index 8c305effa4f45ff18eee37728e9f1cf8a144e732..9d8c2c12a664609e8630e685e1a8e840fa24036c 100644 (file)
@@ -9,6 +9,7 @@
 #include <Model_PluginManager.h>
 #include <Model_Events.h>
 #include <Events_Loop.h>
+#include <Events_Error.h>
 
 #include <TDataStd_Integer.hxx>
 #include <TDataStd_Comment.hxx>
 
 #include <climits>
 
+#ifdef WIN32
+# define _separator_ '\\'
+#else
+# define _separator_ '/'
+#endif
+
 static const int UNDO_LIMIT = 10; // number of possible undo operations
 
 static const int TAG_GENERAL = 1; // general properties tag
@@ -26,83 +33,125 @@ static const int TAG_HISTORY = 3; // tag of the history sub-tree (python dump)
 
 using namespace std;
 
+/// Returns the file name of this document by the nameof directory and identifuer of a document
+static TCollection_ExtendedString DocFileName(const char* theFileName, const string& theID)
+{
+  TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
+  aPath += _separator_;
+  aPath += theID.c_str();
+  aPath += ".cbf"; // standard binary file extension
+  return aPath;
+}
+
 bool Model_Document::load(const char* theFileName)
 {
-  bool myIsError = Standard_False;
-  /*
-   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
-   PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
-   try
-   {
-   Handle(TDocStd_Document) aDoc = this;
-   aStatus = Model_Application::GetApplication()->Open(aPath, aDoc);
-   }
-   catch (Standard_Failure)
-   {}
-   myIsError = aStatus != PCDM_RS_OK;
-   if (myIsError)
-   {
-   switch (aStatus)
-   {
-   case PCDM_RS_UnknownDocument: cout<<"OCAFApp_Appl_RUnknownDocument"<<endl; break;
-   case PCDM_RS_AlreadyRetrieved: cout<<"OCAFApp_Appl_RAlreadyRetrieved"<<endl; break;
-   case PCDM_RS_AlreadyRetrievedAndModified: cout<<"OCAFApp_Appl_RAlreadyRetrievedAndModified"<<endl; break;
-   case PCDM_RS_NoDriver: cout<<"OCAFApp_Appl_RNoDriver"<<endl; break;
-   case PCDM_RS_UnknownFileDriver: cout<<"OCAFApp_Appl_RNoDriver"<<endl; break;
-   case PCDM_RS_OpenError: cout<<"OCAFApp_Appl_ROpenError"<<endl; break;
-   case PCDM_RS_NoVersion: cout<<"OCAFApp_Appl_RNoVersion"<<endl; break;
-   case PCDM_RS_NoModel: cout<<"OCAFApp_Appl_RNoModel"<<endl; break;
-   case PCDM_RS_NoDocument: cout<<"OCAFApp_Appl_RNoDocument"<<endl; break;
-   case PCDM_RS_FormatFailure: cout<<"OCAFApp_Appl_RFormatFailure"<<endl; break;
-   case PCDM_RS_TypeNotFoundInSchema: cout<<"OCAFApp_Appl_RTypeNotFound"<<endl; break;
-   case PCDM_RS_UnrecognizedFileFormat: cout<<"OCAFApp_Appl_RBadFileFormat"<<endl; break;
-   case PCDM_RS_MakeFailure: cout<<"OCAFApp_Appl_RMakeFailure"<<endl; break;
-   case PCDM_RS_PermissionDenied: cout<<"OCAFApp_Appl_RPermissionDenied"<<endl; break;
-   case PCDM_RS_DriverFailure: cout<<"OCAFApp_Appl_RDriverFailure"<<endl; break;
-   default: cout<<"OCAFApp_Appl_RUnknownFail"<<endl; break;
-   }
-   }
-   SetUndoLimit(UNDO_LIMIT);
-   */
-  return !myIsError;
+  Handle(Model_Application) anApp = Model_Application::getApplication();
+  if (this == Model_PluginManager::get()->rootDocument().get()) {
+    anApp->setLoadPath(theFileName);
+  }
+  TCollection_ExtendedString aPath (DocFileName(theFileName, myID));
+  PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
+  try
+  {
+    aStatus = anApp->Open(aPath, myDoc);
+  }
+  catch (Standard_Failure)
+  {
+    Handle(Standard_Failure) aFail = Standard_Failure::Caught();
+    Events_Error::send(string("Exception in opening of document: ") + aFail->GetMessageString());
+    return false;
+  }
+  bool isError = aStatus != PCDM_RS_OK;
+  if (isError)
+  {
+    switch (aStatus)
+    {
+    case PCDM_RS_UnknownDocument: 
+      Events_Error::send(string("Can not open document: PCDM_RS_UnknownDocument")); break;
+    case PCDM_RS_AlreadyRetrieved: 
+      Events_Error::send(string("Can not open document: PCDM_RS_AlreadyRetrieved")); break;
+    case PCDM_RS_AlreadyRetrievedAndModified:
+      Events_Error::send(string("Can not open document: PCDM_RS_AlreadyRetrievedAndModified")); break;
+    case PCDM_RS_NoDriver:
+      Events_Error::send(string("Can not open document: PCDM_RS_NoDriver")); break;
+    case PCDM_RS_UnknownFileDriver:
+      Events_Error::send(string("Can not open document: PCDM_RS_UnknownFileDriver")); break;
+    case PCDM_RS_OpenError:
+      Events_Error::send(string("Can not open document: PCDM_RS_OpenError")); break;
+    case PCDM_RS_NoVersion:
+      Events_Error::send(string("Can not open document: PCDM_RS_NoVersion")); break;
+    case PCDM_RS_NoModel:
+      Events_Error::send(string("Can not open document: PCDM_RS_NoModel")); break;
+    case PCDM_RS_NoDocument:
+      Events_Error::send(string("Can not open document: PCDM_RS_NoDocument")); break;
+    case PCDM_RS_FormatFailure:
+      Events_Error::send(string("Can not open document: PCDM_RS_FormatFailure")); break;
+    case PCDM_RS_TypeNotFoundInSchema:
+      Events_Error::send(string("Can not open document: PCDM_RS_TypeNotFoundInSchema")); break;
+    case PCDM_RS_UnrecognizedFileFormat:
+      Events_Error::send(string("Can not open document: PCDM_RS_UnrecognizedFileFormat")); break;
+    case PCDM_RS_MakeFailure:
+      Events_Error::send(string("Can not open document: PCDM_RS_MakeFailure")); break;
+    case PCDM_RS_PermissionDenied:
+      Events_Error::send(string("Can not open document: PCDM_RS_PermissionDenied")); break;
+    case PCDM_RS_DriverFailure:
+      Events_Error::send(string("Can not open document: PCDM_RS_DriverFailure")); break;
+    default:
+      Events_Error::send(string("Can not open document: unknown error")); break;
+    }
+  }
+  if (!isError) {
+    myDoc->SetUndoLimit(UNDO_LIMIT);
+    synchronizeFeatures();
+  }
+  return !isError;
 }
 
 bool Model_Document::save(const char* theFileName)
 {
-  bool myIsError = true;
-  /*
-   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
-   PCDM_StoreStatus aStatus;
-   try {
-   Handle(TDocStd_Document) aDoc = this;
-   aStatus = Model_Application::GetApplication()->SaveAs (aDoc, aPath);
-   }
-   catch (Standard_Failure) {
-   Handle(Standard_Failure) aFail = Standard_Failure::Caught();
-   cout<<"OCAFApp_Engine:save Error: "<<aFail->GetMessageString()<<endl;
-   return false;
-   }
-   myIsError = aStatus != PCDM_SS_OK;
-   if (myIsError)
-   {
-   switch (aStatus)
-   {
-   case PCDM_SS_DriverFailure:
-   cout<<"OCAFApp_Appl_SDriverFailure"<<endl;
-   break;
-   case PCDM_SS_WriteFailure:
-   cout<<"OCAFApp_Appl_SWriteFailure"<<endl;
-   break;
-   case PCDM_SS_Failure:
-   default:
-   cout<<"OCAFApp_Appl_SUnknownFailure"<<endl;
-   break;
-   }
-   }
-   myTransactionsAfterSave = 0;
-   Standard::Purge(); // Release free memory
-   */
-  return !myIsError;
+  // create a directory in the root document if it is not yet exist
+  if (this == Model_PluginManager::get()->rootDocument().get()) {
+#ifdef WIN32
+    CreateDirectory(theFileName, NULL);
+#else
+    mkdir(theFileName, 0x1ff); 
+#endif
+  }
+  // filename in the dir is id of document inside of the given directory
+  TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
+  PCDM_StoreStatus aStatus;
+  try {
+    aStatus = Model_Application::getApplication()->SaveAs(myDoc, aPath);
+  }
+  catch (Standard_Failure) {
+    Handle(Standard_Failure) aFail = Standard_Failure::Caught();
+    Events_Error::send(string("Exception in saving of document: ") + aFail->GetMessageString());
+    return false;
+  }
+  bool isDone = aStatus == PCDM_SS_OK || aStatus == PCDM_SS_No_Obj;
+  if (!isDone)
+  {
+    switch (aStatus)
+    {
+    case PCDM_SS_DriverFailure:
+      Events_Error::send(string("Can not save document: PCDM_SS_DriverFailure"));
+      break;
+    case PCDM_SS_WriteFailure:
+      Events_Error::send(string("Can not save document: PCDM_SS_WriteFailure"));
+      break;
+    case PCDM_SS_Failure:
+    default:
+      Events_Error::send(string("Can not save document: PCDM_SS_Failure"));
+      break;
+    }
+  }
+  myTransactionsAfterSave = 0;
+  if (isDone) { // save also sub-documents if any
+    set<string>::iterator aSubIter = mySubs.begin();
+    for(; aSubIter != mySubs.end() && isDone; aSubIter++)
+      isDone = subDocument(*aSubIter)->save(theFileName);
+  }
+  return isDone;
 }
 
 void Model_Document::close()
@@ -344,6 +393,8 @@ Model_Document::Model_Document(const std::string theID)
 {
   myDoc->SetUndoLimit(UNDO_LIMIT);
   myTransactionsAfterSave = 0;
+  // to have something in the document and avoid empty doc open/save problem
+  TDataStd_Integer::Set(myDoc->Main().Father(), 0);
 }
 
 TDF_Label Model_Document::groupLabel(const string theGroup)
@@ -458,10 +509,6 @@ void Model_Document::synchronizeFeatures()
         aFeature->setDoc(aThis);
         aFeature->setData(aData);
         aFeature->initAttributes();
-        // event: model is updated
-        static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_CREATED);
-        Model_FeatureUpdatedMessage aMsg(aFeature, anEvent);
-        Events_Loop::loop()->send(aMsg);
 
         if (aFIter == aFeatures.end()) {
           aFeatures.push_back(aFeature);
@@ -470,6 +517,11 @@ void Model_Document::synchronizeFeatures()
           aFIter++;
           aFeatures.insert(aFIter, aFeature);
         }
+        // event: model is updated
+        static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_CREATED);
+        Model_FeatureUpdatedMessage aMsg(aFeature, anEvent);
+        Events_Loop::loop()->send(aMsg);
+
         // feature for this label is added, so go to the next label
         aFLabIter.Next();
       } else { // nothing is changed, both iterators are incremented
index 577e6decae9422acb5def9a2c2e92719452320aa..aeb505f9d7cde160ff24152a6922ca618768320a 100644 (file)
@@ -421,7 +421,7 @@ void XGUI_Workshop::onOpen()
   }
 
   //show file dialog, check if readable and open
-  myCurrentFile = QFileDialog::getOpenFileName(mainWindow());
+  myCurrentFile = QFileDialog::getExistingDirectory(mainWindow());
   if(myCurrentFile.isEmpty())
     return;
   QFileInfo aFileInfo(myCurrentFile);