Salome HOME
8233ef9b61d7b524b61071de715bd96e59d7b130
[modules/paravis.git] / src / PVGUI / PVGUI_Module.cxx
1 // PARAVIS : ParaView wrapper SALOME module
2 //
3 // Copyright (C) 2010-2014  CEA/DEN, EDF R&D
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 //
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 //
21 // File   : PVGUI_Module.cxx
22 // Author : Julia DOROVSKIKH
23
24 #include <Standard_math.hxx>  // E.A. must be included before Python.h to fix compilation on windows
25 #ifdef HAVE_FINITE
26 #undef HAVE_FINITE            // VSR: avoid compilation warning on Linux : "HAVE_FINITE" redefined
27 #endif
28 #include <vtkPython.h> // Python first
29 #include "PVGUI_Module.h"
30
31 #include "SALOMEconfig.h"
32 #ifdef WITH_VISU
33 #include CORBA_CLIENT_HEADER(VISU_Gen)
34 #endif
35 #include CORBA_SERVER_HEADER(SALOMEDS)
36
37
38 #include "PARAVIS_Gen_i.hh"
39
40 #include "PV_Tools.h"
41
42 #include "PVGUI_ViewModel.h"
43 #include "PVGUI_ViewManager.h"
44 #include "PVGUI_ViewWindow.h"
45 #include "PVGUI_Tools.h"
46 #include "PVGUI_ParaViewSettingsPane.h"
47 #include "PVGUI_OutputWindowAdapter.h"
48
49 #include <SUIT_DataBrowser.h>
50 #include <SUIT_Desktop.h>
51 #include <SUIT_MessageBox.h>
52 #include <SUIT_ResourceMgr.h>
53 #include <SUIT_Session.h>
54 #include <SUIT_OverrideCursor.h>
55 #include <SUIT_ExceptionHandler.h>
56
57 // SALOME Includes
58 #include "SALOME_LifeCycleCORBA.hxx"
59 #include "SALOMEDS_SObject.hxx"
60
61 #include "LightApp_SelectionMgr.h"
62 #include "LightApp_NameDlg.h"
63
64 #include <SalomeApp_Application.h>
65 #include <SalomeApp_Study.h>
66 #include <SALOME_ListIO.hxx>
67 #include <SALOMEDS_Tool.hxx>
68 #include <PyInterp_Interp.h>
69 #include <PyInterp_Dispatcher.h>
70 #include <PyConsole_Console.h>
71
72 #include <QtxActionMenuMgr.h>
73 #include <QtxActionToolMgr.h>
74
75 #include <QAction>
76 #include <QApplication>
77 #include <QCursor>
78 #include <QDir>
79 #include <QFile>
80 #include <QFileInfo>
81 #include <QIcon>
82 #include <QInputDialog>
83 #include <QMenu>
84 #include <QStatusBar>
85 #include <QString>
86 #include <QStringList>
87 #include <QTimer>
88 #include <QToolBar>
89 #include <QTextStream>
90 #include <QShortcut>
91 #include <QDockWidget>
92 #include <QHelpEngine>
93
94 #include <pqApplicationCore.h>
95 #include <pqPVApplicationCore.h>
96 #include <pqActiveView.h>
97 #include <pqObjectBuilder.h>
98 #include <pqOptions.h>
99 #include <pqRenderView.h>
100 #include <pqServer.h>
101 #include <pqUndoStack.h>
102 #include <pqVCRController.h>
103 #include <pqTabbedMultiViewWidget.h>
104 #include <pqPipelineSource.h>
105 #include <pqActiveObjects.h>
106 #include <vtkProcessModule.h>
107 #include <vtkSMSession.h>
108 #include <vtkPVSession.h>
109 #include <vtkPVProgressHandler.h>
110 #include <pqParaViewBehaviors.h>
111 #include <pqHelpReaction.h>
112 #include <vtkOutputWindow.h>
113 #include <pqPluginManager.h>
114 #include "pqInterfaceTracker.h"
115 #include <pqSettings.h>
116 #include <pqPythonDialog.h>
117 #include <pqPythonManager.h>
118 #include <pqPythonShell.h>
119 #include <pqLoadDataReaction.h>
120 #include <vtkEventQtSlotConnect.h>
121 #include <pqPythonScriptEditor.h>
122 #include <pqCollaborationBehavior.h>
123 #include <pqDataRepresentation.h>
124 #include <pqPipelineRepresentation.h>
125 //#include <pqLookupTableManager.h>
126 #include <pqDisplayColorWidget.h>
127 #include <pqColorToolbar.h>
128 #include <pqScalarBarVisibilityReaction.h>
129 #include <pqStandardPropertyWidgetInterface.h>
130 #include <pqStandardViewFrameActionsImplementation.h>
131 #include <pqViewStreamingBehavior.h>
132
133 #include <PARAVIS_version.h>
134
135 #include <vtkPVConfig.h>
136
137
138 #include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
139
140 #include <pqAlwaysConnectedBehavior.h>
141 #include <pqApplicationCore.h>
142 #include <pqAutoLoadPluginXMLBehavior.h>
143 #include <pqCommandLineOptionsBehavior.h>
144 #include <pqCrashRecoveryBehavior.h>
145 #include <pqDataTimeStepBehavior.h>
146 #include <pqDefaultViewBehavior.h>
147 #include <pqObjectPickingBehavior.h>
148 #include <pqPersistentMainWindowStateBehavior.h>
149 #include <pqPipelineContextMenuBehavior.h>
150 #include <pqPluginActionGroupBehavior.h>
151 #include <pqPluginDockWidgetsBehavior.h>
152 #include <pqPluginManager.h>
153 #include <pqSpreadSheetVisibilityBehavior.h>
154 #include <pqUndoRedoBehavior.h>
155 #include <pqServerManagerObserver.h>
156 #include <pqVerifyRequiredPluginBehavior.h>
157 #include <pqFixPathsInStateFilesBehavior.h>
158 #include <pqPluginSettingsBehavior.h>
159 #include <pqPropertiesPanel.h>
160
161 #include <pqApplyBehavior.h>
162
163 #include <vtkClientServerInterpreterInitializer.h>
164
165 // Trace related
166 #include <vtkNew.h>
167 #include <vtkSMTrace.h>
168 #include <vtkSMSessionProxyManager.h>
169 #include <vtkSMParaViewPipelineController.h>
170
171 //----------------------------------------------------------------------------
172 pqPVApplicationCore* PVGUI_Module::MyCoreApp = 0;
173 //PVGUI_OutputWindowAdapter* PVGUI_Module::pqImplementation::OutputWindowAdapter = 0;
174 //QPointer<pqHelpWindow> PVGUI_Module::pqImplementation::helpWindow = 0;
175
176 PVGUI_Module* ParavisModule = 0;
177
178 /*!
179   \mainpage
180
181   <h2>Building and installing PARAVIS</h2>
182   As any other SALOME module, PARAVIS requires PARAVIS_ROOT_DIR environment variable to be set to PARAVIS
183   installation directory.
184   Other variables needed for correct detection of ParaView location:
185   \li PVHOME - points at the ParaView installation directory tree
186   \li PVVERSION - number of ParaView version
187
188   It also requires common SALOME environment including GUI_ROOT_DIR and other prerequsites.
189
190
191   PARAVIS module can be launched using the following commands:
192   \li Full SALOME configuration
193   \code
194   runSalome --modules="PARAVIS"
195   \endcode
196
197   <h2>ParaView GUI integration</h2>
198   <h3>ParaView GUI integration overview</h3>
199
200   The main idea is to reuse ParaView GUI internal logic as much as possible, providing a layer 
201   between it and SALOME GUI that hides the following SALOME GUI implementation details from ParaView:
202
203   \li SALOME GUI executable and Qt event loop
204   \li SALOME GUI desktop
205   \li Dock windows areas
206   \li SALOME menu and toolbar managers
207
208   Major part of the integration is implemented in PVGUI_Module class.
209
210   <h3>ParaView client initalization</h3>
211
212   ParaView client initalization is performed when an instance of PVGUI_Module class has been created 
213   and \link PVGUI_Module::initialize() PVGUI_Module::initialize()\endlink method is called by SALOME GUI.
214   The actual client start-up is done in \link PVGUI_Module::pvInit() PVGUI_Module::pvInit()\endlink method. 
215   
216
217   <h3>Multi-view manager</h3>
218
219   SALOME GUI requires that each kind of view be implemnted with help of (at least) three classes. For ParaView multi-view manager 
220   these are:
221
222   \li PVGUI_ViewManager - view manager class
223   \li PVGUI_Viewer      - view model class
224   \li PVGUI_ViewWindow  - view window class that acts as a parent for %pqViewManager
225
226   Single instances of PVGUI_ViewManager and PVGUI_ViewWindow classes are created by \link PVGUI_Module::showView() 
227   PVGUI_Module::showView()\endlink method upon the first PARAVIS module activation. The same method hides the multi-view manager 
228   when the module is deactivated (the user switches to another module or a study is closed). 
229   A special trick is used to make PVGUI_ViewWindow the parent of %pqViewManager widget. It is created initally by %pqMainWindowCore
230   with the desktop as a parent, so when it is shown PVGUI_ViewWindow instance is passed to its setParent() method. In  
231   \link PVGUI_ViewWindow::~PVGUI_ViewWindow() PVGUI_ViewWindow::~PVGUI_ViewWindow()\endlink the parent is nullified to avoid deletion
232   of %pqViewManager widget that would break %pqMainWindowCore class.
233
234   <h3>ParaView plugins</h3>
235   ParaView server and client plugins are managed by %pqMainWindowCore slots that has full access to PARAVIS menus and toolbars. 
236   As a result they appears automatically in PARAVIS menus and toolbars if loaded successfully.
237 */
238
239 /*!
240   \class PVGUI_Module
241   \brief Implementation 
242          SALOME module wrapping ParaView GUI.
243 */
244
245
246 /*
247   Fix for the issue 21730: [CEA 596] Slice of polyhedron in PARAVIS returns no cell.
248   Wrap vtkEDFCutter filter.
249 */
250
251 extern "C" void vtkEDFCutterCS_Initialize(vtkClientServerInterpreter*);
252 static void vtkEDFHelperInit();
253
254 void vtkEDFHelperInit(vtkClientServerInterpreter* interp){
255     vtkEDFCutterCS_Initialize(interp);
256 }
257
258 void vtkEDFHelperInit() {
259     vtkClientServerInterpreterInitializer::GetInitializer()->
260         RegisterCallback(&vtkEDFHelperInit);
261 }
262
263
264   _PTR(SComponent)
265   ClientFindOrCreateParavisComponent(_PTR(Study) theStudyDocument)
266   {
267     _PTR(SComponent) aSComponent = theStudyDocument->FindComponent("PARAVIS");
268     if (!aSComponent) {
269       _PTR(StudyBuilder) aStudyBuilder = theStudyDocument->NewBuilder();
270       aStudyBuilder->NewCommand();
271       int aLocked = theStudyDocument->GetProperties()->IsLocked();
272       if (aLocked) theStudyDocument->GetProperties()->SetLocked(false);
273       aSComponent = aStudyBuilder->NewComponent("PARAVIS");
274       _PTR(GenericAttribute) anAttr =
275         aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeName");
276       _PTR(AttributeName) aName (anAttr);
277
278       CORBA::ORB_var anORB = PARAVIS::PARAVIS_Gen_i::GetORB();
279       SALOME_NamingService *NamingService = new SALOME_NamingService( anORB );
280       CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
281       SALOME_ModuleCatalog::ModuleCatalog_var Catalogue =
282         SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
283       SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "PARAVIS" );
284       if (!Comp->_is_nil()) {
285         aName->SetValue(Comp->componentusername());
286       }
287
288       anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributePixMap");
289       _PTR(AttributePixMap) aPixmap (anAttr);
290       aPixmap->SetPixMap( "pqAppIcon16.png" );
291
292       // Create Attribute parameters for future using
293       anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeParameter");
294
295
296       PARAVIS::PARAVIS_Gen_var aPARAVIS = PARAVIS::PARAVIS_Gen_i::GetParavisGenImpl()->_this();
297
298       aStudyBuilder->DefineComponentInstance(aSComponent, aPARAVIS->GetIOR());
299       if (aLocked) theStudyDocument->GetProperties()->SetLocked(true);
300       aStudyBuilder->CommitCommand();
301     }
302     return aSComponent;
303   }
304
305 /*!
306   Clean up function; used to stop ParaView progress events when
307   exception is caught by global exception handler.
308 */
309 void paravisCleanUp()
310 {
311   if ( pqApplicationCore::instance() ) {
312     pqServer* s = pqApplicationCore::instance()->getActiveServer();
313     if ( s ) s->session()->GetProgressHandler()->CleanupPendingProgress();
314   }
315 }
316
317 /*!
318   \brief Constructor. Sets the default name for the module.
319 */
320 PVGUI_Module::PVGUI_Module()
321   : SalomeApp_Module( "PARAVIS" ),
322     //    Implementation( 0 ),
323     mySelectionControlsTb( -1 ),
324     mySourcesMenuId( -1 ),
325     myFiltersMenuId( -1 ),
326     myMacrosMenuId(-1),
327     myToolbarsMenuId(-1),
328     myRecentMenuId(-1),
329     myOldMsgHandler(0),
330     myTraceWindow(0),
331     myStateCounter(0)
332 {
333 #ifdef HAS_PV_DOC
334   Q_INIT_RESOURCE( PVGUI );
335 #endif
336   ParavisModule = this;
337
338   // Clear old copies of embedded macros files
339   QString aDestPath = QString( "%1/.config/%2/Macros" ).arg( QDir::homePath() ).arg( QApplication::applicationName() );
340   QStringList aFilter;
341   aFilter << "*.py";
342
343   QDir aDestDir(aDestPath);
344   QStringList aDestFiles = aDestDir.entryList(aFilter, QDir::Files);
345   foreach (QString aMacrosPath, getEmbeddedMacrosList()) {
346     QString aMacrosName = QFileInfo(aMacrosPath).fileName();
347     if (aDestFiles.contains(aMacrosName)) {
348       aDestDir.remove(aMacrosName);
349     }
350   }
351 }
352
353 /*!
354   \brief Destructor.
355 */
356 PVGUI_Module::~PVGUI_Module()
357 {
358 }
359
360 /*!
361   \brief Initialize module. Creates menus, prepares context menu, etc.
362   \param app SALOME GUI application instance
363 */
364 void PVGUI_Module::initialize( CAM_Application* app )
365 {
366   // [ABN]: patched in ParaView's sources.
367   // PVGUI_MatplotlibMathTextUtilities::Disable();
368
369   SalomeApp_Module::initialize( app );
370
371   // Create ParaViS actions
372   createActions();
373   // Create ParaViS menus
374   createMenus();
375
376   // Uncomment to debug ParaView initialization
377   // "aa" used instead of "i" as GDB doesn't like "i" variables :)
378   /*
379   int aa = 1;
380   while( aa ){
381     aa = aa;
382   }
383   */
384   
385   // Initialize ParaView client
386   pvInit();
387
388   // Create GUI elements (menus, toolbars, dock widgets)
389   //if ( !Implementation ){
390     SalomeApp_Application* anApp = getApp();
391     SUIT_Desktop* aDesktop = anApp->desktop();
392
393     // connect(aDesktop, SIGNAL()
394
395     // Remember current state of desktop toolbars
396     QList<QToolBar*> foreignToolbars = aDesktop->findChildren<QToolBar*>();
397
398     // Simulate ParaView client main window
399     //Implementation = new pqImplementation( aDesktop );
400
401     setupDockWidgets();
402     
403     pvCreateActions();
404     pvCreateToolBars();
405     pvCreateMenus();
406
407     QList<QDockWidget*> activeDocks = aDesktop->findChildren<QDockWidget*>();
408     QList<QMenu*> activeMenus = aDesktop->findChildren<QMenu*>();
409
410     // new pqParaViewBehaviors(anApp->desktop(), this);
411     // Has to be replaced in order to exclude using of pqQtMessageHandlerBehaviour
412     //  Start pqParaViewBehaviors
413     // Register ParaView interfaces.
414     //pqPluginManager* pgm = pqApplicationCore::instance()->getPluginManager();
415     pqInterfaceTracker* pgm = pqApplicationCore::instance()->interfaceTracker();
416
417     // Register standard types of property widgets.
418     pgm->addInterface(new pqStandardPropertyWidgetInterface(pgm));
419     // Register standard types of view-frame actions.
420     pgm->addInterface(new pqStandardViewFrameActionsImplementation(pgm));
421
422     // Load plugins distributed with application.
423     pqApplicationCore::instance()->loadDistributedPlugins();
424
425     // Define application behaviors.
426     //new pqQtMessageHandlerBehavior(this);
427     new pqDataTimeStepBehavior(this);
428     new pqSpreadSheetVisibilityBehavior(this);
429     new pqPipelineContextMenuBehavior(this);
430     new pqObjectPickingBehavior(this);
431     new pqDefaultViewBehavior(this);
432     new pqUndoRedoBehavior(this);
433     new pqAlwaysConnectedBehavior(this);
434     new pqCrashRecoveryBehavior(this);
435     new pqAutoLoadPluginXMLBehavior(this);
436     new pqPluginDockWidgetsBehavior(aDesktop);
437     new pqVerifyRequiredPluginBehavior(this);
438     new pqPluginActionGroupBehavior(aDesktop);
439     new pqFixPathsInStateFilesBehavior(this);
440     new pqCommandLineOptionsBehavior(this);
441     new pqPersistentMainWindowStateBehavior(aDesktop);
442     new pqCollaborationBehavior(this);
443     new pqViewStreamingBehavior(this);
444     new pqPluginSettingsBehavior(this);
445
446     pqApplyBehavior* applyBehavior = new pqApplyBehavior(this);
447     foreach (pqPropertiesPanel* ppanel, aDesktop->findChildren<pqPropertiesPanel*>())
448       {
449       applyBehavior->registerPanel(ppanel);
450       }
451
452
453     // Setup quick-launch shortcuts.
454     QShortcut *ctrlSpace = new QShortcut(Qt::CTRL + Qt::Key_Space, aDesktop);
455     QObject::connect(ctrlSpace, SIGNAL(activated()),
456       pqApplicationCore::instance(), SLOT(quickLaunch()));
457
458     // Find Plugin Dock Widgets
459     QList<QDockWidget*> currentDocks = aDesktop->findChildren<QDockWidget*>();
460     QList<QDockWidget*>::iterator i;
461     for (i = currentDocks.begin(); i != currentDocks.end(); ++i) {
462       if(!activeDocks.contains(*i)) {
463         myDockWidgets[*i] = false; // hidden by default
464         (*i)->hide();
465       }
466     }
467
468     // Find Plugin Menus
469     // [ABN] TODO: fix this - triggers a SEGFAULT at deactivation() time.
470 //    QList<QMenu*> currentMenus = aDesktop->findChildren<QMenu*>();
471 //    QList<QMenu*>::iterator im;
472 //    for (im = currentMenus.begin(); im != currentMenus.end(); ++im) {
473 //      if(!activeMenus.contains(*im)) {
474 //          QString s = (*im)->title();
475 //          std::cout << " MENU "<<  s.toStdString() << std::endl;
476 //          myMenus.append(*im);
477 //      }
478 //    }
479
480     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
481     QString aPath = resMgr->stringValue("resources", "PARAVIS", QString());
482     if (!aPath.isNull()) {
483       MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewFilters.xml");
484       MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewReaders.xml");
485       MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewSources.xml");
486       MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewWriters.xml");
487     }
488      
489     // Force creation of engine
490     PARAVIS::GetParavisGen(this);
491     updateObjBrowser();
492
493     // Find created toolbars
494     QCoreApplication::processEvents();
495
496     QList<QToolBar*> allToolbars = aDesktop->findChildren<QToolBar*>();
497     foreach(QToolBar* aBar, allToolbars) {
498       if (!foreignToolbars.contains(aBar)) {
499         myToolbars[aBar] = true;
500         myToolbarBreaks[aBar] = false;
501         aBar->setVisible(false);
502         aBar->toggleViewAction()->setVisible(false);
503       }
504     }
505     //}
506
507   updateMacros();
508  
509   // we need to start trace after connection is done
510   connect(pqApplicationCore::instance()->getObjectBuilder(), SIGNAL(finishedAddingServer(pqServer*)), 
511           this, SLOT(onFinishedAddingServer(pqServer*)));
512
513   connect(pqApplicationCore::instance()->getObjectBuilder(), SIGNAL(dataRepresentationCreated(pqDataRepresentation*)), 
514           this, SLOT(onDataRepresentationCreated(pqDataRepresentation*)));
515
516
517   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
518   bool isStop = aResourceMgr->booleanValue( "PARAVIS", "stop_trace", false );
519   // start a timer to schedule the trace start asap:
520   if(!isStop)
521     startTimer( 0 );
522
523   this->VTKConnect = vtkEventQtSlotConnect::New();
524   
525   vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
526   if(pm) {
527     vtkPVSession* pvs = dynamic_cast<vtkPVSession*>(pm->GetSession());
528     if(pvs) {
529       vtkPVProgressHandler* ph = pvs->GetProgressHandler();
530       if(ph) {
531           this->VTKConnect->Connect(ph, vtkCommand::StartEvent,
532                                     this, SLOT(onStartProgress()));
533           this->VTKConnect->Connect(ph, vtkCommand::EndEvent,
534                                     this, SLOT(onEndProgress()));
535       }
536     }
537   }
538   
539   connect(&pqActiveObjects::instance(),
540           SIGNAL(representationChanged(pqRepresentation*)),
541           this, SLOT(onRepresentationChanged(pqRepresentation*)));
542 }
543
544 void PVGUI_Module::onStartProgress()
545 {
546   QApplication::setOverrideCursor(Qt::WaitCursor);
547 }
548
549 void PVGUI_Module::onEndProgress()
550 {
551   QApplication::restoreOverrideCursor();
552 }
553
554 void PVGUI_Module::onFinishedAddingServer(pqServer* /*server*/)
555 {
556   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
557   bool isStop = aResourceMgr->booleanValue( "PARAVIS", "stop_trace", false );
558   if(!isStop) 
559     startTimer( 500 );
560 }
561
562 void PVGUI_Module::onDataRepresentationCreated(pqDataRepresentation* data) {
563 //  if(!data)
564 //    return;
565 //
566 //  if(!data->getLookupTable())
567 //    return;
568 //
569 //  SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
570 //  if(!aResourceMgr)
571 //    return;
572 //
573 //  bool visible = aResourceMgr->booleanValue( "PARAVIS", "show_color_legend", false );
574 //  pqLookupTableManager* lut_mgr = pqApplicationCore::instance()->getLookupTableManager();
575 //
576 //  if(lut_mgr) {
577 //    lut_mgr->setScalarBarVisibility(data,visible);
578 //  }
579 //
580 //  connect(data, SIGNAL(dataUpdated()), this, SLOT(onDataRepresentationUpdated()));
581 }
582
583 void PVGUI_Module::onDataRepresentationUpdated() {
584   SalomeApp_Study* activeStudy = dynamic_cast<SalomeApp_Study*>(application()->activeStudy());
585   if(!activeStudy) return;
586   
587   activeStudy->Modified();
588 }
589
590 void PVGUI_Module::onVariableChanged(pqVariableType t, const QString) {
591   
592   pqDisplayColorWidget* colorWidget = qobject_cast<pqDisplayColorWidget*>(sender());
593   if( !colorWidget )
594     return;
595
596   if( t == VARIABLE_TYPE_NONE )
597     return;
598
599   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
600   
601   if(!aResourceMgr)
602     return;
603
604   bool visible = aResourceMgr->booleanValue( "PARAVIS", "show_color_legend", false );
605   
606   if(!visible)
607     return;
608   
609   /*//VTN: getRepresentation is protected
610   pqDataRepresentation* data  = colorWidget->getRepresentation();
611
612   if( !data->getLookupTable() )
613     return;
614
615   pqLookupTableManager* lut_mgr = pqApplicationCore::instance()->getLookupTableManager();
616
617   if(lut_mgr) {
618     lut_mgr->setScalarBarVisibility(data,visible);
619   }
620   */
621   pqColorToolbar* colorTooBar = qobject_cast<pqColorToolbar*>(colorWidget->parent());
622   if( !colorTooBar )
623     return;
624
625   pqScalarBarVisibilityReaction* scalarBarVisibility = colorTooBar->findChild<pqScalarBarVisibilityReaction *>();
626   if(scalarBarVisibility) {
627     scalarBarVisibility->setScalarBarVisibility(visible);
628   }
629 }
630
631 /*!
632   \brief Launches a tracing of current server
633 */
634 void PVGUI_Module::timerEvent(QTimerEvent* te )
635 {
636 //#ifndef WNT
637 //  if ( PyInterp_Dispatcher::Get()->IsBusy() )
638 //    {
639 //      // Reschedule for later
640 //      MESSAGE("interpreter busy -> rescheduling trace start.");
641 //      startTimer(500);
642 //    }
643 //  else
644 //    {
645       MESSAGE("about to start trace....");
646       startTrace();
647       MESSAGE("trace STARTED....");
648 //     }
649   killTimer( te->timerId() );
650 //#endif
651 }
652   
653 /*!
654   \brief Get list of embedded macros files
655 */
656 QStringList PVGUI_Module::getEmbeddedMacrosList()
657 {
658   QString aRootDir = getenv("PARAVIS_ROOT_DIR");
659
660   QString aSourcePath = aRootDir + "/bin/salome/Macro";
661
662   QStringList aFilter;
663   aFilter << "*.py";
664
665   QDir aSourceDir(aSourcePath);
666   QStringList aSourceFiles = aSourceDir.entryList(aFilter, QDir::Files);
667   QStringList aFullPathSourceFiles;
668   foreach (QString aMacrosName, aSourceFiles) {
669     aFullPathSourceFiles << aSourceDir.absoluteFilePath(aMacrosName);
670   }
671   return aFullPathSourceFiles;
672 }
673
674 void PVGUI_Module::updateMacros()
675 {
676   pqPythonManager* aPythonManager = pqPVApplicationCore::instance()->pythonManager();
677   if(!aPythonManager)  {
678     return;
679   }
680   
681   foreach (QString aStr, getEmbeddedMacrosList()) {
682     aPythonManager->addMacro(aStr);
683   }
684 }
685
686
687 /*!
688   \brief Get list of compliant dockable GUI elements
689   \param m map to be filled in ("type":"default_position")
690 */
691 void PVGUI_Module::windows( QMap<int, int>& m ) const
692 {
693   m.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
694   m.insert( LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
695   // ParaView diagnostic output redirected here
696   m.insert( LightApp_Application::WT_LogWindow, Qt::BottomDockWidgetArea );
697 }
698
699 /*!
700   \brief Static method, performs initialization of ParaView session.
701   \return \c true if ParaView has been initialized successfully, otherwise false
702 */
703 bool PVGUI_Module::pvInit()
704 {
705   //  if ( !pqImplementation::Core ){
706   if ( ! MyCoreApp) {
707     // Obtain command-line arguments
708     int argc = 0;
709     char** argv = 0;
710     QString aOptions = getenv("PARAVIS_OPTIONS");
711     QStringList aOptList = aOptions.split(":", QString::SkipEmptyParts);
712     argv = new char*[aOptList.size() + 1];
713     QStringList args = QApplication::arguments();
714     argv[0] = (args.size() > 0)? strdup(args[0].toLatin1().constData()) : strdup("paravis");
715     argc++;
716
717     foreach (QString aStr, aOptList) {
718       argv[argc] = strdup( aStr.toLatin1().constData() );
719       argc++;
720     }
721     MyCoreApp = new pqPVApplicationCore (argc, argv);
722     if (MyCoreApp->getOptions()->GetHelpSelected() ||
723         MyCoreApp->getOptions()->GetUnknownArgument() ||
724         MyCoreApp->getOptions()->GetErrorMessage() ||
725         MyCoreApp->getOptions()->GetTellVersion()) {
726       return false;
727       }
728
729     /* VTN: Looks like trash. For porting see branded_paraview_initializer.cxx.in
730     // Not sure why this is needed. Andy added this ages ago with comment saying
731     // needed for Mac apps. Need to check that it's indeed still required.
732     QDir dir(QApplication::applicationDirPath());
733     dir.cdUp();
734     dir.cd("Plugins");
735     QApplication::addLibraryPath(dir.absolutePath());
736     // Load required application plugins.
737     QString plugin_string = "";
738     QStringList plugin_list = plugin_string.split(';',QString::SkipEmptyParts);
739     pqBrandPluginsLoader loader;
740     if (loader.loadPlugins(plugin_list) == false) {
741       printf("Failed to load required plugins for this application\n");
742       return false;
743     }
744
745     // Load optional plugins.
746     plugin_string = "";
747     plugin_list = plugin_string.split(';',QString::SkipEmptyParts);
748     loader.loadPlugins(plugin_list, true); //quietly skip not-found plugins.
749     */
750     // End of Initializer code
751
752     MyCoreApp->settings();
753
754     vtkOutputWindow::SetInstance(PVGUI_OutputWindowAdapter::New());
755     
756     new pqTabbedMultiViewWidget(); // it registers as "MULTIVIEW_WIDGET on creation
757     
758     for (int i = 0; i < argc; i++)
759       free(argv[i]);
760     delete[] argv;
761   }
762   
763   return true;
764 }
765  
766 /*!
767   \brief Shows (toShow = true) or hides ParaView view window
768 */
769 void PVGUI_Module::showView( bool toShow )
770 {
771   SalomeApp_Application* anApp = getApp();
772   PVGUI_ViewManager* viewMgr =
773     dynamic_cast<PVGUI_ViewManager*>( anApp->getViewManager( PVGUI_Viewer::Type(), false ) );
774   if ( !viewMgr ) {
775     viewMgr = new PVGUI_ViewManager( anApp->activeStudy(), anApp->desktop() );
776     anApp->addViewManager( viewMgr );
777     connect( viewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
778              anApp, SLOT( onCloseView( SUIT_ViewManager* ) ) );
779   }
780
781   PVGUI_ViewWindow* pvWnd = dynamic_cast<PVGUI_ViewWindow*>( viewMgr->getActiveView() );
782   if ( !pvWnd ) {
783     pvWnd = dynamic_cast<PVGUI_ViewWindow*>( viewMgr->createViewWindow() );
784   }
785
786   pvWnd->setShown( toShow );
787   if ( toShow ) pvWnd->setFocus();
788 }
789
790 /*!
791   \brief Slot to show help for proxy.
792 */
793 void PVGUI_Module::showHelpForProxy( const QString& groupname, const QString& proxyname )
794 {
795   pqHelpReaction::showProxyHelp(groupname, proxyname);
796 }
797
798
799 /*!
800   \brief Slot to show the waiting state.
801 */
802 void PVGUI_Module::onPreAccept()
803 {
804   getApp()->desktop()->statusBar()->showMessage(tr("STB_PREACCEPT"));
805   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
806 }
807
808 /*!
809   \brief Slot to show the ready state.
810 */
811 void PVGUI_Module::onPostAccept()
812 {
813   getApp()->desktop()->statusBar()->showMessage(tr("STB_POSTACCEPT"), 2000);
814   QTimer::singleShot(0, this, SLOT(endWaitCursor()));
815 }
816
817 /*!
818   \brief Slot to switch off wait cursor.
819 */
820 void PVGUI_Module::endWaitCursor()
821 {
822   QApplication::restoreOverrideCursor();
823 }
824
825 /*!
826   \brief Returns the ParaView multi-view manager.
827 */
828 pqTabbedMultiViewWidget* PVGUI_Module::getMultiViewManager() const
829 {
830   return qobject_cast<pqTabbedMultiViewWidget*>(pqApplicationCore::instance()->manager("MULTIVIEW_WIDGET"));
831 }
832
833
834 static void ParavisMessageOutput(QtMsgType type, const char *msg)
835 {
836   switch(type)
837     {
838   case QtDebugMsg:
839     vtkOutputWindow::GetInstance()->DisplayText(msg);
840     break;
841   case QtWarningMsg:
842     vtkOutputWindow::GetInstance()->DisplayErrorText(msg);
843     break;
844   case QtCriticalMsg:
845     vtkOutputWindow::GetInstance()->DisplayErrorText(msg);
846     break;
847   case QtFatalMsg:
848     vtkOutputWindow::GetInstance()->DisplayErrorText(msg);
849     break;
850     }
851 }
852
853
854
855 /*!
856   \brief Activate module.
857   \param study current study
858   \return \c true if activaion is done successfully or 0 to prevent
859   activation on error
860 */
861 bool PVGUI_Module::activateModule( SUIT_Study* study )
862 {
863   myOldMsgHandler = qInstallMsgHandler(ParavisMessageOutput);
864   
865   SUIT_ExceptionHandler::addCleanUpRoutine( paravisCleanUp );
866
867   storeCommonWindowsState();
868
869   bool isDone = SalomeApp_Module::activateModule( study );
870   if ( !isDone ) return false;
871
872   showView( true );
873   if ( mySourcesMenuId != -1 ) menuMgr()->show(mySourcesMenuId);
874   if ( myFiltersMenuId != -1 ) menuMgr()->show(myFiltersMenuId);
875   if ( myFiltersMenuId != -1 ) menuMgr()->show(myMacrosMenuId);
876   if ( myFiltersMenuId != -1 ) menuMgr()->show(myToolbarsMenuId);
877   setMenuShown( true );
878   setToolShown( true );
879
880   restoreDockWidgetsState();
881
882   QMenu* aMenu = menuMgr()->findMenu( myRecentMenuId );
883   if(aMenu) {
884     QList<QAction*> anActns = aMenu->actions();
885     for (int i = 0; i < anActns.size(); ++i) {
886       QAction* a = anActns.at(i);
887       if(a)
888         a->setVisible(true);
889     }
890   }
891
892   QList<QMenu*>::iterator it;
893   for (it = myMenus.begin(); it != myMenus.end(); ++it) {
894     QAction* a = (*it)->menuAction();
895     if(a)
896       a->setVisible(true);
897   }
898
899   if ( myRecentMenuId != -1 ) menuMgr()->show(myRecentMenuId);
900
901   ClientFindOrCreateParavisComponent(PARAVIS::GetCStudy(this));
902
903   return isDone;
904 }
905
906
907 /*!
908   \brief Deactivate module.
909   \param study current study
910   \return \c true if deactivaion is done successfully or 0 to prevent
911   deactivation on error
912 */
913 bool PVGUI_Module::deactivateModule( SUIT_Study* study )
914 {
915   QMenu* aMenu = menuMgr()->findMenu( myRecentMenuId );
916   if(aMenu) {
917     QList<QAction*> anActns = aMenu->actions();
918     for (int i = 0; i < anActns.size(); ++i) {
919       QAction* a = anActns.at(i);
920       if(a)
921         a->setVisible(false);
922     }
923   }
924
925   QList<QMenu*>::iterator it;
926   for (it = myMenus.begin(); it != myMenus.end(); ++it) {
927     QAction* a = (*it)->menuAction();
928     if(a)
929       a->setVisible(false);
930   }
931
932   QList<QDockWidget*> aStreamingViews = application()->desktop()->findChildren<QDockWidget*>("pqStreamingControls");
933   foreach(QDockWidget* aView, aStreamingViews) {
934     if (!myDockWidgets.contains(aView))
935       myDockWidgets[aView] = aView->isVisible();
936   }
937
938   /*if (pqImplementation::helpWindow) {
939     pqImplementation::helpWindow->hide();
940     }*/
941   showView( false );
942   // hide menus
943   menuMgr()->hide(myRecentMenuId);
944   menuMgr()->hide(mySourcesMenuId);
945   menuMgr()->hide(myFiltersMenuId);
946   menuMgr()->hide(myMacrosMenuId);
947   menuMgr()->hide(myToolbarsMenuId);
948   setMenuShown( false );
949   setToolShown( false );
950
951
952   saveDockWidgetsState();
953
954   SUIT_ExceptionHandler::removeCleanUpRoutine( paravisCleanUp );
955
956   if (myOldMsgHandler)
957     qInstallMsgHandler(myOldMsgHandler);
958
959   restoreCommonWindowsState();
960   
961   return SalomeApp_Module::deactivateModule( study );
962 }
963
964
965 /*!
966   \brief Called when application is closed.
967
968   Process finalize application functionality from ParaView in order to save server settings
969   and nullify application pointer if the application is being closed.
970
971   \param theApp application
972 */
973 void PVGUI_Module::onApplicationClosed( SUIT_Application* theApp )
974 {
975   pqApplicationCore::instance()->settings()->sync();
976   int aAppsNb = SUIT_Session::session()->applications().size();
977   if (aAppsNb == 1) {
978     deleteTemporaryFiles();
979     MyCoreApp->deleteLater();
980   }
981   CAM_Module::onApplicationClosed(theApp);
982 }
983
984
985 /*!
986   \brief Called when study is closed.
987
988   Removes data model from the \a study.
989
990   \param study study being closed
991 */
992 void PVGUI_Module::studyClosed(SUIT_Study* study)
993 {
994   clearParaviewState();
995
996   SalomeApp_Module::studyClosed(study);
997 }
998
999 /*!
1000   \brief Called when study is opened.
1001 */
1002 void PVGUI_Module::onModelOpened()
1003 {
1004   _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
1005   if(!studyDS) {
1006     return;
1007   }
1008   
1009   _PTR(SComponent) paravisComp = 
1010     studyDS->FindComponent(PARAVIS::GetParavisGen(this)->ComponentDataType());
1011   if(!paravisComp) {
1012     return;
1013   }
1014
1015   _PTR(ChildIterator) anIter(studyDS->NewChildIterator(paravisComp));
1016   for (; anIter->More(); anIter->Next()) {
1017     _PTR(SObject) aSObj = anIter->Value();
1018     _PTR(GenericAttribute) anAttr;
1019     if (!aSObj->FindAttribute(anAttr, "AttributeLocalID")) {
1020       continue;
1021     }
1022     _PTR(AttributeLocalID) anID(anAttr);
1023     if (anID->Value() == PVSTATEID) {
1024       myStateCounter++;
1025     }
1026   }
1027 }
1028
1029 /*!
1030   \brief Returns IOR of current engine
1031 */
1032 QString PVGUI_Module::engineIOR() const
1033 {
1034   CORBA::String_var anIOR = PARAVIS::GetParavisGen(this)->GetIOR();
1035   return QString(anIOR.in());
1036 }
1037
1038
1039 /*!
1040   \brief Open file of format supported by ParaView
1041 */
1042 void PVGUI_Module::openFile(const char* theName)
1043 {
1044   QStringList aFiles;
1045   aFiles<<theName;
1046   pqLoadDataReaction::loadData(aFiles);
1047 }
1048
1049 /**!
1050  * Start trace invoking the newly introduced C++ API (PV 4.2)
1051  * (inspired from pqTraceReaction::start())
1052  */
1053 void PVGUI_Module::startTrace()
1054 {
1055   vtkSMSessionProxyManager* pxm = pqActiveObjects::instance().activeServer()->proxyManager();
1056
1057   vtkSmartPointer<vtkSMProxy> proxy;
1058   proxy.TakeReference(pxm->NewProxy("pythontracing", "PythonTraceOptions"));
1059   if (proxy)
1060     {
1061       vtkNew<vtkSMParaViewPipelineController> controller;
1062       controller->InitializeProxy(proxy);
1063     }
1064   vtkSMTrace* trace = vtkSMTrace::StartTrace();
1065   if (proxy)
1066     {
1067       // Set manually the properties entered via the dialog box poping-up when requiring
1068       // a trace start in PV4.2 (trace options)
1069       trace->SetPropertiesToTraceOnCreate(vtkSMTrace::RECORD_USER_MODIFIED_PROPERTIES);
1070       trace->SetFullyTraceSupplementalProxies(false);
1071     }
1072 }
1073
1074 void PVGUI_Module::stopTrace()
1075 {
1076   vtkSMTrace::StopTrace();
1077 }
1078
1079 void PVGUI_Module::executeScript(const char *script)
1080 {
1081 #ifndef WNT
1082   pqPythonManager* manager = qobject_cast<pqPythonManager*>(
1083                              pqApplicationCore::instance()->manager("PYTHON_MANAGER"));
1084   if (manager)  {
1085     pqPythonDialog* pyDiag = manager->pythonShellDialog();
1086     if (pyDiag) {
1087       pyDiag->runString(script);  
1088       }
1089     }
1090 #endif
1091 }
1092
1093 ///**
1094 // *  Debug function printing out the given interpreter's execution context
1095 // */
1096 //void printInterpContext(PyInterp_Interp * interp )
1097 //{
1098 //  // Extract __smtraceString from interpreter's context
1099 //  const PyObject* ctxt = interp->getExecutionContext();
1100 //
1101 //  PyObject* lst = PyDict_Keys((PyObject *)ctxt);
1102 //  Py_ssize_t siz = PyList_GET_SIZE(lst);
1103 //  for (Py_ssize_t i = 0; i < siz; i++)
1104 //    {
1105 //      PyObject * elem = PyList_GetItem(lst, i);
1106 //      if (PyString_Check(elem))
1107 //        {
1108 //          std::cout << "At pos:" << i << ", " << PyString_AsString(elem) << std::endl;
1109 //        }
1110 //      else
1111 //        std::cout << "At pos:" << i << ", not a string!" << std::endl;
1112 //    }
1113 //  Py_XDECREF(lst);
1114 //}
1115
1116 /*!
1117   \brief Returns trace string
1118 */
1119 static const QString MYReplaceStr("paraview.simple");
1120 static const QString MYReplaceImportStr("except: from pvsimple import *");
1121 QString PVGUI_Module::getTraceString()
1122 {
1123   vtkSMTrace *tracer = vtkSMTrace::GetActiveTracer();
1124   if (!tracer) // trace is not started
1125     return QString("");
1126
1127   QString traceString(tracer->GetCurrentTrace());
1128
1129   // Replace import "paraview.simple" by "pvsimple"
1130   if ((!traceString.isNull()) && traceString.length() != 0) {
1131     int aPos = traceString.indexOf(MYReplaceStr);
1132     while (aPos != -1) {
1133       traceString = traceString.replace(aPos, MYReplaceStr.length(), "pvsimple");
1134       aPos = traceString.indexOf(MYReplaceStr, aPos);
1135     }
1136     int aImportPos = traceString.indexOf(MYReplaceImportStr);
1137     if(aImportPos != -1)
1138       {
1139       traceString = traceString.replace(aImportPos, MYReplaceImportStr.length(), "except:\n  import pvsimple\n  from pvsimple import *");
1140       }
1141   }
1142
1143   return traceString;
1144 }
1145
1146 /*!
1147   \brief Saves trace string to disk file
1148 */
1149 void PVGUI_Module::saveTrace(const char* theName)
1150 {
1151   QFile file(theName);
1152   if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
1153     MESSAGE( "Could not open file:" << theName );
1154     return;
1155   }
1156   QTextStream out(&file);
1157   out << getTraceString();
1158   file.close();
1159 }
1160
1161 /*!
1162   \brief Saves ParaView state to a disk file
1163 */
1164 void PVGUI_Module::saveParaviewState(const char* theFileName)
1165 {
1166   pqApplicationCore::instance()->saveState(theFileName);
1167 }
1168
1169 /*!
1170   \brief Delete all objects for Paraview Pipeline Browser
1171 */
1172 void PVGUI_Module::clearParaviewState()
1173 {
1174   QAction* deleteAllAction = action(DeleteAllId);
1175   if (deleteAllAction) {
1176     deleteAllAction->activate(QAction::Trigger);
1177   }
1178 }
1179
1180 /*!
1181   \brief Restores ParaView state from a disk file
1182
1183   If toClear == true, the current ojects will be deleted
1184 */
1185 void PVGUI_Module::loadParaviewState(const char* theFileName)
1186 {
1187   pqApplicationCore::instance()->loadState(theFileName, getActiveServer());
1188 }
1189
1190 /*!
1191   \brief Imports MED data from VISU module by data entry
1192 */
1193 void PVGUI_Module::onImportFromVisu(QString theEntry)
1194 {
1195 #ifdef WITH_VISU
1196   SUIT_OverrideCursor aWaitCursor;
1197
1198   // get active study
1199   SalomeApp_Study* activeStudy = dynamic_cast<SalomeApp_Study*>(application()->activeStudy());
1200   if(!activeStudy) return;
1201
1202   // get SALOMEDS client study
1203   _PTR(Study) aStudy = activeStudy->studyDS();
1204   if(!aStudy) return;
1205
1206   // find VISU component in a study
1207   _PTR(SComponent) aVisuComp = aStudy->FindComponent( "VISU" );
1208   if(!aVisuComp) return;
1209
1210   // get SObject client by entry
1211   _PTR(SObject) aSObj = aStudy->FindObjectID(qPrintable(theEntry));
1212   if (!aSObj) return;
1213
1214   // get CORBA SObject
1215   SALOMEDS_SObject* aSObject = _CAST(SObject, aSObj);
1216   if ( !aSObject ) return;
1217
1218   // load VISU engine
1219   SALOME_NamingService* aNamingService = SalomeApp_Application::namingService();
1220   SALOME_LifeCycleCORBA aLCC(aNamingService);
1221
1222   Engines::EngineComponent_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","VISU");
1223   VISU::VISU_Gen_var aVISU = VISU::VISU_Gen::_narrow(aComponent);
1224   if(CORBA::is_nil(aVISU)) return;
1225
1226   _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
1227   aStudyBuilder->LoadWith( aVisuComp, SalomeApp_Application::orb()->object_to_string(aVISU) );
1228
1229   // get VISU result object
1230   CORBA::Object_var aResultObject = aSObject->GetObject();
1231   if (CORBA::is_nil(aResultObject)) return;
1232   VISU::Result_var aResult = VISU::Result::_narrow( aResultObject );
1233   if (CORBA::is_nil(aResult)) return;
1234
1235   // export VISU result to the MED file
1236   std::string aTmpDir = SALOMEDS_Tool::GetTmpDir();
1237   std::string aFileName = aSObject->GetName();
1238   std::string aFilePath = aTmpDir + aFileName;
1239
1240   if (aResult->ExportMED(aFilePath.c_str())) {
1241     openFile(aFilePath.c_str());
1242     myTemporaryFiles.append(QString(aFilePath.c_str()));
1243   }
1244 #else
1245   MESSAGE("Visu module is not found.");
1246 #endif
1247 }
1248
1249 /*!
1250   \brief Deletes temporary files created during import operation from VISU
1251 */
1252 void PVGUI_Module::deleteTemporaryFiles()
1253 {
1254   foreach(QString aFile, myTemporaryFiles) {
1255     if (QFile::exists(aFile)) {
1256       QFile::remove(aFile);
1257     }
1258   }
1259 }
1260
1261
1262 /*!
1263   \brief Returns current active ParaView server
1264 */
1265 pqServer* PVGUI_Module::getActiveServer()
1266 {
1267   return pqApplicationCore::instance()->getActiveServer();
1268 }
1269
1270
1271 /*!
1272   \brief Creates PARAVIS preference pane 
1273 */
1274 void PVGUI_Module::createPreferences()
1275 {
1276   // Paraview settings tab
1277   int aParaViewSettingsTab = addPreference( tr( "TIT_PVIEWSETTINGS" ) );
1278   int aPanel = addPreference(QString(), aParaViewSettingsTab, LightApp_Preferences::UserDefined, "PARAVIS", "");
1279   setPreferenceProperty(aPanel, "content", (qint64)(new PVGUI_ParaViewSettingsPane()));
1280
1281   // Paravis settings tab
1282   int aParaVisSettingsTab = addPreference( tr( "TIT_PVISSETTINGS" ) );
1283   addPreference( tr( "PREF_STOP_TRACE" ), aParaVisSettingsTab, LightApp_Preferences::Bool, "PARAVIS", "stop_trace");
1284
1285   int aSaveType = addPreference(tr( "PREF_SAVE_TYPE_LBL" ), aParaVisSettingsTab,
1286                                 LightApp_Preferences::Selector,
1287                                 "PARAVIS", "savestate_type");
1288   QList<QVariant> aIndices;
1289   QStringList aStrings;
1290   aIndices<<0<<1<<2;
1291   aStrings<<tr("PREF_SAVE_TYPE_0");
1292   aStrings<<tr("PREF_SAVE_TYPE_1");
1293   aStrings<<tr("PREF_SAVE_TYPE_2");
1294   setPreferenceProperty(aSaveType, "strings", aStrings);
1295   setPreferenceProperty(aSaveType, "indexes", aIndices);
1296
1297   //rnv: imp 21712: [CEA 581] Preference to display legend by default 
1298   // [ABN]: now fixed in ParaView.
1299 //  int aDispColoreLegend = addPreference( tr( "PREF_SHOW_COLOR_LEGEND" ), aParaVisSettingsTab,
1300 //                                        LightApp_Preferences::Bool, "PARAVIS", "show_color_legend");
1301 }
1302
1303 /*!
1304   \brief Creates ParaViS context menu popup
1305 */
1306 void PVGUI_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
1307 {
1308   SalomeApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
1309   
1310   // Check if we are in Object Browser
1311   SUIT_DataBrowser* ob = getApp()->objectBrowser();
1312   bool isOBClient = (ob && theClient == ob->popupClientType());
1313   if (!isOBClient) {
1314     return;
1315   }
1316
1317   // Get list of selected objects
1318   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1319   SALOME_ListIO aListIO;
1320   aSelectionMgr->selectedObjects(aListIO);
1321   if (aListIO.Extent() == 1 && aListIO.First()->hasEntry()) {
1322     QString entry = QString(aListIO.First()->getEntry());
1323     
1324     // Get active study
1325     SalomeApp_Study* activeStudy = 
1326       dynamic_cast<SalomeApp_Study*>(getApp()->activeStudy());
1327     if(!activeStudy) {
1328       return;
1329     }
1330
1331     // Get SALOMEDS client study 
1332     _PTR(Study) studyDS = activeStudy->studyDS();
1333     if(!studyDS) {
1334       return;
1335     }
1336
1337     QString paravisDataType(PARAVIS::GetParavisGen(this)->ComponentDataType());
1338     if(activeStudy && activeStudy->isComponent(entry) && 
1339        activeStudy->componentDataType(entry) == paravisDataType) {
1340       // ParaViS module object
1341       theMenu->addSeparator();
1342       theMenu->addAction(action(SaveStatePopupId));
1343     }
1344     else {
1345       // Try to get state object
1346       _PTR(SObject) stateSObj = 
1347           studyDS->FindObjectID(entry.toLatin1().constData());
1348       if (!stateSObj) {
1349           return;
1350       }
1351       
1352       // Check local id
1353       _PTR(GenericAttribute) anAttr;
1354       if (!stateSObj->FindAttribute(anAttr, "AttributeLocalID")) {
1355           return;
1356       }
1357
1358       _PTR(AttributeLocalID) anID(anAttr);
1359       
1360       if (anID->Value() == PVSTATEID) {
1361         // Paraview state object
1362         theMenu->addSeparator();
1363         theMenu->addAction(action(AddStatePopupId));
1364         theMenu->addAction(action(CleanAndAddStatePopupId));
1365         theMenu->addSeparator();
1366         theMenu->addAction(action(ParaVisRenameId));
1367         theMenu->addAction(action(ParaVisDeleteId));
1368       }
1369     }
1370   }
1371 }
1372
1373 /*!
1374   \brief. Show ParaView python trace.
1375 */
1376 void PVGUI_Module::onShowTrace()
1377 {
1378   if (!myTraceWindow) {
1379     myTraceWindow = new pqPythonScriptEditor(getApp()->desktop());
1380   }
1381   myTraceWindow->setText(getTraceString());
1382   myTraceWindow->show();
1383   myTraceWindow->raise();
1384   myTraceWindow->activateWindow();
1385 }
1386
1387
1388 /*!
1389   \brief. Re-initialize ParaView python trace.
1390 */
1391 void PVGUI_Module::onRestartTrace()
1392 {
1393   stopTrace();
1394   startTrace();
1395 }
1396
1397 /*!
1398   \brief Show ParaView view.
1399 */
1400 void PVGUI_Module::onNewParaViewWindow()
1401 {
1402   showView(true);
1403 }
1404
1405 /*!
1406   \brief Save state under the module root object.
1407 */
1408 void PVGUI_Module::onSaveMultiState()
1409 {
1410   // Create state study object
1411   
1412   // Get SALOMEDS client study
1413   _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
1414   if(!studyDS) {
1415     return;
1416   }
1417   
1418   _PTR(SComponent) paravisComp = 
1419     studyDS->FindComponent(PARAVIS::GetParavisGen(this)->ComponentDataType());
1420   if(!paravisComp) {
1421     return;
1422   }
1423
1424   // Unlock the study if it is locked
1425   bool isLocked = studyDS->GetProperties()->IsLocked();
1426   if (isLocked) {
1427     studyDS->GetProperties()->SetLocked(false);
1428   }
1429   
1430   QString stateName = tr("SAVED_PARAVIEW_STATE_NAME") + 
1431     QString::number(myStateCounter + 1);
1432
1433   _PTR(StudyBuilder) studyBuilder = studyDS->NewBuilder();
1434   _PTR(SObject) newSObj = studyBuilder->NewObject(paravisComp);
1435
1436   // Set name
1437   _PTR(GenericAttribute) anAttr;
1438   anAttr = studyBuilder->FindOrCreateAttribute(newSObj, "AttributeName");
1439   _PTR(AttributeName) nameAttr(anAttr);
1440   
1441   nameAttr->SetValue(stateName.toLatin1().constData());
1442
1443   // Set local id
1444   anAttr = studyBuilder->FindOrCreateAttribute(newSObj, "AttributeLocalID");
1445   _PTR(AttributeLocalID) localIdAttr(anAttr);
1446   
1447   localIdAttr->SetValue(PVSTATEID);
1448
1449   // Set file name
1450   QString stateEntry = QString::fromStdString(newSObj->GetID());
1451  
1452   // File name for state saving
1453   QString tmpDir = QString::fromStdString(SALOMEDS_Tool::GetTmpDir());
1454   QString fileName = QString("%1_paravisstate:%2").arg(tmpDir, 
1455                                                        stateEntry);
1456
1457   anAttr = studyBuilder->FindOrCreateAttribute(newSObj, "AttributeString");
1458   _PTR(AttributeString) stringAttr(anAttr);
1459   
1460   stringAttr->SetValue(fileName.toLatin1().constData());
1461
1462   // Lock the study back if necessary
1463   if (isLocked) {
1464     studyDS->GetProperties()->SetLocked(true);
1465   }
1466   
1467   // Save state
1468   saveParaviewState(fileName.toLatin1().constData());
1469   myTemporaryFiles.append(fileName);
1470   
1471   // Increment the counter
1472   myStateCounter++;
1473
1474   updateObjBrowser();
1475 }
1476
1477 /*!
1478   \brief Restore the selected state by merging with the current one.
1479 */
1480 void PVGUI_Module::onAddState()
1481 {
1482   loadSelectedState(false);
1483 }
1484
1485 /*!
1486   \brief Clean the current state and restore the selected one.
1487 */
1488 void PVGUI_Module::onCleanAddState()
1489 {
1490   loadSelectedState(true);
1491 }
1492
1493 /*!
1494   \brief Rename the selected object.
1495 */
1496 void PVGUI_Module::onRename()
1497 {
1498   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1499   SALOME_ListIO aListIO;
1500   aSelectionMgr->selectedObjects(aListIO);
1501   
1502   if (aListIO.Extent() == 1 && aListIO.First()->hasEntry()) {
1503     std::string entry = aListIO.First()->getEntry();
1504     
1505     // Get SALOMEDS client study 
1506     _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
1507     if(!studyDS) {
1508       return;
1509     }
1510     
1511     // Unlock the study if it is locked
1512     bool isLocked = studyDS->GetProperties()->IsLocked();
1513     if (isLocked) {
1514       studyDS->GetProperties()->SetLocked(false);
1515     }
1516     
1517     // Rename the selected state object
1518     _PTR(SObject) stateSObj = studyDS->FindObjectID(entry);
1519     if (!stateSObj) {
1520       return;
1521     }
1522     
1523     _PTR(GenericAttribute) anAttr;
1524     if (stateSObj->FindAttribute(anAttr, "AttributeName")) {
1525       _PTR(AttributeName) nameAttr (anAttr);
1526       QString newName = 
1527         LightApp_NameDlg::getName(getApp()->desktop(), nameAttr->Value().c_str());
1528       if (!newName.isEmpty()) {
1529         nameAttr->SetValue(newName.toLatin1().constData());
1530         aListIO.First()->setName(newName.toLatin1().constData());
1531       }
1532     }
1533     
1534     // Lock the study back if necessary
1535     if (isLocked) {
1536       studyDS->GetProperties()->SetLocked(true);
1537     }
1538     
1539     // Update object browser
1540     updateObjBrowser();
1541     
1542   }
1543 }
1544
1545 /*!
1546   \brief Delete the selected objects.
1547 */
1548 void PVGUI_Module::onDelete()
1549 {
1550   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1551   SALOME_ListIO aListIO;
1552   aSelectionMgr->selectedObjects(aListIO);
1553   
1554   if (aListIO.Extent() == 1 && aListIO.First()->hasEntry()) {
1555     std::string entry = aListIO.First()->getEntry();
1556     
1557     // Get SALOMEDS client study 
1558     _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
1559     if(!studyDS) {
1560       return;
1561     }
1562     
1563     // Unlock the study if it is locked
1564     bool isLocked = studyDS->GetProperties()->IsLocked();
1565     if (isLocked) {
1566       studyDS->GetProperties()->SetLocked(false);
1567     }
1568     
1569     // Remove the selected state from the study
1570     _PTR(StudyBuilder) studyBuilder = studyDS->NewBuilder();
1571     _PTR(SObject) stateSObj = studyDS->FindObjectID(entry);
1572     studyBuilder->RemoveObject(stateSObj);
1573     
1574     // Lock the study back if necessary
1575     if (isLocked) {
1576       studyDS->GetProperties()->SetLocked(true);
1577     }
1578     
1579     // Update object browser
1580     updateObjBrowser();
1581   }
1582 }
1583
1584 /*!
1585   \brief Discover help project files from the resources.
1586   \return name of the help file. 
1587 */
1588 QString PVGUI_Module::getHelpFileName() {
1589   QString aPVHome(getenv("PVHOME"));
1590   if (aPVHome.isNull()) {
1591     qWarning("Wariable PVHOME is not defined");
1592     return QString();
1593   }
1594   QChar aSep = QDir::separator();
1595   //PARAVIEW_VERSION from the vtkPVConfig.h file
1596   QString aFileName =  aPVHome + aSep + "share" + aSep + "doc" + aSep + "paraview-"+ PARAVIEW_VERSION + aSep + "paraview.qch";
1597   return aFileName;
1598 }
1599
1600
1601 /*!
1602   \brief Load selected paraview state
1603
1604   If toClear == true, the current state will be cleared
1605 */
1606 void PVGUI_Module::loadSelectedState(bool toClear)
1607 {
1608   QString fileName;
1609
1610   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1611   SALOME_ListIO aListIO;
1612   aSelectionMgr->selectedObjects(aListIO);
1613   
1614   if (aListIO.Extent() == 1 && aListIO.First()->hasEntry()) {
1615     std::string entry = aListIO.First()->getEntry();
1616     
1617     // Get SALOMEDS client study 
1618     _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
1619     if(!studyDS) {
1620       return;
1621     }
1622
1623     // Check local id
1624     _PTR(SObject) stateSObj = studyDS->FindObjectID(entry);
1625     _PTR(GenericAttribute) anAttr;
1626     if (!stateSObj->FindAttribute(anAttr, "AttributeLocalID")) {
1627       return;
1628     }
1629     _PTR(AttributeLocalID) anID(anAttr);
1630     if (!anID->Value() == PVSTATEID) {
1631       return;
1632     }
1633
1634     // Get state file name
1635     if (stateSObj->FindAttribute(anAttr, "AttributeString")) {
1636       _PTR(AttributeString) aStringAttr(anAttr);
1637       QString stringValue(aStringAttr->Value().c_str());
1638
1639       if (QFile::exists(stringValue)) {
1640         fileName = stringValue;
1641       }
1642     }
1643   }
1644   
1645   if (!fileName.isEmpty()) {
1646     if (toClear) {
1647       clearParaviewState();
1648     }
1649
1650     loadParaviewState(fileName.toLatin1().constData());
1651   } 
1652   else {
1653     SUIT_MessageBox::critical(getApp()->desktop(),
1654                               tr("ERR_ERROR"),
1655                               tr("ERR_STATE_CANNOT_BE_RESTORED"));
1656   }
1657 }
1658
1659 void PVGUI_Module::onRepresentationChanged(pqRepresentation*) {
1660
1661
1662   //rnv: to fix the issue "21712: [CEA 581] Preference to display legend by default"
1663   //     find the pqDisplayColorWidget instances and connect the variableChanged SIGNAL on the 
1664   //     onVariableChanged slot of this class. This connection needs to change visibility 
1665   //     of the "Colored Legend" after change the "Color By" array.
1666   QList<pqDisplayColorWidget*> aWidget = getApp()->desktop()->findChildren<pqDisplayColorWidget*>();
1667   
1668   for (int i = 0; i < aWidget.size() ; i++ ) {
1669     if( aWidget[i] ) {
1670       connect( aWidget[i], SIGNAL ( variableChanged ( pqVariableType, const QString ) ), 
1671               this, SLOT(onVariableChanged( pqVariableType, const QString) ), Qt::UniqueConnection );
1672     }    
1673   }
1674 }
1675
1676
1677 /*!
1678   \fn CAM_Module* createModule();
1679   \brief Export module instance (factory function).
1680   \return new created instance of the module
1681 */
1682
1683 #ifdef WNT
1684 #define PVGUI_EXPORT __declspec(dllexport)
1685 #else   // WNT
1686 #define PVGUI_EXPORT
1687 #endif  // WNT
1688
1689 extern "C" {
1690
1691   bool flag = false;
1692   PVGUI_EXPORT CAM_Module* createModule() {
1693     if(!flag) {
1694         vtkEDFHelperInit();
1695         flag = true;
1696     }      
1697     return new PVGUI_Module();
1698   }
1699   
1700   PVGUI_EXPORT char* getModuleVersion() {
1701     return (char*)PARAVIS_VERSION_STR;
1702   }
1703 }