Salome HOME
Copyright update 2021
[modules/gui.git] / src / SalomeApp / SalomeApp_Application.cxx
index cf148a6571259e6a146446d0d4918565cd0e6c8f..19d0973798de5032c548c046710abc6d8b5294d8 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2019  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
 
 #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
@@ -173,18 +175,13 @@ namespace
   };
 }
 
-/*!Create new instance of SalomeApp_Application.*/
-extern "C" SALOMEAPP_EXPORT SUIT_Application* createApplication()
-{
-  return new SalomeApp_Application();
-}
-
 /*!Constructor.*/
-SalomeApp_Application::SalomeApp_Application()
-  : LightApp_Application(),
-    myIsCloseFromExit( false ),
-    myToIgnoreMessages( 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.
@@ -226,9 +223,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();
@@ -237,7 +235,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) {
@@ -812,6 +810,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();
 }
@@ -965,6 +965,7 @@ void SalomeApp_Application::onDumpStudy( )
       bool res;
       {
         SUIT_OverrideCursor wc;
+        ensureShaperIsActivated();
         res = appStudy->dump( aFileName, toPublish, isMultiFile, toSaveGUI );
       }
       if ( !res )
@@ -998,7 +999,7 @@ void SalomeApp_Application::onLoadScript( )
   if ( !aFile.isEmpty() )
   {
 
-    QString command = QString("exec(open(\"%1\", \"rb\").read())").arg(aFile);
+    QString command = QString("exec(compile(open('%1', 'rb').read(), '%1', 'exec'))").arg(aFile);
 
 #ifndef DISABLE_PYCONSOLE
     PyConsole_Console* pyConsole = pythonConsole();
@@ -1188,7 +1189,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;
@@ -1231,6 +1232,7 @@ bool SalomeApp_Application::closeAction( const int choice, bool& closePermanentl
       onSaveDoc();
     else if ( !onSaveAsDoc() )
       res = false;
+    // fall through!
   case CloseDisconnect:
     closeActiveDoc( false );
     closePermanently = false;
@@ -1279,9 +1281,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;
@@ -1366,10 +1367,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.*/
@@ -1486,9 +1486,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() ) );
+      }
     }
   }
 
@@ -1896,7 +1901,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() );
 
@@ -2087,6 +2092,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.
 */
@@ -2104,3 +2117,21 @@ PyConsole_Interp* SalomeApp_Application::createPyInterp()
 }
 
 #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");
+  }
+}