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