Salome HOME
Further clean-up and minor fix on wrongly connected Qt signals.
[modules/paravis.git] / src / PVGUI / PVGUI_Module.cxx
index 8233ef9b61d7b524b61071de715bd96e59d7b130..7ff12a68a8311e789869b864545f866292f72bad 100644 (file)
@@ -19,7 +19,6 @@
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 // File   : PVGUI_Module.cxx
-// Author : Julia DOROVSKIKH
 
 #include <Standard_math.hxx>  // E.A. must be included before Python.h to fix compilation on windows
 #ifdef HAVE_FINITE
 #include <vtkPython.h> // Python first
 #include "PVGUI_Module.h"
 
-#include "SALOMEconfig.h"
-#ifdef WITH_VISU
-#include CORBA_CLIENT_HEADER(VISU_Gen)
+#ifdef PARAVIS_WITH_FULL_CORBA
+# include "PARAVIS_Gen_i.hh"
 #endif
-#include CORBA_SERVER_HEADER(SALOMEDS)
-
 
-#include "PARAVIS_Gen_i.hh"
-
-#include "PV_Tools.h"
+#include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
+#include CORBA_SERVER_HEADER(SALOMEDS)
 
 #include "PVGUI_ViewModel.h"
 #include "PVGUI_ViewManager.h"
@@ -45,7 +40,9 @@
 #include "PVGUI_Tools.h"
 #include "PVGUI_ParaViewSettingsPane.h"
 #include "PVGUI_OutputWindowAdapter.h"
+#include "PVGUI_Behaviors.h"
 
+// SALOME Includes
 #include <SUIT_DataBrowser.h>
 #include <SUIT_Desktop.h>
 #include <SUIT_MessageBox.h>
 #include <SUIT_OverrideCursor.h>
 #include <SUIT_ExceptionHandler.h>
 
-// SALOME Includes
-#include "SALOME_LifeCycleCORBA.hxx"
-#include "SALOMEDS_SObject.hxx"
-
-#include "LightApp_SelectionMgr.h"
-#include "LightApp_NameDlg.h"
+#include <SALOME_LifeCycleCORBA.hxx>
+#include <SALOMEDS_SObject.hxx>
 
+#include <LightApp_SelectionMgr.h>
+#include <LightApp_NameDlg.h>
 #include <SalomeApp_Application.h>
 #include <SalomeApp_Study.h>
 #include <SALOME_ListIO.hxx>
 #include <SALOMEDS_Tool.hxx>
-#include <PyInterp_Interp.h>
-#include <PyInterp_Dispatcher.h>
-#include <PyConsole_Console.h>
+#include <Utils_ORB_INIT.hxx>
+#include <Utils_SINGLETON.hxx>
 
 #include <QtxActionMenuMgr.h>
 #include <QtxActionToolMgr.h>
 
+#include <PARAVIS_version.h>
+
+// External includes
+#include <sstream>
+
 #include <QAction>
 #include <QApplication>
 #include <QCursor>
 #include <QDockWidget>
 #include <QHelpEngine>
 
+// Paraview includes
+#include <vtkPVConfig.h>  // for symbol PARAVIEW_VERSION
+#include <vtkProcessModule.h>
+#include <vtkPVSession.h>
+#include <vtkPVProgressHandler.h>
+#include <vtkOutputWindow.h>
+#include <vtkEventQtSlotConnect.h>
+#include <vtkNew.h>
+#include <vtkSMProxy.h>
+#include <vtkSmartPointer.h>
+#include <vtkSMSession.h>
+#include <vtkSMTrace.h>
+#include <vtkSMSessionProxyManager.h>
+#include <vtkSMParaViewPipelineController.h>
+
 #include <pqApplicationCore.h>
 #include <pqPVApplicationCore.h>
 #include <pqActiveView.h>
 #include <pqObjectBuilder.h>
 #include <pqOptions.h>
-#include <pqRenderView.h>
+#include <pqSettings.h>
 #include <pqServer.h>
 #include <pqUndoStack.h>
-#include <pqVCRController.h>
 #include <pqTabbedMultiViewWidget.h>
-#include <pqPipelineSource.h>
 #include <pqActiveObjects.h>
-#include <vtkProcessModule.h>
-#include <vtkSMSession.h>
-#include <vtkPVSession.h>
-#include <vtkPVProgressHandler.h>
-#include <pqParaViewBehaviors.h>
 #include <pqHelpReaction.h>
-#include <vtkOutputWindow.h>
 #include <pqPluginManager.h>
-#include "pqInterfaceTracker.h"
-#include <pqSettings.h>
 #include <pqPythonDialog.h>
 #include <pqPythonManager.h>
-#include <pqPythonShell.h>
 #include <pqLoadDataReaction.h>
-#include <vtkEventQtSlotConnect.h>
 #include <pqPythonScriptEditor.h>
-#include <pqCollaborationBehavior.h>
 #include <pqDataRepresentation.h>
-#include <pqPipelineRepresentation.h>
-//#include <pqLookupTableManager.h>
 #include <pqDisplayColorWidget.h>
 #include <pqColorToolbar.h>
 #include <pqScalarBarVisibilityReaction.h>
-#include <pqStandardPropertyWidgetInterface.h>
-#include <pqStandardViewFrameActionsImplementation.h>
-#include <pqViewStreamingBehavior.h>
-
-#include <PARAVIS_version.h>
-
-#include <vtkPVConfig.h>
-
-
-#include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
-
-#include <pqAlwaysConnectedBehavior.h>
-#include <pqApplicationCore.h>
-#include <pqAutoLoadPluginXMLBehavior.h>
-#include <pqCommandLineOptionsBehavior.h>
-#include <pqCrashRecoveryBehavior.h>
-#include <pqDataTimeStepBehavior.h>
-#include <pqDefaultViewBehavior.h>
-#include <pqObjectPickingBehavior.h>
-#include <pqPersistentMainWindowStateBehavior.h>
-#include <pqPipelineContextMenuBehavior.h>
-#include <pqPluginActionGroupBehavior.h>
-#include <pqPluginDockWidgetsBehavior.h>
-#include <pqPluginManager.h>
-#include <pqSpreadSheetVisibilityBehavior.h>
-#include <pqUndoRedoBehavior.h>
-#include <pqServerManagerObserver.h>
-#include <pqVerifyRequiredPluginBehavior.h>
-#include <pqFixPathsInStateFilesBehavior.h>
-#include <pqPluginSettingsBehavior.h>
-#include <pqPropertiesPanel.h>
-
-#include <pqApplyBehavior.h>
-
-#include <vtkClientServerInterpreterInitializer.h>
-
-// Trace related
-#include <vtkNew.h>
-#include <vtkSMTrace.h>
-#include <vtkSMSessionProxyManager.h>
-#include <vtkSMParaViewPipelineController.h>
+#include <pqServerResource.h>
+#include <pqServerConnectReaction.h>
+#include <pqServerDisconnectReaction.h>
 
 //----------------------------------------------------------------------------
 pqPVApplicationCore* PVGUI_Module::MyCoreApp = 0;
-//PVGUI_OutputWindowAdapter* PVGUI_Module::pqImplementation::OutputWindowAdapter = 0;
-//QPointer<pqHelpWindow> PVGUI_Module::pqImplementation::helpWindow = 0;
 
 PVGUI_Module* ParavisModule = 0;
 
+PARAVIS_ORB::PARAVIS_Gen_var PVGUI_Module::myEngine;
+
 /*!
   \mainpage
 
@@ -242,66 +203,6 @@ PVGUI_Module* ParavisModule = 0;
          SALOME module wrapping ParaView GUI.
 */
 
-
-/*
-  Fix for the issue 21730: [CEA 596] Slice of polyhedron in PARAVIS returns no cell.
-  Wrap vtkEDFCutter filter.
-*/
-
-extern "C" void vtkEDFCutterCS_Initialize(vtkClientServerInterpreter*);
-static void vtkEDFHelperInit();
-
-void vtkEDFHelperInit(vtkClientServerInterpreter* interp){
-    vtkEDFCutterCS_Initialize(interp);
-}
-
-void vtkEDFHelperInit() {
-    vtkClientServerInterpreterInitializer::GetInitializer()->
-        RegisterCallback(&vtkEDFHelperInit);
-}
-
-
-  _PTR(SComponent)
-  ClientFindOrCreateParavisComponent(_PTR(Study) theStudyDocument)
-  {
-    _PTR(SComponent) aSComponent = theStudyDocument->FindComponent("PARAVIS");
-    if (!aSComponent) {
-      _PTR(StudyBuilder) aStudyBuilder = theStudyDocument->NewBuilder();
-      aStudyBuilder->NewCommand();
-      int aLocked = theStudyDocument->GetProperties()->IsLocked();
-      if (aLocked) theStudyDocument->GetProperties()->SetLocked(false);
-      aSComponent = aStudyBuilder->NewComponent("PARAVIS");
-      _PTR(GenericAttribute) anAttr =
-        aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeName");
-      _PTR(AttributeName) aName (anAttr);
-
-      CORBA::ORB_var anORB = PARAVIS::PARAVIS_Gen_i::GetORB();
-      SALOME_NamingService *NamingService = new SALOME_NamingService( anORB );
-      CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
-      SALOME_ModuleCatalog::ModuleCatalog_var Catalogue =
-        SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
-      SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "PARAVIS" );
-      if (!Comp->_is_nil()) {
-        aName->SetValue(Comp->componentusername());
-      }
-
-      anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributePixMap");
-      _PTR(AttributePixMap) aPixmap (anAttr);
-      aPixmap->SetPixMap( "pqAppIcon16.png" );
-
-      // Create Attribute parameters for future using
-      anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeParameter");
-
-
-      PARAVIS::PARAVIS_Gen_var aPARAVIS = PARAVIS::PARAVIS_Gen_i::GetParavisGenImpl()->_this();
-
-      aStudyBuilder->DefineComponentInstance(aSComponent, aPARAVIS->GetIOR());
-      if (aLocked) theStudyDocument->GetProperties()->SetLocked(true);
-      aStudyBuilder->CommitCommand();
-    }
-    return aSComponent;
-  }
-
 /*!
   Clean up function; used to stop ParaView progress events when
   exception is caught by global exception handler.
@@ -319,7 +220,6 @@ void paravisCleanUp()
 */
 PVGUI_Module::PVGUI_Module()
   : SalomeApp_Module( "PARAVIS" ),
-    //    Implementation( 0 ),
     mySelectionControlsTb( -1 ),
     mySourcesMenuId( -1 ),
     myFiltersMenuId( -1 ),
@@ -328,7 +228,9 @@ PVGUI_Module::PVGUI_Module()
     myRecentMenuId(-1),
     myOldMsgHandler(0),
     myTraceWindow(0),
-    myStateCounter(0)
+    myStateCounter(0),
+    myInitTimer(0),
+    myPushTraceTimer(0)
 {
 #ifdef HAS_PV_DOC
   Q_INIT_RESOURCE( PVGUI );
@@ -355,6 +257,28 @@ PVGUI_Module::PVGUI_Module()
 */
 PVGUI_Module::~PVGUI_Module()
 {
+  if (myPushTraceTimer)
+    delete myPushTraceTimer;
+  if (myInitTimer)
+    delete myInitTimer;
+  // Disconnect from server
+  pqServer* server = pqActiveObjects::instance().activeServer();
+  if (server && server->isRemote())
+    {
+      MESSAGE("~PVGUI_Module(): Disconnecting from remote server ...");
+      pqServerDisconnectReaction::disconnectFromServer();
+    }
+}
+
+PARAVIS_ORB::PARAVIS_Gen_var PVGUI_Module::GetEngine()
+{
+  // initialize PARAVIS module engine (load, if necessary)
+  if ( CORBA::is_nil( myEngine ) ) {
+    Engines::EngineComponent_var comp =
+        SalomeApp_Application::lcc()->FindOrLoad_Component( "FactoryServer", "PARAVIS" );
+    myEngine = PARAVIS_ORB::PARAVIS_Gen::_narrow( comp );
+  }
+  return myEngine;
 }
 
 /*!
@@ -363,9 +287,6 @@ PVGUI_Module::~PVGUI_Module()
 */
 void PVGUI_Module::initialize( CAM_Application* app )
 {
-  // [ABN]: patched in ParaView's sources.
-  // PVGUI_MatplotlibMathTextUtilities::Disable();
-
   SalomeApp_Module::initialize( app );
 
   // Create ParaViS actions
@@ -386,84 +307,38 @@ void PVGUI_Module::initialize( CAM_Application* app )
   pvInit();
 
   // Create GUI elements (menus, toolbars, dock widgets)
-  //if ( !Implementation ){
-    SalomeApp_Application* anApp = getApp();
-    SUIT_Desktop* aDesktop = anApp->desktop();
+  SalomeApp_Application* anApp = getApp();
+  SUIT_Desktop* aDesktop = anApp->desktop();
 
-    // connect(aDesktop, SIGNAL()
+  // Remember current state of desktop toolbars
+  QList<QToolBar*> foreignToolbars = aDesktop->findChildren<QToolBar*>();
 
-    // Remember current state of desktop toolbars
-    QList<QToolBar*> foreignToolbars = aDesktop->findChildren<QToolBar*>();
+  setupDockWidgets();
 
-    // Simulate ParaView client main window
-    //Implementation = new pqImplementation( aDesktop );
+  pvCreateActions();
+  pvCreateToolBars();
+  pvCreateMenus();
 
-    setupDockWidgets();
-    
-    pvCreateActions();
-    pvCreateToolBars();
-    pvCreateMenus();
-
-    QList<QDockWidget*> activeDocks = aDesktop->findChildren<QDockWidget*>();
-    QList<QMenu*> activeMenus = aDesktop->findChildren<QMenu*>();
-
-    // new pqParaViewBehaviors(anApp->desktop(), this);
-    // Has to be replaced in order to exclude using of pqQtMessageHandlerBehaviour
-    //  Start pqParaViewBehaviors
-    // Register ParaView interfaces.
-    //pqPluginManager* pgm = pqApplicationCore::instance()->getPluginManager();
-    pqInterfaceTracker* pgm = pqApplicationCore::instance()->interfaceTracker();
-
-    // Register standard types of property widgets.
-    pgm->addInterface(new pqStandardPropertyWidgetInterface(pgm));
-    // Register standard types of view-frame actions.
-    pgm->addInterface(new pqStandardViewFrameActionsImplementation(pgm));
-
-    // Load plugins distributed with application.
-    pqApplicationCore::instance()->loadDistributedPlugins();
-
-    // Define application behaviors.
-    //new pqQtMessageHandlerBehavior(this);
-    new pqDataTimeStepBehavior(this);
-    new pqSpreadSheetVisibilityBehavior(this);
-    new pqPipelineContextMenuBehavior(this);
-    new pqObjectPickingBehavior(this);
-    new pqDefaultViewBehavior(this);
-    new pqUndoRedoBehavior(this);
-    new pqAlwaysConnectedBehavior(this);
-    new pqCrashRecoveryBehavior(this);
-    new pqAutoLoadPluginXMLBehavior(this);
-    new pqPluginDockWidgetsBehavior(aDesktop);
-    new pqVerifyRequiredPluginBehavior(this);
-    new pqPluginActionGroupBehavior(aDesktop);
-    new pqFixPathsInStateFilesBehavior(this);
-    new pqCommandLineOptionsBehavior(this);
-    new pqPersistentMainWindowStateBehavior(aDesktop);
-    new pqCollaborationBehavior(this);
-    new pqViewStreamingBehavior(this);
-    new pqPluginSettingsBehavior(this);
-
-    pqApplyBehavior* applyBehavior = new pqApplyBehavior(this);
-    foreach (pqPropertiesPanel* ppanel, aDesktop->findChildren<pqPropertiesPanel*>())
-      {
-      applyBehavior->registerPanel(ppanel);
-      }
+  QList<QDockWidget*> activeDocks = aDesktop->findChildren<QDockWidget*>();
+  QList<QMenu*> activeMenus = aDesktop->findChildren<QMenu*>();
 
+  PVGUI_Behaviors * behav = new PVGUI_Behaviors(this);
+  behav->instanciateAllBehaviors(aDesktop);
 
-    // Setup quick-launch shortcuts.
-    QShortcut *ctrlSpace = new QShortcut(Qt::CTRL + Qt::Key_Space, aDesktop);
-    QObject::connect(ctrlSpace, SIGNAL(activated()),
-      pqApplicationCore::instance(), SLOT(quickLaunch()));
+  // Setup quick-launch shortcuts.
+  QShortcut *ctrlSpace = new QShortcut(Qt::CTRL + Qt::Key_Space, aDesktop);
+  QObject::connect(ctrlSpace, SIGNAL(activated()),
+    pqApplicationCore::instance(), SLOT(quickLaunch()));
 
-    // Find Plugin Dock Widgets
-    QList<QDockWidget*> currentDocks = aDesktop->findChildren<QDockWidget*>();
-    QList<QDockWidget*>::iterator i;
-    for (i = currentDocks.begin(); i != currentDocks.end(); ++i) {
-      if(!activeDocks.contains(*i)) {
-        myDockWidgets[*i] = false; // hidden by default
-        (*i)->hide();
-      }
+  // Find Plugin Dock Widgets
+  QList<QDockWidget*> currentDocks = aDesktop->findChildren<QDockWidget*>();
+  QList<QDockWidget*>::iterator i;
+  for (i = currentDocks.begin(); i != currentDocks.end(); ++i) {
+    if(!activeDocks.contains(*i)) {
+      myDockWidgets[*i] = false; // hidden by default
+      (*i)->hide();
     }
+  }
 
     // Find Plugin Menus
     // [ABN] TODO: fix this - triggers a SEGFAULT at deactivation() time.
@@ -477,48 +352,52 @@ void PVGUI_Module::initialize( CAM_Application* app )
 //      }
 //    }
 
-    SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
-    QString aPath = resMgr->stringValue("resources", "PARAVIS", QString());
-    if (!aPath.isNull()) {
-      MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewFilters.xml");
-      MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewReaders.xml");
-      MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewSources.xml");
-      MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewWriters.xml");
-    }
-     
-    // Force creation of engine
-    PARAVIS::GetParavisGen(this);
-    updateObjBrowser();
+  SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
+  QString aPath = resMgr->stringValue("resources", "PARAVIS", QString());
+  if (!aPath.isNull()) {
+    MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewFilters.xml");
+    MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewReaders.xml");
+    MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewSources.xml");
+    MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewWriters.xml");
+  }
+
+  // Force creation of the PARAVIS engine
+  GetEngine();
+  updateObjBrowser();
 
-    // Find created toolbars
-    QCoreApplication::processEvents();
+  // Find created toolbars
+  QCoreApplication::processEvents();
 
-    QList<QToolBar*> allToolbars = aDesktop->findChildren<QToolBar*>();
-    foreach(QToolBar* aBar, allToolbars) {
-      if (!foreignToolbars.contains(aBar)) {
-        myToolbars[aBar] = true;
-        myToolbarBreaks[aBar] = false;
-        aBar->setVisible(false);
-        aBar->toggleViewAction()->setVisible(false);
-      }
+  QList<QToolBar*> allToolbars = aDesktop->findChildren<QToolBar*>();
+  foreach(QToolBar* aBar, allToolbars) {
+    if (!foreignToolbars.contains(aBar)) {
+      myToolbars[aBar] = true;
+      myToolbarBreaks[aBar] = false;
+      aBar->setVisible(false);
+      aBar->toggleViewAction()->setVisible(false);
     }
-    //}
+  }
 
   updateMacros();
  
-  // we need to start trace after connection is done
-  connect(pqApplicationCore::instance()->getObjectBuilder(), SIGNAL(finishedAddingServer(pqServer*)), 
-          this, SLOT(onFinishedAddingServer(pqServer*)));
-
-  connect(pqApplicationCore::instance()->getObjectBuilder(), SIGNAL(dataRepresentationCreated(pqDataRepresentation*)), 
-          this, SLOT(onDataRepresentationCreated(pqDataRepresentation*)));
-
-
   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
   bool isStop = aResourceMgr->booleanValue( "PARAVIS", "stop_trace", false );
-  // start a timer to schedule the trace start asap:
   if(!isStop)
-    startTimer( 0 );
+    {
+      // Start a timer to schedule asap:
+      //  - the connection to the server
+      //  - the trace start
+      myInitTimer = new QTimer(aDesktop);
+      QObject::connect(myInitTimer, SIGNAL(timeout()), this, SLOT(onInitTimer()) );
+      myInitTimer->setSingleShot(true);
+      myInitTimer->start(0);
+
+      // Another timer to regularly push the trace onto the engine:
+      myPushTraceTimer = new QTimer(aDesktop);
+      QObject::connect(myPushTraceTimer, SIGNAL(timeout()), this, SLOT(onPushTraceTimer()) );
+      myPushTraceTimer->setSingleShot(false);
+      myPushTraceTimer->start(500);
+    }
 
   this->VTKConnect = vtkEventQtSlotConnect::New();
   
@@ -535,10 +414,48 @@ void PVGUI_Module::initialize( CAM_Application* app )
       }
     }
   }
-  
-  connect(&pqActiveObjects::instance(),
-          SIGNAL(representationChanged(pqRepresentation*)),
-          this, SLOT(onRepresentationChanged(pqRepresentation*)));
+}
+
+bool PVGUI_Module::connectToExternalPVServer()
+{
+  pqServer* server = pqActiveObjects::instance().activeServer();
+  if (server && server->isRemote())
+    {
+      // Already connected to an external server, do nothing
+      MESSAGE("connectToExternalPVServer(): Already connected to an external PVServer, won't reconnect.");
+      return false;
+    }
+
+  std::stringstream msg;
+
+  // Try to connect to the external PVServer - gives priority to an externally specified URL:
+  QString serverUrlEnv = getenv("PARAVIS_PVSERVER_URL");
+  std::string serverUrl;
+  if (!serverUrlEnv.isEmpty())
+    serverUrl = serverUrlEnv.toStdString();
+  else
+    {
+      // Get the URL from the engine (possibly starting the pvserver)
+      CORBA::String_var url = GetEngine()->FindOrStartPVServer(0);  // take the first free port
+      serverUrl = (char *)url;
+    }
+
+  msg << "connectToExternalPVServer(): Trying to connect to the external PVServer '" << serverUrl << "' ...";
+  MESSAGE(msg.str());
+
+  if (!pqServerConnectReaction::connectToServer(pqServerResource(serverUrl.c_str())))
+    {
+      std::stringstream msg2;
+      msg2 << "Error while connecting to the requested pvserver '" << serverUrl;
+      msg2 << "'. Might use default built-in connection instead!" << std::endl;
+      qWarning(msg2.str().c_str());  // will go to the ParaView console (see ParavisMessageOutput below)
+      SUIT_MessageBox::warning( getApp()->desktop(),
+                                QString("Error connecting to PVServer"), QString(msg2.str().c_str()));
+      return false;
+    }
+  else
+    MESSAGE("connectToExternalPVServer(): Connected!");
+  return true;
 }
 
 void PVGUI_Module::onStartProgress()
@@ -551,35 +468,6 @@ void PVGUI_Module::onEndProgress()
   QApplication::restoreOverrideCursor();
 }
 
-void PVGUI_Module::onFinishedAddingServer(pqServer* /*server*/)
-{
-  SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
-  bool isStop = aResourceMgr->booleanValue( "PARAVIS", "stop_trace", false );
-  if(!isStop) 
-    startTimer( 500 );
-}
-
-void PVGUI_Module::onDataRepresentationCreated(pqDataRepresentation* data) {
-//  if(!data)
-//    return;
-//
-//  if(!data->getLookupTable())
-//    return;
-//
-//  SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
-//  if(!aResourceMgr)
-//    return;
-//
-//  bool visible = aResourceMgr->booleanValue( "PARAVIS", "show_color_legend", false );
-//  pqLookupTableManager* lut_mgr = pqApplicationCore::instance()->getLookupTableManager();
-//
-//  if(lut_mgr) {
-//    lut_mgr->setScalarBarVisibility(data,visible);
-//  }
-//
-//  connect(data, SIGNAL(dataUpdated()), this, SLOT(onDataRepresentationUpdated()));
-}
-
 void PVGUI_Module::onDataRepresentationUpdated() {
   SalomeApp_Study* activeStudy = dynamic_cast<SalomeApp_Study*>(application()->activeStudy());
   if(!activeStudy) return;
@@ -587,67 +475,16 @@ void PVGUI_Module::onDataRepresentationUpdated() {
   activeStudy->Modified();
 }
 
-void PVGUI_Module::onVariableChanged(pqVariableType t, const QString) {
-  
-  pqDisplayColorWidget* colorWidget = qobject_cast<pqDisplayColorWidget*>(sender());
-  if( !colorWidget )
-    return;
-
-  if( t == VARIABLE_TYPE_NONE )
-    return;
-
-  SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
-  
-  if(!aResourceMgr)
-    return;
-
-  bool visible = aResourceMgr->booleanValue( "PARAVIS", "show_color_legend", false );
-  
-  if(!visible)
-    return;
-  
-  /*//VTN: getRepresentation is protected
-  pqDataRepresentation* data  = colorWidget->getRepresentation();
-
-  if( !data->getLookupTable() )
-    return;
-
-  pqLookupTableManager* lut_mgr = pqApplicationCore::instance()->getLookupTableManager();
-
-  if(lut_mgr) {
-    lut_mgr->setScalarBarVisibility(data,visible);
-  }
-  */
-  pqColorToolbar* colorTooBar = qobject_cast<pqColorToolbar*>(colorWidget->parent());
-  if( !colorTooBar )
-    return;
-
-  pqScalarBarVisibilityReaction* scalarBarVisibility = colorTooBar->findChild<pqScalarBarVisibilityReaction *>();
-  if(scalarBarVisibility) {
-    scalarBarVisibility->setScalarBarVisibility(visible);
-  }
-}
-
 /*!
-  \brief Launches a tracing of current server
+  \brief Initialisation timer event - fired only once, after the GUI loop is ready.
+  See creation in initialize().
 */
-void PVGUI_Module::timerEvent(QTimerEvent* te )
+void PVGUI_Module::onInitTimer()
 {
-//#ifndef WNT
-//  if ( PyInterp_Dispatcher::Get()->IsBusy() )
-//    {
-//      // Reschedule for later
-//      MESSAGE("interpreter busy -> rescheduling trace start.");
-//      startTimer(500);
-//    }
-//  else
-//    {
-      MESSAGE("about to start trace....");
-      startTrace();
-      MESSAGE("trace STARTED....");
-//     }
-  killTimer( te->timerId() );
-//#endif
+#ifndef PARAVIS_WITH_FULL_CORBA
+  connectToExternalPVServer();
+#endif
+  startTrace();
 }
   
 /*!
@@ -725,35 +562,11 @@ bool PVGUI_Module::pvInit()
         MyCoreApp->getOptions()->GetTellVersion()) {
       return false;
       }
-
-    /* VTN: Looks like trash. For porting see branded_paraview_initializer.cxx.in
-    // Not sure why this is needed. Andy added this ages ago with comment saying
-    // needed for Mac apps. Need to check that it's indeed still required.
-    QDir dir(QApplication::applicationDirPath());
-    dir.cdUp();
-    dir.cd("Plugins");
-    QApplication::addLibraryPath(dir.absolutePath());
-    // Load required application plugins.
-    QString plugin_string = "";
-    QStringList plugin_list = plugin_string.split(';',QString::SkipEmptyParts);
-    pqBrandPluginsLoader loader;
-    if (loader.loadPlugins(plugin_list) == false) {
-      printf("Failed to load required plugins for this application\n");
-      return false;
-    }
-
-    // Load optional plugins.
-    plugin_string = "";
-    plugin_list = plugin_string.split(';',QString::SkipEmptyParts);
-    loader.loadPlugins(plugin_list, true); //quietly skip not-found plugins.
-    */
-    // End of Initializer code
-
-    MyCoreApp->settings();
-
+    // Connect VTK log messages to SALOME messages (TODO: review this)
     vtkOutputWindow::SetInstance(PVGUI_OutputWindowAdapter::New());
     
-    new pqTabbedMultiViewWidget(); // it registers as "MULTIVIEW_WIDGET on creation
+    // Create render view:
+    new pqTabbedMultiViewWidget(); // it registers as "MULTIVIEW_WIDGET" on creation
     
     for (int i = 0; i < argc; i++)
       free(argv[i]);
@@ -822,13 +635,13 @@ void PVGUI_Module::endWaitCursor()
   QApplication::restoreOverrideCursor();
 }
 
-/*!
-  \brief Returns the ParaView multi-view manager.
-*/
-pqTabbedMultiViewWidget* PVGUI_Module::getMultiViewManager() const
-{
-  return qobject_cast<pqTabbedMultiViewWidget*>(pqApplicationCore::instance()->manager("MULTIVIEW_WIDGET"));
-}
+///*!
+//  \brief Returns the ParaView multi-view manager.
+//*/
+//pqTabbedMultiViewWidget* PVGUI_Module::getMultiViewManager() const
+//{
+//  return qobject_cast<pqTabbedMultiViewWidget*>(pqApplicationCore::instance()->manager("MULTIVIEW_WIDGET"));
+//}
 
 
 static void ParavisMessageOutput(QtMsgType type, const char *msg)
@@ -898,7 +711,7 @@ bool PVGUI_Module::activateModule( SUIT_Study* study )
 
   if ( myRecentMenuId != -1 ) menuMgr()->show(myRecentMenuId);
 
-  ClientFindOrCreateParavisComponent(PARAVIS::GetCStudy(this));
+//  ClientFindOrCreateParavisComponent(PARAVIS::GetCStudy(this));
 
   return isDone;
 }
@@ -912,6 +725,8 @@ bool PVGUI_Module::activateModule( SUIT_Study* study )
 */
 bool PVGUI_Module::deactivateModule( SUIT_Study* study )
 {
+  MESSAGE("PARAVIS deactivation ...")
+
   QMenu* aMenu = menuMgr()->findMenu( myRecentMenuId );
   if(aMenu) {
     QList<QAction*> anActns = aMenu->actions();
@@ -948,7 +763,6 @@ bool PVGUI_Module::deactivateModule( SUIT_Study* study )
   setMenuShown( false );
   setToolShown( false );
 
-
   saveDockWidgetsState();
 
   SUIT_ExceptionHandler::removeCleanUpRoutine( paravisCleanUp );
@@ -982,6 +796,19 @@ void PVGUI_Module::onApplicationClosed( SUIT_Application* theApp )
 }
 
 
+/*!
+  \brief Deletes temporary files created during import operation from VISU
+*/
+void PVGUI_Module::deleteTemporaryFiles()
+{
+  foreach(QString aFile, myTemporaryFiles) {
+    if (QFile::exists(aFile)) {
+      QFile::remove(aFile);
+    }
+  }
+}
+
+
 /*!
   \brief Called when study is closed.
 
@@ -1007,7 +834,7 @@ void PVGUI_Module::onModelOpened()
   }
   
   _PTR(SComponent) paravisComp = 
-    studyDS->FindComponent(PARAVIS::GetParavisGen(this)->ComponentDataType());
+    studyDS->FindComponent(GetEngine()->ComponentDataType());
   if(!paravisComp) {
     return;
   }
@@ -1031,7 +858,7 @@ void PVGUI_Module::onModelOpened()
 */
 QString PVGUI_Module::engineIOR() const
 {
-  CORBA::String_var anIOR = PARAVIS::GetParavisGen(this)->GetIOR();
+  CORBA::String_var anIOR = GetEngine()->GetIOR();
   return QString(anIOR.in());
 }
 
@@ -1187,78 +1014,6 @@ void PVGUI_Module::loadParaviewState(const char* theFileName)
   pqApplicationCore::instance()->loadState(theFileName, getActiveServer());
 }
 
-/*!
-  \brief Imports MED data from VISU module by data entry
-*/
-void PVGUI_Module::onImportFromVisu(QString theEntry)
-{
-#ifdef WITH_VISU
-  SUIT_OverrideCursor aWaitCursor;
-
-  // get active study
-  SalomeApp_Study* activeStudy = dynamic_cast<SalomeApp_Study*>(application()->activeStudy());
-  if(!activeStudy) return;
-
-  // get SALOMEDS client study
-  _PTR(Study) aStudy = activeStudy->studyDS();
-  if(!aStudy) return;
-
-  // find VISU component in a study
-  _PTR(SComponent) aVisuComp = aStudy->FindComponent( "VISU" );
-  if(!aVisuComp) return;
-
-  // get SObject client by entry
-  _PTR(SObject) aSObj = aStudy->FindObjectID(qPrintable(theEntry));
-  if (!aSObj) return;
-
-  // get CORBA SObject
-  SALOMEDS_SObject* aSObject = _CAST(SObject, aSObj);
-  if ( !aSObject ) return;
-
-  // load VISU engine
-  SALOME_NamingService* aNamingService = SalomeApp_Application::namingService();
-  SALOME_LifeCycleCORBA aLCC(aNamingService);
-
-  Engines::EngineComponent_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","VISU");
-  VISU::VISU_Gen_var aVISU = VISU::VISU_Gen::_narrow(aComponent);
-  if(CORBA::is_nil(aVISU)) return;
-
-  _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
-  aStudyBuilder->LoadWith( aVisuComp, SalomeApp_Application::orb()->object_to_string(aVISU) );
-
-  // get VISU result object
-  CORBA::Object_var aResultObject = aSObject->GetObject();
-  if (CORBA::is_nil(aResultObject)) return;
-  VISU::Result_var aResult = VISU::Result::_narrow( aResultObject );
-  if (CORBA::is_nil(aResult)) return;
-
-  // export VISU result to the MED file
-  std::string aTmpDir = SALOMEDS_Tool::GetTmpDir();
-  std::string aFileName = aSObject->GetName();
-  std::string aFilePath = aTmpDir + aFileName;
-
-  if (aResult->ExportMED(aFilePath.c_str())) {
-    openFile(aFilePath.c_str());
-    myTemporaryFiles.append(QString(aFilePath.c_str()));
-  }
-#else
-  MESSAGE("Visu module is not found.");
-#endif
-}
-
-/*!
-  \brief Deletes temporary files created during import operation from VISU
-*/
-void PVGUI_Module::deleteTemporaryFiles()
-{
-  foreach(QString aFile, myTemporaryFiles) {
-    if (QFile::exists(aFile)) {
-      QFile::remove(aFile);
-    }
-  }
-}
-
-
 /*!
   \brief Returns current active ParaView server
 */
@@ -1293,11 +1048,6 @@ void PVGUI_Module::createPreferences()
   aStrings<<tr("PREF_SAVE_TYPE_2");
   setPreferenceProperty(aSaveType, "strings", aStrings);
   setPreferenceProperty(aSaveType, "indexes", aIndices);
-
-  //rnv: imp 21712: [CEA 581] Preference to display legend by default 
-  // [ABN]: now fixed in ParaView.
-//  int aDispColoreLegend = addPreference( tr( "PREF_SHOW_COLOR_LEGEND" ), aParaVisSettingsTab,
-//                                        LightApp_Preferences::Bool, "PARAVIS", "show_color_legend");
 }
 
 /*!
@@ -1334,7 +1084,7 @@ void PVGUI_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QS
       return;
     }
 
-    QString paravisDataType(PARAVIS::GetParavisGen(this)->ComponentDataType());
+    QString paravisDataType(GetEngine()->ComponentDataType());
     if(activeStudy && activeStudy->isComponent(entry) && 
        activeStudy->componentDataType(entry) == paravisDataType) {
       // ParaViS module object
@@ -1416,7 +1166,7 @@ void PVGUI_Module::onSaveMultiState()
   }
   
   _PTR(SComponent) paravisComp = 
-    studyDS->FindComponent(PARAVIS::GetParavisGen(this)->ComponentDataType());
+    studyDS->FindComponent(GetEngine()->ComponentDataType());
   if(!paravisComp) {
     return;
   }
@@ -1451,8 +1201,7 @@ void PVGUI_Module::onSaveMultiState()
  
   // File name for state saving
   QString tmpDir = QString::fromStdString(SALOMEDS_Tool::GetTmpDir());
-  QString fileName = QString("%1_paravisstate:%2").arg(tmpDir, 
-                                                      stateEntry);
+  QString fileName = QString("%1_paravisstate:%2").arg(tmpDir, stateEntry);
 
   anAttr = studyBuilder->FindOrCreateAttribute(newSObj, "AttributeString");
   _PTR(AttributeString) stringAttr(anAttr);
@@ -1524,10 +1273,10 @@ void PVGUI_Module::onRename()
     if (stateSObj->FindAttribute(anAttr, "AttributeName")) {
       _PTR(AttributeName) nameAttr (anAttr);
       QString newName = 
-       LightApp_NameDlg::getName(getApp()->desktop(), nameAttr->Value().c_str());
+          LightApp_NameDlg::getName(getApp()->desktop(), nameAttr->Value().c_str());
       if (!newName.isEmpty()) {
-       nameAttr->SetValue(newName.toLatin1().constData());
-       aListIO.First()->setName(newName.toLatin1().constData());
+        nameAttr->SetValue(newName.toLatin1().constData());
+        aListIO.First()->setName(newName.toLatin1().constData());
       }
     }
     
@@ -1581,6 +1330,12 @@ void PVGUI_Module::onDelete()
   }
 }
 
+void PVGUI_Module::onPushTraceTimer()
+{
+//  MESSAGE("onPushTraceTimer(): Pushing trace to engine...");
+  GetEngine()->PutPythonTraceStringToEngine(getTraceString().toStdString().c_str());
+}
+
 /*!
   \brief Discover help project files from the resources.
   \return name of the help file. 
@@ -1637,7 +1392,7 @@ void PVGUI_Module::loadSelectedState(bool toClear)
       QString stringValue(aStringAttr->Value().c_str());
 
       if (QFile::exists(stringValue)) {
-       fileName = stringValue;
+          fileName = stringValue;
       }
     }
   }
@@ -1656,24 +1411,6 @@ void PVGUI_Module::loadSelectedState(bool toClear)
   }
 }
 
-void PVGUI_Module::onRepresentationChanged(pqRepresentation*) {
-
-
-  //rnv: to fix the issue "21712: [CEA 581] Preference to display legend by default"
-  //     find the pqDisplayColorWidget instances and connect the variableChanged SIGNAL on the 
-  //     onVariableChanged slot of this class. This connection needs to change visibility 
-  //     of the "Colored Legend" after change the "Color By" array.
-  QList<pqDisplayColorWidget*> aWidget = getApp()->desktop()->findChildren<pqDisplayColorWidget*>();
-  
-  for (int i = 0; i < aWidget.size() ; i++ ) {
-    if( aWidget[i] ) {
-      connect( aWidget[i], SIGNAL ( variableChanged ( pqVariableType, const QString ) ), 
-              this, SLOT(onVariableChanged( pqVariableType, const QString) ), Qt::UniqueConnection );
-    }    
-  }
-}
-
-
 /*!
   \fn CAM_Module* createModule();
   \brief Export module instance (factory function).
@@ -1690,10 +1427,6 @@ extern "C" {
 
   bool flag = false;
   PVGUI_EXPORT CAM_Module* createModule() {
-    if(!flag) {
-        vtkEDFHelperInit();
-        flag = true;
-    }      
     return new PVGUI_Module();
   }