]> SALOME platform Git repositories - modules/paravis.git/blob - src/PVGUI/PVGUI_Module.cxx
Salome HOME
1. Changing PVGUI_Module design
[modules/paravis.git] / src / PVGUI / PVGUI_Module.cxx
1 // LIGHT : sample (no-corba-engine) SALOME module
2 //
3 // Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : PVGUI_Module.cxx
23 // Author : Julia DOROVSKIKH
24 //
25
26 #include "PVGUI_Module.h"
27 #include "PVGUI_ProcessModuleHelper.h"
28 #include "PVGUI_ViewModel.h"
29 #include "PVGUI_ViewManager.h"
30 #include "PVGUI_ViewWindow.h"
31
32 #include <SUIT_MessageBox.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_Session.h>
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 #include <QApplication>
39 #include <QInputDialog>
40 #include <QStringList>
41 #include <QMenu>
42 #include <QIcon>
43 #include <QString>
44
45 #include <pqOptions.h>
46 #include <pqApplicationCore.h>
47 #include <pqActiveServer.h>
48 #include <pqMainWindowCore.h>
49 #include <pqObjectBuilder.h>
50 #include <pqServer.h>
51 #include <pqServerManagerModel.h>
52 #include <pqServerResource.h>
53 #include <pqViewManager.h>
54 #include <vtkPVMain.h>
55 #include <vtkProcessModule.h>
56
57 /*
58  * Make sure all the kits register their classes with vtkInstantiator.
59  * Since ParaView uses Tcl wrapping, all of VTK is already compiled in
60  * anyway.  The instantiators will add no more code for the linker to
61  * collect.
62  */
63
64 #include <vtkCommonInstantiator.h>
65 #include <vtkFilteringInstantiator.h>
66 #include <vtkGenericFilteringInstantiator.h>
67 #include <vtkIOInstantiator.h>
68 #include <vtkImagingInstantiator.h>
69 #include <vtkInfovisInstantiator.h>
70 #include <vtkGraphicsInstantiator.h>
71
72 #include <vtkRenderingInstantiator.h>
73 #include <vtkVolumeRenderingInstantiator.h>
74 #include <vtkHybridInstantiator.h>
75 #include <vtkParallelInstantiator.h>
76
77 #include <vtkPVServerCommonInstantiator.h>
78 #include <vtkPVFiltersInstantiator.h>
79 #include <vtkPVServerManagerInstantiator.h>
80 #include <vtkClientServerInterpreter.h>
81
82
83 //----------------------------------------------------------------------------
84 // ClientServer wrapper initialization functions.
85 // Taken from ParaView sources (file pqMain.cxx)
86 extern "C" void vtkCommonCS_Initialize(vtkClientServerInterpreter*);
87 extern "C" void vtkFilteringCS_Initialize(vtkClientServerInterpreter*);
88 extern "C" void vtkGenericFilteringCS_Initialize(vtkClientServerInterpreter*);
89 extern "C" void vtkImagingCS_Initialize(vtkClientServerInterpreter*);
90 extern "C" void vtkInfovisCS_Initialize(vtkClientServerInterpreter*);
91 extern "C" void vtkGraphicsCS_Initialize(vtkClientServerInterpreter*);
92 extern "C" void vtkIOCS_Initialize(vtkClientServerInterpreter*);
93 extern "C" void vtkRenderingCS_Initialize(vtkClientServerInterpreter*);
94 extern "C" void vtkVolumeRenderingCS_Initialize(vtkClientServerInterpreter*);
95 extern "C" void vtkHybridCS_Initialize(vtkClientServerInterpreter*);
96 extern "C" void vtkWidgetsCS_Initialize(vtkClientServerInterpreter*);
97 extern "C" void vtkParallelCS_Initialize(vtkClientServerInterpreter*);
98 extern "C" void vtkPVServerCommonCS_Initialize(vtkClientServerInterpreter*);
99 extern "C" void vtkPVFiltersCS_Initialize(vtkClientServerInterpreter*);
100 extern "C" void vtkXdmfCS_Initialize(vtkClientServerInterpreter *);
101
102 //----------------------------------------------------------------------------
103 void ParaViewInitializeInterpreter(vtkProcessModule* pm)
104 {
105   // Initialize built-in wrapper modules.
106   vtkCommonCS_Initialize(pm->GetInterpreter());
107   vtkFilteringCS_Initialize(pm->GetInterpreter());
108   vtkGenericFilteringCS_Initialize(pm->GetInterpreter());
109   vtkImagingCS_Initialize(pm->GetInterpreter());
110   vtkInfovisCS_Initialize(pm->GetInterpreter());
111   vtkGraphicsCS_Initialize(pm->GetInterpreter());
112   vtkIOCS_Initialize(pm->GetInterpreter());
113   vtkRenderingCS_Initialize(pm->GetInterpreter());
114   vtkVolumeRenderingCS_Initialize(pm->GetInterpreter());
115   vtkHybridCS_Initialize(pm->GetInterpreter());
116   vtkWidgetsCS_Initialize(pm->GetInterpreter());
117   vtkParallelCS_Initialize(pm->GetInterpreter());
118   vtkPVServerCommonCS_Initialize(pm->GetInterpreter());
119   vtkPVFiltersCS_Initialize(pm->GetInterpreter());
120   vtkXdmfCS_Initialize(pm->GetInterpreter());
121 }
122
123 //////////////////////////////////////////////////////////////////////////////
124 // PVGUI_Module::pqImplementation
125
126 class PVGUI_Module::pqImplementation
127 {
128 public:
129   pqImplementation(QWidget* parent) :
130     //AssistantClient(0),
131     Core(parent)//,
132     //RecentFilesMenu(0),
133     //ViewMenu(0),
134     //ToolbarsMenu(0)
135   {
136   }
137
138   ~pqImplementation()
139   {
140     //delete this->ViewMenu;
141     //delete this->ToolbarsMenu;
142     //if(this->AssistantClient)
143     //  {
144     //  this->AssistantClient->closeAssistant();
145     //  delete this->AssistantClient;
146     //  }
147   }
148
149   //QPointer<QAssistantClient> AssistantClient;
150   //Ui::MainWindow UI;
151   pqMainWindowCore Core;
152   //pqRecentFilesMenu* RecentFilesMenu;
153   //pqViewMenu* ViewMenu;
154   //pqViewMenu* ToolbarsMenu;
155   //QLineEdit* CurrentTimeWidget;
156   //QSpinBox* CurrentTimeIndexWidget;
157   QPointer<pqServer> ActiveServer;
158   QString DocumentationDir;
159
160   static vtkPVMain* myPVMain;
161   static pqOptions* myPVOptions;
162   static PVGUI_ProcessModuleHelper* myPVHelper;
163 };
164
165
166 vtkPVMain*                 PVGUI_Module::pqImplementation::myPVMain = 0;
167 pqOptions*                 PVGUI_Module::pqImplementation::myPVOptions = 0;
168 PVGUI_ProcessModuleHelper* PVGUI_Module::pqImplementation::myPVHelper = 0;
169
170 /*!
171   \class PVGUI_Module
172   \brief Implementation of light (no-CORBA-engine) 
173          SALOME module wrapping ParaView GUI.
174 */
175
176 /*!
177   \brief Constructor. Sets the default name for the module.
178 */
179 PVGUI_Module::PVGUI_Module()
180   : LightApp_Module( "PARAVIS" ),
181     Implementation( 0 )
182 {
183 }
184
185 /*!
186   \brief Destructor.
187 */
188 PVGUI_Module::~PVGUI_Module()
189 {
190 }
191
192 /*!
193   \brief Initialize module. Creates menus, prepares context menu, etc.
194   \param app application instance
195 */
196 void PVGUI_Module::initialize( CAM_Application* app )
197 {
198   LightApp_Module::initialize( app );
199
200   /*
201   int i = 1;
202   while( i ){
203     i = i;
204   }
205   */
206
207   pvInit();
208
209   /*
210   createAction( lgLoadFile, tr( "TOP_LOAD_FILE" ), QIcon(), tr( "MEN_LOAD_FILE" ),
211                 tr( "STB_LOAD_FILE" ), 0, desk, false, this, SLOT( onLoadFile() ) );
212   createAction( lgDisplayLine, tr( "TOP_DISPLAY_LINE" ), QIcon(), tr( "MEN_DISPLAY_LINE" ),
213                 tr( "STB_DISPLAY_LINE" ), 0, desk, false, this, SLOT( onDisplayLine() ) );
214   createAction( lgEraseLine, tr( "TOP_ERASE_LINE" ), QIcon(), tr( "MEN_ERASE_LINE" ),
215                 tr( "STB_ERASE_LINE" ), 0, desk, false, this, SLOT( onEraseLine() ) );
216   createAction( lgSaveFile, tr( "TOP_SAVE_FILE" ), QIcon(), tr( "MEN_SAVE_FILE" ),
217                 tr( "STB_SAVE_FILE" ), 0, desk, false, this, SLOT( onSaveFile() ) );
218   createAction( lgEditLine, tr( "TOP_EDIT_LINE" ), QIcon(), tr( "MEN_EDIT_LINE" ),
219                 tr( "STB_EDIT_LINE" ), 0, desk, false, this, SLOT( onEditLine() ) );
220   createAction( lgAddLine,  tr( "TOP_ADD_LINE" ),  QIcon(), tr( "MEN_ADD_LINE" ),
221                 tr( "STB_ADD_LINE" ),  0, desk, false, this, SLOT( onAddLine() ) );
222   createAction( lgDelLine,  tr( "TOP_DEL_LINE" ),  QIcon(), tr( "MEN_DEL_LINE" ),
223                 tr( "STB_DEL_LINE" ),  0, desk, false, this, SLOT( onDelLine() ) );
224   createAction( lgClear,    tr( "TOP_CLEAR_ALL" ), QIcon(), tr( "MEN_CLEAR_ALL" ),
225                 tr( "STB_CLEAR_ALL" ), 0, desk, false, this, SLOT( onClear() ) );
226
227   int aFileMnu = createMenu( tr( "MEN_FILE" ), -1, -1 );
228   createMenu( separator(), aFileMnu, -1, 10 );
229   createMenu( lgLoadFile,  aFileMnu, 10 );
230   createMenu( lgSaveFile,  aFileMnu, 10 );
231
232   int aLightMnu = createMenu( tr( "MEN_LIGHT" ), -1, -1, 50 );
233   createMenu( lgAddLine,      aLightMnu, 10 );
234   createMenu( lgEditLine,     aLightMnu, 10 );
235   createMenu( lgDelLine,      aLightMnu, 10 );
236   createMenu( separator(),    aLightMnu, -1, 10 );
237   createMenu( lgClear,        aLightMnu, 10 );
238
239   QString rule = "(client='ObjectBrowser' or client='OCCViewer') and selcount=1 and type='TextLine' and !empty";
240
241   popupMgr()->insert ( action( lgDisplayLine ), -1, 0 );
242   popupMgr()->setRule( action( lgDisplayLine ), rule + " and !visible"  );
243
244   popupMgr()->insert ( action( lgEraseLine ), -1, 0 );
245   popupMgr()->setRule( action( lgEraseLine ), rule + " and activeView='OCCViewer' and visible"  );
246
247   rule = "client='ObjectBrowser' and selcount=1 and type='TextLine'";
248
249   popupMgr()->insert ( action( lgEditLine ), -1, 0 );
250   popupMgr()->setRule( action( lgEditLine ), rule  );
251
252   popupMgr()->insert ( action( lgAddLine ),  -1, 0 );
253   popupMgr()->setRule( action( lgAddLine ),  rule );
254
255   popupMgr()->insert ( separator(),          -1, 0 );
256
257   popupMgr()->insert ( action( lgDelLine ),  -1, 0 );
258   popupMgr()->setRule( action( lgDelLine ),  rule );
259
260   rule = "client='ObjectBrowser'";
261
262   popupMgr()->insert ( action( lgClear ),    -1, 0 );
263   popupMgr()->setRule( action( lgClear ),    rule );*/
264 }
265
266 /*!
267   \brief Get list of compliant dockable GUI elements
268   \param m map to be filled in ("type":"default_position")
269 */
270 void PVGUI_Module::windows( QMap<int, int>& m ) const
271 {
272   m.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
273   // TODO: creation of Python console leads to SIGSEGV on Python initialization...
274   //m.insert( LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
275   // ParaView diagnostic output redirected here
276   m.insert( LightApp_Application::WT_LogWindow, Qt::BottomDockWidgetArea );
277 }
278
279 /*!
280   \brief Create custom popup menu selection object.
281   \return new selected object
282 */
283 /*LightApp_Selection* PVGUI_Module::createSelection() const
284 {
285   return new PVGUI_Selection();
286 }*/
287
288 /*!
289   \brief Create data model.
290   \return module specific data model
291 */
292 /*CAM_DataModel* PVGUI_Module::createDataModel()
293 {
294   return new PVGUI_DataModel( this );
295 }*/
296
297 /*!
298   \brief Static method, performs initialization of ParaView session.
299   \return \c true if ParaView has been initialized successfully, otherwise false
300 */
301 bool PVGUI_Module::pvInit()
302 {
303   if ( !pqImplementation::myPVMain ){
304     // Obtain command-line arguments
305     int argc = 0;
306     QStringList args = QApplication::arguments();
307     char** argv = new char*[args.size()];
308     for ( QStringList::const_iterator it = args.begin(); argc < 1 && it != args.end(); it++, argc++ )
309       argv[argc] = strdup( (*it).toLatin1().constData() );
310
311     vtkPVMain::SetInitializeMPI(0);  // pvClient never runs with MPI.
312     vtkPVMain::Initialize(&argc, &argv); // Perform any initializations.
313
314     // TODO: Set plugin dir from preferences
315     //QApplication::setLibraryPaths(QStringList(dir.absolutePath()));
316
317     pqImplementation::myPVMain = vtkPVMain::New();
318     if ( !pqImplementation::myPVOptions )
319       pqImplementation::myPVOptions = pqOptions::New();
320     if ( !pqImplementation::myPVHelper )
321       pqImplementation::myPVHelper = PVGUI_ProcessModuleHelper::New();
322
323     pqImplementation::myPVOptions->SetProcessType(vtkPVOptions::PVCLIENT);
324
325     // This creates the Process Module and initializes it.
326     int ret = pqImplementation::myPVMain->Initialize(pqImplementation::myPVOptions, 
327                                                      pqImplementation::myPVHelper, 
328                                                      ParaViewInitializeInterpreter,
329                                                      argc, argv);
330     if (!ret){
331       // Tell process module that we support Multiple connections.
332       // This must be set before starting the event loop.
333       vtkProcessModule::GetProcessModule()->SupportMultipleConnectionsOn();
334       ret = pqImplementation::myPVHelper->Run(pqImplementation::myPVOptions);
335     }
336
337     delete[] argv;
338     return !ret;
339   }
340   
341   return true;
342 }
343  
344 /*!
345   \brief Static method, cleans up ParaView session at application exit.
346 */
347 void PVGUI_Module::pvShutdown()
348 {
349   // TODO...
350 }  
351
352 /*!
353   \brief Shows (toShow = true) or hides ParaView view window
354 */
355 void PVGUI_Module::showView( bool toShow )
356 {
357   // TODO: check if ParaView view already exists
358   if ( !Implementation ){
359     LightApp_Application* anApp = getApp();
360     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
361     PVGUI_ViewManager* viewMgr = new PVGUI_ViewManager( anApp->activeStudy(), anApp->desktop() );
362     anApp->addViewManager( viewMgr );
363     connect( viewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
364              anApp, SLOT( onCloseView( SUIT_ViewManager* ) ) );
365     //connect( viewMgr, SIGNAL( viewCreated( SUIT_ViewWindow* ) ), vm, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
366     //connect( viewMgr, SIGNAL( deleteView( SUIT_ViewWindow* ) ), this, SLOT( onViewDeleted( SUIT_ViewWindow* ) ) );
367     SUIT_ViewWindow* wnd = viewMgr->createViewWindow();  
368
369     // Simulate ParaView client main window
370     Implementation = new pqImplementation( wnd );
371     PVGUI_ViewWindow* pvWnd = dynamic_cast<PVGUI_ViewWindow*>( wnd );
372     pvWnd->setMultiViewManager( &Implementation->Core.multiViewManager() );
373
374     pvCreateActions();
375     setupDockWidgets();
376   }
377 }
378  
379 /*!
380   \brief Create actions for ParaView GUI operations
381   duplicating menus and toolbars in pqMainWindow ParaView class
382 */
383 void PVGUI_Module::pvCreateActions()
384 {
385   // TODO...
386   SUIT_Desktop* desk = application()->desktop();
387
388   // TEST
389   int actionManagePlugins = 999;
390   createAction( actionManagePlugins, tr( "TOP_MANAGE_PLUGINS" ), QIcon(), tr( "MEN_MANAGE_PLUGINS" ),
391                 tr( "STB_MANAGE_PLUGINS" ), 0, desk, false, &Implementation->Core, SLOT( onManagePlugins() ) );
392   int aPVMnu = createMenu( tr( "MEN_TEST_PARAVIEW" ), -1, -1, 50 );
393   createMenu( actionManagePlugins, aPVMnu, 10 );
394 }
395
396
397 /*!
398   \brief Create dock widgets for ParaView widgets such as object inspector, pipeline browser, etc.
399 */
400 void PVGUI_Module::setupDockWidgets()
401 {
402 }
403
404 /*!
405   \brief Returns the ParaView multi-view manager.
406 */
407 pqViewManager* PVGUI_Module::getMultiViewManager() const
408 {
409   pqViewManager* aMVM = 0; 
410   if ( Implementation )
411     aMVM = &Implementation->Core.multiViewManager();
412   return aMVM;
413 }
414
415
416 /*!
417   \brief Activate module.
418   \param study current study
419   \return \c true if activaion is done successfully or 0 to prevent
420   activation on error
421 */
422 bool PVGUI_Module::activateModule( SUIT_Study* study )
423 {
424   bool isDone = LightApp_Module::activateModule( study );
425   if ( !isDone ) return false;
426
427   setMenuShown( true );
428
429   showView( true );
430
431   // Make default server connection
432   if ( Implementation )
433     Implementation->Core.makeDefaultConnectionIfNoneExists();
434
435   return isDone;
436 }
437
438
439 /*!
440   \brief Deactivate module.
441   \param study current study
442   \return \c true if deactivaion is done successfully or 0 to prevent
443   deactivation on error
444 */
445 bool PVGUI_Module::deactivateModule( SUIT_Study* study )
446 {
447   // hide menus
448   setMenuShown( false );
449
450   return LightApp_Module::deactivateModule( study );
451 }
452
453 /*!
454   \fn CAM_Module* createModule();
455   \brief Export module instance (factory function).
456   \return new created instance of the module
457 */
458
459 #ifdef WNT
460 #define PVGUI_EXPORT __declspec(dllexport)
461 #else   // WNT
462 #define PVGUI_EXPORT
463 #endif  // WNT
464
465 extern "C" {
466   PVGUI_EXPORT CAM_Module* createModule() {
467     return new PVGUI_Module();
468   }
469 }