Salome HOME
updated copyright message
[modules/gui.git] / src / SalomeApp / SalomeApp_Study.cxx
index c438b2de84c9c765660b3b26b2c5ea70aca83d67..d08a14074cda308ba5ffeca603f41c893da2d5f0 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -154,7 +154,7 @@ public:
           SalomeApp_DataObject* aFatherDO = it->second;
 
           it = entry2SuitObject.find(theID);
-          if (it != entry2SuitObject.end()) { // this SOobject is already added somethere
+          if (it != entry2SuitObject.end()) { // this SOobject is already added somewhere
             suit_obj = it->second;
             SUIT_DataObject* oldFather = suit_obj->parent();
             if (oldFather) {
@@ -181,15 +181,7 @@ public:
 
           suit_obj->updateItem();
           // define position in the data tree (in aFatherDO) to insert the aSObj
-          int pos = -1;
-          //int childDataObjCount = aFatherDO->childCount();
-          _PTR(UseCaseIterator) aUseCaseIter = aUseCaseBuilder->GetUseCaseIterator(aFatherSO);
-          for (int cur = 0; aUseCaseIter->More() && pos < 0; cur++, aUseCaseIter->Next()) {
-            if (aUseCaseIter->Value()->GetID() == theID) {
-              pos = cur;
-              break;
-            }
-          }
+          int pos = aUseCaseBuilder->GetIndexInFather(aFatherSO, aSObj);
 
           aFatherDO->insertChildAtPos(suit_obj, pos);
           //aFatherDO->insertChild(suit_obj, pos);
@@ -198,17 +190,16 @@ public:
           /* Define visibility state */
           bool isComponent = dynamic_cast<SalomeApp_ModuleObject*>( suit_obj ) != 0;
           if ( suit_obj && !isComponent && myStudy->visibilityState( theID.c_str() ) == Qtx::UnpresentableState ) {
-            QString moduleTitle = ((CAM_Application*)myStudy->application())->moduleTitle(suit_obj->componentDataType());
-            if (!moduleTitle.isEmpty()) {
-              LightApp_Displayer* aDisplayer = LightApp_Displayer::FindDisplayer(moduleTitle,false);
-              if (aDisplayer) {
-                if(aDisplayer->canBeDisplayed(theID.c_str())) {
-                  myStudy->setVisibilityState( theID.c_str(), Qtx::HiddenState ); //hide the just added object
-                  //MESSAGE("Object with entry : "<< theID <<" CAN be displayed !!!");
-                }
-                //else
-                  //MESSAGE("Object with entry : "<< theID <<" CAN'T be displayed !!!");
+            LightApp_Displayer* aDisplayer = LightApp_Displayer::FindDisplayer
+              (suit_obj->componentDataType(),false);
+            if (aDisplayer) {
+              if (aDisplayer->canBeDisplayed(theID.c_str())) {
+                //hide the just added object
+                myStudy->setVisibilityState( theID.c_str(), Qtx::HiddenState );
+                //MESSAGE("Object with entry : "<< theID <<" CAN be displayed !!!");
               }
+              //else
+              //  MESSAGE("Object with entry : "<< theID <<" CAN'T be displayed !!!");
             }
           }
         } // END: work with tree nodes structure
@@ -219,7 +210,7 @@ public:
             return;
           }
 
-          int last2Pnt_pos = theID.rfind( ":" );
+          int last2Pnt_pos = (int)theID.rfind( ":" ); //!< TODO: conversion from size_t to int
           std::string parent_id = theID.substr( 0, last2Pnt_pos );
           int tag = atoi( theID.substr( last2Pnt_pos+1 ).c_str() );
 
@@ -331,17 +322,15 @@ public:
         /* Define visibility state */
         bool isComponent = dynamic_cast<SalomeApp_ModuleObject*>( suit_obj ) != 0;
         if ( suit_obj && !isComponent ) {
-          QString moduleTitle = ((CAM_Application*)myStudy->application())->moduleTitle(suit_obj->componentDataType());
-          if (!moduleTitle.isEmpty()) {
-            LightApp_Displayer* aDisplayer = LightApp_Displayer::FindDisplayer(moduleTitle,false);
-            if (aDisplayer) {
-              if(aDisplayer->canBeDisplayed(theID.c_str())) {
-                myStudy->setVisibilityState( theID.c_str(), Qtx::HiddenState );
-                //MESSAGE("Object with entry : "<< theID <<" CAN be displayed !!!");
-              }
-              //else
-                //MESSAGE("Object with entry : "<< theID <<" CAN'T be displayed !!!");
+          LightApp_Displayer* aDisplayer = LightApp_Displayer::FindDisplayer
+            (suit_obj->componentDataType(),false);
+          if (aDisplayer) {
+            if (aDisplayer->canBeDisplayed(theID.c_str())) {
+              myStudy->setVisibilityState( theID.c_str(), Qtx::HiddenState );
+              //MESSAGE("Object with entry : "<< theID <<" CAN be displayed !!!");
             }
+            //else
+            //  MESSAGE("Object with entry : "<< theID <<" CAN'T be displayed !!!");
           }
         }
         break;
@@ -365,7 +354,7 @@ private:
     while (o) {
       SalomeApp_DataObject* so = dynamic_cast<SalomeApp_DataObject*>( o );
       if ( so ) {
-        std::string entry = so->entry().toLatin1().constData();
+        std::string entry = so->entry().toUtf8().constData();
         if ( entry.size() )
           entry2SuitObject[entry] = so;
       }
@@ -373,7 +362,7 @@ private:
         // parse the children
         o = o->firstChild();
       }
-      else if ( o->nextBrother() > 0 ) {
+      else if ( o->nextBrother() ) {
         o = o->nextBrother();
       }
       else {
@@ -413,6 +402,7 @@ SalomeApp_Study::~SalomeApp_Study()
   if ( myObserver ) {
     PortableServer::ObjectId_var oid = myObserver->_default_POA()->servant_to_id( myObserver );
     myObserver->_default_POA()->deactivate_object( oid.in() );
+    myObserver = 0;
   }
 }
 
@@ -433,6 +423,8 @@ QString SalomeApp_Study::studyName() const
   // TEMPORARILY SOLUTION: better to be implemented with help of SALOMEDS observers
   if ( studyDS() ) {
     QString newName = QString::fromUtf8(studyDS()->URL().c_str());
+    if ( newName.isEmpty() )
+      newName = QString::fromUtf8(studyDS()->Name().c_str());
     if ( LightApp_Study::studyName() != newName ) {
       SalomeApp_Study* that = const_cast<SalomeApp_Study*>( this );
       that->setStudyName( newName );
@@ -569,6 +561,13 @@ bool SalomeApp_Study::loadDocument( const QString& theStudyName )
   while ( it.hasNext() )
     openDataModel( studyName(), it.next() );
 
+  bool res = CAM_Study::openDocument( theStudyName );
+
+  //rnv: to fix the "0051779: TC7.2.0: Save operation works incorrectly for study loaded from data server"
+  //     mark study as "not saved" after call openDocument( ... ) method.
+  setIsSaved(false);
+  emit opened( this ); // myRoot is set to Object Browser here
+
   // 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.
@@ -581,13 +580,6 @@ bool SalomeApp_Study::loadDocument( const QString& theStudyName )
   myStudyDS->attach(myObserver->_this(),true);
 #endif
 
-  bool res = CAM_Study::openDocument( theStudyName );
-  
-  //rnv: to fix the "0051779: TC7.2.0: Save operation works incorrectly for study loaded from data server"
-  //     mark study as "not saved" after call openDocument( ... ) method.
-  setIsSaved(false);
-  emit opened( this );
-
   bool restore = application()->resourceMgr()->booleanValue( "Study", "store_visual_state", true );
   if ( restore ) {
     std::vector<int> savePoints = getSavePoints();
@@ -621,7 +613,8 @@ bool SalomeApp_Study::saveDocumentAs( const QString& theFileName )
       listOfFiles.clear();
       aModel->saveAs( theFileName, this, listOfFiles );
       if ( !listOfFiles.isEmpty() )
-        saveModuleData(aModel->module()->name(), listOfFiles);
+        saveModuleData(aModel->module()->name(), 0, // 0 means persistence file
+                       listOfFiles);
     }
   }
 
@@ -635,7 +628,7 @@ bool SalomeApp_Study::saveDocumentAs( const QString& theFileName )
   bool res = studyDS()->SaveAs( theFileName.toUtf8().data(), isMultiFile, isAscii )
     && CAM_Study::saveDocumentAs( theFileName );
 
-  res = res && saveStudyData(theFileName);
+  res = res && saveStudyData(theFileName, 0); // 0 means persistence file
 
   if ( res )
     emit saved( this );
@@ -664,7 +657,8 @@ bool SalomeApp_Study::saveDocument()
       listOfFiles.clear();
       aModel->save(listOfFiles);
       if ( !listOfFiles.isEmpty() )
-        saveModuleData(aModel->module()->name(), listOfFiles);
+        saveModuleData(aModel->module()->name(), 0, // 0 means persistence file
+                       listOfFiles);
     }
   }
 
@@ -677,7 +671,7 @@ bool SalomeApp_Study::saveDocument()
   bool isAscii = resMgr->booleanValue( "Study", "ascii_file", false );
   bool res = studyDS()->Save( isMultiFile, isAscii ) && CAM_Study::saveDocument();
 
-  res = res && saveStudyData(studyName());
+  res = res && saveStudyData(studyName(), 0); // 0 means persistence file
   if ( res )
     emit saved( this );
 
@@ -727,7 +721,7 @@ bool SalomeApp_Study::dump( const QString& theFileName,
                             bool isMultiFile,
                             bool toSaveGUI )
 {
-  int savePoint;
+  int savePoint = 0;
   _PTR(AttributeParameter) ap;
   _PTR(IParameters) ip = ClientFactory::getIParameters(ap);
 
@@ -758,7 +752,8 @@ bool SalomeApp_Study::dump( const QString& theFileName,
         // This call simply passes the data model's dump output to SalomeApp_Engine servant.
         // This code is shared with persistence mechanism.
         // NOTE: this should be revised if behavior of saveModuleData() changes!
-        saveModuleData(aModel->module()->name(), listOfFiles);
+        saveModuleData(aModel->module()->name(), 1, // 1 means dump file
+                       listOfFiles);
     }
   }
 
@@ -775,7 +770,7 @@ bool SalomeApp_Study::dump( const QString& theFileName,
   // Issue 21377 - Clean up light module data in SalomeApp_Engine servant
   // This code is shared with persistence mechanism.
   // NOTE: this should be revised if behavior of saveStudyData() changes!
-  saveStudyData( theFileName );
+  saveStudyData( theFileName, 1 ); // 0 means persistence file
 
   return res;
 }
@@ -818,7 +813,7 @@ bool SalomeApp_Study::isSaved() const
   \param theModuleName - name of module
   \param theListOfFiles - list of files to be saved
 */
-void SalomeApp_Study::saveModuleData( QString theModuleName, QStringList theListOfFiles )
+void SalomeApp_Study::saveModuleData( QString theModuleName, int type, QStringList theListOfFiles )
 {
   int aNb = theListOfFiles.count();
   if ( aNb == 0 )
@@ -832,7 +827,7 @@ void SalomeApp_Study::saveModuleData( QString theModuleName, QStringList theList
     aListOfFiles[anIndex] = (*it).toUtf8().data();
     anIndex++;
   }
-  SetListOfFiles(theModuleName.toStdString().c_str(), aListOfFiles);
+  SetListOfFiles(theModuleName.toStdString().c_str(), type, aListOfFiles);
 }
 
 /*!
@@ -840,11 +835,11 @@ void SalomeApp_Study::saveModuleData( QString theModuleName, QStringList theList
   \param theModuleName - name of module
   \param theListOfFiles - list of files to be loaded
 */
-void SalomeApp_Study::openModuleData( QString theModuleName, QStringList& theListOfFiles )
+void SalomeApp_Study::openModuleData( QString theModuleName, int type, QStringList& theListOfFiles )
 {
-  std::vector<std::string> aListOfFiles =  GetListOfFiles( theModuleName.toStdString().c_str() );
+  std::vector<std::string> aListOfFiles = GetListOfFiles( theModuleName.toStdString().c_str(), type );
 
-  int i, aLength = aListOfFiles.size() - 1;
+  int i, aLength = (int)aListOfFiles.size() - 1; //!< TODO: conversion from size_t to int
   if ( aLength < 0 )
     return;
 
@@ -859,18 +854,17 @@ void SalomeApp_Study::openModuleData( QString theModuleName, QStringList& theLis
   Re-implemented from LightApp_Study, actually does not save anything but
   simply cleans up light modules' data
 */
-bool SalomeApp_Study::saveStudyData( const QString& theFileName )
+bool SalomeApp_Study::saveStudyData( const QString& /*theFileName*/, int type )
 {
   ModelList list; dataModels( list );
   QListIterator<CAM_DataModel*> it( list );
-  std::vector<std::string> listOfFiles(0);
   while ( it.hasNext() ){
     LightApp_DataModel* aLModel = 
       dynamic_cast<LightApp_DataModel*>( it.next() );
     // It is safe to call SetListOfFiles() for any kind of module
     // because SetListOfFiles() does nothing for full modules :)
     if ( aLModel )
-      SetListOfFiles(aLModel->module()->name().toStdString().c_str(), listOfFiles);
+      SetListOfFiles(aLModel->module()->name().toStdString().c_str(), type, std::vector<std::string>());
   }
   return true;
 }
@@ -878,9 +872,9 @@ bool SalomeApp_Study::saveStudyData( const QString& theFileName )
 /*!
   Loads data for study
 */
-bool SalomeApp_Study::openStudyData( const QString& theFileName )
+bool SalomeApp_Study::openStudyData( const QString& /*theFileName*/, int /*type*/ )
 {
- return true;
 return true;
 }
 
 /*!
@@ -1036,14 +1030,15 @@ bool SalomeApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm
     // for a given component in the study yet
   }
   QStringList listOfFiles;
-  openModuleData(dm->module()->name(), listOfFiles);
+  openModuleData(dm->module()->name(), 0, // 0 means persistence file
+                 listOfFiles);
   if (dm && dm->open(studyName, this, listOfFiles)) {
     // Remove the files and temporary directory, created
     // for this module by LightApp_Engine_i::Load()
     bool isMultiFile = false; // TODO: decide, how to access this parameter
-    RemoveTemporaryFiles( dm->module()->name().toStdString().c_str(), isMultiFile );
-    std::vector<std::string> listOfFiles   ;
-    SetListOfFiles( dm->module()->name().toStdString().c_str(), listOfFiles );
+    RemoveTemporaryFiles( dm->module()->name().toStdString().c_str(), isMultiFile, true );
+    SetListOfFiles( dm->module()->name().toStdString().c_str(), 0, // 0 means persistence file
+                    std::vector<std::string>() );
 
     // Something has been read -> create data model tree
     LightApp_DataModel* aDM = dynamic_cast<LightApp_DataModel*>( dm );
@@ -1061,15 +1056,14 @@ bool SalomeApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm
   \return list of files used by module: to be used by CORBAless modules
   \param theModuleName - name of module
 */
-std::vector<std::string> SalomeApp_Study::GetListOfFiles( const char* theModuleName  ) const
+std::vector<std::string> SalomeApp_Study::GetListOfFiles( const char* theModuleName, int type ) const
 {
   // Issue 21377 - using separate engine for each type of light module
   SalomeApp_Engine_i* aDefaultEngine = SalomeApp_Engine_i::GetInstance( theModuleName, false );
   if (aDefaultEngine)
-    return aDefaultEngine->GetListOfFiles();
+    return aDefaultEngine->GetListOfFiles( type );
 
-  std::vector<std::string> aListOfFiles;
-  return aListOfFiles;
+  return std::vector<std::string>();
 }
 
 /*!
@@ -1080,13 +1074,13 @@ std::vector<std::string> SalomeApp_Study::GetListOfFiles( const char* theModuleN
   \param theModuleName - name of module
   \param theListOfFiles - list of files
 */
-void SalomeApp_Study::SetListOfFiles ( const char* theModuleName,
+void SalomeApp_Study::SetListOfFiles ( const char* theModuleName, int type,
                                        const std::vector<std::string> theListOfFiles )
 {
   // Issue 21377 - using separate engine for each type of light module
   SalomeApp_Engine_i* aDefaultEngine = SalomeApp_Engine_i::GetInstance( theModuleName, false );
   if (aDefaultEngine)
-    aDefaultEngine->SetListOfFiles(theListOfFiles);
+    aDefaultEngine->SetListOfFiles(type, theListOfFiles);
 }
 
 /*!
@@ -1102,22 +1096,28 @@ std::string SalomeApp_Study::GetTmpDir ( const char* theURL, const bool  isMulti
 /*!
   Removes temporary files
 */
-void SalomeApp_Study::RemoveTemporaryFiles ( const char* theModuleName, const bool isMultiFile ) const
+void SalomeApp_Study::RemoveTemporaryFiles ( const char* theModuleName, bool isMultiFile, bool force )
 {
   if (isMultiFile)
     return;
 
-  SALOMEDS_Tool::ListOfFiles aListOfFiles = GetListOfFiles( theModuleName );
+  SALOMEDS_Tool::ListOfFiles aListOfFiles = GetListOfFiles( theModuleName, 0 );
   if (aListOfFiles.size() > 0) {
     std::string aTmpDir = aListOfFiles[0];
 
-    const int n = aListOfFiles.size() - 1;
+    const int n = (int)aListOfFiles.size() - 1; //!< TODO: conversion from size_t to int
     std::vector<std::string> aSeq;
     aSeq.reserve(n);
     for (int i = 0; i < n; i++)
       aSeq.push_back(CORBA::string_dup(aListOfFiles[i + 1].c_str()));
 
-    SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq, true);
+    SalomeApp_Engine_i* engine = SalomeApp_Engine_i::GetInstance( theModuleName, false );
+    bool toRemove = force || ( engine && !engine->keepFiles() );
+
+    if ( toRemove ) {
+      SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq, true);
+      engine->keepFiles( false );
+    }
   }
 }
 
@@ -1138,7 +1138,7 @@ LightApp_DataObject* SalomeApp_Study::findObjectByEntry( const QString& theEntry
 {
   LightApp_DataObject* o = 0;
   if ( myObserver ) {
-    o = dynamic_cast<LightApp_DataObject*>( myObserver->findObject( theEntry.toLatin1().constData() ) );
+    o = dynamic_cast<LightApp_DataObject*>( myObserver->findObject( theEntry.toUtf8().constData() ) );
   }
   if ( !o ) {
     o = LightApp_Study::findObjectByEntry( theEntry );
@@ -1154,7 +1154,7 @@ 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++ )
+  for( size_t i=0, n=aRefs.size(); i<n; i++ )
   {
     _PTR( SObject ) o = aRefs[i];
     if( o->GetFatherComponent()->ComponentDataType()==obj->GetFatherComponent()->ComponentDataType() )