Salome HOME
Merge branch 'V8_3_asterstudy' into V8_3_BR
authorvsr <vsr@opencascade.com>
Wed, 27 Sep 2017 08:45:09 +0000 (11:45 +0300)
committervsr <vsr@opencascade.com>
Wed, 27 Sep 2017 08:45:09 +0000 (11:45 +0300)
src/SalomeApp/SalomeApp_Engine_i.cxx
src/SalomeApp/SalomeApp_Engine_i.h

index 16c1fcc29fdbebf2f92f01983c0ac65bc9ae8652..689d55192a0e878c440f7572d1f2aa8d310878fb 100644 (file)
 
 #include "SalomeApp_Engine_i.h"
 #include "SalomeApp_Application.h"
+#include "SalomeApp_Study.h"
+#include "SUIT_Session.h"
+#include "CAM_Module.h"
+#include "LightApp_DataModel.h"
 
 #include <SALOME_NamingService.hxx>
 #include <SALOMEDS_Tool.hxx>
 
 #include <iostream>
 
+namespace
+{
+  SalomeApp_Study* getStudyById( int id )
+  {
+    SalomeApp_Study* study = 0;
+    QList<SUIT_Application*> apps = SUIT_Session::session()->applications();
+    for ( int i = 0; i < apps.count() && !study; i++ )
+    {
+      SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( apps[i]->activeStudy() );
+      if ( appStudy && appStudy->id() == id )
+        study = appStudy;
+    }
+    return study;
+  }
+}
+
 /*!
   Constructor
 */
 SalomeApp_Engine_i::SalomeApp_Engine_i( const char* theComponentName )
+  : myComponentName( theComponentName )
 {
-  myComponentName = theComponentName;
   MESSAGE("SalomeApp_Engine_i::SalomeApp_Engine_i(): myComponentName = " <<
-         myComponentName << ", this = " << this);
+         qPrintable( myComponentName ) << ", this = " << this);
 }
 
 /*!
@@ -56,7 +76,7 @@ SalomeApp_Engine_i::SalomeApp_Engine_i( const char* theComponentName )
 SalomeApp_Engine_i::~SalomeApp_Engine_i()
 {
   MESSAGE("SalomeApp_Engine_i::~SalomeApp_Engine_i(): myComponentName = " << 
-         myComponentName << ", this = " << this);
+         qPrintable( myComponentName ) << ", this = " << this);
 }
 
 SALOMEDS::TMPFile* SalomeApp_Engine_i::Save (SALOMEDS::SComponent_ptr theComponent,
@@ -65,21 +85,60 @@ SALOMEDS::TMPFile* SalomeApp_Engine_i::Save (SALOMEDS::SComponent_ptr theCompone
 {
   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
 
-  if (CORBA::is_nil(theComponent) || CORBA::is_nil(theComponent->GetStudy()))
+  if ( CORBA::is_nil(theComponent) || CORBA::is_nil( theComponent->GetStudy() ) )
+    return aStreamFile._retn();
+
+  // Component type
+  QString componentName = theComponent->ComponentDataType();
+  // Error somewhere outside - Save() called with wrong SComponent instance
+  if ( myComponentName != componentName )
     return aStreamFile._retn();
 
+  // Get study ID
   const int studyId = theComponent->GetStudy()->StudyId();
 
-  // Get a temporary directory to store a file
-  //std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
+  bool manuallySaved = false;
 
-  if (myMap.count(studyId)) {
-    std::string componentName (theComponent->ComponentDataType());
+  if ( !myMap.count( studyId ) ) {
+    // Save was probably called from outside GUI, so SetListOfFiles was not called!
+    // Try to get list of files from directly from data model
 
-    // Error somewhere outside - Save() called with
-    // wrong SComponent instance
-    if ( myComponentName != componentName )
+    MESSAGE("SalomeApp_Engine_i::Save(): myComponentName = " <<
+            qPrintable( myComponentName ) <<
+            "it seems Save() was called from outside GUI" );
+
+    // - Get study
+    SalomeApp_Study* study = getStudyById( studyId );
+    if ( !study )
+      return aStreamFile._retn();
+    // - Get app
+    SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study->application() );
+    if ( !app )
+      return aStreamFile._retn();
+    // - Get module
+    CAM_Module* module = app->module( SalomeApp_Application::moduleTitle( componentName ) );
+    if ( !module ) // load module???
       return aStreamFile._retn();
+    // - Get data model
+    LightApp_DataModel* dataModel = dynamic_cast<LightApp_DataModel*>( module->dataModel() );
+    if ( !dataModel )
+      return aStreamFile._retn();
+    // - Save data files
+    QStringList dataFiles;
+    dataModel->saveAs( theURL, study, dataFiles );
+    std::vector<std::string> names;
+    foreach ( QString name, dataFiles ) {
+      if ( !name.isEmpty() )
+        names.push_back(name.toUtf8().data());
+    }
+    SetListOfFiles( names, studyId );
+    manuallySaved = true;
+  }
+
+  // Get a temporary directory to store a file
+  //std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
+
+  if ( myMap.count( studyId ) ) {
 
     const ListOfFiles& listOfFiles = myMap[studyId];
 
@@ -104,6 +163,9 @@ SALOMEDS::TMPFile* SalomeApp_Engine_i::Save (SALOMEDS::SComponent_ptr theCompone
     }
   }
 
+  if ( manuallySaved )
+    SetListOfFiles( ListOfFiles(), studyId );
+
   return aStreamFile._retn();
 }
 
@@ -118,7 +180,7 @@ CORBA::Boolean SalomeApp_Engine_i::Load (SALOMEDS::SComponent_ptr theComponent,
 
   // Error somewhere outside - Load() called with
   // wrong SComponent instance
-  std::string componentName (theComponent->ComponentDataType());
+  QString componentName = theComponent->ComponentDataType();
   if ( myComponentName != componentName )
     return false;
 
@@ -159,7 +221,10 @@ SalomeApp_Engine_i::ListOfFiles SalomeApp_Engine_i::GetListOfFiles (const int th
 void SalomeApp_Engine_i::SetListOfFiles (const ListOfFiles& theListOfFiles,
                                          const int          theStudyId)
 {
-  myMap[theStudyId] = theListOfFiles;
+  if ( theListOfFiles.empty() )
+    myMap.erase( theStudyId );
+  else
+    myMap[theStudyId] = theListOfFiles;
 }
 
 /*! 
@@ -171,7 +236,7 @@ Engines::TMPFile* SalomeApp_Engine_i::DumpPython(CORBA::Object_ptr theStudy,
                                                 CORBA::Boolean& isValidScript)
 {
   MESSAGE("SalomeApp_Engine_i::DumpPython(): myComponentName = "<<
-         myComponentName << ", this = " << this);
+         qPrintable( myComponentName ) << ", this = " << this);
   
   // Temporary solution: returning a non-empty sequence
   // even if there's nothing to dump, to avoid crashes in SALOMEDS
@@ -276,7 +341,7 @@ Engines::TMPFile* SalomeApp_Engine_i::DumpPython(CORBA::Object_ptr theStudy,
 */
 char* SalomeApp_Engine_i::ComponentDataType()
 {
-  return const_cast<char*>( myComponentName.c_str() );
+  return CORBA::string_dup( myComponentName.toLatin1().constData() );
 }
 
 /*!
@@ -288,7 +353,7 @@ char* SalomeApp_Engine_i::getVersion()
   QString version;
   SalomeApp_Application::ModuleShortInfo version_info;
   foreach ( version_info, versions ) {
-    if ( SalomeApp_Application::moduleName( version_info.name ) == myComponentName.c_str() ) {
+    if ( SalomeApp_Application::moduleName( version_info.name ) == myComponentName ) {
       version = version_info.version;
       break;
     }
index ec2641913840c79fef3740f00033faa38de75ac2..d9b3d4b2255ab7d1de77e29b454ff842bde8a692 100644 (file)
@@ -31,6 +31,7 @@
 
 #include "SALOME_Component_i.hxx"
 
+#include <QString>
 #include <vector> 
 #include <map> 
 
@@ -101,7 +102,7 @@ private:
   typedef std::map<int, ListOfFiles> MapOfListOfFiles;
   MapOfListOfFiles                   myMap;
 
-  std::string                        myComponentName;
+  QString                            myComponentName;
 };
 
 #endif