Salome HOME
getting access to Python plugin helpers in other modules
[modules/gui.git] / src / SalomeApp / SalomeApp_Study.cxx
index f5b0f58c4f7036aae73788072ad596576b0be768..8598e2bdb9b52c409bd9c5708a1f90e564e096f0 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2015  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
 #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"
@@ -248,12 +255,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 );
@@ -261,13 +269,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;
@@ -280,14 +290,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
         {
@@ -334,11 +341,13 @@ public:
         }
         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
@@ -402,10 +411,12 @@ SalomeApp_Study::~SalomeApp_Study()
   }
 }
 
+#ifndef DISABLE_PYCONSOLE
 void SalomeApp_Study::onNoteBookVarUpdate( QString theVarName)
 {
   emit notebookVarUpdated( theVarName );
 }
+#endif
 
 /*!
   Gets study id.
@@ -454,7 +465,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;
 
@@ -490,7 +522,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;
 
@@ -690,8 +742,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) );
@@ -1150,6 +1212,7 @@ 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
@@ -1160,6 +1223,7 @@ void SalomeApp_Study::updateFromNotebook( const QString& theFileName, bool isSav
   studyDS()->Name(theFileName.toStdString());
   setIsSaved( isSaved );
 }
+#endif
 
 LightApp_DataObject* SalomeApp_Study::findObjectByEntry( const QString& theEntry )
 {