Salome HOME
PV version is now bfaf7b82ed22ee (master of 17th of january 2019)
[modules/paravis.git] / src / PVGUI / PVGUI_Module.cxx
index 8e3d3f638de701c2ea399377f6d7e7f33d194504..02cc591668d560bb81929f488c044615f8ac326e 100644 (file)
@@ -1,6 +1,6 @@
 // PARAVIS : ParaView wrapper SALOME module
 //
-// Copyright (C) 2010-2016  CEA/DEN, EDF R&D
+// Copyright (C) 2010-2019  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
@@ -87,7 +87,6 @@
 #include <vtkProcessModule.h>
 #include <vtkPVSession.h>
 #include <vtkPVProgressHandler.h>
-#include <vtkOutputWindow.h>
 #include <vtkEventQtSlotConnect.h>
 #include <vtkNew.h>
 #include <vtkSMProxy.h>
 #include <pqActiveObjects.h>
 #include <pqHelpReaction.h>
 #include <pqPluginManager.h>
-#include <pqPythonDialog.h>
+//#include <pqPythonDialog.h>
 #include <pqPythonManager.h>
 #include <pqLoadDataReaction.h>
 #include <pqPythonScriptEditor.h>
 #include <pqAnimationScene.h>
 #include <pqServerManagerModel.h>
 #include <pqAnimationTimeToolbar.h>
+#include <pqPipelineBrowserWidget.h>
+#include <pqCoreUtilities.h>
+
+#if PY_VERSION_HEX < 0x03050000
+static char*
+Py_EncodeLocale(const wchar_t *arg, size_t *size)
+{
+       return _Py_wchar2char(arg, size);
+}
+static wchar_t*
+Py_DecodeLocale(const char *arg, size_t *size)
+{
+       return _Py_char2wchar(arg, size);
+}
+#endif
 
 //----------------------------------------------------------------------------
 PVGUI_Module* ParavisModule = 0;
@@ -141,7 +155,7 @@ PVGUI_Module* ParavisModule = 0;
 
 /*!
   \class PVGUI_Module
-  \brief Implementation
+  \brief Implementation 
          SALOME module wrapping ParaView GUI.
 */
 
@@ -166,11 +180,9 @@ PVGUI_Module::PVGUI_Module()
   : LightApp_Module( PARAVIS_MODULE_NAME ),
     mySourcesMenuId( -1 ),
     myFiltersMenuId( -1 ),
-#ifdef PVCATALYST_ENABLED
-    myCatalystMenuId(-1),
-#endif
     myMacrosMenuId(-1),
     myRecentMenuId(-1),
+    myCatalystMenuId(-1),
     myOldMsgHandler(0),
     myTraceWindow(0),
     myInitTimer(0),
@@ -180,20 +192,6 @@ PVGUI_Module::PVGUI_Module()
   Q_INIT_RESOURCE( PVGUI );
 #endif
   ParavisModule = this;
-
-  // 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 aMacrosPath, getEmbeddedMacrosList()) {
-    QString aMacrosName = QFileInfo(aMacrosPath).fileName();
-    if (aDestFiles.contains(aMacrosName)) {
-      aDestDir.remove(aMacrosName);
-    }
-  }
 }
 
 /*!
@@ -258,9 +256,26 @@ void PVGUI_Module::initialize( CAM_Application* app )
 
   // Initialize ParaView client and associated behaviors
   // and connect to externally launched pvserver
-  PVViewer_Core::ParaviewInitApp(aDesktop, anApp->logWindow());
+  PVViewer_Core::ParaviewInitApp(aDesktop);
+
+  // Clear old copies of embedded macros files
+  //QString aDestPath = QString( "%1/.config/%2/Macros" ).arg( QDir::homePath() ).arg( QApplication::applicationName() );
+  QString aDestPath = pqCoreUtilities::getParaViewUserDirectory() + "/Macros";
+  QStringList aFilter;
+  aFilter << "*.py";
+
+  QDir aDestDir(aDestPath);
+  QStringList aDestFiles = aDestDir.entryList(aFilter, QDir::Files);
+  foreach(QString aMacrosPath, getEmbeddedMacrosList()) {
+         QString aMacrosName = QFileInfo(aMacrosPath).fileName();
+         if (aDestFiles.contains(aMacrosName)) {
+                 aDestDir.remove(aMacrosName);
+         }
+  }
+
   myGuiElements = PVViewer_GUIElements::GetInstance(aDesktop);
 
+
   // [ABN]: careful with the order of the GUI element creation, the loading of the configuration
   // and the connection to the server. This order is very sensitive if one wants to make
   // sure all menus, etc ... are correctly populated.
@@ -336,7 +351,7 @@ void PVGUI_Module::initialize( CAM_Application* app )
   }
 
   updateMacros();
-
   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
   bool isStop = aResourceMgr->booleanValue( PARAVIS_MODULE_NAME, "stop_trace", false );
   if(!isStop)
@@ -350,7 +365,7 @@ void PVGUI_Module::initialize( CAM_Application* app )
     }
 
   this->VTKConnect = vtkEventQtSlotConnect::New();
-
+  
   vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
   if(pm) {
     vtkPVSession* pvs = dynamic_cast<vtkPVSession*>(pm->GetSession());
@@ -392,7 +407,7 @@ void PVGUI_Module::onEndProgress()
 void PVGUI_Module::onDataRepresentationUpdated() {
   LightApp_Study* activeStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy());
   if(!activeStudy) return;
-
+  
   activeStudy->Modified();
 }
 
@@ -403,7 +418,7 @@ void PVGUI_Module::onInitTimer()
 {
   startTrace();
 }
-
+  
 /*!
   \brief Get list of embedded macros files
 */
@@ -434,7 +449,7 @@ void PVGUI_Module::updateMacros()
   if(!aPythonManager)  {
     return;
   }
-
+  
   foreach (QString aStr, getEmbeddedMacrosList()) {
     aPythonManager->addMacro(aStr);
   }
@@ -467,7 +482,7 @@ void PVGUI_Module::showView( bool toShow )
   PVViewer_ViewManager* viewMgr =
     dynamic_cast<PVViewer_ViewManager*>( anApp->getViewManager( PVViewer_Viewer::Type(), false ) );
   if ( !viewMgr ) {
-    viewMgr = new PVViewer_ViewManager( anApp->activeStudy(), anApp->desktop(), anApp->logWindow() );
+    viewMgr = new PVViewer_ViewManager( anApp->activeStudy(), anApp->desktop() );
     anApp->addViewManager( viewMgr );
     connect( viewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
              anApp, SLOT( onCloseView( SUIT_ViewManager* ) ) );
@@ -516,51 +531,7 @@ void PVGUI_Module::endWaitCursor()
 {
   QApplication::restoreOverrideCursor();
 }
-#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
-/*!
-  \brief Handler method for the output of messages.
-*/
-static void ParavisMessageOutput(QtMsgType type, const char *msg)
-{
-  switch(type)
-    {
-  case QtDebugMsg:
-    vtkOutputWindow::GetInstance()->DisplayText(msg);
-    break;
-  case QtWarningMsg:
-    vtkOutputWindow::GetInstance()->DisplayErrorText(msg);
-    break;
-  case QtCriticalMsg:
-    vtkOutputWindow::GetInstance()->DisplayErrorText(msg);
-    break;
-  case QtFatalMsg:
-    vtkOutputWindow::GetInstance()->DisplayErrorText(msg);
-    break;
-    }
-}
-#else
-/*!
-  \brief Handler method for the output of messages.
-*/
-static void ParavisMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
-{
-  switch(type)
-    {
-  case QtDebugMsg:
-    vtkOutputWindow::GetInstance()->DisplayText(msg.toLatin1().constData());
-    break;
-  case QtWarningMsg:
-    vtkOutputWindow::GetInstance()->DisplayErrorText(msg.toLatin1().constData());
-    break;
-  case QtCriticalMsg:
-    vtkOutputWindow::GetInstance()->DisplayErrorText(msg.toLatin1().constData());
-    break;
-  case QtFatalMsg:
-    vtkOutputWindow::GetInstance()->DisplayErrorText(msg.toLatin1().constData());
-    break;
-    }
-}
-#endif
+
 /*!
   \brief Activate module.
   \param study current study
@@ -569,11 +540,6 @@ static void ParavisMessageOutput(QtMsgType type, const QMessageLogContext &conte
 */
 bool PVGUI_Module::activateModule( SUIT_Study* study )
 {
-#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
-  myOldMsgHandler = qInstallMsgHandler(ParavisMessageOutput);
-#else
-  myOldMsgHandler = qInstallMessageHandler(ParavisMessageOutput);
-#endif
   SUIT_ExceptionHandler::addCleanUpRoutine( paravisCleanUp );
 
   storeCommonWindowsState();
@@ -584,20 +550,14 @@ bool PVGUI_Module::activateModule( SUIT_Study* study )
   showView( true );
   if ( mySourcesMenuId != -1 ) menuMgr()->show(mySourcesMenuId);
   if ( myFiltersMenuId != -1 ) menuMgr()->show(myFiltersMenuId);
-#ifdef PVCATALYST_ENABLED
-  if ( myCatalystMenuId != -1 ) menuMgr()->show(myCatalystMenuId);
-#endif
   if ( myMacrosMenuId != -1 ) menuMgr()->show(myMacrosMenuId);
+  if ( myCatalystMenuId != -1 ) menuMgr()->show(myCatalystMenuId);
 
   // Update the various menus with the content pre-loaded in myGuiElements
 //  QMenu* srcMenu = menuMgr()->findMenu( mySourcesMenuId );
 //  myGuiElements->updateSourcesMenu(srcMenu);
 //  QMenu* filtMenu = menuMgr()->findMenu( myFiltersMenuId );
 //  myGuiElements->updateFiltersMenu(filtMenu);
-//#ifdef PVCATALYST_ENABLED
-//  QMenu* catalystMenu = menuMgr()->findMenu( myCatalystMenuId );
-//  myGuiElements->updateCatalystMenu(catalystMenu);
-//#endif
 //  QMenu* macMenu = menuMgr()->findMenu( myMacrosMenuId );
 //  myGuiElements->updateMacrosMenu(macMenu);
 
@@ -625,6 +585,12 @@ bool PVGUI_Module::activateModule( SUIT_Study* study )
 
   if ( myRecentMenuId != -1 ) menuMgr()->show(myRecentMenuId);
 
+  // VSR 18/10/2018 - 0023170: Workaround to re-select current index after module activation
+  QItemSelectionModel* selection_model = myGuiElements->getPipelineBrowserWidget()->getSelectionModel();
+  QModelIndex idx = selection_model->currentIndex();
+  selection_model->clearCurrentIndex();
+  selection_model->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect);
+
   return isDone;
 }
 
@@ -669,10 +635,8 @@ bool PVGUI_Module::deactivateModule( SUIT_Study* study )
   menuMgr()->hide(myRecentMenuId);
   menuMgr()->hide(mySourcesMenuId);
   menuMgr()->hide(myFiltersMenuId);
-#ifdef PVCATALYST_ENABLED
-  menuMgr()->hide(myCatalystMenuId);
-#endif
   menuMgr()->hide(myMacrosMenuId);
+  menuMgr()->hide(myCatalystMenuId);
   setMenuShown( false );
   setToolShown( false );
 
@@ -681,13 +645,10 @@ bool PVGUI_Module::deactivateModule( SUIT_Study* study )
   SUIT_ExceptionHandler::removeCleanUpRoutine( paravisCleanUp );
 
   if (myOldMsgHandler)
-#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
-    qInstallMsgHandler(myOldMsgHandler);
-#else
     qInstallMessageHandler(myOldMsgHandler);
-#endif
-  restoreCommonWindowsState();
 
+  restoreCommonWindowsState();
+  
   return LightApp_Module::deactivateModule( study );
 }
 
@@ -717,7 +678,7 @@ void PVGUI_Module::studyClosed(SUIT_Study* study)
 {
   showView(false); // VSR: this seems to be not needed (all views are automatically closed)
   clearParaviewState();
-  //Re-start trace
+  //Re-start trace 
   onRestartTrace();
 
   LightApp_Module::studyClosed(study);
@@ -735,7 +696,7 @@ void PVGUI_Module::openFile( const char* theName )
 
 /*!
   \brief Starts Python trace.
-
   Start trace invoking the newly introduced C++ API (PV 4.2)
   (inspired from pqTraceReaction::start())
 */
@@ -773,16 +734,30 @@ void PVGUI_Module::stopTrace()
 */
 void PVGUI_Module::executeScript( const char* script )
 {
+  // ???
+  // Not sure this is the right fix, but the PYTHON_MANAGER has a function named
+  // executeScript() which seems to do what the runScript on pyShellDialog() class
+  // was doing.
+#ifndef WNT
+  pqPythonManager* manager =
+    qobject_cast<pqPythonManager*>(pqApplicationCore::instance()->manager("PYTHON_MANAGER"));
+
+  if ( manager )  {
+    manager->executeScript(script);
+  }
+#endif
+  /*
 #ifndef WNT
   pqPythonManager* manager = qobject_cast<pqPythonManager*>(
                              pqApplicationCore::instance()->manager( "PYTHON_MANAGER" ) );
   if ( manager )  {
     pqPythonDialog* pyDiag = manager->pythonShellDialog();
     if ( pyDiag ) {
-      pyDiag->runString(script);
+      pyDiag->runString(script);  
     }
   }
 #endif
+  */
 }
 
 ///**
@@ -800,7 +775,7 @@ void PVGUI_Module::executeScript( const char* script )
 //      PyObject * elem = PyList_GetItem(lst, i);
 //      if (PyString_Check(elem))
 //        {
-//          std::cout << "At pos:" << i << ", " << PyString_AsString(elem) << std::endl;
+//          std::cout << "At pos:" << i << ", " << Py_EncodeLocale(PyUnicode_AS_UNICODE(elem), NULL) << std::endl;
 //        }
 //      else
 //        std::cout << "At pos:" << i << ", not a string!" << std::endl;
@@ -843,21 +818,21 @@ QString PVGUI_Module::getTraceString()
     vtkPythonScopeGilEnsurer psge;
     PyObject * mods(PySys_GetObject(const_cast<char*>("modules")));
     PyObject * trace_mod(PyDict_GetItemString(mods, "paraview.smtrace"));  // module was already (really) imported by vtkSMTrace
-    if (PyModule_Check(trace_mod)) {
+    if (trace_mod && trace_mod != Py_None && PyModule_Check(trace_mod)) {
         vtkSmartPyObject save_cam(PyObject_GetAttrString(trace_mod, const_cast<char*>("SaveCameras")));
         vtkSmartPyObject camera_trace(PyObject_CallMethod(save_cam, const_cast<char*>("get_trace"), NULL));
         // Convert to a single string
-        vtkSmartPyObject ret(PyString_FromString(end_line.toStdString().c_str()));
+        vtkSmartPyObject ret(PyUnicode_FromUnicode(Py_DecodeLocale(end_line.toStdString().c_str(), NULL), end_line.size()));
         vtkSmartPyObject final_string(PyObject_CallMethod(ret, const_cast<char*>("join"),
             const_cast<char*>("O"), (PyObject*)camera_trace));
-        if (PyString_CheckExact(final_string))
+        if (PyUnicode_CheckExact(final_string))
           {
-            QString camera_qs(PyString_AsString(final_string));  // deep copy
+            QString camera_qs(Py_EncodeLocale(PyUnicode_AS_UNICODE(final_string.GetPointer()), NULL));  // deep copy
             traceString = traceString + end_line  + end_line + QString("#### saving camera placements for all active views")
                 + end_line + end_line + camera_qs + end_line;
           }
       }
-  }
+  } 
 
   return traceString;
 }
@@ -933,7 +908,7 @@ void PVGUI_Module::createPreferences()
   // Paravis settings tab
   int aParaVisSettingsTab = addPreference( tr( "TIT_PVISSETTINGS" ) );
 
-  addPreference( tr( "PREF_NO_EXT_PVSERVER" ), aParaVisSettingsTab,
+  addPreference( tr( "PREF_NO_EXT_PVSERVER" ), aParaVisSettingsTab, 
                  LightApp_Preferences::Bool, PARAVIS_MODULE_NAME, "no_ext_pv_server" );
 
   int aSaveType = addPreference( tr( "PREF_SAVE_TYPE_LBL" ), aParaVisSettingsTab,
@@ -950,7 +925,7 @@ void PVGUI_Module::createPreferences()
   // ... "Language" group <<start>>
   int traceGroup = addPreference( tr( "PREF_GROUP_TRACE" ), aParaVisSettingsTab );
 
-  int stopTrace = addPreference( tr( "PREF_STOP_TRACE" ), traceGroup,
+  int stopTrace = addPreference( tr( "PREF_STOP_TRACE" ), traceGroup, 
                                  LightApp_Preferences::Bool, PARAVIS_MODULE_NAME, "stop_trace" );
   setPreferenceProperty( stopTrace, "restart",  true );
 
@@ -1003,7 +978,7 @@ void PVGUI_Module::onStopTrace()
 void PVGUI_Module::onViewManagerAdded( SUIT_ViewManager* vm )
 {
   if ( PVViewer_ViewManager* pvvm = dynamic_cast<PVViewer_ViewManager*>( vm ) ) {
-    connect( pvvm, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
+    connect( pvvm, SIGNAL( viewCreated( SUIT_ViewWindow* ) ), 
              this, SLOT( onPVViewCreated( SUIT_ViewWindow* ) ) );
     connect( pvvm, SIGNAL( deleteView( SUIT_ViewWindow* ) ),
              this,  SLOT( onPVViewDelete( SUIT_ViewWindow* ) ) );
@@ -1056,7 +1031,7 @@ extern "C" {
   PVGUI_EXPORT CAM_Module* createModule() {
     return new PVGUI_Module();
   }
-
+  
   PVGUI_EXPORT char* getModuleVersion() {
     return (char*)PARAVIS_VERSION_STR;
   }