Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / LightApp / LightApp_Study.cxx
index 9b05ef2b2adcdf77e3f54fe816bfe26eff191193..09eb3c3a0cd4ae181395c440fafe7ce6cab41e90 100644 (file)
@@ -1,3 +1,21 @@
+// Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
+// 
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either 
+// version 2.1 of the License.
+// 
+// This library is distributed in the hope that it will be useful 
+// but WITHOUT ANY WARRANTY; without even the implied warranty of 
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public  
+// License along with this library; if not, write to the Free Software 
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/
+//
 #include "LightApp_Study.h"
 
 #include "CAM_DataModel.h"
@@ -5,7 +23,7 @@
 #include "LightApp_DataModel.h"
 #include "LightApp_DataObject.h"
 #include "LightApp_RootObject.h"
-#include "LightApp_Driver.h"
+#include "LightApp_HDFDriver.h"
 
 #include "SUIT_ResourceMgr.h"
 #include "SUIT_DataObjectIterator.h"
@@ -23,6 +41,7 @@
 #include <OSD_SingleProtection.hxx>
 #include <OSD_FileIterator.hxx>
 
+#include <set>
 #include <qstring.h>
 
 /*!
 LightApp_Study::LightApp_Study( SUIT_Application* app )
 : CAM_Study( app )
 {
-  myDriver = new LightApp_Driver();
+  // HDF persistence
+  myDriver = new LightApp_HDFDriver();
+  //myDriver = new LightApp_Driver();
 }
+
 /*!
   Destructor.
 */
@@ -46,6 +67,8 @@ LightApp_Study::~LightApp_Study()
 */
 void LightApp_Study::createDocument()
 {
+  setStudyName( QString( "Study%1" ).arg( LightApp_Application::studyId() ) );
+
   // create myRoot
   setRoot( new LightApp_RootObject( this ) );
 
@@ -54,10 +77,9 @@ void LightApp_Study::createDocument()
   emit created( this );
 }
 
-//=======================================================================
-// name    : openDocument
-/*! Purpose : Open document*/
-//=======================================================================
+/*!
+  Opens document
+*/
 bool LightApp_Study::openDocument( const QString& theFileName )
 {
   myDriver->ClearDriverContents();
@@ -83,10 +105,9 @@ bool LightApp_Study::openDocument( const QString& theFileName )
   return res;
 }
 
-//=======================================================================
-// name    : loadDocument
-/*! Purpose : Load document */
-//=======================================================================
+/*!
+  Loads document
+*/
 bool LightApp_Study::loadDocument( const QString& theStudyName )
 {
   myDriver->ClearDriverContents();
@@ -115,23 +136,57 @@ bool LightApp_Study::loadDocument( const QString& theStudyName )
   return res;
 }
 
-//=======================================================================
-// name    : saveDocumentAs
-/*! Purpose : Save document */
-//=======================================================================
+/*!
+  Saves document
+*/
 bool LightApp_Study::saveDocumentAs( const QString& theFileName )
 {
-  ModelList list; dataModels( list );
+  SUIT_ResourceMgr* resMgr = application()->resourceMgr();
+  if( !resMgr )
+    return false;
+
+  ModelList list; 
+  dataModels( list );
 
   LightApp_DataModel* aModel = (LightApp_DataModel*)list.first();
 
-  myDriver->ClearDriverContents();
   QStringList listOfFiles;
-  for ( ; aModel; aModel = (LightApp_DataModel*)list.next() ) {
+  bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
+  for ( ; aModel; aModel = (LightApp_DataModel*)list.next() ) 
+  {
+    std::vector<std::string> anOldList = myDriver->GetListOfFiles( aModel->module()->name() );
     listOfFiles.clear();
     aModel->saveAs( theFileName, this, listOfFiles );
     if ( !listOfFiles.isEmpty() )
       saveModuleData(aModel->module()->name(), listOfFiles);
+
+    // Remove files if necessary. File is removed if it was in the list of files before
+    // saving and it is not contained in the list after saving. This provides correct 
+    // removing previous temporary files. These files are not removed before saving
+    // because they may be required for it.
+
+    std::vector<std::string> aNewList = myDriver->GetListOfFiles( aModel->module()->name() );
+    
+    std::set<std::string> aNewNames;
+    std::set<std::string> toRemove;
+    int i, n;
+    for( i = 0, n = aNewList.size(); i < n; i++ )
+      aNewNames.insert( aNewList[ i ] );
+    for( i = 0, n = anOldList.size(); i < n; i++ )
+    {
+      if ( i == 0 ) // directory is always inserted in list
+        toRemove.insert( anOldList[ i ] );
+      else if ( aNewNames.find( anOldList[ i ] ) == aNewNames.end() )
+        toRemove.insert( anOldList[ i ] );
+    }
+        
+    std::vector<std::string> toRemoveList( toRemove.size() );
+    std::set<std::string>::iterator anIter;
+    for( anIter = toRemove.begin(), i = 0; anIter != toRemove.end(); ++anIter, ++i )
+      toRemoveList[ i ] = *anIter;
+
+    
+    myDriver->RemoveFiles( toRemoveList, isMultiFile );
   }
 
   bool res = saveStudyData(theFileName);
@@ -143,10 +198,9 @@ bool LightApp_Study::saveDocumentAs( const QString& theFileName )
   return res;
 }
 
-//=======================================================================
-// name    : saveDocument
-/*! Purpose : Save document */
-//=======================================================================
+/*!
+  Saves document
+*/
 bool LightApp_Study::saveDocument()
 {
   ModelList list; dataModels( list );
@@ -169,10 +223,9 @@ bool LightApp_Study::saveDocument()
   return res;
 }
 
-//================================================================
-// Function : closeDocument
-/*! Purpose  : Close document */
-//================================================================
+/*!
+  Closes document
+*/
 void LightApp_Study::closeDocument(bool permanently)
 {
   // Inform everybody that this study is going to close when it's most safe to,
@@ -180,21 +233,51 @@ void LightApp_Study::closeDocument(bool permanently)
   emit closed( this );
 
   CAM_Study::closeDocument(permanently);
+  
+  // Remove temporary files
+  myDriver->ClearDriverContents();
 }
 
-//================================================================
-// Function : referencedToEntry
-/*! Purpose  : Return current entry*/
-//================================================================
-QString LightApp_Study::referencedToEntry( const QString& entry )
+/*!
+  \return real entry by entry of reference
+  \param entry - entry of reference object
+*/
+QString LightApp_Study::referencedToEntry( const QString& entry ) const
 {
   return entry;
 }
-//================================================================
-// Function : componentDataType
-/*! Purpose  : Return component data type from entry*/
-//================================================================
-QString LightApp_Study::componentDataType( const QString& entry )
+
+/*!
+  \return entries of object children
+*/
+void LightApp_Study::children( const QString&, QStringList& ) const
+{
+}
+
+/*!
+  \return true if entry corresponds to component
+*/
+bool LightApp_Study::isComponent( const QString& entry ) const
+{
+  if( !root() )
+    return false;
+
+  DataObjectList ch;
+  root()->children( ch );
+  DataObjectList::const_iterator anIt = ch.begin(), aLast = ch.end();
+  for( ; anIt!=aLast; anIt++ )
+  {
+    LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( *anIt );
+    if( obj && obj->entry()==entry )
+      return true;
+  }
+  return false;
+}
+
+/*!
+  \return component data type for entry
+*/
+QString LightApp_Study::componentDataType( const QString& entry ) const
 {
   LightApp_DataObject* aCurObj;
   for ( SUIT_DataObjectIterator it( root(), SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
@@ -206,10 +289,9 @@ QString LightApp_Study::componentDataType( const QString& entry )
   return "";
 }
 
-//================================================================
-// Function : isModified
-// Purpose  : 
-//================================================================
+/*!
+  \return true if study is modified
+*/
 bool LightApp_Study::isModified() const
 {
   bool isAnyChanged = CAM_Study::isModified();
@@ -224,36 +306,24 @@ bool LightApp_Study::isModified() const
   return isAnyChanged; 
 }
 
-//================================================================
-// Function : isSaved
-/*! Purpose  : Check: data model is saved?*/
-//================================================================
+/*!
+  \return true if data model is saved
+*/
 bool LightApp_Study::isSaved() const
 {
-  bool isAllSaved = CAM_Study::isSaved();
-  ModelList list; dataModels( list );
-
-  LightApp_DataModel* aModel = 0;
-  for ( QPtrListIterator<CAM_DataModel> it( list ); it.current() && isAllSaved; ++it ){
-    aModel = dynamic_cast<LightApp_DataModel*>( it.current() );
-    if ( aModel )
-      isAllSaved = aModel->isSaved();
-  }
-  return isAllSaved; 
+  return CAM_Study::isSaved();
 }
 
-//=======================================================================
-// name    : saveModuleData
-/*! Purpose :  Create SComponent for module, necessary for SalomeApp study */
-//=======================================================================
+/*!
+  Creates SComponent for module, necessary for SalomeApp study
+*/
 void LightApp_Study::addComponent(const CAM_DataModel* dm)
 {
 }
 
-//=======================================================================
-// name    : saveModuleData
-/*! Purpose : save list file for module 'theModuleName' */
-//=======================================================================
+/*!
+  Saves list file for module 'theModuleName'
+*/
 void LightApp_Study::saveModuleData(QString theModuleName, QStringList theListOfFiles)
 {
   int aNb = theListOfFiles.count();
@@ -271,10 +341,9 @@ void LightApp_Study::saveModuleData(QString theModuleName, QStringList theListOf
   myDriver->SetListOfFiles(theModuleName, aListOfFiles);
 }
 
-//=======================================================================
-// name    : openModuleData
-/*! Purpose : gets list of file for module 'theModuleNam' */
-//=======================================================================
+/*!
+  Gets list of file for module 'theModuleNam'
+*/
 void LightApp_Study::openModuleData(QString theModuleName, QStringList& theListOfFiles)
 {
   std::vector<std::string> aListOfFiles =  myDriver->GetListOfFiles(theModuleName);
@@ -288,10 +357,9 @@ void LightApp_Study::openModuleData(QString theModuleName, QStringList& theListO
     theListOfFiles.append(aListOfFiles[i+1].c_str());
 }
 
-//=======================================================================
-// name    : saveStudyData
-/*! Purpose : save data from study */
-//=======================================================================
+/*!
+  Saves data from study
+*/
 bool LightApp_Study::saveStudyData( const QString& theFileName )
 {
   ModelList list; dataModels( list );
@@ -301,18 +369,12 @@ bool LightApp_Study::saveStudyData( const QString& theFileName )
   bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
 
   bool aRes = myDriver->SaveDatasInFile(theFileName.latin1(), isMultiFile);
-  // clear map
-  std::vector<std::string> aList(0);
-  for ( ModelListIterator it( list ); it.current(); ++it )
-    myDriver->SetListOfFiles(it.current()->module()->name(), aList);
-
   return aRes;
 }
 
-//=======================================================================
-// name    : openStudyData
-/*! Purpose : open data for study */
-//=======================================================================
+/*!
+  Opens data for study
+*/
 bool LightApp_Study::openStudyData( const QString& theFileName )
 {
   SUIT_ResourceMgr* resMgr = application()->resourceMgr();
@@ -324,10 +386,9 @@ bool LightApp_Study::openStudyData( const QString& theFileName )
   return aRes;
 }
 
-//================================================================
-// Function : openDataModel
-/*! Purpose  : Open data model */
-//================================================================
+/*!
+  Opens data model
+*/
 bool LightApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm )
 {
   if (!dm)
@@ -336,12 +397,7 @@ bool LightApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm
   QStringList listOfFiles;
   openModuleData(dm->module()->name(), listOfFiles);
   if (dm && dm->open(studyName, this, listOfFiles)) {
-    // Remove the files and temporary directory, created
-    // for this module by LightApp_Driver::OpenStudyData()
-    bool isMultiFile = false; // TODO: decide, how to access this parameter
-    RemoveTemporaryFiles( dm->module()->name(), isMultiFile );
-
-     // Something has been read -> create data model tree
+    // Something has been read -> create data model tree
     LightApp_DataModel* aDM = dynamic_cast<LightApp_DataModel*>( dm );
     if ( aDM )
       aDM->update(NULL, this);
@@ -350,20 +406,19 @@ bool LightApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm
   return false;
 }
 
-//================================================================
-// Function : GetTmpDir
-/*! Purpose  : to be used by modules*/
-//================================================================
+/*!
+  \return temporary directory for saving files of modules
+*/
 std::string LightApp_Study::GetTmpDir (const char* theURL,
                                        const bool  isMultiFile)
 {
   return myDriver->GetTmpDir(theURL, isMultiFile);
 }
 
-//================================================================
-// Function : GetListOfFiles
-/*! Purpose  : to be used by modules*/
-//================================================================
+/*!
+  \return list of files necessary for module
+  \param theModuleName - name of module
+*/
 std::vector<std::string> LightApp_Study::GetListOfFiles(const char* theModuleName) const
 {
   std::vector<std::string> aListOfFiles;
@@ -371,19 +426,19 @@ std::vector<std::string> LightApp_Study::GetListOfFiles(const char* theModuleNam
   return aListOfFiles;
 }
 
-//================================================================
-// Function : SetListOfFiles
-/*! Purpose  : to be used by modules*/
-//================================================================
+/*!
+  Sets list of files necessary for module
+  \param theModuleName - name of module
+  \param theListOfFiles - list of files
+*/
 void LightApp_Study::SetListOfFiles (const char* theModuleName, const std::vector<std::string> theListOfFiles)
 {
   myDriver->SetListOfFiles(theModuleName, theListOfFiles);
 }
 
-//================================================================
-// Function : RemoveTemporaryFiles
-/*! Purpose  : to be used by modules*/
-//================================================================
+/*!
+  Removes temporary files
+*/
 void LightApp_Study::RemoveTemporaryFiles (const char* theModuleName, const bool isMultiFile) const
 {
   if (isMultiFile)
@@ -391,3 +446,19 @@ void LightApp_Study::RemoveTemporaryFiles (const char* theModuleName, const bool
   bool isDirDeleted = true;
   myDriver->RemoveTemporaryFiles(theModuleName, isDirDeleted);
 }
+
+/*!
+  Fills list with components names
+  \param comp - list to be filled
+*/
+void LightApp_Study::components( QStringList& comp ) const
+{
+  DataObjectList children = root()->children();
+  DataObjectList::const_iterator anIt = children.begin(), aLast = children.end();
+  for( ; anIt!=aLast; anIt++ )
+  {
+    LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( *anIt );
+    if( obj && obj->entry()!="Interface Applicative" )
+      comp.append( obj->entry() );
+  }
+}