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