]> SALOME platform Git repositories - modules/paravis.git/blob - src/PVGUI/PVGUI_Module.cxx
Salome HOME
c2847dab1264d7f4257296ec752e280ab781081f
[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     vtkOutputWindow::SetInstance(PVGUI_OutputWindowAdapter::New());
753     
754     new pqTabbedMultiViewWidget(); // it registers as "MULTIVIEW_WIDGET on creation
755     
756     for (int i = 0; i < argc; i++)
757       free(argv[i]);
758     delete[] argv;
759   }
760   
761   return true;
762 }
763  
764 /*!
765   \brief Shows (toShow = true) or hides ParaView view window
766 */
767 void PVGUI_Module::showView( bool toShow )
768 {
769   SalomeApp_Application* anApp = getApp();
770   PVGUI_ViewManager* viewMgr =
771     dynamic_cast<PVGUI_ViewManager*>( anApp->getViewManager( PVGUI_Viewer::Type(), false ) );
772   if ( !viewMgr ) {
773     viewMgr = new PVGUI_ViewManager( anApp->activeStudy(), anApp->desktop() );
774     anApp->addViewManager( viewMgr );
775     connect( viewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
776              anApp, SLOT( onCloseView( SUIT_ViewManager* ) ) );
777   }
778
779   PVGUI_ViewWindow* pvWnd = dynamic_cast<PVGUI_ViewWindow*>( viewMgr->getActiveView() );
780   if ( !pvWnd ) {
781     pvWnd = dynamic_cast<PVGUI_ViewWindow*>( viewMgr->createViewWindow() );
782   }
783
784   pvWnd->setShown( toShow );
785   if ( toShow ) pvWnd->setFocus();
786 }
787
788 /*!
789   \brief Slot to show help for proxy.
790 */
791 void PVGUI_Module::showHelpForProxy( const QString& groupname, const QString& proxyname )
792 {
793   pqHelpReaction::showProxyHelp(groupname, proxyname);
794 }
795
796
797 /*!
798   \brief Slot to show the waiting state.
799 */
800 void PVGUI_Module::onPreAccept()
801 {
802   getApp()->desktop()->statusBar()->showMessage(tr("STB_PREACCEPT"));
803   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
804 }
805
806 /*!
807   \brief Slot to show the ready state.
808 */
809 void PVGUI_Module::onPostAccept()
810 {
811   getApp()->desktop()->statusBar()->showMessage(tr("STB_POSTACCEPT"), 2000);
812   QTimer::singleShot(0, this, SLOT(endWaitCursor()));
813 }
814
815 /*!
816   \brief Slot to switch off wait cursor.
817 */
818 void PVGUI_Module::endWaitCursor()
819 {
820   QApplication::restoreOverrideCursor();
821 }
822
823 /*!
824   \brief Returns the ParaView multi-view manager.
825 */
826 pqTabbedMultiViewWidget* PVGUI_Module::getMultiViewManager() const
827 {
828   return qobject_cast<pqTabbedMultiViewWidget*>(pqApplicationCore::instance()->manager("MULTIVIEW_WIDGET"));
829 }
830
831
832 static void ParavisMessageOutput(QtMsgType type, const char *msg)
833 {
834   switch(type)
835     {
836   case QtDebugMsg:
837     vtkOutputWindow::GetInstance()->DisplayText(msg);
838     break;
839   case QtWarningMsg:
840     vtkOutputWindow::GetInstance()->DisplayErrorText(msg);
841     break;
842   case QtCriticalMsg:
843     vtkOutputWindow::GetInstance()->DisplayErrorText(msg);
844     break;
845   case QtFatalMsg:
846     vtkOutputWindow::GetInstance()->DisplayErrorText(msg);
847     break;
848     }
849 }
850
851
852
853 /*!
854   \brief Activate module.
855   \param study current study
856   \return \c true if activaion is done successfully or 0 to prevent
857   activation on error
858 */
859 bool PVGUI_Module::activateModule( SUIT_Study* study )
860 {
861   myOldMsgHandler = qInstallMsgHandler(ParavisMessageOutput);
862   
863   SUIT_ExceptionHandler::addCleanUpRoutine( paravisCleanUp );
864
865   storeCommonWindowsState();
866
867   bool isDone = SalomeApp_Module::activateModule( study );
868   if ( !isDone ) return false;
869
870   showView( true );
871   if ( mySourcesMenuId != -1 ) menuMgr()->show(mySourcesMenuId);
872   if ( myFiltersMenuId != -1 ) menuMgr()->show(myFiltersMenuId);
873   if ( myFiltersMenuId != -1 ) menuMgr()->show(myMacrosMenuId);
874   if ( myFiltersMenuId != -1 ) menuMgr()->show(myToolbarsMenuId);
875   setMenuShown( true );
876   setToolShown( true );
877
878   restoreDockWidgetsState();
879
880   QMenu* aMenu = menuMgr()->findMenu( myRecentMenuId );
881   if(aMenu) {
882     QList<QAction*> anActns = aMenu->actions();
883     for (int i = 0; i < anActns.size(); ++i) {
884       QAction* a = anActns.at(i);
885       if(a)
886         a->setVisible(true);
887     }
888   }
889
890   QList<QMenu*>::iterator it;
891   for (it = myMenus.begin(); it != myMenus.end(); ++it) {
892     QAction* a = (*it)->menuAction();
893     if(a)
894       a->setVisible(true);
895   }
896
897   if ( myRecentMenuId != -1 ) menuMgr()->show(myRecentMenuId);
898
899   ClientFindOrCreateParavisComponent(PARAVIS::GetCStudy(this));
900
901   return isDone;
902 }
903
904
905 /*!
906   \brief Deactivate module.
907   \param study current study
908   \return \c true if deactivaion is done successfully or 0 to prevent
909   deactivation on error
910 */
911 bool PVGUI_Module::deactivateModule( SUIT_Study* study )
912 {
913   QMenu* aMenu = menuMgr()->findMenu( myRecentMenuId );
914   if(aMenu) {
915     QList<QAction*> anActns = aMenu->actions();
916     for (int i = 0; i < anActns.size(); ++i) {
917       QAction* a = anActns.at(i);
918       if(a)
919         a->setVisible(false);
920     }
921   }
922
923   QList<QMenu*>::iterator it;
924   for (it = myMenus.begin(); it != myMenus.end(); ++it) {
925     QAction* a = (*it)->menuAction();
926     if(a)
927       a->setVisible(false);
928   }
929
930   QList<QDockWidget*> aStreamingViews = application()->desktop()->findChildren<QDockWidget*>("pqStreamingControls");
931   foreach(QDockWidget* aView, aStreamingViews) {
932     if (!myDockWidgets.contains(aView))
933       myDockWidgets[aView] = aView->isVisible();
934   }
935
936   /*if (pqImplementation::helpWindow) {
937     pqImplementation::helpWindow->hide();
938     }*/
939   showView( false );
940   // hide menus
941   menuMgr()->hide(myRecentMenuId);
942   menuMgr()->hide(mySourcesMenuId);
943   menuMgr()->hide(myFiltersMenuId);
944   menuMgr()->hide(myMacrosMenuId);
945   menuMgr()->hide(myToolbarsMenuId);
946   setMenuShown( false );
947   setToolShown( false );
948
949
950   saveDockWidgetsState();
951
952   SUIT_ExceptionHandler::removeCleanUpRoutine( paravisCleanUp );
953
954   if (myOldMsgHandler)
955     qInstallMsgHandler(myOldMsgHandler);
956
957   restoreCommonWindowsState();
958   
959   return SalomeApp_Module::deactivateModule( study );
960 }
961
962
963 /*!
964   \brief Called when application is closed.
965
966   Process finalize application functionality from ParaView in order to save server settings
967   and nullify application pointer if the application is being closed.
968
969   \param theApp application
970 */
971 void PVGUI_Module::onApplicationClosed( SUIT_Application* theApp )
972 {
973   pqApplicationCore::instance()->settings()->sync();
974   int aAppsNb = SUIT_Session::session()->applications().size();
975   if (aAppsNb == 1) {
976     deleteTemporaryFiles();
977     MyCoreApp->deleteLater();
978   }
979   CAM_Module::onApplicationClosed(theApp);
980 }
981
982
983 /*!
984   \brief Called when study is closed.
985
986   Removes data model from the \a study.
987
988   \param study study being closed
989 */
990 void PVGUI_Module::studyClosed(SUIT_Study* study)
991 {
992   clearParaviewState();
993
994   SalomeApp_Module::studyClosed(study);
995 }
996
997 /*!
998   \brief Called when study is opened.
999 */
1000 void PVGUI_Module::onModelOpened()
1001 {
1002   _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
1003   if(!studyDS) {
1004     return;
1005   }
1006   
1007   _PTR(SComponent) paravisComp = 
1008     studyDS->FindComponent(PARAVIS::GetParavisGen(this)->ComponentDataType());
1009   if(!paravisComp) {
1010     return;
1011   }
1012
1013   _PTR(ChildIterator) anIter(studyDS->NewChildIterator(paravisComp));
1014   for (; anIter->More(); anIter->Next()) {
1015     _PTR(SObject) aSObj = anIter->Value();
1016     _PTR(GenericAttribute) anAttr;
1017     if (!aSObj->FindAttribute(anAttr, "AttributeLocalID")) {
1018       continue;
1019     }
1020     _PTR(AttributeLocalID) anID(anAttr);
1021     if (anID->Value() == PVSTATEID) {
1022       myStateCounter++;
1023     }
1024   }
1025 }
1026
1027 /*!
1028   \brief Returns IOR of current engine
1029 */
1030 QString PVGUI_Module::engineIOR() const
1031 {
1032   CORBA::String_var anIOR = PARAVIS::GetParavisGen(this)->GetIOR();
1033   return QString(anIOR.in());
1034 }
1035
1036
1037 /*!
1038   \brief Open file of format supported by ParaView
1039 */
1040 void PVGUI_Module::openFile(const char* theName)
1041 {
1042   QStringList aFiles;
1043   aFiles<<theName;
1044   pqLoadDataReaction::loadData(aFiles);
1045 }
1046
1047 /**!
1048  * Start trace invoking the newly introduced C++ API (PV 4.2)
1049  * (inspired from pqTraceReaction::start())
1050  */
1051 void PVGUI_Module::startTrace()
1052 {
1053   vtkSMSessionProxyManager* pxm = pqActiveObjects::instance().activeServer()->proxyManager();
1054
1055   vtkSmartPointer<vtkSMProxy> proxy;
1056   proxy.TakeReference(pxm->NewProxy("pythontracing", "PythonTraceOptions"));
1057   if (proxy)
1058     {
1059       vtkNew<vtkSMParaViewPipelineController> controller;
1060       controller->InitializeProxy(proxy);
1061     }
1062   vtkSMTrace* trace = vtkSMTrace::StartTrace();
1063   if (proxy)
1064     {
1065       // Set manually the properties entered via the dialog box poping-up when requiring
1066       // a trace start in PV4.2 (trace options)
1067       trace->SetPropertiesToTraceOnCreate(vtkSMTrace::RECORD_USER_MODIFIED_PROPERTIES);
1068       trace->SetFullyTraceSupplementalProxies(false);
1069     }
1070 }
1071
1072 void PVGUI_Module::stopTrace()
1073 {
1074   vtkSMTrace::StopTrace();
1075 }
1076
1077 void PVGUI_Module::executeScript(const char *script)
1078 {
1079 #ifndef WNT
1080   pqPythonManager* manager = qobject_cast<pqPythonManager*>(
1081                              pqApplicationCore::instance()->manager("PYTHON_MANAGER"));
1082   if (manager)  {
1083     pqPythonDialog* pyDiag = manager->pythonShellDialog();
1084     if (pyDiag) {
1085       pyDiag->runString(script);  
1086       }
1087     }
1088 #endif
1089 }
1090
1091 ///**
1092 // *  Debug function printing out the given interpreter's execution context
1093 // */
1094 //void printInterpContext(PyInterp_Interp * interp )
1095 //{
1096 //  // Extract __smtraceString from interpreter's context
1097 //  const PyObject* ctxt = interp->getExecutionContext();
1098 //
1099 //  PyObject* lst = PyDict_Keys((PyObject *)ctxt);
1100 //  Py_ssize_t siz = PyList_GET_SIZE(lst);
1101 //  for (Py_ssize_t i = 0; i < siz; i++)
1102 //    {
1103 //      PyObject * elem = PyList_GetItem(lst, i);
1104 //      if (PyString_Check(elem))
1105 //        {
1106 //          std::cout << "At pos:" << i << ", " << PyString_AsString(elem) << std::endl;
1107 //        }
1108 //      else
1109 //        std::cout << "At pos:" << i << ", not a string!" << std::endl;
1110 //    }
1111 //  Py_XDECREF(lst);
1112 //}
1113
1114 /*!
1115   \brief Returns trace string
1116 */
1117 static const QString MYReplaceStr("paraview.simple");
1118 static const QString MYReplaceImportStr("except: from pvsimple import *");
1119 QString PVGUI_Module::getTraceString()
1120 {
1121   vtkSMTrace *tracer = vtkSMTrace::GetActiveTracer();
1122   if (!tracer) // trace is not started
1123     return QString("");
1124
1125   QString traceString(tracer->GetCurrentTrace());
1126
1127   // Replace import "paraview.simple" by "pvsimple"
1128   if ((!traceString.isNull()) && traceString.length() != 0) {
1129     int aPos = traceString.indexOf(MYReplaceStr);
1130     while (aPos != -1) {
1131       traceString = traceString.replace(aPos, MYReplaceStr.length(), "pvsimple");
1132       aPos = traceString.indexOf(MYReplaceStr, aPos);
1133     }
1134     int aImportPos = traceString.indexOf(MYReplaceImportStr);
1135     if(aImportPos != -1)
1136       {
1137       traceString = traceString.replace(aImportPos, MYReplaceImportStr.length(), "except:\n  import pvsimple\n  from pvsimple import *");
1138       }
1139   }
1140
1141   return traceString;
1142 }
1143
1144 /*!
1145   \brief Saves trace string to disk file
1146 */
1147 void PVGUI_Module::saveTrace(const char* theName)
1148 {
1149   QFile file(theName);
1150   if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
1151     MESSAGE( "Could not open file:" << theName );
1152     return;
1153   }
1154   QTextStream out(&file);
1155   out << getTraceString();
1156   file.close();
1157 }
1158
1159 /*!
1160   \brief Saves ParaView state to a disk file
1161 */
1162 void PVGUI_Module::saveParaviewState(const char* theFileName)
1163 {
1164   pqApplicationCore::instance()->saveState(theFileName);
1165 }
1166
1167 /*!
1168   \brief Delete all objects for Paraview Pipeline Browser
1169 */
1170 void PVGUI_Module::clearParaviewState()
1171 {
1172   QAction* deleteAllAction = action(DeleteAllId);
1173   if (deleteAllAction) {
1174     deleteAllAction->activate(QAction::Trigger);
1175   }
1176 }
1177
1178 /*!
1179   \brief Restores ParaView state from a disk file
1180
1181   If toClear == true, the current ojects will be deleted
1182 */
1183 void PVGUI_Module::loadParaviewState(const char* theFileName)
1184 {
1185   pqApplicationCore::instance()->loadState(theFileName, getActiveServer());
1186 }
1187
1188 /*!
1189   \brief Imports MED data from VISU module by data entry
1190 */
1191 void PVGUI_Module::onImportFromVisu(QString theEntry)
1192 {
1193 #ifdef WITH_VISU
1194   SUIT_OverrideCursor aWaitCursor;
1195
1196   // get active study
1197   SalomeApp_Study* activeStudy = dynamic_cast<SalomeApp_Study*>(application()->activeStudy());
1198   if(!activeStudy) return;
1199
1200   // get SALOMEDS client study
1201   _PTR(Study) aStudy = activeStudy->studyDS();
1202   if(!aStudy) return;
1203
1204   // find VISU component in a study
1205   _PTR(SComponent) aVisuComp = aStudy->FindComponent( "VISU" );
1206   if(!aVisuComp) return;
1207
1208   // get SObject client by entry
1209   _PTR(SObject) aSObj = aStudy->FindObjectID(qPrintable(theEntry));
1210   if (!aSObj) return;
1211
1212   // get CORBA SObject
1213   SALOMEDS_SObject* aSObject = _CAST(SObject, aSObj);
1214   if ( !aSObject ) return;
1215
1216   // load VISU engine
1217   SALOME_NamingService* aNamingService = SalomeApp_Application::namingService();
1218   SALOME_LifeCycleCORBA aLCC(aNamingService);
1219
1220   Engines::EngineComponent_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","VISU");
1221   VISU::VISU_Gen_var aVISU = VISU::VISU_Gen::_narrow(aComponent);
1222   if(CORBA::is_nil(aVISU)) return;
1223
1224   _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
1225   aStudyBuilder->LoadWith( aVisuComp, SalomeApp_Application::orb()->object_to_string(aVISU) );
1226
1227   // get VISU result object
1228   CORBA::Object_var aResultObject = aSObject->GetObject();
1229   if (CORBA::is_nil(aResultObject)) return;
1230   VISU::Result_var aResult = VISU::Result::_narrow( aResultObject );
1231   if (CORBA::is_nil(aResult)) return;
1232
1233   // export VISU result to the MED file
1234   std::string aTmpDir = SALOMEDS_Tool::GetTmpDir();
1235   std::string aFileName = aSObject->GetName();
1236   std::string aFilePath = aTmpDir + aFileName;
1237
1238   if (aResult->ExportMED(aFilePath.c_str())) {
1239     openFile(aFilePath.c_str());
1240     myTemporaryFiles.append(QString(aFilePath.c_str()));
1241   }
1242 #else
1243   MESSAGE("Visu module is not found.");
1244 #endif
1245 }
1246
1247 /*!
1248   \brief Deletes temporary files created during import operation from VISU
1249 */
1250 void PVGUI_Module::deleteTemporaryFiles()
1251 {
1252   foreach(QString aFile, myTemporaryFiles) {
1253     if (QFile::exists(aFile)) {
1254       QFile::remove(aFile);
1255     }
1256   }
1257 }
1258
1259
1260 /*!
1261   \brief Returns current active ParaView server
1262 */
1263 pqServer* PVGUI_Module::getActiveServer()
1264 {
1265   return pqApplicationCore::instance()->getActiveServer();
1266 }
1267
1268
1269 /*!
1270   \brief Creates PARAVIS preference pane 
1271 */
1272 void PVGUI_Module::createPreferences()
1273 {
1274   // Paraview settings tab
1275   int aParaViewSettingsTab = addPreference( tr( "TIT_PVIEWSETTINGS" ) );
1276   int aPanel = addPreference(QString(), aParaViewSettingsTab, LightApp_Preferences::UserDefined, "PARAVIS", "");
1277   setPreferenceProperty(aPanel, "content", (qint64)(new PVGUI_ParaViewSettingsPane()));
1278
1279   // Paravis settings tab
1280   int aParaVisSettingsTab = addPreference( tr( "TIT_PVISSETTINGS" ) );
1281   addPreference( tr( "PREF_STOP_TRACE" ), aParaVisSettingsTab, LightApp_Preferences::Bool, "PARAVIS", "stop_trace");
1282
1283   int aSaveType = addPreference(tr( "PREF_SAVE_TYPE_LBL" ), aParaVisSettingsTab,
1284                                 LightApp_Preferences::Selector,
1285                                 "PARAVIS", "savestate_type");
1286   QList<QVariant> aIndices;
1287   QStringList aStrings;
1288   aIndices<<0<<1<<2;
1289   aStrings<<tr("PREF_SAVE_TYPE_0");
1290   aStrings<<tr("PREF_SAVE_TYPE_1");
1291   aStrings<<tr("PREF_SAVE_TYPE_2");
1292   setPreferenceProperty(aSaveType, "strings", aStrings);
1293   setPreferenceProperty(aSaveType, "indexes", aIndices);
1294
1295   //rnv: imp 21712: [CEA 581] Preference to display legend by default 
1296   int aDispColoreLegend = addPreference( tr( "PREF_SHOW_COLOR_LEGEND" ), aParaVisSettingsTab,
1297                                         LightApp_Preferences::Bool, "PARAVIS", "show_color_legend");
1298 }
1299
1300 /*!
1301   \brief Creates ParaViS context menu popup
1302 */
1303 void PVGUI_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
1304 {
1305   SalomeApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
1306   
1307   // Check if we are in Object Browser
1308   SUIT_DataBrowser* ob = getApp()->objectBrowser();
1309   bool isOBClient = (ob && theClient == ob->popupClientType());
1310   if (!isOBClient) {
1311     return;
1312   }
1313
1314   // Get list of selected objects
1315   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1316   SALOME_ListIO aListIO;
1317   aSelectionMgr->selectedObjects(aListIO);
1318   if (aListIO.Extent() == 1 && aListIO.First()->hasEntry()) {
1319     QString entry = QString(aListIO.First()->getEntry());
1320     
1321     // Get active study
1322     SalomeApp_Study* activeStudy = 
1323       dynamic_cast<SalomeApp_Study*>(getApp()->activeStudy());
1324     if(!activeStudy) {
1325       return;
1326     }
1327
1328     // Get SALOMEDS client study 
1329     _PTR(Study) studyDS = activeStudy->studyDS();
1330     if(!studyDS) {
1331       return;
1332     }
1333
1334     QString paravisDataType(PARAVIS::GetParavisGen(this)->ComponentDataType());
1335     if(activeStudy && activeStudy->isComponent(entry) && 
1336        activeStudy->componentDataType(entry) == paravisDataType) {
1337       // ParaViS module object
1338       theMenu->addSeparator();
1339       theMenu->addAction(action(SaveStatePopupId));
1340     }
1341     else {
1342       // Try to get state object
1343       _PTR(SObject) stateSObj = 
1344           studyDS->FindObjectID(entry.toLatin1().constData());
1345       if (!stateSObj) {
1346           return;
1347       }
1348       
1349       // Check local id
1350       _PTR(GenericAttribute) anAttr;
1351       if (!stateSObj->FindAttribute(anAttr, "AttributeLocalID")) {
1352           return;
1353       }
1354
1355       _PTR(AttributeLocalID) anID(anAttr);
1356       
1357       if (anID->Value() == PVSTATEID) {
1358         // Paraview state object
1359         theMenu->addSeparator();
1360         theMenu->addAction(action(AddStatePopupId));
1361         theMenu->addAction(action(CleanAndAddStatePopupId));
1362         theMenu->addSeparator();
1363         theMenu->addAction(action(ParaVisRenameId));
1364         theMenu->addAction(action(ParaVisDeleteId));
1365       }
1366     }
1367   }
1368 }
1369
1370 /*!
1371   \brief. Show ParaView python trace.
1372 */
1373 void PVGUI_Module::onShowTrace()
1374 {
1375   if (!myTraceWindow) {
1376     myTraceWindow = new pqPythonScriptEditor(getApp()->desktop());
1377   }
1378   myTraceWindow->setText(getTraceString());
1379   myTraceWindow->show();
1380   myTraceWindow->raise();
1381   myTraceWindow->activateWindow();
1382 }
1383
1384
1385 /*!
1386   \brief. Re-initialize ParaView python trace.
1387 */
1388 void PVGUI_Module::onRestartTrace()
1389 {
1390   stopTrace();
1391   startTrace();
1392 }
1393
1394 /*!
1395   \brief Show ParaView view.
1396 */
1397 void PVGUI_Module::onNewParaViewWindow()
1398 {
1399   showView(true);
1400 }
1401
1402 /*!
1403   \brief Save state under the module root object.
1404 */
1405 void PVGUI_Module::onSaveMultiState()
1406 {
1407   // Create state study object
1408   
1409   // Get SALOMEDS client study
1410   _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
1411   if(!studyDS) {
1412     return;
1413   }
1414   
1415   _PTR(SComponent) paravisComp = 
1416     studyDS->FindComponent(PARAVIS::GetParavisGen(this)->ComponentDataType());
1417   if(!paravisComp) {
1418     return;
1419   }
1420
1421   // Unlock the study if it is locked
1422   bool isLocked = studyDS->GetProperties()->IsLocked();
1423   if (isLocked) {
1424     studyDS->GetProperties()->SetLocked(false);
1425   }
1426   
1427   QString stateName = tr("SAVED_PARAVIEW_STATE_NAME") + 
1428     QString::number(myStateCounter + 1);
1429
1430   _PTR(StudyBuilder) studyBuilder = studyDS->NewBuilder();
1431   _PTR(SObject) newSObj = studyBuilder->NewObject(paravisComp);
1432
1433   // Set name
1434   _PTR(GenericAttribute) anAttr;
1435   anAttr = studyBuilder->FindOrCreateAttribute(newSObj, "AttributeName");
1436   _PTR(AttributeName) nameAttr(anAttr);
1437   
1438   nameAttr->SetValue(stateName.toLatin1().constData());
1439
1440   // Set local id
1441   anAttr = studyBuilder->FindOrCreateAttribute(newSObj, "AttributeLocalID");
1442   _PTR(AttributeLocalID) localIdAttr(anAttr);
1443   
1444   localIdAttr->SetValue(PVSTATEID);
1445
1446   // Set file name
1447   QString stateEntry = QString::fromStdString(newSObj->GetID());
1448  
1449   // File name for state saving
1450   QString tmpDir = QString::fromStdString(SALOMEDS_Tool::GetTmpDir());
1451   QString fileName = QString("%1_paravisstate:%2").arg(tmpDir, 
1452                                                        stateEntry);
1453
1454   anAttr = studyBuilder->FindOrCreateAttribute(newSObj, "AttributeString");
1455   _PTR(AttributeString) stringAttr(anAttr);
1456   
1457   stringAttr->SetValue(fileName.toLatin1().constData());
1458
1459   // Lock the study back if necessary
1460   if (isLocked) {
1461     studyDS->GetProperties()->SetLocked(true);
1462   }
1463   
1464   // Save state
1465   saveParaviewState(fileName.toLatin1().constData());
1466   myTemporaryFiles.append(fileName);
1467   
1468   // Increment the counter
1469   myStateCounter++;
1470
1471   updateObjBrowser();
1472 }
1473
1474 /*!
1475   \brief Restore the selected state by merging with the current one.
1476 */
1477 void PVGUI_Module::onAddState()
1478 {
1479   loadSelectedState(false);
1480 }
1481
1482 /*!
1483   \brief Clean the current state and restore the selected one.
1484 */
1485 void PVGUI_Module::onCleanAddState()
1486 {
1487   loadSelectedState(true);
1488 }
1489
1490 /*!
1491   \brief Rename the selected object.
1492 */
1493 void PVGUI_Module::onRename()
1494 {
1495   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1496   SALOME_ListIO aListIO;
1497   aSelectionMgr->selectedObjects(aListIO);
1498   
1499   if (aListIO.Extent() == 1 && aListIO.First()->hasEntry()) {
1500     std::string entry = aListIO.First()->getEntry();
1501     
1502     // Get SALOMEDS client study 
1503     _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
1504     if(!studyDS) {
1505       return;
1506     }
1507     
1508     // Unlock the study if it is locked
1509     bool isLocked = studyDS->GetProperties()->IsLocked();
1510     if (isLocked) {
1511       studyDS->GetProperties()->SetLocked(false);
1512     }
1513     
1514     // Rename the selected state object
1515     _PTR(SObject) stateSObj = studyDS->FindObjectID(entry);
1516     if (!stateSObj) {
1517       return;
1518     }
1519     
1520     _PTR(GenericAttribute) anAttr;
1521     if (stateSObj->FindAttribute(anAttr, "AttributeName")) {
1522       _PTR(AttributeName) nameAttr (anAttr);
1523       QString newName = 
1524         LightApp_NameDlg::getName(getApp()->desktop(), nameAttr->Value().c_str());
1525       if (!newName.isEmpty()) {
1526         nameAttr->SetValue(newName.toLatin1().constData());
1527         aListIO.First()->setName(newName.toLatin1().constData());
1528       }
1529     }
1530     
1531     // Lock the study back if necessary
1532     if (isLocked) {
1533       studyDS->GetProperties()->SetLocked(true);
1534     }
1535     
1536     // Update object browser
1537     updateObjBrowser();
1538     
1539   }
1540 }
1541
1542 /*!
1543   \brief Delete the selected objects.
1544 */
1545 void PVGUI_Module::onDelete()
1546 {
1547   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1548   SALOME_ListIO aListIO;
1549   aSelectionMgr->selectedObjects(aListIO);
1550   
1551   if (aListIO.Extent() == 1 && aListIO.First()->hasEntry()) {
1552     std::string entry = aListIO.First()->getEntry();
1553     
1554     // Get SALOMEDS client study 
1555     _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
1556     if(!studyDS) {
1557       return;
1558     }
1559     
1560     // Unlock the study if it is locked
1561     bool isLocked = studyDS->GetProperties()->IsLocked();
1562     if (isLocked) {
1563       studyDS->GetProperties()->SetLocked(false);
1564     }
1565     
1566     // Remove the selected state from the study
1567     _PTR(StudyBuilder) studyBuilder = studyDS->NewBuilder();
1568     _PTR(SObject) stateSObj = studyDS->FindObjectID(entry);
1569     studyBuilder->RemoveObject(stateSObj);
1570     
1571     // Lock the study back if necessary
1572     if (isLocked) {
1573       studyDS->GetProperties()->SetLocked(true);
1574     }
1575     
1576     // Update object browser
1577     updateObjBrowser();
1578   }
1579 }
1580
1581 /*!
1582   \brief Discover help project files from the resources.
1583   \return name of the help file. 
1584 */
1585 QString PVGUI_Module::getHelpFileName() {
1586   QString aPVHome(getenv("PVHOME"));
1587   if (aPVHome.isNull()) {
1588     qWarning("Wariable PVHOME is not defined");
1589     return QString();
1590   }
1591   QChar aSep = QDir::separator();
1592   //PARAVIEW_VERSION from the vtkPVConfig.h file
1593   QString aFileName =  aPVHome + aSep + "share" + aSep + "doc" + aSep + "paraview-"+ PARAVIEW_VERSION + aSep + "paraview.qch";
1594   return aFileName;
1595 }
1596
1597
1598 /*!
1599   \brief Load selected paraview state
1600
1601   If toClear == true, the current state will be cleared
1602 */
1603 void PVGUI_Module::loadSelectedState(bool toClear)
1604 {
1605   QString fileName;
1606
1607   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1608   SALOME_ListIO aListIO;
1609   aSelectionMgr->selectedObjects(aListIO);
1610   
1611   if (aListIO.Extent() == 1 && aListIO.First()->hasEntry()) {
1612     std::string entry = aListIO.First()->getEntry();
1613     
1614     // Get SALOMEDS client study 
1615     _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
1616     if(!studyDS) {
1617       return;
1618     }
1619
1620     // Check local id
1621     _PTR(SObject) stateSObj = studyDS->FindObjectID(entry);
1622     _PTR(GenericAttribute) anAttr;
1623     if (!stateSObj->FindAttribute(anAttr, "AttributeLocalID")) {
1624       return;
1625     }
1626     _PTR(AttributeLocalID) anID(anAttr);
1627     if (!anID->Value() == PVSTATEID) {
1628       return;
1629     }
1630
1631     // Get state file name
1632     if (stateSObj->FindAttribute(anAttr, "AttributeString")) {
1633       _PTR(AttributeString) aStringAttr(anAttr);
1634       QString stringValue(aStringAttr->Value().c_str());
1635
1636       if (QFile::exists(stringValue)) {
1637         fileName = stringValue;
1638       }
1639     }
1640   }
1641   
1642   if (!fileName.isEmpty()) {
1643     if (toClear) {
1644       clearParaviewState();
1645     }
1646
1647     loadParaviewState(fileName.toLatin1().constData());
1648   } 
1649   else {
1650     SUIT_MessageBox::critical(getApp()->desktop(),
1651                               tr("ERR_ERROR"),
1652                               tr("ERR_STATE_CANNOT_BE_RESTORED"));
1653   }
1654 }
1655
1656 void PVGUI_Module::onRepresentationChanged(pqRepresentation*) {
1657
1658
1659   //rnv: to fix the issue "21712: [CEA 581] Preference to display legend by default"
1660   //     find the pqDisplayColorWidget instances and connect the variableChanged SIGNAL on the 
1661   //     onVariableChanged slot of this class. This connection needs to change visibility 
1662   //     of the "Colored Legend" after change the "Color By" array.
1663   QList<pqDisplayColorWidget*> aWidget = getApp()->desktop()->findChildren<pqDisplayColorWidget*>();
1664   
1665   for (int i = 0; i < aWidget.size() ; i++ ) {
1666     if( aWidget[i] ) {
1667       connect( aWidget[i], SIGNAL ( variableChanged ( pqVariableType, const QString ) ), 
1668               this, SLOT(onVariableChanged( pqVariableType, const QString) ), Qt::UniqueConnection );
1669     }    
1670   }
1671 }
1672
1673
1674 /*!
1675   \fn CAM_Module* createModule();
1676   \brief Export module instance (factory function).
1677   \return new created instance of the module
1678 */
1679
1680 #ifdef WNT
1681 #define PVGUI_EXPORT __declspec(dllexport)
1682 #else   // WNT
1683 #define PVGUI_EXPORT
1684 #endif  // WNT
1685
1686 extern "C" {
1687
1688   bool flag = false;
1689   PVGUI_EXPORT CAM_Module* createModule() {
1690     if(!flag) {
1691         vtkEDFHelperInit();
1692         flag = true;
1693     }      
1694     return new PVGUI_Module();
1695   }
1696   
1697   PVGUI_EXPORT char* getModuleVersion() {
1698     return (char*)PARAVIS_VERSION_STR;
1699   }
1700 }