Salome HOME
fe41664575913513d80caa7b7652d6d93c693c88
[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
31 #include <SUIT_MessageBox.h>
32 #include <SUIT_Desktop.h>
33 #include <SUIT_Session.h>
34 #include <LightApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <QApplication>
38 #include <QInputDialog>
39 #include <QStringList>
40 #include <QMenu>
41 #include <QIcon>
42 #include <QString>
43
44 #include <pqOptions.h>
45 #include <vtkPVMain.h>
46 #include <vtkProcessModule.h>
47
48 /*
49  * Make sure all the kits register their classes with vtkInstantiator.
50  * Since ParaView uses Tcl wrapping, all of VTK is already compiled in
51  * anyway.  The instantiators will add no more code for the linker to
52  * collect.
53  */
54
55 #include <vtkCommonInstantiator.h>
56 #include <vtkFilteringInstantiator.h>
57 #include <vtkGenericFilteringInstantiator.h>
58 #include <vtkIOInstantiator.h>
59 #include <vtkImagingInstantiator.h>
60 #include <vtkInfovisInstantiator.h>
61 #include <vtkGraphicsInstantiator.h>
62
63 #include <vtkRenderingInstantiator.h>
64 #include <vtkVolumeRenderingInstantiator.h>
65 #include <vtkHybridInstantiator.h>
66 #include <vtkParallelInstantiator.h>
67
68 #include <vtkPVServerCommonInstantiator.h>
69 #include <vtkPVFiltersInstantiator.h>
70 #include <vtkPVServerManagerInstantiator.h>
71 #include <vtkClientServerInterpreter.h>
72
73
74 //----------------------------------------------------------------------------
75 // ClientServer wrapper initialization functions.
76 // Taken from ParaView sources (file pqMain.cxx)
77 extern "C" void vtkCommonCS_Initialize(vtkClientServerInterpreter*);
78 extern "C" void vtkFilteringCS_Initialize(vtkClientServerInterpreter*);
79 extern "C" void vtkGenericFilteringCS_Initialize(vtkClientServerInterpreter*);
80 extern "C" void vtkImagingCS_Initialize(vtkClientServerInterpreter*);
81 extern "C" void vtkInfovisCS_Initialize(vtkClientServerInterpreter*);
82 extern "C" void vtkGraphicsCS_Initialize(vtkClientServerInterpreter*);
83 extern "C" void vtkIOCS_Initialize(vtkClientServerInterpreter*);
84 extern "C" void vtkRenderingCS_Initialize(vtkClientServerInterpreter*);
85 extern "C" void vtkVolumeRenderingCS_Initialize(vtkClientServerInterpreter*);
86 extern "C" void vtkHybridCS_Initialize(vtkClientServerInterpreter*);
87 extern "C" void vtkWidgetsCS_Initialize(vtkClientServerInterpreter*);
88 extern "C" void vtkParallelCS_Initialize(vtkClientServerInterpreter*);
89 extern "C" void vtkPVServerCommonCS_Initialize(vtkClientServerInterpreter*);
90 extern "C" void vtkPVFiltersCS_Initialize(vtkClientServerInterpreter*);
91 extern "C" void vtkXdmfCS_Initialize(vtkClientServerInterpreter *);
92
93 //----------------------------------------------------------------------------
94 void ParaViewInitializeInterpreter(vtkProcessModule* pm)
95 {
96   // Initialize built-in wrapper modules.
97   vtkCommonCS_Initialize(pm->GetInterpreter());
98   vtkFilteringCS_Initialize(pm->GetInterpreter());
99   vtkGenericFilteringCS_Initialize(pm->GetInterpreter());
100   vtkImagingCS_Initialize(pm->GetInterpreter());
101   vtkInfovisCS_Initialize(pm->GetInterpreter());
102   vtkGraphicsCS_Initialize(pm->GetInterpreter());
103   vtkIOCS_Initialize(pm->GetInterpreter());
104   vtkRenderingCS_Initialize(pm->GetInterpreter());
105   vtkVolumeRenderingCS_Initialize(pm->GetInterpreter());
106   vtkHybridCS_Initialize(pm->GetInterpreter());
107   vtkWidgetsCS_Initialize(pm->GetInterpreter());
108   vtkParallelCS_Initialize(pm->GetInterpreter());
109   vtkPVServerCommonCS_Initialize(pm->GetInterpreter());
110   vtkPVFiltersCS_Initialize(pm->GetInterpreter());
111   vtkXdmfCS_Initialize(pm->GetInterpreter());
112 }
113
114 vtkPVMain*                 PVGUI_Module::myPVMain = 0;
115 pqOptions*                 PVGUI_Module::myPVOptions = 0;
116 PVGUI_ProcessModuleHelper* PVGUI_Module::myPVHelper = 0;
117
118 /*!
119   \class PVGUI_Module
120   \brief Implementation of light (no-CORBA-engine) 
121          SALOME module wrapping ParaView GUI.
122 */
123
124 /*!
125   \brief Constructor. Sets the default name for the module.
126 */
127 PVGUI_Module::PVGUI_Module()
128 : LightApp_Module( "PARAVIS" )
129 {
130 }
131
132 /*!
133   \brief Destructor.
134 */
135 PVGUI_Module::~PVGUI_Module()
136 {
137 }
138
139 /*!
140   \brief Initialize module. Creates menus, prepares context menu, etc.
141   \param app application instance
142 */
143 void PVGUI_Module::initialize( CAM_Application* app )
144 {
145   LightApp_Module::initialize( app );
146
147   SUIT_Desktop* desk = application()->desktop();
148
149   /*
150   int i = 1;
151   while( i ){
152     i = i;
153   }
154   */
155
156   if ( pvInit() ) {
157     pvCreateActions();
158   }
159   /*
160   createAction( lgLoadFile, tr( "TOP_LOAD_FILE" ), QIcon(), tr( "MEN_LOAD_FILE" ),
161                 tr( "STB_LOAD_FILE" ), 0, desk, false, this, SLOT( onLoadFile() ) );
162   createAction( lgDisplayLine, tr( "TOP_DISPLAY_LINE" ), QIcon(), tr( "MEN_DISPLAY_LINE" ),
163                 tr( "STB_DISPLAY_LINE" ), 0, desk, false, this, SLOT( onDisplayLine() ) );
164   createAction( lgEraseLine, tr( "TOP_ERASE_LINE" ), QIcon(), tr( "MEN_ERASE_LINE" ),
165                 tr( "STB_ERASE_LINE" ), 0, desk, false, this, SLOT( onEraseLine() ) );
166   createAction( lgSaveFile, tr( "TOP_SAVE_FILE" ), QIcon(), tr( "MEN_SAVE_FILE" ),
167                 tr( "STB_SAVE_FILE" ), 0, desk, false, this, SLOT( onSaveFile() ) );
168   createAction( lgEditLine, tr( "TOP_EDIT_LINE" ), QIcon(), tr( "MEN_EDIT_LINE" ),
169                 tr( "STB_EDIT_LINE" ), 0, desk, false, this, SLOT( onEditLine() ) );
170   createAction( lgAddLine,  tr( "TOP_ADD_LINE" ),  QIcon(), tr( "MEN_ADD_LINE" ),
171                 tr( "STB_ADD_LINE" ),  0, desk, false, this, SLOT( onAddLine() ) );
172   createAction( lgDelLine,  tr( "TOP_DEL_LINE" ),  QIcon(), tr( "MEN_DEL_LINE" ),
173                 tr( "STB_DEL_LINE" ),  0, desk, false, this, SLOT( onDelLine() ) );
174   createAction( lgClear,    tr( "TOP_CLEAR_ALL" ), QIcon(), tr( "MEN_CLEAR_ALL" ),
175                 tr( "STB_CLEAR_ALL" ), 0, desk, false, this, SLOT( onClear() ) );
176
177   int aFileMnu = createMenu( tr( "MEN_FILE" ), -1, -1 );
178   createMenu( separator(), aFileMnu, -1, 10 );
179   createMenu( lgLoadFile,  aFileMnu, 10 );
180   createMenu( lgSaveFile,  aFileMnu, 10 );
181
182   int aLightMnu = createMenu( tr( "MEN_LIGHT" ), -1, -1, 50 );
183   createMenu( lgAddLine,      aLightMnu, 10 );
184   createMenu( lgEditLine,     aLightMnu, 10 );
185   createMenu( lgDelLine,      aLightMnu, 10 );
186   createMenu( separator(),    aLightMnu, -1, 10 );
187   createMenu( lgClear,        aLightMnu, 10 );
188
189   QString rule = "(client='ObjectBrowser' or client='OCCViewer') and selcount=1 and type='TextLine' and !empty";
190
191   popupMgr()->insert ( action( lgDisplayLine ), -1, 0 );
192   popupMgr()->setRule( action( lgDisplayLine ), rule + " and !visible"  );
193
194   popupMgr()->insert ( action( lgEraseLine ), -1, 0 );
195   popupMgr()->setRule( action( lgEraseLine ), rule + " and activeView='OCCViewer' and visible"  );
196
197   rule = "client='ObjectBrowser' and selcount=1 and type='TextLine'";
198
199   popupMgr()->insert ( action( lgEditLine ), -1, 0 );
200   popupMgr()->setRule( action( lgEditLine ), rule  );
201
202   popupMgr()->insert ( action( lgAddLine ),  -1, 0 );
203   popupMgr()->setRule( action( lgAddLine ),  rule );
204
205   popupMgr()->insert ( separator(),          -1, 0 );
206
207   popupMgr()->insert ( action( lgDelLine ),  -1, 0 );
208   popupMgr()->setRule( action( lgDelLine ),  rule );
209
210   rule = "client='ObjectBrowser'";
211
212   popupMgr()->insert ( action( lgClear ),    -1, 0 );
213   popupMgr()->setRule( action( lgClear ),    rule );*/
214 }
215
216 /*!
217   \brief Get list of compliant dockable GUI elements
218   \param m map to be filled in ("type":"default_position")
219 */
220 void PVGUI_Module::windows( QMap<int, int>& m ) const
221 {
222   m.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
223   // TODO: creation of Python console leads to SIGSEGV on Python initialization...
224   //m.insert( LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
225   // ParaView diagnostic output redirected here
226   m.insert( LightApp_Application::WT_LogWindow, Qt::BottomDockWidgetArea );
227 }
228
229 /*!
230   \brief Create custom popup menu selection object.
231   \return new selected object
232 */
233 /*LightApp_Selection* PVGUI_Module::createSelection() const
234 {
235   return new PVGUI_Selection();
236 }*/
237
238 /*!
239   \brief Create data model.
240   \return module specific data model
241 */
242 /*CAM_DataModel* PVGUI_Module::createDataModel()
243 {
244   return new PVGUI_DataModel( this );
245 }*/
246
247 /*!
248   \brief Static method, performs initialization of ParaView session.
249   \return \c true if ParaView has been initialized successfully, otherwise false
250 */
251 bool PVGUI_Module::pvInit()
252 {
253   if ( !myPVMain ){
254     // Obtain command-line arguments
255     int argc = 0;
256     QStringList args = QApplication::arguments();
257     char** argv = new char*[args.size()];
258     for ( QStringList::const_iterator it = args.begin(); argc < 1 && it != args.end(); it++, argc++ )
259       argv[argc] = strdup( (*it).toLatin1().constData() );
260
261     vtkPVMain::SetInitializeMPI(0);  // pvClient never runs with MPI.
262     vtkPVMain::Initialize(&argc, &argv); // Perform any initializations.
263
264     // TODO: Set plugin dir from preferences
265     //QApplication::setLibraryPaths(QStringList(dir.absolutePath()));
266
267     myPVMain = vtkPVMain::New();
268     if ( !myPVOptions )
269       myPVOptions = pqOptions::New();
270     if ( !myPVHelper )
271       myPVHelper = PVGUI_ProcessModuleHelper::New();
272
273     myPVOptions->SetProcessType(vtkPVOptions::PVCLIENT);
274
275     // This creates the Process Module and initializes it.
276     int ret = myPVMain->Initialize(myPVOptions, myPVHelper, ParaViewInitializeInterpreter,
277                                    argc, argv);
278     if (!ret){
279       // Tell process module that we support Multiple connections.
280       // This must be set before starting the event loop.
281       vtkProcessModule::GetProcessModule()->SupportMultipleConnectionsOn();
282       ret = myPVHelper->Run(myPVOptions);
283     }
284
285     //delete[] argv;
286     cout << "*** ParaView client initalized!!!" << endl;
287     return !ret;
288   }
289   
290   return true;
291 }
292  
293 /*!
294   \brief Static method, cleans up ParaView session at application exit.
295 */
296 void PVGUI_Module::pvShutdown()
297 {
298   // TODO...
299 }  
300
301 /*!
302   \brief Shows (toShow = true) or hides ParaView view window
303 */
304 void PVGUI_Module::showView( bool toShow )
305 {
306   // TODO: check if ParaView view already exists
307   LightApp_Application* anApp = getApp();
308   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
309   PVGUI_ViewManager* viewMgr = new PVGUI_ViewManager( anApp->activeStudy(), anApp->desktop() );
310   anApp->addViewManager( viewMgr );
311   connect( viewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
312            anApp, SLOT( onCloseView( SUIT_ViewManager* ) ) );
313   //connect( viewMgr, SIGNAL( viewCreated( SUIT_ViewWindow* ) ), vm, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
314   //connect( viewMgr, SIGNAL( deleteView( SUIT_ViewWindow* ) ), this, SLOT( onViewDeleted( SUIT_ViewWindow* ) ) );
315   SUIT_ViewWindow* wnd = viewMgr->createViewWindow();  
316 }
317  
318 /*!
319   \brief Create actions for ParaView GUI operations
320   duplicating menus and toolbars in pqMainWindow ParaView class
321 */
322 void PVGUI_Module::pvCreateActions()
323 {
324   // TODO...
325 }
326
327 /*!
328   \brief Activate module.
329   \param study current study
330   \return \c true if activaion is done successfully or 0 to prevent
331   activation on error
332 */
333 bool PVGUI_Module::activateModule( SUIT_Study* study )
334 {
335   bool isDone = LightApp_Module::activateModule( study );
336   if ( !isDone ) return false;
337
338   setMenuShown( true );
339
340   showView( true );
341
342   return isDone;
343 }
344
345 /*!
346   \brief Deactivate module.
347   \param study current study
348   \return \c true if deactivaion is done successfully or 0 to prevent
349   deactivation on error
350 */
351 bool PVGUI_Module::deactivateModule( SUIT_Study* study )
352 {
353   // hide menus
354   setMenuShown( false );
355
356   return LightApp_Module::deactivateModule( study );
357 }
358
359 /*!
360   \fn CAM_Module* createModule();
361   \brief Export module instance (factory function).
362   \return new created instance of the module
363 */
364
365 #ifdef WNT
366 #define PVGUI_EXPORT __declspec(dllexport)
367 #else   // WNT
368 #define PVGUI_EXPORT
369 #endif  // WNT
370
371 extern "C" {
372   PVGUI_EXPORT CAM_Module* createModule() {
373     return new PVGUI_Module();
374   }
375 }