Salome HOME
modified method getStudy
[modules/gui.git] / src / SalomeApp / SalomeApp_Study.cxx
index bd38efc32facd226b34673be5346d6e39ae9d866..e1065c9fef289aae4017356b7bf5bf9f7e0d83d7 100644 (file)
 
 #include "utilities.h"
 
+/*!
+  Constructor.
+*/
 SalomeApp_Study::SalomeApp_Study( SUIT_Application* app )
 : CAM_Study( app )
-{}  
-
+{
+}  
 
+/*!
+  Destructor.
+*/
 SalomeApp_Study::~SalomeApp_Study()
 {
-  closeDocument();
 }
 
+/*!
+  Gets study id.
+*/
 int SalomeApp_Study::id() const
 {
   int id = -1;
@@ -30,21 +38,29 @@ int SalomeApp_Study::id() const
   return id;
 }
 
+/*!
+  Gets studyDS pointer.
+*/
 _PTR(Study) SalomeApp_Study::studyDS() const
 {
   return myStudyDS;
 }
 
+/*!
+  Create document.
+*/
 void SalomeApp_Study::createDocument()
 {
   MESSAGE( "openDocument" );
 
   // initialize myStudyDS, read HDF file
-  _PTR(Study) study ( SalomeApp_Application::studyMgr()->NewStudy( newStudyName().latin1() ) );
+  QString aName = newStudyName();
+  _PTR(Study) study ( SalomeApp_Application::studyMgr()->NewStudy( aName.latin1() ) );
   if ( !study )
     return;
 
   setStudyDS( study );
+  setStudyName( aName );
 
   // create myRoot
   setRoot( new SalomeApp_RootObject( this ) );
@@ -56,7 +72,7 @@ void SalomeApp_Study::createDocument()
 
 //=======================================================================
 // name    : openDocument
-// Purpose : Open document
+/*! Purpose : Open document*/
 //=======================================================================
 bool SalomeApp_Study::openDocument( const QString& theFileName )
 {
@@ -69,24 +85,20 @@ bool SalomeApp_Study::openDocument( const QString& theFileName )
 
   setStudyDS( study );
 
-  // build a SUIT_DataObject-s tree under myRoot member field
-  // 1. create myRoot
-  setRoot( new SalomeApp_RootObject( this ) );
-  // 2. iterate through all components and create corresponding sub-trees under them
-  _PTR(SComponentIterator) it ( studyDS()->NewComponentIterator() );
-  for ( ; it->More(); it->Next() ) {
-    // don't use shared_ptr here, for Data Object will take 
-    // ownership of this pointer
-    _PTR(SComponent) aComponent ( it->Value() ); 
-
-    if ( aComponent->ComponentDataType() == "Interface Applicative" )
-      continue; // skip the magic "Interface Applicative" component
-    
-    SalomeApp_DataModel::BuildTree( aComponent, root(), this );
-  }
+  setRoot( new SalomeApp_RootObject( this ) ); // create myRoot
 
-  bool res = CAM_Study::openDocument( theFileName );
+  // update loaded data models: call open() and update() on them.
+  ModelList dm_s;
+  dataModels( dm_s );
+  for ( ModelListIterator it( dm_s ); it.current(); ++it )
+    openDataModel( studyName(), it.current() );
+
+  // this will build a SUIT_DataObject-s tree under myRoot member field
+  // passing "false" in order NOT to rebuild existing data models' trees - it was done in previous step
+  // but tree that corresponds to not-loaded data models will be updated any way. 
+  ((SalomeApp_Application*)application())->updateObjectBrowser( false ); 
 
+  bool res = CAM_Study::openDocument( theFileName );
   emit opened( this );
 
   return res;
@@ -94,7 +106,7 @@ bool SalomeApp_Study::openDocument( const QString& theFileName )
 
 //=======================================================================
 // name    : loadDocument
-// Purpose : Connects GUI study to SALOMEDS one already loaded into StudyManager
+/*! Purpose : Connects GUI study to SALOMEDS one already loaded into StudyManager*/
 //=======================================================================
 bool SalomeApp_Study::loadDocument( const QString& theStudyName )
 {
@@ -107,33 +119,32 @@ bool SalomeApp_Study::loadDocument( const QString& theStudyName )
 
   setStudyDS( study );
 
-  // build a SUIT_DataObject-s tree under myRoot member field
-  // 1. create myRoot
-  setRoot( new SalomeApp_RootObject( this ) );
-  // 2. iterate through all components and create corresponding sub-trees under them
-  _PTR(SComponentIterator) it ( studyDS()->NewComponentIterator() );
-  for ( ; it->More(); it->Next() ) {
-    // don't use shared_ptr here, for Data Object will take 
-    // ownership of this pointer
-    _PTR(SComponent) aComponent ( it->Value() ); 
-
-    if ( aComponent->ComponentDataType() == "Interface Applicative" )
-      continue; // skip the magic "Interface Applicative" component
-    
-    SalomeApp_DataModel::BuildTree( aComponent, root(), this );
-  }
+  setRoot( new SalomeApp_RootObject( this ) ); // create myRoot
 
-  // TODO: potentially unsafe call, since base study's openDocument() might try to access the file directly - to be improved
-  bool res = CAM_Study::openDocument( theStudyName );
+  //SRN: BugID IPAL9021, put there the same code as in a method openDocument
+
+  // update loaded data models: call open() and update() on them.
+  ModelList dm_s;
+  dataModels( dm_s );
+  for ( ModelListIterator it( dm_s ); it.current(); ++it )
+    openDataModel( studyName(), it.current() );
+
+  // this will build a SUIT_DataObject-s tree under myRoot member field
+  // passing "false" in order NOT to rebuild existing data models' trees - it was done in previous step
+  // but tree that corresponds to not-loaded data models will be updated any way. 
+  ((SalomeApp_Application*)application())->updateObjectBrowser( false ); 
 
+  bool res = CAM_Study::openDocument( theStudyName );
   emit opened( this );
 
-  return res;  
+  //SRN: BugID IPAL9021: End
+
+  return res;
 }
 
 //=======================================================================
 // name    : saveDocumentAs
-// Purpose : Save document
+/*! Purpose : Save document */
 //=======================================================================
 bool SalomeApp_Study::saveDocumentAs( const QString& theFileName )
 {
@@ -143,21 +154,27 @@ bool SalomeApp_Study::saveDocumentAs( const QString& theFileName )
   for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() )
     aModel->saveAs( theFileName, this );
 
-  bool res = CAM_Study::saveDocumentAs( theFileName );
-
   // save SALOMEDS document
-  bool isMultiFile = false, isAscii = false;// TODO: This information should be taken from preferences afterwards!
+  SUIT_ResourceMgr* resMgr = application()->resourceMgr();
+  if( !resMgr )
+    return false;
+
+  bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false ),
+       isAscii = resMgr->booleanValue( "Study", "ascii_file", true );
   isAscii ? SalomeApp_Application::studyMgr()->SaveAsASCII( theFileName.latin1(), studyDS(), isMultiFile ) :
             SalomeApp_Application::studyMgr()->SaveAs     ( theFileName.latin1(), studyDS(), isMultiFile );
 
-  emit saved( this );
+  bool res = CAM_Study::saveDocumentAs( theFileName );  //SRN: BugID IPAL9377, removed usage of uninitialized variable <res>
+
+  if ( res )
+    emit saved( this );
 
   return res;
 }
 
 //=======================================================================
 // name    : saveDocument
-// Purpose : Save document
+/*! Purpose : Save document */
 //=======================================================================
 void SalomeApp_Study::saveDocument()
 {
@@ -170,7 +187,12 @@ void SalomeApp_Study::saveDocument()
   CAM_Study::saveDocument();
 
   // save SALOMEDS document
-  bool isMultiFile = false, isAscii = false;// TODO: This information should be taken from preferences afterwards!
+  SUIT_ResourceMgr* resMgr = application()->resourceMgr();
+  if( !resMgr )
+    return;
+  
+  bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false ),
+       isAscii = resMgr->booleanValue( "Study", "ascii_file", true );
   isAscii ? SalomeApp_Application::studyMgr()->SaveASCII( studyDS(), isMultiFile ) :
             SalomeApp_Application::studyMgr()->Save     ( studyDS(), isMultiFile );
 
@@ -179,25 +201,29 @@ void SalomeApp_Study::saveDocument()
 
 //================================================================
 // Function : closeDocument
-// Purpose  : 
+/*! Purpose  : Close document */
 //================================================================
-void SalomeApp_Study::closeDocument()
+void SalomeApp_Study::closeDocument(bool permanently)
 {
   // Inform everybody that this study is going to close when it's most safe to,
   // i.e. in the very beginning
   emit closed( this );
 
   // close SALOMEDS document
-  SalomeApp_Application::studyMgr()->Close( studyDS() );
-  SALOMEDSClient_Study* aStudy = NULL;
-  setStudyDS( _PTR(Study)(aStudy) );
+  _PTR(Study) studyPtr = studyDS();
+  if ( studyPtr )
+  {
+    if(permanently) SalomeApp_Application::studyMgr()->Close( studyPtr );
+    SALOMEDSClient_Study* aStudy = 0;
+    setStudyDS( _PTR(Study)(aStudy) );
+  }
 
-  CAM_Study::closeDocument();
+  CAM_Study::closeDocument(permanently);
 }
 
 //================================================================
 // Function : isModified
-// Purpose  : 
+/*! Purpose  : Check data model on modifications.*/
 //================================================================
 bool SalomeApp_Study::isModified() const
 {
@@ -215,7 +241,7 @@ bool SalomeApp_Study::isModified() const
 
 //================================================================
 // Function : isSaved
-// Purpose  : 
+/*! Purpose  : Check: data model is saved?*/
 //================================================================
 bool SalomeApp_Study::isSaved() const
 {
@@ -231,11 +257,17 @@ bool SalomeApp_Study::isSaved() const
   return isAllSaved; 
 }
 
+/*!
+  Set studyDS.
+*/
 void SalomeApp_Study::setStudyDS( const _PTR(Study)& s )
 {
   myStudyDS = s;
 }
 
+/*!
+  Insert data model.
+*/
 void SalomeApp_Study::dataModelInserted (const CAM_DataModel* dm)
 {
   MESSAGE("SalomeApp_Study::dataModelInserted() : module name() = " << dm->module()->name());
@@ -255,12 +287,15 @@ void SalomeApp_Study::dataModelInserted (const CAM_DataModel* dm)
         aComp = aBuilder->NewComponent(dm->module()->name());
 
         // Set default engine IOR
-        aBuilder->DefineComponentInstance(aComp, SalomeApp_Application::defaultEngineIOR());
+        aBuilder->DefineComponentInstance(aComp, SalomeApp_Application::defaultEngineIOR().latin1());
       }
     }
   }
 }
 
+/*!
+  Open data model
+*/
 bool SalomeApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm )
 {
   if (!dm)
@@ -276,12 +311,9 @@ bool SalomeApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm
   return false;
 }
 
-void SalomeApp_Study::updateModelRoot( const CAM_DataModel* dm )
-{
-  CAM_Study::updateModelRoot( dm );
-  ((SalomeApp_Application*)application())->objectBrowser()->updateTree();
-}
-
+/*!
+  Create new study name.
+*/
 QString SalomeApp_Study::newStudyName() const
 {
   std::vector<std::string> studies = SalomeApp_Application::studyMgr()->GetOpenStudies();
@@ -300,3 +332,18 @@ QString SalomeApp_Study::newStudyName() const
   }
   return newName;
 }
+
+void SalomeApp_Study::deleteReferencesTo( _PTR( SObject ) obj )
+{
+  _PTR(StudyBuilder) sb = studyDS()->NewBuilder();
+  std::vector<_PTR(SObject)> aRefs = studyDS()->FindDependances( obj );
+  for( int i=0, n=aRefs.size(); i<n; i++ )
+  {
+    _PTR( SObject ) o = aRefs[i];
+    if( o->GetFatherComponent()->ComponentDataType()==obj->GetFatherComponent()->ComponentDataType() )
+    {
+      sb->RemoveReference( o );
+      sb->RemoveObjectWithChildren( o );
+    }
+  }
+}