Salome HOME
[bos #35159][EDF] (2023-T1) Following commands in Python console. Fixed intermediate...
[modules/gui.git] / src / SalomeApp / SalomeApp_Application.cxx
index d5cc4fb4452aa4856d1726df763a1babf3726086..a7046eaca0523675b8d361f7ca4c88e0f9d4c681 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2023  CEA, EDF, 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>
 
+#include <SALOMEconfig.h>
+#include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
+
 std::unique_ptr<SALOME_NamingService_Abstract> SalomeApp_Application::_ns;
 
 /*!Internal class that updates object browser item properties */
@@ -173,6 +176,25 @@ namespace
     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.*/
@@ -255,10 +277,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
@@ -287,6 +311,7 @@ void SalomeApp_Application::start()
 
               script.remove( QRegExp("^python.*[\\s]+") );
               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
@@ -509,29 +534,7 @@ void SalomeApp_Application::onUnloadDoc( bool ask )
 /*!SLOT. Create new study and load script*/
 void SalomeApp_Application::onNewWithScript()
 {
-  QStringList filtersList;
-  filtersList.append(tr("PYTHON_FILES_FILTER"));
-  filtersList.append(tr("ALL_FILES_FILTER"));
-
-  QString anInitialPath = "";
-  if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
-    anInitialPath = QDir::currentPath();
-
-  QString aFile = SUIT_FileDlg::getFileName( desktop(), anInitialPath, filtersList, tr( "TOT_DESK_FILE_LOAD_SCRIPT" ), true, true );
-
-  if ( !aFile.isEmpty() )
-  {
-    onNewDoc();
-
-    QString command = QString("exec(open(\"%1\", \"rb\").read())").arg(aFile);
-
-#ifndef DISABLE_PYCONSOLE
-    PyConsole_Console* pyConsole = pythonConsole();
-
-    if ( pyConsole )
-      pyConsole->exec( command );
-#endif
-  }
+  execScript(true);
 }
 
 
@@ -810,6 +813,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();
 }
@@ -984,28 +989,7 @@ void SalomeApp_Application::onLoadScript( )
     return;
   }
 
-  QStringList filtersList;
-  filtersList.append(tr("PYTHON_FILES_FILTER"));
-  filtersList.append(tr("ALL_FILES_FILTER"));
-
-  QString anInitialPath = "";
-  if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
-    anInitialPath = QDir::currentPath();
-
-  QString aFile = SUIT_FileDlg::getFileName( desktop(), anInitialPath, filtersList, tr( "TOT_DESK_FILE_LOAD_SCRIPT" ), true, true );
-
-  if ( !aFile.isEmpty() )
-  {
-
-    QString command = QString("exec(compile(open('%1', 'rb').read(), '%1', 'exec'))").arg(aFile);
-
-#ifndef DISABLE_PYCONSOLE
-    PyConsole_Console* pyConsole = pythonConsole();
-
-    if ( pyConsole )
-      pyConsole->exec( command );
-#endif
-  }
+  execScript(false);
 }
 
 /*!Private SLOT. On save GUI state.*/
@@ -1345,8 +1329,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;
@@ -1484,9 +1468,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() ) );
+      }
     }
   }
 
@@ -2039,8 +2028,10 @@ bool SalomeApp_Application::onRestoreStudy( const QString& theDumpScript,
 
 #ifndef DISABLE_PYCONSOLE
   PyConsole_Console* pyConsole = app->pythonConsole();
-  if ( pyConsole )
+  if ( pyConsole ) {
+    PropertyMgr propm( this, "IsLoadedScript", true );
     pyConsole->execAndWait( command );
+  }
 #endif
 
   // remove temporary directory
@@ -2106,11 +2097,55 @@ bool SalomeApp_Application::checkExistingDoc()
 
 PyConsole_Interp* SalomeApp_Application::createPyInterp()
 {
-  return new SalomeApp_PyInterp;
+  return new SalomeApp_PyInterp( resourceMgr() );
 }
 
 #endif // DISABLE_PYCONSOLE
 
+/*
+  Opens a file dialog to choose a python script.
+*/
+QString SalomeApp_Application::getScriptFileName()
+{
+  QStringList filtersList;
+  filtersList.append(tr("PYTHON_FILES_FILTER"));
+  filtersList.append(tr("ALL_FILES_FILTER"));
+
+  const QString anInitialPath =
+    SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : "";
+    
+  return SUIT_FileDlg::getFileName(desktop(), anInitialPath, filtersList, tr("TOT_DESK_FILE_LOAD_SCRIPT"), true, true);
+}
+
+/*
+  Execute script in python console.
+*/
+void SalomeApp_Application::execScript(bool isNewDoc)
+{
+  const QString aFile = getScriptFileName();
+  if (aFile.isEmpty())
+  {
+    return;
+  }
+
+  if (isNewDoc)
+  {
+    onNewDoc();
+  }
+
+#ifndef DISABLE_PYCONSOLE
+  PyConsole_Console* pyConsole = pythonConsole();
+  PropertyMgr propm(this, "IsLoadedScript", true);
+  if (pyConsole)
+  {
+    QString command = QString("exec(compile(open('%1', 'rb').read(), '%1', 'exec'))").arg(aFile);
+    SUIT_Tools::addTraceToPythonCommand(aFile, command);
+
+    pyConsole->exec(command);
+  }
+#endif
+}
+
 void SalomeApp_Application::ensureShaperIsActivated()
 {
   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
@@ -2128,3 +2163,19 @@ void SalomeApp_Application::ensureShaperIsActivated()
       onDesktopMessage("register_module_in_study/Shaper");
   }
 }
+
+void SalomeApp_Application::addCatalogue( const QString& moduleName, const QString& catalogue )
+{
+  CORBA::Object_var obj = namingService()->Resolve( "/Kernel/ModulCatalog" );
+  SALOME_ModuleCatalog::ModuleCatalog_var moduleCatalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow( obj );
+  QFileInfo fi( catalogue );
+  if ( !CORBA::is_nil( moduleCatalogue ) && fi.isFile() )
+  {
+    SALOME_ModuleCatalog::ListOfComponents_var known = moduleCatalogue->GetComponentList();
+    bool loaded = false;
+    for ( int i = 0; i < (int)known->length() && !loaded; i++ )
+      loaded = QString( known[i].in() ) == moduleName;
+    if ( !loaded )
+      moduleCatalogue->ImportXmlCatalogFile( catalogue.toUtf8().constData() );
+  }
+}