Salome HOME
Extra paths in embedded interpreter
[modules/gui.git] / src / SalomeApp / SalomeApp_Application.cxx
index ec97e8c9d10112e2f392d1c0c8165a6c339f17b5..0cf0f4c81ef558432a70332cbe501666f6f42cac 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2021  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
@@ -80,8 +80,6 @@
 //#include <OB_ListItem.h>
 
 
-#include <Utils_ORB_INIT.hxx>
-#include <Utils_SINGLETON.hxx>
 #include <SALOME_LifeCycleCORBA.hxx>
 
 #include <QApplication>
@@ -97,7 +95,9 @@
 #include <QtDebug>
 
 #include <SALOMEDSClient_ClientFactory.hxx>
+#include <ArgvKeeper.hxx>
 #include <Basics_Utils.hxx>
+#include <OpUtil.hxx>
 
 #include <SALOME_ListIO.hxx>
 #include <SALOME_Prs.h>
 
 #include <SALOMEDS_Tool.hxx>
 
+std::unique_ptr<SALOME_NamingService_Abstract> SalomeApp_Application::_ns;
+
 /*!Internal class that updates object browser item properties */
 // temporary commented
 /*class SalomeApp_Updater : public OB_Updater
@@ -155,17 +157,50 @@ void SalomeApp_Updater::update( SUIT_DataObject* theObj, OB_ListItem* theItem )
   //}
 }*/
 
-/*!Create new instance of SalomeApp_Application.*/
-extern "C" SALOMEAPP_EXPORT SUIT_Application* createApplication()
+namespace
 {
-  return new SalomeApp_Application();
+  /*!
+    \brief Flag locker.
+  */
+  class MessageLocker
+  {
+  public:
+    //! Constructor. Sets passed boolean flag to \c true.
+    MessageLocker( bool& Lock ) : myPrevState( Lock ), myLock( Lock ) { myLock = true; }
+    //! Destructor. Clear external boolean flag passed as parameter to the constructor to \c false.
+    ~MessageLocker() { myLock = myPrevState; }
+  private:
+    bool  myPrevState;
+    bool& myLock; //! External 'Lock state' boolean flag
+  };
+
+  /*!
+    \brief Dynamic property manager
+  */
+  class PropertyMgr
+  {
+    QObject* myObject;
+    QString myProperty;
+  public:
+    PropertyMgr(QObject* object, const QString& property, const QVariant& value)
+      : myObject(object), myProperty(property)
+    {
+      myObject->setProperty(qPrintable(myProperty), value);
+    }
+    ~PropertyMgr()
+    {
+      myObject->setProperty(qPrintable(myProperty), QVariant());
+    }
+  };
 }
 
 /*!Constructor.*/
-SalomeApp_Application::SalomeApp_Application()
-  : LightApp_Application(),
-    myIsCloseFromExit( false )
+SalomeApp_Application::SalomeApp_Application(SALOME_NamingService_Abstract *ns):myIsCloseFromExit( false ),myToIgnoreMessages( false )
 {
+  if(!ns)
+    _ns.reset(new SALOME_NamingService(orb()));
+  else
+    _ns.reset(ns);
 }
 
 /*!Destructor.
@@ -207,9 +242,10 @@ void SalomeApp_Application::start()
     QString hdffile;
     QStringList pyfiles;
 
-    for (int i = 1; i < qApp->arguments().size(); i++) {
+    QStringList args = QApplication::arguments();
+    for (int i = 1; i < args.count(); i++) {
       QRegExp rxs ("--study-hdf=(.+)");
-      if ( rxs.indexIn( qApp->arguments()[i] ) >= 0 && rxs.capturedTexts().count() > 1 ) {
+      if ( rxs.indexIn( args[i] ) >= 0 && rxs.capturedTexts().count() > 1 ) {
         QString file = rxs.capturedTexts()[1];
         QFileInfo fi ( file );
         QString extension = fi.suffix().toLower();
@@ -218,7 +254,7 @@ void SalomeApp_Application::start()
       }
       else {
         QRegExp rxp ("--pyscript=\\[(.+)\\]");
-        if ( rxp.indexIn( qApp->arguments()[i] ) >= 0 && rxp.capturedTexts().count() > 1 ) {
+        if ( rxp.indexIn( args[i] ) >= 0 && rxp.capturedTexts().count() > 1 ) {
           // pyscript
           QStringList dictList = rxp.capturedTexts()[1].split("},", QString::SkipEmptyParts);
           for (int k = 0; k < dictList.count(); ++k) {
@@ -238,10 +274,12 @@ void SalomeApp_Application::start()
     LightApp_Application::start();
     SALOME_EventFilter::Init();
 
-    setProperty("open_study_from_command_line", true);
-    if ( !hdffile.isEmpty() ) // open hdf file given as parameter
+    if ( !hdffile.isEmpty() )
+    {
+      // open hdf file given as parameter
+      PropertyMgr propm( this, "open_study_from_command_line", true );
       onOpenDoc( hdffile );
-    setProperty("open_study_from_command_line", QVariant());
+    }
 
 #ifndef DISABLE_PYCONSOLE
     // import/execute python scripts
@@ -252,28 +290,25 @@ void SalomeApp_Application::start()
         if ( !getStudy()->GetProperties()->IsLocked() ) {
           // pyfiles[j] is a dictionary: {"/absolute/path/to/script.py": [script_args]}
           // Path is absolute, script has .py extension
-          for (uint j = 0; j < pyfiles.count(); j++ ) {
+          for (int j = 0; j < pyfiles.count(); j++ ) {
             // Extract scripts and their arguments, if any
             QRegExp rxp ("\"(.+)\":[\\s]*\\[(.*)\\]");
             if ( rxp.indexIn( pyfiles[j] ) >= 0 && rxp.capturedTexts().count() == 3 ) {
               QString script = rxp.capturedTexts()[1];
-              QString args = "";
+              QStringList args;
               QStringList argList = __getArgsList(rxp.capturedTexts()[2]);
-              for (uint k = 0; k < argList.count(); k++ ) {
+              for (int k = 0; k < argList.count(); k++ ) {
                 QString arg = argList[k].trimmed();
                 arg.remove( QRegExp("^[\"]") );
                 arg.remove( QRegExp("[\"]$") );
-                args += arg+",";
-              }
-              args.remove( QRegExp("[,]$") );
-              if (!args.isEmpty()) {
-                args = "args:"+args;
+                args << QString("\"%1\"").arg(arg);
               }
+              if (args.count() == 1)
+                args << "";
 
               script.remove( QRegExp("^python.*[\\s]+") );
-              QString cmd = script+" "+args;
-
-              QString command = QString( "exec(open(\"%1\", \"rb\").read())" ).arg(cmd.trimmed());
+              QString command = QString( "exec(open(\"%1\", \"rb\").read(), args=(%2))" ).arg(script).arg(args.join(","));
+              PropertyMgr propm( this, "IsLoadedScript", true );
               pyConsole->exec(command);
             }
           } // end for loop on pyfiles QStringList
@@ -382,6 +417,8 @@ void SalomeApp_Application::createActions()
 */
 void SalomeApp_Application::onExit()
 {
+  //MessageLocker ml( myToIgnoreMessages );
+
   bool killServers = false;
   bool result = true;
 
@@ -398,9 +435,19 @@ void SalomeApp_Application::onExit()
   }
 }
 
+/*!SLOT. Create a document.*/
+void SalomeApp_Application::onNewDoc()
+{
+  MessageLocker ml( myToIgnoreMessages );
+
+  LightApp_Application::onNewDoc();
+}
+
 /*!SLOT. Load document.*/
 void SalomeApp_Application::onLoadDoc()
 {
+  MessageLocker ml( myToIgnoreMessages );
+
   QString studyName;
 
   // rnv: According to the single-study approach on the server side
@@ -498,11 +545,10 @@ void SalomeApp_Application::onNewWithScript()
   {
     onNewDoc();
 
-    QString command = QString("exec(open(\"%1\", \"rb\").read())").arg(aFile);
-
 #ifndef DISABLE_PYCONSOLE
+    QString command = QString("exec(open(\"%1\", \"rb\").read())").arg(aFile);
     PyConsole_Console* pyConsole = pythonConsole();
-
+    PropertyMgr propm( this, "IsLoadedScript", true );
     if ( pyConsole )
       pyConsole->exec( command );
 #endif
@@ -549,6 +595,11 @@ bool SalomeApp_Application::onLoadDoc( const QString& aName )
 /*!SLOT. Parse message for desktop.*/
 void SalomeApp_Application::onDesktopMessage( const QString& message )
 {
+  if ( myToIgnoreMessages )
+    return; // a message from SALOMEDS is caused by GUI action
+
+  MessageLocker ml( myToIgnoreMessages );
+
   if (message.indexOf("studyCreated") == 0) {
     if (!activeStudy()) {
       onNewDoc();
@@ -566,7 +617,8 @@ void SalomeApp_Application::onDesktopMessage( const QString& message )
     }
   }
   else if ( message.toLower() == "connect_to_study" ) {
-    onLoadDoc();
+    if ( activeStudy() )
+      useStudy( activeStudy()->studyName() );
   }
   if (message.indexOf("studyNameChanged") == 0) {
     updateDesktopTitle();
@@ -672,10 +724,37 @@ void SalomeApp_Application::onCloseDoc( bool ask )
                                     SUIT_MessageBox::No) == SUIT_MessageBox::No ) return;
 
   }
+  MessageLocker ml( myToIgnoreMessages );
+
   LightApp_Application::onCloseDoc( ask );
 
   // reinitialize study to have empty data
-  getStudy()->Init();
+  //getStudy()->Init();
+}
+
+/*!SLOT. Reload document from the file.*/
+bool SalomeApp_Application::onReopenDoc()
+{
+  MessageLocker ml( myToIgnoreMessages );
+
+  return LightApp_Application::onReopenDoc();
+}
+
+
+/*!SLOT. Load document.*/
+void SalomeApp_Application::onOpenDoc()
+{
+  MessageLocker ml( myToIgnoreMessages );
+
+  LightApp_Application::onOpenDoc();
+}
+
+/*!SLOT. Load document.*/
+bool SalomeApp_Application::onOpenDoc(const QString& name)
+{
+  MessageLocker ml( myToIgnoreMessages );
+
+  return LightApp_Application::onOpenDoc(name);
 }
 
 /*!Sets enable or disable some actions on selection changed.*/
@@ -752,6 +831,8 @@ void SalomeApp_Application::onOpenWith()
   Handle(SALOME_InteractiveObject) aIObj = aList.First();
   QString aModuleName(aIObj->getComponentDataType());
   QString aModuleTitle = moduleTitle(aModuleName);
+  if (aModuleTitle.isEmpty()) // no gui
+    aModuleTitle = moduleDisplayer(aModuleName);
   activateModule(aModuleTitle);
   QApplication::restoreOverrideCursor();
 }
@@ -775,6 +856,8 @@ SUIT_Study* SalomeApp_Application::createNewStudy()
            this, SIGNAL(notebookVarUpdated(QString)) );
 #endif
 
+  getStudy()->Init();
+
   return aStudy;
 }
 
@@ -903,6 +986,7 @@ void SalomeApp_Application::onDumpStudy( )
       bool res;
       {
         SUIT_OverrideCursor wc;
+        ensureShaperIsActivated();
         res = appStudy->dump( aFileName, toPublish, isMultiFile, toSaveGUI );
       }
       if ( !res )
@@ -935,14 +1019,12 @@ void SalomeApp_Application::onLoadScript( )
 
   if ( !aFile.isEmpty() )
   {
-
-    QString command = QString("exec(open(\"%1\", \"rb\").read())").arg(aFile);
-
 #ifndef DISABLE_PYCONSOLE
+    QString command = QString("exec(compile(open('%1', 'rb').read(), '%1', 'exec'))").arg(aFile);
     PyConsole_Console* pyConsole = pythonConsole();
-
+    PropertyMgr propm( this, "IsLoadedScript", true );
     if ( pyConsole )
-      pyConsole->exec( command );
+      pyConsole->exec(command);
 #endif
   }
 }
@@ -1126,7 +1208,7 @@ void SalomeApp_Application::updateDesktopTitle() {
   desktop()->setWindowTitle( aTitle );
 }
 
-int SalomeApp_Application::closeChoice( const QString& docName )
+int SalomeApp_Application::closeChoice( const QString& /*docName*/ )
 {
   QStringList buttons;
   QMap<int, int> choices;
@@ -1169,6 +1251,7 @@ bool SalomeApp_Application::closeAction( const int choice, bool& closePermanentl
       onSaveDoc();
     else if ( !onSaveAsDoc() )
       res = false;
+    // fall through!
   case CloseDisconnect:
     closeActiveDoc( false );
     closePermanently = false;
@@ -1217,9 +1300,8 @@ bool SalomeApp_Application::openAction( const int aChoice, const QString& aName
   switch ( choice )
   {
   case OpenRefresh:
-    {
-      choice = OpenNew;
-    }
+    choice = OpenNew;
+    // fall through!
   default:
     res = LightApp_Application::openAction( choice, aName );
     break;
@@ -1284,8 +1366,8 @@ CORBA::ORB_var SalomeApp_Application::orb()
 
   if ( CORBA::is_nil( _orb ) ) {
     Qtx::CmdLineArgs args;
-    ORB_INIT& init = *SINGLETON_<ORB_INIT>::Instance();
-    _orb = init( args.argc(), args.argv() );
+    SetArgcArgv( args.argc(), args.argv() );
+    _orb = KERNEL::GetRefToORB();
   }
 
   return _orb;
@@ -1304,10 +1386,9 @@ _PTR(Study) SalomeApp_Application::getStudy()
 }
 
 /*!Create and return SALOME_NamingService.*/
-SALOME_NamingServiceSalomeApp_Application::namingService()
+SALOME_NamingService_Abstract *SalomeApp_Application::namingService()
 {
-  static SALOME_NamingService _ns(orb());
-  return &_ns;
+  return _ns.get();
 }
 
 /*!Create and return SALOME_LifeCycleCORBA.*/
@@ -1424,9 +1505,14 @@ void SalomeApp_Application::contextMenuPopup( const QString& type, QMenu* thePop
     if ( !entry.startsWith( tr( "SAVE_POINT_DEF_NAME" ) ) ) {
       QString aModuleName( aIObj->getComponentDataType() );
       QString aModuleTitle = moduleTitle( aModuleName );
+      if (aModuleTitle.isEmpty()) {
+        // use displayer module, if given
+        aModuleTitle = moduleDisplayer( aModuleName );
+      }
       CAM_Module* currentModule = activeModule();
-      if ( ( !currentModule || currentModule->moduleName() != aModuleTitle ) && !aModuleTitle.isEmpty() )
+      if ( ( !currentModule || currentModule->moduleName() != aModuleTitle ) && !aModuleTitle.isEmpty() ) {
         thePopup->addAction( tr( "MEN_OPENWITH" ).arg( aModuleTitle ), this, SLOT( onOpenWith() ) );
+      }
     }
   }
 
@@ -1658,7 +1744,7 @@ void SalomeApp_Application::updateSavePointDataObjects( SalomeApp_Study* study )
 
   // iterate new save points.  if DataObject with such ID not found in map - create DataObject
   // if in the map - remove it from map.
-  for ( int i = 0; i < savePoints.size(); i++ )
+  for ( size_t i = 0; i < savePoints.size(); i++ )
     if ( !mapDO.contains( savePoints[i] ) )
       new SalomeApp_SavePointObject( guiRootObj, savePoints[i], study );
     else
@@ -1834,7 +1920,7 @@ bool SalomeApp_Application::renameAllowed( const QString& entry) const
   \param name new name of the object
   \brief Return \c true if rename operation finished successfully, \c false otherwise.
 */
-bool SalomeApp_Application::renameObject( const QString& entry, const QString& name )
+bool SalomeApp_Application::renameObject( const QString& /*entry*/, const QString& name )
 {
   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( activeStudy() );
 
@@ -1887,6 +1973,7 @@ bool SalomeApp_Application::updateStudy()
 
   // get unique temporary directory name
   QString aTmpDir = QString::fromStdString( SALOMEDS_Tool::GetTmpDir() );
+
   if( aTmpDir.isEmpty() )
     return false;
 
@@ -1966,21 +2053,25 @@ bool SalomeApp_Application::onRestoreStudy( const QString& theDumpScript,
 {
   bool ok = true;
 
+  // create a new study
+  onNewDoc();
+
   // get active application
   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
 
   // load study from the temporary directory
-
-  QString command = QString( "exec(open(\"%1\" ,\"rb\").read())" ).arg( theDumpScript );
+  QFileInfo aScriptInfo = QFileInfo(theDumpScript);
+  QString command = QString( "exec(open(\"%1\" ,\"rb\").read())" ).arg(aScriptInfo.canonicalFilePath());
 
 #ifndef DISABLE_PYCONSOLE
   PyConsole_Console* pyConsole = app->pythonConsole();
-  if ( pyConsole )
+  if ( pyConsole ) {
+    PropertyMgr propm( this, "IsLoadedScript", true );
     pyConsole->execAndWait( command );
+  }
 #endif
 
   // remove temporary directory
-  QFileInfo aScriptInfo = QFileInfo( theDumpScript );
   QString aStudyName = aScriptInfo.baseName();
   QDir aDir = aScriptInfo.absoluteDir();
   QStringList aFiles = aDir.entryList( QStringList( "*.py*" ) );
@@ -2022,6 +2113,14 @@ void SalomeApp_Application::afterCloseDoc()
   LightApp_Application::afterCloseDoc();
 }
 
+bool SalomeApp_Application::canOpenDoc( const QString& url )
+{
+  _PTR(Study) aStudyDS = getStudy();
+  if ( aStudyDS )
+    return aStudyDS->CanOpen( url.toUtf8().data() );
+  return false;
+}
+
 /*
   Asks to close existing document.
 */
@@ -2035,7 +2134,25 @@ bool SalomeApp_Application::checkExistingDoc()
 
 PyConsole_Interp* SalomeApp_Application::createPyInterp()
 {
-  return new SalomeApp_PyInterp;
+  return new SalomeApp_PyInterp( resourceMgr() );
 }
 
 #endif // DISABLE_PYCONSOLE
+
+void SalomeApp_Application::ensureShaperIsActivated()
+{
+  SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
+  _PTR(Study) studyDS = getStudy();
+  if ( study && studyDS )
+  {
+    _PTR(SObject) shaper = studyDS->FindObjectByPath("/Shaper"); // non null result if shaper data is present in the study
+    bool shaperIsActive = false;
+    QList<CAM_DataModel*> models;
+    study->dataModels( models );
+    for( int i = 0; i < models.count() && !shaperIsActive; i++ )
+      shaperIsActive = models[i]->module()->moduleName() == "Shaper";
+       
+    if (shaper && !shaperIsActive)
+      onDesktopMessage("register_module_in_study/Shaper");
+  }
+}