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