Salome HOME
[EDF]: SALOME GUI module can't be compiled if all viewers are disabled
[modules/gui.git] / src / SALOME_SWIG / SALOMEGUI_Swig.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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
23 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
24 // File   : SALOMEGUI_Swig.cxx
25 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
26 //
27 #include "SALOMEGUI_Swig.hxx"
28
29 #include <SUIT_Session.h>
30 #include <SUIT_Desktop.h>
31 #include <SUIT_ViewWindow.h>
32 #include <SUIT_ViewManager.h>
33 #include <SUIT_ViewModel.h>
34 #include <SUIT_DataObjectIterator.h>
35 #include <CAM_DataModel.h>
36 #include <LightApp_Application.h>
37 #include <LightApp_Displayer.h>
38 #include <LightApp_Study.h>
39 #include <LightApp_Module.h>
40 #include <LightApp_DataObject.h>
41 #include <LightApp_SelectionMgr.h>
42 #include <LightApp_DataOwner.h>
43 #include <SALOME_Prs.h>
44 #include <SALOME_Event.h>
45
46 #ifndef DISABLE_SALOMEOBJECT
47   #include <SALOME_ListIO.hxx>
48   #include <SALOME_InteractiveObject.hxx>
49 #ifndef DISABLE_OCCVIEWER
50     #include <SOCC_ViewModel.h>
51     #include <SOCC_ViewWindow.h>
52 #endif
53 #ifndef DISABLE_VTKVIEWER
54     #include <SVTK_ViewModel.h>
55     #include <SVTK_ViewWindow.h>
56     #include <SVTK_Renderer.h>
57     
58     #include <vtkCamera.h>
59     #include <vtkRenderer.h>
60 #endif
61 #ifndef DISABLE_PLOT2DVIEWER
62     #include <SPlot2d_ViewWindow.h>
63 #endif
64 #endif
65
66 #include <utilities.h>
67
68 /*!
69   \class SALOMEGUI_Swig
70   \brief Python interface module for SALOME GUI.
71
72   This module provides an access to the SALOME GUI implementing set of functions
73   which can be used from Python. This module is implemented using SWIG wrappings
74   for some GUI functionality:
75   - getActiveStudyName() : get active study name
76   - updateObjBrowser() : update contents of the Object Browser
77   - SelectedCount() : get number of currently selected items
78   - getSelected() : get entry of the speicified selected item
79   - ClearIObjects() : clear selection
80   - Display(), DisplayOnly(), Erase() : display/erase objects
81   - etc.
82
83   Instance of this class is created every time "import salome" line is typed 
84   - in IAPP embedded Python interpretor  (SALOME_Session_Server executable)
85   - in inline Python nodes in Supervisor (in SALOME_Container executable)
86   - in stand-alone Python console outside any executable
87
88   SALOME GUI (desktop and other objects) is only available in SALOME_Session_Server.
89   It means that it can not be accessed from the external Python console.
90
91   The usage in Python:
92   \code
93   import libSALOME_Swig
94   sg = libSALOME_Swig.SALOMEGUI_Swig()
95   if sg.hasDesktop():
96       selcount = sg.SelectedCount()
97       if selcount > 0:
98           sg.Erase( sg.getSelected( 0 ) )
99       pass
100   \endcode
101 */
102
103 /*
104   --- INTERNAL COMMENTS SECTION ---
105
106   ASV : 03.12.04 : added checking for NULL GUI objects in almost all methods.
107   In the scope of fixing bug PAL6869.
108
109   VSR : 19.04.05 : Reimplemented for new SALOME GUI (SUIT-based)
110   All methods are implemeted using Event mechanism.
111   Display/Erase methods use SALOME_Prs/SALOME_View mechanism. It is currently
112   implemented only for OCC and VTK viewers.
113 */
114
115 /*!
116   \brief Get active application object
117   \internal
118   \return active application or 0 if there is no any
119 */
120 static LightApp_Application* getApplication()
121 {
122   if ( SUIT_Session::session() )
123     return dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() );
124   return 0;
125 }
126
127 /*!
128   \brief Get active study object
129   \internal
130   \return active study or 0 if there is no study opened
131 */
132 static LightApp_Study* getActiveStudy()
133 {
134   if ( getApplication() )
135     return dynamic_cast<LightApp_Study*>( getApplication()->activeStudy() );
136   return 0;
137 }
138
139 /*!
140   \brief Constructor.
141 */
142 SALOMEGUI_Swig::SALOMEGUI_Swig()
143 {
144 }
145
146 /*!
147   \brief Destructor
148 */
149 SALOMEGUI_Swig::~SALOMEGUI_Swig()
150 {
151 }
152
153 /*!
154   \fn bool SALOMEGUI_Swig::hasDesktop()
155   \brief Check GUI availability.
156   \return \c true if GUI is available
157 */
158
159 class THasDesktopEvent: public SALOME_Event
160 {
161 public:
162   typedef bool TResult;
163   TResult myResult;
164   THasDesktopEvent() : myResult( false ) {}
165   virtual void Execute()
166   {
167     myResult = (bool)( getApplication() && getApplication()->desktop() );
168   }
169 };
170 bool SALOMEGUI_Swig::hasDesktop()
171 {
172   return ProcessEvent( new THasDesktopEvent() );
173 }
174
175 /*!
176   \brief Update active study's Object Browser.
177 */
178 void SALOMEGUI_Swig::updateObjBrowser()
179 {
180   class TEvent: public SALOME_Event
181   {
182   public:
183     TEvent() {}
184     virtual void Execute()
185     {
186       if ( LightApp_Application* anApp = getApplication() ) {
187         anApp->updateObjectBrowser();
188         anApp->updateActions(); //SRN: added in order to update the toolbar
189       }
190     }
191   };
192   ProcessVoidEvent( new TEvent() );
193 }
194
195 /*!
196   \fn const char* SALOMEGUI_Swig::getActiveStudyName()
197   \brief Get active study name
198   \return active study's name or null string if there is no active study
199 */
200
201 class TGetActiveStudyNameEvent: public SALOME_Event
202 {
203 public:
204   typedef std::string TResult;
205   TResult myResult;
206   TGetActiveStudyNameEvent() {}
207   virtual void Execute()
208   {
209     if ( LightApp_Study* aStudy = getActiveStudy() ) {
210       myResult = aStudy->studyName().toUtf8().constData();
211     }
212   }
213 };
214 const char* SALOMEGUI_Swig::getActiveStudyName()
215 {
216   std::string result = ProcessEvent( new TGetActiveStudyNameEvent() );
217   return result.empty() ? 0 : result.c_str();
218 }
219
220 /*!
221   \fn const char* SALOMEGUI_Swig::getComponentName( const char* componentUserName )
222   \brief Get name of the component by its title (user name)
223   \param componentUserName component title (user name)
224   \return component name or null string if component title is invalid
225 */
226
227 /*!
228   \fn const char* SALOMEGUI_Swig::getComponentUserName( const char* componentName )
229   \brief Get title (user name) of the component by its name
230   \param componentName component name
231   \return component title or null string if component name is invalid
232 */
233
234 class TGetComponentNameEvent: public SALOME_Event
235 {
236 public:
237   typedef QString TResult;
238   TResult myResult;
239   QString myName;
240   bool    myIsUserName;
241   TGetComponentNameEvent( const QString& name, bool isUserName )
242     : myName( name ), myIsUserName( isUserName ) {}
243   virtual void Execute()
244   {
245     if ( LightApp_Application* app = getApplication() ) {
246       myResult = myIsUserName ? app->moduleTitle( myName ) : app->moduleName( myName );
247     }
248   }
249 };
250 const char* SALOMEGUI_Swig::getComponentName( const char* componentUserName )
251 {
252   QString result = ProcessEvent( new TGetComponentNameEvent( componentUserName, false ) );
253   return result.isEmpty() ? 0 : strdup( result.toLatin1().constData() );
254 }
255 const char* SALOMEGUI_Swig::getComponentUserName( const char* componentName )
256 {
257   QString result = ProcessEvent( new TGetComponentNameEvent( componentName, true ) );
258   return result.isEmpty() ? 0 : strdup( result.toLatin1().constData() );
259 }
260
261 /*!
262   \fn int SALOMEGUI_Swig::SelectedCount()
263   \brief Get number of selected items
264   \return number of selected items in the active study
265 */
266
267 /*!
268   \fn const char* SALOMEGUI_Swig::getSelected( int index )
269   \brief Get entry of the specified selected item
270   \param index selected object index
271   \return selected object entry (null string if index is invalid)
272 */
273
274 class TGetSelectedEvent: public SALOME_Event
275 {
276 public:
277   typedef QStringList TResult;
278   TResult myResult;
279   TGetSelectedEvent() {}
280   virtual void Execute()
281   {
282     if ( LightApp_Application* anApp = getApplication() ) {
283       LightApp_Study* aStudy  = dynamic_cast<LightApp_Study*>( anApp->activeStudy() ); // for sure!
284       LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
285       if ( aStudy && aSelMgr ) {
286         SUIT_DataOwnerPtrList aList;
287         aSelMgr->selected( aList );
288
289         for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); 
290               itr != aList.end(); ++itr ) {
291           const LightApp_DataOwner* owner = 
292             dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
293           if( !owner )
294             continue;
295           QString entry = owner->entry();
296           if( !myResult.contains( entry ) )
297             myResult.append( entry );
298         }
299       }
300     }
301   }
302 };
303 int SALOMEGUI_Swig::SelectedCount()
304 {
305   QStringList selected = ProcessEvent( new TGetSelectedEvent() );
306   return selected.count();
307 }
308 const char* SALOMEGUI_Swig::getSelected( int index )
309 {
310   QStringList selected = ProcessEvent( new TGetSelectedEvent() );
311   return index >= 0 && index < selected.count() ? 
312     strdup( selected[ index ].toUtf8().constData() ) : 0;
313 }
314
315 /*!
316   \brief Add an object to the current selection.
317   \param theEntry object entry
318 */
319 void SALOMEGUI_Swig::AddIObject( const char* theEntry )
320 {
321   class TEvent: public SALOME_Event
322   {
323   public:
324     QString myEntry;
325     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
326     virtual void Execute()
327     {
328       if ( LightApp_Application* anApp = getApplication() ) {
329         LightApp_Study*       aStudy  = dynamic_cast<LightApp_Study*>( anApp->activeStudy() ); // for sure!
330         LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
331         if ( aStudy && aSelMgr ) {
332           SALOME_ListIO anIOList;
333           anIOList.Append( new SALOME_InteractiveObject( myEntry.toUtf8(), "", "" ) );
334           aSelMgr->setSelectedObjects( anIOList, true );
335         }
336       }
337     }
338   };
339   ProcessVoidEvent( new TEvent( theEntry ) );
340 }
341
342 /*!
343   \brief Remove the object from the selection.
344   \param theEntry object entry
345 */
346 void SALOMEGUI_Swig::RemoveIObject( const char* theEntry )
347 {
348   class TEvent: public SALOME_Event
349   {
350   public:
351     QString myEntry;
352     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
353     virtual void Execute()
354     {
355       if ( LightApp_Application* anApp = getApplication() ) {
356         LightApp_Study* aStudy  = dynamic_cast<LightApp_Study*>( anApp->activeStudy() ); // for sure!
357         LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
358         if ( aStudy && aSelMgr ) {
359           SALOME_ListIO anIOList;
360           // VSR: temporary solution, until LightApp_SelectionMgr::unsetSelectedObjects() method appears
361           // Lately this should be replaced by the following:
362           // anIOList.Append( new SALOME_InteractiveObject( myEntry, "", "" ) );
363           // aSelMgr->unsetSelectedObjects( anIOList );
364           ///////////////////////////////////////////////
365           aSelMgr->selectedObjects( anIOList );
366           SALOME_ListIteratorOfListIO anIter( anIOList );
367           for( ; anIter.More(); anIter.Next() ) {
368             if ( anIter.Value()->isSame( new SALOME_InteractiveObject( myEntry.toUtf8(), "", "" ) ) ) { 
369               anIOList.Remove( anIter );
370               aSelMgr->setSelectedObjects( anIOList, true );
371               return;
372             }
373           }
374         }
375       }
376     }
377   };
378   ProcessVoidEvent( new TEvent( theEntry ) );
379 }
380
381 /*!
382   \brief Clear selection (unselect all objects).
383 */
384 void SALOMEGUI_Swig::ClearIObjects()
385 {
386   class TEvent: public SALOME_Event
387   {
388   public:
389     TEvent() {}
390     virtual void Execute()
391     {
392       if ( LightApp_Application* anApp = getApplication() ) {
393         LightApp_Study* aStudy  = dynamic_cast<LightApp_Study*>( anApp->activeStudy() ); // for sure!
394         LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
395         if ( aStudy && aSelMgr )
396           aSelMgr->clearSelected();
397       }
398     }
399   };
400   ProcessVoidEvent( new TEvent() );
401 }
402
403 /*!
404   \brief Display an object in the current view window.
405
406   The presentable object should be previously created and
407   displayed in this viewer.
408
409   \param theEntry object entry
410 */              
411 void SALOMEGUI_Swig::Display( const char* theEntry )
412 {
413   class TEvent: public SALOME_Event
414   {
415     QString myEntry;
416   public:
417     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
418     virtual void Execute() {
419       LightApp_Application* anApp  = getApplication();
420       LightApp_Study*       aStudy = getActiveStudy();
421       if ( anApp && aStudy ) {
422         QString mname = aStudy->componentDataType( myEntry );
423         LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mname, true );
424         if ( d ) {
425           QStringList entries;
426           if( aStudy->isComponent( myEntry ) )
427             aStudy->children( myEntry, entries );
428           else
429             entries.append( myEntry );
430           foreach( QString entry, entries )
431             d->Display( aStudy->referencedToEntry( entry ), false, 0 );
432         }
433       }
434     }
435   };
436   ProcessVoidEvent( new TEvent( theEntry ) );
437 }
438
439 /*!
440   \brief Displays an object in the current view window and 
441   erases all other ones.
442
443   The presentable object should be previously created and 
444   displayed in this viewer.
445
446   \param theEntry object entry
447 */
448 void SALOMEGUI_Swig::DisplayOnly( const char* theEntry )
449 {
450   class TEvent: public SALOME_Event
451   {
452     QString myEntry;
453   public:
454     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
455     virtual void Execute()
456     {
457       LightApp_Application* anApp  = getApplication();
458       LightApp_Study*       aStudy = getActiveStudy();
459       if ( anApp && aStudy ) {
460         QStringList comps;
461         aStudy->components( comps );
462         foreach( QString comp, comps ) {
463           LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( comp, false );
464           if ( d ) d->EraseAll( false, false, 0 );
465         }
466
467         QString mname = aStudy->componentDataType( myEntry );
468         LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mname, true );
469         if ( d ) {
470           QStringList entries;
471           if( aStudy->isComponent( myEntry ) )
472             aStudy->children( myEntry, entries );
473           else
474             entries.append( myEntry );
475           foreach( QString entry, entries )
476             d->Display( aStudy->referencedToEntry( entry ), false, 0 );
477         }
478       }
479     }
480   };
481   ProcessVoidEvent( new TEvent( theEntry ) );
482 }
483
484 /*!
485   \brief Erase an object in the current view window.
486
487   The presentable object should be previously created and 
488   displayed in this viewer.
489
490   \param theEntry object entry
491 */              
492 void SALOMEGUI_Swig::Erase( const char* theEntry )
493 {
494   class TEvent: public SALOME_Event
495   {
496     QString myEntry;
497   public:
498     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
499     virtual void Execute()
500     {
501       LightApp_Application* anApp  = getApplication();
502       LightApp_Study*       aStudy = getActiveStudy();
503       if ( anApp && aStudy ) {
504         QString mname = aStudy->componentDataType( myEntry );
505         LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mname, true );
506         if ( d ) {
507           QStringList entries;
508           if( aStudy->isComponent( myEntry ) )
509             aStudy->children( myEntry, entries );
510           else
511             entries.append( myEntry );
512           foreach( QString entry, entries )
513             d->Erase( aStudy->referencedToEntry( entry ), false, false, 0 );
514         }
515       }
516     }
517   };
518   ProcessVoidEvent( new TEvent( theEntry ) );
519 }
520
521 /*!
522   \brief Display all active module's presentable 
523   child objects in the current view window.
524   
525   The presentable objects should be previously created and
526   displayed in this viewer.
527 */
528 void SALOMEGUI_Swig::DisplayAll()
529 {
530   class TEvent: public SALOME_Event
531   {
532   public:
533     TEvent() {}
534     virtual void Execute()
535     {
536       LightApp_Application* anApp  = getApplication();
537       LightApp_Study*       aStudy = getActiveStudy();
538       if ( anApp && aStudy ) {
539         QStringList comps;
540         aStudy->components( comps );
541         foreach( QString comp, comps ) {
542           LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( comp, true );
543           if ( d ) {
544             QStringList entries;
545             aStudy->children( aStudy->centry( comp ), entries );
546             foreach( QString entry, entries )
547               d->Display( aStudy->referencedToEntry( entry ), false, 0 );
548           }
549         }
550       }
551     }
552   };
553   ProcessVoidEvent( new TEvent() );
554 }
555
556 /*!
557   \brief Erase all objects from the current view window.
558 */
559 void SALOMEGUI_Swig::EraseAll()
560 {
561   class TEvent: public SALOME_Event
562   {
563   public:
564     TEvent() {}
565     virtual void Execute()
566     {
567       LightApp_Application* anApp  = getApplication();
568       LightApp_Study*       aStudy = getActiveStudy();
569       if ( anApp && aStudy ) {
570         QStringList comps;
571         aStudy->components( comps );
572         foreach( QString comp, comps ) {
573           LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( comp, false );
574           if ( d ) d->EraseAll( false, false, 0 );
575         }
576       }
577     }
578   };
579   ProcessVoidEvent( new TEvent() );
580 }
581
582 /*!
583   \fn bool SALOMEGUI_Swig::IsInCurrentView( const char* theEntry )
584   \brief Check it the object is displayed in the current view window.
585
586   VSR: For the current moment implemented for OCC and VTK viewers only.
587
588   \param theEntry object entry
589   \return \c true if the object with given entry is displayed 
590           in the current viewer
591 */
592
593 class TIsInViewerEvent: public SALOME_Event
594 {
595   QString myEntry;
596 public:
597   typedef bool TResult;
598   TResult myResult;
599   TIsInViewerEvent( const char* theEntry ) : myEntry( theEntry ), myResult( false ) {}
600   virtual void Execute()
601   {
602     if ( LightApp_Application* anApp = getApplication() ) {
603       SUIT_ViewManager* viewMgr = anApp->activeViewManager();
604       if (!viewMgr) return;
605       SUIT_ViewWindow* window = viewMgr->getActiveView();
606       if ( window ) {
607         SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
608         if ( view ) {
609           SALOME_Prs* aPrs = view->CreatePrs( myEntry.toUtf8() );
610           myResult = !aPrs->IsNull();
611         }
612       }
613     }
614   }
615 };
616 bool SALOMEGUI_Swig::IsInCurrentView( const char* theEntry )
617 {
618   return ProcessEvent( new TIsInViewerEvent( theEntry ) );
619 }
620
621 /*!
622   \brief Update (repaint) current view window.
623 */
624 void SALOMEGUI_Swig::UpdateView()
625 {
626   class TEvent: public SALOME_Event
627   {
628   public:
629     TEvent() {}
630     virtual void Execute()
631     {
632       if ( LightApp_Application* anApp = getApplication() ) {
633         SUIT_ViewManager* viewMgr = anApp->activeViewManager();
634         if (!viewMgr) return;
635         SUIT_ViewWindow* window = viewMgr->getActiveView();
636         if ( window ) {
637           SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
638           if ( view )
639             view->Repaint();
640         }
641       }
642     }
643   };
644   ProcessVoidEvent( new TEvent() );
645 }
646
647 /*!
648   \brief Fit current view window to display all its contents.
649 */
650 void SALOMEGUI_Swig::FitAll()
651 {
652   MESSAGE("FitAll");
653   class TEvent: public SALOME_Event
654   {
655   public:
656     TEvent() {}
657     virtual void Execute()
658     {
659       if ( LightApp_Application* anApp = getApplication() ) {
660         SUIT_ViewManager* viewMgr = anApp->activeViewManager();
661         if (!viewMgr) return;
662         SUIT_ViewWindow* window = viewMgr->getActiveView();
663         if ( window ) {
664 #ifndef DISABLE_SALOMEOBJECT
665 #ifndef DISABLE_VTKVIEWER
666           if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
667             ( dynamic_cast<SVTK_ViewWindow*>( window ) )->onFitAll();
668 #endif
669 #ifndef DISABLE_PLOT2DVIEWER
670           if ( dynamic_cast<SPlot2d_ViewWindow*>( window ) )
671             ( dynamic_cast<SPlot2d_ViewWindow*>( window ) )->onFitAll();
672 #endif
673 #endif
674 #ifndef DISABLE_OCCVIEWER
675           if ( dynamic_cast<OCCViewer_ViewWindow*>( window ) )
676             ( dynamic_cast<OCCViewer_ViewWindow*>( window ) )->onFitAll();
677 #endif
678         }
679       }
680     }
681   };
682   ProcessVoidEvent( new TEvent() );
683 }
684
685 void SALOMEGUI_Swig::FitSelection()
686 {
687   class TEvent: public SALOME_Event
688   {
689   public:
690     TEvent() {}
691     virtual void Execute()
692     {
693       if ( LightApp_Application* anApp = getApplication() ) {
694         SUIT_ViewManager* viewMgr = anApp->activeViewManager();
695         if (!viewMgr) return;
696         SUIT_ViewWindow* window = viewMgr->getActiveView();
697         if ( window ) {
698 #if !defined(DISABLE_SALOMEOBJECT) && !defined(DISABLE_VTKVIEWER)
699           if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
700             (dynamic_cast<SVTK_ViewWindow*>( window ))->onFitSelection();
701 #endif
702 #if !defined(DISABLE_OCCVIEWER)
703           if ( dynamic_cast<OCCViewer_ViewWindow*>( window ) )
704             (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onFitSelection();
705 #endif
706         }
707       }
708     }
709   };
710   ProcessVoidEvent( new TEvent() );
711 }
712
713 void SALOMEGUI_Swig::FitIObjects(const std::list<std::string>& entries)
714 {
715   class TEvent: public SALOME_Event
716   {
717     const std::list<std::string>& myEntries;
718   public:
719     TEvent( const std::list<std::string>& objs ) : myEntries( objs ) {}
720     virtual void Execute()
721     {
722       if ( LightApp_Application* anApp = getApplication() ) {
723         SUIT_ViewManager* viewMgr = anApp->activeViewManager();
724         if (!viewMgr) return;
725         SUIT_ViewWindow* window = viewMgr->getActiveView();
726         if ( window ) {
727           SALOME_ListIO objects;
728           std::list<std::string>::const_iterator it;
729           for ( it = myEntries.begin(); it != myEntries.end(); ++it )
730             objects.Append( new SALOME_InteractiveObject( (*it).c_str(), "" ) );
731 #if !defined(DISABLE_SALOMEOBJECT) && !defined(DISABLE_VTKVIEWER)
732           if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
733             (dynamic_cast<SVTK_ViewWindow*>( window ))->onFitIObjects( objects );
734 #endif
735         }
736       }
737     }
738   };
739   ProcessVoidEvent( new TEvent( entries ) );
740 }
741
742 /*!
743   \brief Reset current view window to the default state.
744 */
745 void SALOMEGUI_Swig::ResetView()
746 {
747   class TEvent: public SALOME_Event
748   {
749   public:
750     TEvent() {}
751     virtual void Execute()
752     {
753       if ( LightApp_Application* anApp = getApplication() ) {
754         SUIT_ViewManager* viewMgr = anApp->activeViewManager();
755         if (!viewMgr) return;
756         SUIT_ViewWindow* window = viewMgr->getActiveView();
757         if ( window ) {
758 #ifndef DISABLE_SALOMEOBJECT
759 #ifndef DISABLE_VTKVIEWER
760           if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
761             (dynamic_cast<SVTK_ViewWindow*>( window ))->onResetView();
762 #endif
763 #ifndef DISABLE_PLOT2DVIEWER
764           if ( dynamic_cast<SPlot2d_ViewWindow*>( window ) )
765             (dynamic_cast<SPlot2d_ViewWindow*>( window ))->onFitAll();
766           // VSR: there is no 'ResetView' functionality for Plot2d viewer,
767           // so we use 'FitAll' instead.
768 #endif
769 #endif
770 #ifndef DISABLE_OCCVIEWER
771           if ( dynamic_cast<OCCViewer_ViewWindow*>( window ) )
772             (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onResetView();
773 #endif
774         }
775       }
776     }
777   };
778   ProcessVoidEvent( new TEvent() );
779 }
780
781 /*!
782   \brief View operation type.
783   \internal
784 */
785 enum {
786   __ViewTop,          //!< view top side
787   __ViewBottom,       //!< view bottom side
788   __ViewLeft,         //!< view left side
789   __ViewRight,        //!< view right side
790   __ViewFront,        //!< view front side
791   __ViewBack          //!< view back side
792 };
793
794 /*!
795   \brief Change the view of the current view window.
796   \internal
797   \param view view operation type
798 */
799 static void setView( int view )
800 {
801   class TEvent: public SALOME_Event
802   {
803   private:
804     int myView;
805   public:
806     TEvent( int view ) : myView( view ) {}
807     virtual void Execute()
808     {
809       if ( LightApp_Application* anApp = getApplication() ) {
810             SUIT_ViewManager* viewMgr = anApp->activeViewManager();
811             if (!viewMgr) return;
812             SUIT_ViewWindow* window = viewMgr->getActiveView();
813         if ( window ) {
814 #ifndef DISABLE_SALOMEOBJECT
815 #ifndef DISABLE_VTKVIEWER
816           if ( dynamic_cast<SVTK_ViewWindow*>( window ) ) {
817             switch( myView ) {
818             case __ViewTop:
819               (dynamic_cast<SVTK_ViewWindow*>( window ))->onTopView(); break;
820             case __ViewBottom:
821               (dynamic_cast<SVTK_ViewWindow*>( window ))->onBottomView(); break;
822             case __ViewLeft:
823               (dynamic_cast<SVTK_ViewWindow*>( window ))->onLeftView(); break;
824             case __ViewRight:
825               (dynamic_cast<SVTK_ViewWindow*>( window ))->onRightView(); break;
826             case __ViewFront:
827               (dynamic_cast<SVTK_ViewWindow*>( window ))->onFrontView(); break;
828             case __ViewBack:
829               (dynamic_cast<SVTK_ViewWindow*>( window ))->onBackView(); break;
830             default:
831               break;
832             }
833           }
834 #endif
835 #endif
836 #ifndef DISABLE_OCCVIEWER
837           if ( dynamic_cast<OCCViewer_ViewWindow*>( window ) ) {
838             switch( myView ) {
839             case __ViewTop:
840               (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onTopView(); break;
841             case __ViewBottom:
842               (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onBottomView(); break;
843             case __ViewLeft:
844               (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onLeftView(); break;
845             case __ViewRight:
846               (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onRightView(); break;
847             case __ViewFront:
848               (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onFrontView(); break;
849             case __ViewBack:
850               (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onBackView(); break;
851             default:
852               break;
853             }
854           }
855 #endif
856         }
857       }
858     }
859   };
860   ProcessVoidEvent( new TEvent( view ) );
861 }
862
863 /*!
864   \brief Switch current view window to show the top view.
865 */
866 void SALOMEGUI_Swig::ViewTop()
867 {
868   setView( __ViewTop );
869 }
870
871 /*!
872   \brief Switch current view window to show the bottom view
873 */
874 void SALOMEGUI_Swig::ViewBottom()
875 {
876   setView( __ViewBottom );
877 }
878
879 /*!
880   \brief Switch current view window to show the left view
881 */
882 void SALOMEGUI_Swig::ViewLeft()
883 {
884   setView( __ViewLeft );
885 }
886
887 /*!
888   \brief Switch current view window to show the right view
889 */
890 void SALOMEGUI_Swig::ViewRight()
891 {
892   setView( __ViewRight );
893 }
894
895 /*!
896   \brief Switch current view window to show the front view
897 */
898 void SALOMEGUI_Swig::ViewFront()
899 {
900   setView( __ViewFront );
901 }
902
903 /*!
904   \brief Switch current view window to show the back view
905 */
906 void SALOMEGUI_Swig::ViewBack()
907 {
908   setView( __ViewBack );
909 }
910
911 /*
912   \fn bool SALOMEGUI_Swig::getViewParameters()
913   \brief Get camera parameters of the active view.
914
915   NOTE: For the current moment implemented for VTK viewer only.
916
917   \return \c string with the view parameters
918 */
919
920 class TGetViewParameters: public SALOME_Event
921 {
922 public:
923   typedef QString TResult;
924   TResult myResult;
925   TGetViewParameters() : myResult( "" ) {}
926   virtual void Execute() {  
927     if ( LightApp_Application* anApp = getApplication() ) {
928           SUIT_ViewManager* viewMgr = anApp->activeViewManager();
929           if (!viewMgr) return;
930           if ( SUIT_ViewWindow* window = viewMgr->getActiveView() ) {
931 #ifndef DISABLE_VTKVIEWER
932         if ( SVTK_ViewWindow* svtk = dynamic_cast<SVTK_ViewWindow*>( window ) ) {         
933           if ( vtkRenderer* ren = svtk->getRenderer()) {                    
934             if ( vtkCamera* camera = ren->GetActiveCamera() ) {
935               double pos[3], focalPnt[3], viewUp[3], scale[3], parScale;            
936               
937               // save position, focal point, viewUp, scale
938               camera->GetPosition( pos );
939               camera->GetFocalPoint( focalPnt );
940               camera->GetViewUp( viewUp );
941               parScale = camera->GetParallelScale();
942               svtk->GetRenderer()->GetScale( scale );
943
944               myResult += QString("sg.setCameraPosition( %1, %2, %3 )\n").arg(pos[0]).arg(pos[1]).arg(pos[2]);
945               myResult += QString("sg.setCameraFocalPoint( %1, %2, %3 )\n").arg(focalPnt[0]).arg(focalPnt[1]).arg(focalPnt[2]);
946               myResult += QString("sg.setCameraViewUp( %1, %2, %3 )\n").arg(viewUp[0]).arg(viewUp[1]).arg(viewUp[2]);
947               myResult += QString("sg.setViewScale(%1, %2, %3, %4 )\n").arg(parScale).arg(scale[0]).arg(scale[1]).arg(scale[2]);
948             }
949           }
950         }
951 #endif
952       }
953     }
954   }
955 };
956         
957 const char* SALOMEGUI_Swig::getViewParameters() {
958   QString result = ProcessEvent( new TGetViewParameters() );
959   return result.isEmpty() ? 0 : strdup( result.toUtf8().constData() );  
960 }
961
962
963 /*!
964   \brief View parameter type.
965   \internal
966 */
967 enum {
968   __CameraPosition,   //!< position of the active camera
969   __CameraFocalPoint, //!< focal point of the active camera      
970   __CameraViewUp,     //!< view up of the active camera         
971   __ViewScale         //!< scale of the view
972 };
973
974
975 /*!
976   \brief Change the camera parameters of the current view window.
977   \internal
978
979   NOTE: For the current moment implemented for VTK viewer only.
980
981   \param parameter type of the parameter
982   \param values value of the parameter
983 */
984 static void setViewParameter( int parameter, QList<double>& values ) {
985   class TEvent: public SALOME_Event {
986   private:
987     int           myParameter;
988     QList<double> myValues;
989   public:
990     TEvent( int parameter , QList<double>& values ) : myParameter(parameter), myValues( values ) {}
991
992     virtual void Execute() {
993       if ( LightApp_Application* anApp = getApplication() ) {
994           SUIT_ViewManager* viewMgr = anApp->activeViewManager();
995           if (!viewMgr) return;
996           if ( SUIT_ViewWindow* window = viewMgr->getActiveView() ) {
997 #ifndef DISABLE_VTKVIEWER
998           if ( SVTK_ViewWindow* svtk = dynamic_cast<SVTK_ViewWindow*>( window ) ) {       
999             if ( vtkRenderer* ren = svtk->getRenderer()) {                  
1000               if ( vtkCamera* camera = ren->GetActiveCamera() ) {
1001                 switch(myParameter) {       
1002                   case __CameraPosition : {
1003                     if ( myValues.size() == 3 ) {  
1004                       camera->SetPosition( myValues[0], myValues[1], myValues[2] );
1005                     }
1006                     break;
1007                   }
1008                   case __CameraFocalPoint : {
1009                     if ( myValues.size() == 3 ) {  
1010                       camera->SetFocalPoint( myValues[0], myValues[1], myValues[2] );
1011                     }
1012                     break;
1013                   }
1014                   case __CameraViewUp : {
1015                     if ( myValues.size() == 3 ) {  
1016                       camera->SetViewUp( myValues[0], myValues[1], myValues[2] );
1017                     }
1018                     break;
1019                   }
1020                   case __ViewScale : {
1021                     if ( myValues.size() == 4 ) {  
1022                       camera->SetParallelScale( myValues[0] );
1023                       double scale[] = { myValues[1], myValues[2], myValues[3] };
1024                       svtk->GetRenderer()->SetScale( scale );
1025                     }
1026                     break;
1027                   }
1028                   default: break;
1029                 }
1030               }
1031             }
1032             svtk->Repaint();
1033           }
1034 #endif
1035         }
1036       }
1037     }
1038   };
1039   ProcessVoidEvent( new TEvent( parameter, values ) );
1040 }
1041
1042 /*!
1043   \brief Set camera position of the active view .
1044   \param x - X coordinate of the camera
1045   \param y - Y coordinate of the camera
1046   \param z - Z coordinate of the camera
1047 */
1048 void SALOMEGUI_Swig::setCameraPosition( double x, double y, double z ) {
1049   QList<double> lst;
1050   lst.push_back( x );
1051   lst.push_back( y );
1052   lst.push_back( z );
1053   setViewParameter( __CameraPosition, lst );
1054 }
1055
1056 /*!
1057   \brief Set camera focal point of the active view.
1058   \param x - X coordinate of the focal point
1059   \param y - Y coordinate of the focal point
1060   \param z - Z coordinate of the focal point
1061 */
1062 void SALOMEGUI_Swig::setCameraFocalPoint( double x, double y, double z ) {
1063   QList<double> lst;
1064   lst.push_back( x );
1065   lst.push_back( y );
1066   lst.push_back( z );
1067   setViewParameter( __CameraFocalPoint, lst );
1068 }
1069
1070 /*!
1071   \brief Set the view up direction for the camera.
1072   \param x - X component of the direction vector
1073   \param y - Y component of the direction vector
1074   \param z - Z component of the direction vector
1075 */
1076 void SALOMEGUI_Swig::setCameraViewUp( double x, double y, double z ) {
1077   QList<double> lst;
1078   lst.push_back( x );
1079   lst.push_back( y );
1080   lst.push_back( z );
1081   setViewParameter( __CameraViewUp, lst );
1082 }
1083
1084 /*!
1085   \brief Set view scale.
1086   \param parallelScale  - scaling used for a parallel projection.
1087   \param x - X scale
1088   \param y - Y scale
1089   \param z - Z scale
1090 */
1091 void SALOMEGUI_Swig::setViewScale( double parallelScale, double x, double y, double z ) {
1092   QList<double> lst;
1093   lst.push_back( parallelScale );
1094   lst.push_back( x );
1095   lst.push_back( y );
1096   lst.push_back( z );
1097   setViewParameter( __ViewScale, lst );
1098 }
1099
1100