Salome HOME
Merge branch 'master' into agy/NewPV1_For_850
[modules/gui.git] / src / SalomeApp / SalomeApp_Study.cxx
index 1a38c88e12a179a31704ddca75a4d17a7ffa0905..b60de1a8a0d93a4a544f82f5cdad2f5b72642599 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016  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
@@ -6,7 +6,7 @@
 // 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.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -26,7 +26,7 @@
 #include "SalomeApp_DataObject.h"
 #include "SalomeApp_DataModel.h"
 #include "SalomeApp_Application.h"
-#include "SalomeApp_Engine_i.hxx"
+#include "SalomeApp_Engine_i.h"
 #include "SalomeApp_VisualState.h"
 
 // temporary commented
 #include <SUIT_ResourceMgr.h>
 #include <SUIT_TreeModel.h>
 #include <SUIT_DataBrowser.h>
+#include <SUIT_MessageBox.h>
+#include <SUIT_Session.h>
+#include <SUIT_Desktop.h>
 
 #include <LightApp_Displayer.h>
 
+#ifndef DISABLE_PYCONSOLE
+  #include "SalomeApp_PyInterp.h" // WARNING! This include must be the first!
+#endif
+
 #include "utilities.h"
 
 #include "SALOMEDS_Tool.hxx"
@@ -75,7 +82,7 @@ class SalomeApp_Study::Observer_i : public virtual POA_SALOMEDS::Observer, QObje
 
 public:
 
-  Observer_i(_PTR(Study) aStudyDS, SalomeApp_Study* aStudy)
+  Observer_i(_PTR(Study) aStudyDS, SalomeApp_Study* aStudy):QObject(aStudy)
   {
     myStudyDS=aStudyDS;
     myStudy=aStudy;
@@ -153,10 +160,16 @@ public:
             SUIT_DataObject* oldFather = suit_obj->parent();
             if (oldFather) {
               oldFather->removeChild(suit_obj, false);
-             SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( myStudy->application() );
-             SUIT_AbstractModel* model = dynamic_cast<SUIT_AbstractModel*>(app->objectBrowser()->model());
-             model->forgetObject( suit_obj );
-               
+              SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( myStudy->application() );
+//              MESSAGE("myStudy: " << myStudy->id() << " app " << app);
+//              MESSAGE("objectBrowser: "<< app->objectBrowser());
+              if (!app->objectBrowser()) {
+                MESSAGE("Object Browser not found. Problem ??");
+                return;
+              }
+              SUIT_AbstractModel* model = dynamic_cast<SUIT_AbstractModel*>(app->objectBrowser()->model());
+              model->forgetObject( suit_obj );
+                
               if (SalomeApp_DataObject* oldFatherSA = dynamic_cast<SalomeApp_DataObject*>(oldFather)) {
                 oldFatherSA->updateItem();
               }
@@ -167,7 +180,7 @@ public:
             entry2SuitObject[theID] = suit_obj;
           }
 
-         suit_obj->updateItem();
+          suit_obj->updateItem();
           // define position in the data tree (in aFatherDO) to insert the aSObj
           int pos = -1;
           //int childDataObjCount = aFatherDO->childCount();
@@ -183,6 +196,22 @@ public:
           //aFatherDO->insertChild(suit_obj, pos);
           aFatherDO->updateItem();
 
+          /* 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 !!!");
+              }
+            }
+          }
         } // END: work with tree nodes structure
         else { // BEGIN: work with study structure
           EntryMapIter it = entry2SuitObject.find( theID );
@@ -232,12 +261,13 @@ public:
               std::string::size_type debut = 0;
               std::string::size_type fin;
               SalomeApp_DataObject* anObj = dynamic_cast<SalomeApp_DataObject*>( myStudy->root() );
-              while ( 1 ) {
+              while ( anObj ) {
                 fin = obj_id.find_first_of( ':', debut );
                 if ( fin == std::string::npos ) {
                   //last id
                   anObj = dynamic_cast<SalomeApp_DataObject*>(anObj->childObject(atoi(obj_id.substr(debut).c_str())-1));
-                  entry2SuitObject[parent_id] = anObj;
+                  if ( anObj )
+                    entry2SuitObject[parent_id] = anObj;
                   break;
                 }
                 anID = root_id + obj_id.substr( 0, fin );
@@ -245,13 +275,15 @@ public:
                 if ( it2 == entry2SuitObject.end() ) {
                   //the ID is not known in entry2SuitObject
                   anObj = dynamic_cast<SalomeApp_DataObject*>(anObj->childObject(atoi(obj_id.substr(debut, fin-debut).c_str())-1));
-                  entry2SuitObject[anID] = anObj;
+                  if ( anObj )
+                    entry2SuitObject[anID] = anObj;
                 }
                 else
                   anObj = it2->second;
                 debut = fin+1;
               }
-              anObj->insertChildAtTag( suit_obj, tag );
+              if ( anObj )
+                anObj->insertChildAtTag( suit_obj, tag );
             }
           }
           entry2SuitObject[theID] = suit_obj;
@@ -264,14 +296,11 @@ public:
         if ( it != entry2SuitObject.end() )
         {
           suit_obj = it->second;
-          // VSR: object is not removed, since SALOMEDS::SObject is not actually removed,
-          //      only its attributes are cleared;
-          //      thus, the object can be later reused
           suit_obj->updateItem();
-          //SUIT_DataObject* father=suit_obj->parent();
-          //if(father)
-          //  father->removeChild(suit_obj);
-          //entry2SuitObject.erase(it);
+          SUIT_DataObject* father=suit_obj->parent();
+          if(father)
+            father->removeChild(suit_obj);
+          entry2SuitObject.erase(it);
         }
         else
         {
@@ -311,13 +340,20 @@ public:
                 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 !!!");
+              //else
+                //MESSAGE("Object with entry : "<< theID <<" CAN'T be displayed !!!");
             }
           }
         }
         break;
       }
+#ifndef DISABLE_PYCONSOLE
+    case 6: //NoteBook variables were modified
+      {
+        myStudy->onNoteBookVarUpdate( QString( theID.c_str() ) );
+        break;
+      }
+#endif
     default:MESSAGE("Unknown event: "  << event);break;
     } //switch
   } //notifyObserverID_real
@@ -375,7 +411,18 @@ SalomeApp_Study::SalomeApp_Study( SUIT_Application* app )
 */
 SalomeApp_Study::~SalomeApp_Study()
 {
+  if ( myObserver ) {
+    PortableServer::ObjectId_var oid = myObserver->_default_POA()->servant_to_id( myObserver );
+    myObserver->_default_POA()->deactivate_object( oid.in() );
+  }
+}
+
+#ifndef DISABLE_PYCONSOLE
+void SalomeApp_Study::onNoteBookVarUpdate( QString theVarName)
+{
+  emit notebookVarUpdated( theVarName );
 }
+#endif
 
 /*!
   Gets study id.
@@ -397,7 +444,7 @@ QString SalomeApp_Study::studyName() const
   // it can be changed outside of GUI
   // TEMPORARILY SOLUTION: better to be implemented with help of SALOMEDS observers
   if ( studyDS() ) {
-    QString newName = studyDS()->Name().c_str();
+    QString newName = QString::fromUtf8(studyDS()->Name().c_str());
     if ( LightApp_Study::studyName() != newName ) {
       SalomeApp_Study* that = const_cast<SalomeApp_Study*>( this );
       that->setStudyName( newName );
@@ -424,7 +471,28 @@ bool SalomeApp_Study::createDocument( const QString& theStr )
 
   // initialize myStudyDS, read HDF file
   QString aName = newStudyName();
-  _PTR(Study) study ( SalomeApp_Application::studyMgr()->NewStudy( aName.toUtf8().data() ) );
+
+  _PTR(Study) study;
+  bool showError = !application()->property("open_study_from_command_line").isValid() || 
+    !application()->property("open_study_from_command_line").toBool();
+  try {
+    study = _PTR(Study)( SalomeApp_Application::studyMgr()->NewStudy( aName.toUtf8().data() ) );
+  }
+  catch(const SALOME_Exception& ex) {
+    application()->putInfo(tr(ex.what()));
+    if ( showError )
+      SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(),
+                                 tr("ERR_ERROR"), tr(ex.what()));
+    return false;
+  } 
+  catch(...) {
+    application()->putInfo(tr("CREATE_DOCUMENT_PROBLEM"));
+    if ( showError )
+      SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(),
+                                 tr("ERR_ERROR"), tr("CREATE_DOCUMENT_PROBLEM"));
+    return false;
+  }
+
   if ( !study )
     return false;
 
@@ -460,7 +528,27 @@ bool SalomeApp_Study::openDocument( const QString& theFileName )
   MESSAGE( "openDocument" );
 
   // initialize myStudyDS, read HDF file
-  _PTR(Study) study ( SalomeApp_Application::studyMgr()->Open( theFileName.toUtf8().data() ) );
+  _PTR(Study) study;
+  bool showError = !application()->property("open_study_from_command_line").isValid() || 
+    !application()->property("open_study_from_command_line").toBool();
+  try {
+    study = _PTR(Study) ( SalomeApp_Application::studyMgr()->Open( theFileName.toUtf8().data() ) );
+  }
+  catch(const SALOME_Exception& ex) {
+    application()->putInfo(tr(ex.what()));
+    if ( showError )
+      SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(),
+                                 tr("ERR_ERROR"), tr(ex.what()));
+    return false;
+  } 
+  catch(...) {
+    application()->putInfo(tr("OPEN_DOCUMENT_PROBLEM"));
+    if ( showError )
+      SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(),
+                                 tr("ERR_ERROR"), tr("OPEN_DOCUMENT_PROBLEM"));
+    return false;
+  }
+
   if ( !study )
     return false;
 
@@ -543,6 +631,10 @@ bool SalomeApp_Study::loadDocument( const QString& theStudyName )
 #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 );
@@ -574,7 +666,7 @@ bool SalomeApp_Study::saveDocumentAs( const QString& theFileName )
     // Cast to LightApp class in order to give a chance
     // to light modules to save their data
     if ( LightApp_DataModel* aModel = 
-        dynamic_cast<LightApp_DataModel*>( it.next() ) ) {
+         dynamic_cast<LightApp_DataModel*>( it.next() ) ) {
       listOfFiles.clear();
       aModel->saveAs( theFileName, this, listOfFiles );
       if ( !listOfFiles.isEmpty() )
@@ -619,7 +711,7 @@ bool SalomeApp_Study::saveDocument()
     // Cast to LightApp class in order to give a chance
     // to light modules to save their data
     if ( LightApp_DataModel* aModel = 
-        dynamic_cast<LightApp_DataModel*>( it.next() ) ) {
+         dynamic_cast<LightApp_DataModel*>( it.next() ) ) {
       listOfFiles.clear();
       aModel->save(listOfFiles);
       if ( !listOfFiles.isEmpty() )
@@ -656,8 +748,18 @@ void SalomeApp_Study::closeDocument(bool permanently)
   _PTR(Study) studyPtr = studyDS();
   if ( studyPtr )
   {
-    if(permanently) {
+    if ( myObserver )
+      myStudyDS->detach( myObserver->_this() );
+    if ( permanently ) {
+      SUIT_Desktop* desk = SUIT_Session::session()->activeApplication()->desktop();
+      bool isBlocked = desk->signalsBlocked();
+      desk->blockSignals( true );
       SalomeApp_Application::studyMgr()->Close( studyPtr );
+      desk->blockSignals( isBlocked );
+#ifndef DISABLE_PYCONSOLE
+      SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( application() );
+      app->getPyInterp()->destroy();
+#endif
     }
     SALOMEDSClient_Study* aStudy = 0;
     setStudyDS( _PTR(Study)(aStudy) );
@@ -680,9 +782,9 @@ void SalomeApp_Study::closeDocument(bool permanently)
   \return - true if the operation succeeds, and false otherwise.
 */
 bool SalomeApp_Study::dump( const QString& theFileName, 
-                           bool toPublish, 
-                           bool isMultiFile,
-                           bool toSaveGUI )
+                            bool toPublish, 
+                            bool isMultiFile,
+                            bool toSaveGUI )
 {
   int savePoint;
   _PTR(AttributeParameter) ap;
@@ -709,13 +811,13 @@ bool SalomeApp_Study::dump( const QString& theFileName,
   QStringList listOfFiles;
   while ( it.hasNext() ) {
     if ( LightApp_DataModel* aModel = 
-        dynamic_cast<LightApp_DataModel*>( it.next() ) ) {
+         dynamic_cast<LightApp_DataModel*>( it.next() ) ) {
       listOfFiles.clear();
       if ( aModel->dumpPython( theFileName, this, isMultiFile, listOfFiles ) && 
-          !listOfFiles.isEmpty() )
-       // 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!
+           !listOfFiles.isEmpty() )
+        // 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);
     }
   }
@@ -724,9 +826,9 @@ bool SalomeApp_Study::dump( const QString& theFileName,
   // any light module is present in the current configuration
   QFileInfo aFileInfo( theFileName );
   bool res = aStudy->DumpStudy( aFileInfo.absolutePath().toUtf8().data(),
-                               aFileInfo.baseName().toUtf8().data(),
-                               toPublish,
-                               isMultiFile);
+                                aFileInfo.baseName().toUtf8().data(),
+                                toPublish,
+                                isMultiFile);
   if ( toSaveGUI )
     removeSavePoint( savePoint ); //SRN: remove the created temporary save point.
 
@@ -860,7 +962,7 @@ void SalomeApp_Study::setStudyDS( const _PTR(Study)& s )
   \sa LightApp_Study class, LightApp_DataModel class
 */
 CAM_ModuleObject* SalomeApp_Study::createModuleObject( LightApp_DataModel* theDataModel, 
-                                                      SUIT_DataObject* theParent ) const
+                                                       SUIT_DataObject* theParent ) const
 {
   SalomeApp_Study* that = const_cast<SalomeApp_Study*>( this );
   
@@ -941,7 +1043,7 @@ void SalomeApp_Study::addComponent(const CAM_DataModel* dm)
       // Set default engine IOR
       // Issue 21377 - using separate engine for each type of light module
       std::string anEngineIOR = SalomeApp_Engine_i::EngineIORForComponent( aCompDataType.c_str(),
-                                                                          true );
+                                                                           true );
       aBuilder->DefineComponentInstance(aComp, anEngineIOR);
       //SalomeApp_DataModel::BuildTree( aComp, root(), this, /*skipExisitng=*/true );
       SalomeApp_DataModel::synchronize( aComp, this );
@@ -1017,6 +1119,8 @@ bool SalomeApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm
     // 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 );
 
     // Something has been read -> create data model tree
     LightApp_DataModel* aDM = dynamic_cast<LightApp_DataModel*>( dm );
@@ -1116,19 +1220,28 @@ void SalomeApp_Study::RemoveTemporaryFiles ( const char* theModuleName, const bo
   }
 }
 
+#ifndef DISABLE_PYCONSOLE
 /*!
   Mark the study as saved in the file
   \param theFileName - the name of file
 */
-void SalomeApp_Study::markAsSavedIn(QString theFileName)
+void SalomeApp_Study::updateFromNotebook( const QString& theFileName, bool isSaved )
 {
   setStudyName(theFileName);
-  setIsSaved(true);
+  studyDS()->Name(theFileName.toStdString());
+  setIsSaved( isSaved );
 }
+#endif
 
 LightApp_DataObject* SalomeApp_Study::findObjectByEntry( const QString& theEntry )
 {
-  LightApp_DataObject* o = dynamic_cast<LightApp_DataObject*>( myObserver ? myObserver->findObject( theEntry.toLatin1().constData() ) : 0 );
+  LightApp_DataObject* o = 0;
+  if ( myObserver ) {
+    o = dynamic_cast<LightApp_DataObject*>( myObserver->findObject( theEntry.toLatin1().constData() ) );
+  }
+  if ( !o ) {
+    o = LightApp_Study::findObjectByEntry( theEntry );
+  }
   return o;
 }