Salome HOME
Fix of 0022530: [CEA 1108] The new macros are deleted at Paravis launch
[modules/paravis.git] / src / PVGUI / PVGUI_Module.cxx
index fa3cfb7ebd0884bcad05ac4bfe58f3d22551c033..dcf2a70253be0a4987ecafb4270656f0cfc47f6d 100644 (file)
@@ -1,11 +1,11 @@
 // PARAVIS : ParaView wrapper SALOME module
 //
-// Copyright (C) 2010-2012  CEA/DEN, EDF R&D
+// Copyright (C) 2010-2014  CEA/DEN, EDF R&D
 //
 // 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
@@ -66,6 +66,7 @@
 #include <SALOME_ListIO.hxx>
 #include <SALOMEDS_Tool.hxx>
 #include <PyInterp_Dispatcher.h>
+#include <PyConsole_Console.h>
 
 #include <QtxActionMenuMgr.h>
 #include <QtxActionToolMgr.h>
 #include <pqActiveObjects.h>
 #include <vtkProcessModule.h>
 #include <vtkSMSession.h>
+#include <vtkPVSession.h>
 #include <vtkPVProgressHandler.h>
 #include <pqParaViewBehaviors.h>
 #include <pqHelpReaction.h>
@@ -349,15 +351,18 @@ PVGUI_Module::PVGUI_Module()
 #endif
   ParavisModule = this;
 
-  // Clear old macros
+  // Clear old copies of embedded macros files
   QString aDestPath = QString( "%1/.config/%2/Macros" ).arg( QDir::homePath() ).arg( QApplication::applicationName() );
   QStringList aFilter;
   aFilter << "*.py";
 
   QDir aDestDir(aDestPath);
   QStringList aDestFiles = aDestDir.entryList(aFilter, QDir::Files);
-  foreach (QString aStr, aDestFiles) {
-    aDestDir.remove(aStr);
+  foreach (QString aMacrosPath, getEmbeddedMacrosList()) {
+    QString aMacrosName = QFileInfo(aMacrosPath).fileName();
+    if (aDestFiles.contains(aMacrosName)) {
+      aDestDir.remove(aMacrosName);
+    }
   }
 }
 
@@ -463,10 +468,6 @@ void PVGUI_Module::initialize( CAM_Application* app )
     QShortcut *ctrlSpace = new QShortcut(Qt::CTRL + Qt::Key_Space, aDesktop);
     QObject::connect(ctrlSpace, SIGNAL(activated()),
       pqApplicationCore::instance(), SLOT(quickLaunch()));
-    QShortcut *altSpace = new QShortcut(Qt::ALT + Qt::Key_Space, aDesktop);
-    QObject::connect(altSpace, SIGNAL(activated()),
-      pqApplicationCore::instance(), SLOT(quickLaunch()));
-    //  End pqParaViewBehaviors
 
     // Find Plugin Dock Widgets
     QList<QDockWidget*> currentDocks = aDesktop->findChildren<QDockWidget*>();
@@ -528,16 +529,24 @@ void PVGUI_Module::initialize( CAM_Application* app )
   bool isStop = aResourceMgr->booleanValue( "PARAVIS", "stop_trace", false );
   // start timer to activate trace in a proper moment
   if(!isStop) 
-    startTimer( 50 );
+    startTimer( 1000 );
 
   this->VTKConnect = vtkEventQtSlotConnect::New();
+  
   vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
-
-  this->VTKConnect->Connect(pm, vtkCommand::StartEvent,
-    this, SLOT(onStartProgress()));
-  this->VTKConnect->Connect(pm, vtkCommand::EndEvent,
-    this, SLOT(onEndProgress()));
-
+  if(pm) {
+    vtkPVSession* pvs = dynamic_cast<vtkPVSession*>(pm->GetSession());
+    if(pvs) {
+      vtkPVProgressHandler* ph = pvs->GetProgressHandler();
+      if(ph) {
+       this->VTKConnect->Connect(ph, vtkCommand::StartEvent,
+                                 this, SLOT(onStartProgress()));
+       this->VTKConnect->Connect(ph, vtkCommand::EndEvent,
+                                 this, SLOT(onEndProgress()));
+      }
+    }
+  }
+  
   connect(&pqActiveObjects::instance(),
          SIGNAL(representationChanged(pqRepresentation*)),
          this, SLOT(onRepresentationChanged(pqRepresentation*)));
@@ -558,7 +567,7 @@ void PVGUI_Module::onFinishedAddingServer(pqServer* /*server*/)
   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
   bool isStop = aResourceMgr->booleanValue( "PARAVIS", "stop_trace", false );
   if(!isStop) 
-    startTimer( 50 );
+    startTimer( 500 );
 }
 
 void PVGUI_Module::onDataRepresentationCreated(pqDataRepresentation* data) {
@@ -630,15 +639,17 @@ void PVGUI_Module::onVariableChanged(pqVariableType t, const QString) {
   }
 }
 
-
-/*!
-  \brief Launches a tracing of current server
-*/
-void PVGUI_Module::timerEvent(QTimerEvent* te )
+void PVGUI_Module::execPythonCommand(const QString& cmd, bool inSalomeConsole)
 {
-#ifndef WNT
-  PyInterp_Dispatcher* aDispatcher = PyInterp_Dispatcher::Get();
-  if ( !aDispatcher->IsBusy() ) {
+  if ( PyInterp_Dispatcher::Get()->IsBusy() ) return;
+  if ( inSalomeConsole ) {
+    SalomeApp_Application* app =
+      dynamic_cast< SalomeApp_Application* >(SUIT_Session::session()->activeApplication());
+    PyConsole_Console* pyConsole = app->pythonConsole();
+    if (pyConsole)
+      pyConsole->exec(cmd);
+  }
+  else {
     pqPythonManager* manager = qobject_cast<pqPythonManager*>
       ( pqApplicationCore::instance()->manager( "PYTHON_MANAGER" ) );
     if ( manager )  {
@@ -646,23 +657,29 @@ void PVGUI_Module::timerEvent(QTimerEvent* te )
       if ( pyDiag ) {
        pqPythonShell* shell = pyDiag->shell();
        if ( shell ) {
-         QString script = "from paraview import smtrace\nsmtrace.start_trace()\n";
-         shell->executeScript(script);
-         killTimer( te->timerId() );
+         shell->executeScript(cmd);
        }
       }
     }
   }
+}
+
+/*!
+  \brief Launches a tracing of current server
+*/
+void PVGUI_Module::timerEvent(QTimerEvent* te )
+{
+#ifndef WNT
+  execPythonCommand("from paraview import smtrace\nsmtrace.start_trace()\n", false);
+  killTimer( te->timerId() );
 #endif
 }
   
-void PVGUI_Module::updateMacros()
+/*!
+  \brief Get list of embedded macros files
+*/
+QStringList PVGUI_Module::getEmbeddedMacrosList()
 {
-  pqPythonManager* aPythonManager = pqPVApplicationCore::instance()->pythonManager();
-  if(!aPythonManager)  {
-    return;
-  }
-  
   QString aRootDir = getenv("PARAVIS_ROOT_DIR");
 
   QString aSourcePath = aRootDir + "/bin/salome/Macro";
@@ -672,8 +689,22 @@ void PVGUI_Module::updateMacros()
 
   QDir aSourceDir(aSourcePath);
   QStringList aSourceFiles = aSourceDir.entryList(aFilter, QDir::Files);
-  foreach (QString aStr, aSourceFiles) {
-    aPythonManager->addMacro(aSourcePath + "/" + aStr);
+  QStringList aFullPathSourceFiles;
+  foreach (QString aMacrosName, aSourceFiles) {
+    aFullPathSourceFiles << aSourceDir.absoluteFilePath(aMacrosName);
+  }
+  return aFullPathSourceFiles;
+}
+
+void PVGUI_Module::updateMacros()
+{
+  pqPythonManager* aPythonManager = pqPVApplicationCore::instance()->pythonManager();
+  if(!aPythonManager)  {
+    return;
+  }
+  
+  foreach (QString aStr, getEmbeddedMacrosList()) {
+    aPythonManager->addMacro(aStr);
   }
 }
 
@@ -856,6 +887,8 @@ bool PVGUI_Module::activateModule( SUIT_Study* study )
   
   SUIT_ExceptionHandler::addCleanUpRoutine( paravisCleanUp );
 
+  storeCommonWindowsState();
+
   bool isDone = SalomeApp_Module::activateModule( study );
   if ( !isDone ) return false;
 
@@ -946,6 +979,8 @@ bool PVGUI_Module::deactivateModule( SUIT_Study* study )
   if (myOldMsgHandler)
     qInstallMsgHandler(myOldMsgHandler);
 
+  restoreCommonWindowsState();
+  
   return SalomeApp_Module::deactivateModule( study );
 }
 
@@ -1317,6 +1352,9 @@ void PVGUI_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QS
   }
 }
 
+/*!
+  \brief. Show ParaView python trace.
+*/
 void PVGUI_Module::onShowTrace()
 {
   if (!myTraceWindow) {
@@ -1328,6 +1366,18 @@ void PVGUI_Module::onShowTrace()
   myTraceWindow->activateWindow();
 }
 
+
+/*!
+  \brief. Re-initialize ParaView python trace.
+*/
+void PVGUI_Module::onRestartTrace()
+{
+  QString script = "from paraview import smtrace\n";
+  script += "smtrace.stop_trace()\n";
+  script += "smtrace.start_trace()\n";
+  execPythonCommand(script, false);
+}
+
 /*!
   \brief Show ParaView view.
 */