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