Salome HOME
[EDF] AsterStudy: fit 3D view to given presentations
[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()->getViewModel() );
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()->getViewModel() );
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 void SALOMEGUI_Swig::FitSelection()
710 {
711   class TEvent: public SALOME_Event
712   {
713   public:
714     TEvent() {}
715     virtual void Execute()
716     {
717       if ( LightApp_Application* anApp = getApplication() ) {
718         SUIT_ViewManager* viewMgr = anApp->activeViewManager();
719         if (!viewMgr) return;
720         SUIT_ViewWindow* window = viewMgr->getActiveView();
721         if ( window ) {
722 #if !defined(DISABLE_SALOMEOBJECT) && !defined(DISABLE_VTKVIEWER)
723           if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
724             (dynamic_cast<SVTK_ViewWindow*>( window ))->onFitSelection();
725 #endif
726 #if !defined(DISABLE_OCCVIEWER)
727           if ( dynamic_cast<OCCViewer_ViewWindow*>( window ) )
728             (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onFitSelection();
729 #endif
730         }
731       }
732     }
733   };
734   ProcessVoidEvent( new TEvent() );
735 }
736
737 void SALOMEGUI_Swig::FitIObjects(const std::list<std::string>& entries)
738 {
739   class TEvent: public SALOME_Event
740   {
741     const std::list<std::string>& myEntries;
742   public:
743     TEvent( const std::list<std::string>& objs ) : myEntries( objs ) {}
744     virtual void Execute()
745     {
746       if ( LightApp_Application* anApp = getApplication() ) {
747         SUIT_ViewManager* viewMgr = anApp->activeViewManager();
748         if (!viewMgr) return;
749         SUIT_ViewWindow* window = viewMgr->getActiveView();
750         if ( window ) {
751           SALOME_ListIO objects;
752           std::list<std::string>::const_iterator it;
753           for ( it = myEntries.begin(); it != myEntries.end(); ++it )
754             objects.Append( new SALOME_InteractiveObject( (*it).c_str(), "" ) );
755 #if !defined(DISABLE_SALOMEOBJECT) && !defined(DISABLE_VTKVIEWER)
756           if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
757             (dynamic_cast<SVTK_ViewWindow*>( window ))->onFitIObjects( objects );
758 #endif
759         }
760       }
761     }
762   };
763   ProcessVoidEvent( new TEvent( entries ) );
764 }
765
766 /*!
767   \brief Reset current view window to the default state.
768 */
769 void SALOMEGUI_Swig::ResetView()
770 {
771   class TEvent: public SALOME_Event
772   {
773   public:
774     TEvent() {}
775     virtual void Execute()
776     {
777       if ( LightApp_Application* anApp = getApplication() ) {
778         SUIT_ViewManager* viewMgr = anApp->activeViewManager();
779         if (!viewMgr) return;
780         SUIT_ViewWindow* window = viewMgr->getActiveView();
781         if ( window ) {
782 #ifndef DISABLE_SALOMEOBJECT
783 #ifndef DISABLE_VTKVIEWER
784           if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
785             (dynamic_cast<SVTK_ViewWindow*>( window ))->onResetView();
786 #endif
787 #ifndef DISABLE_PLOT2DVIEWER
788           if ( dynamic_cast<SPlot2d_ViewWindow*>( window ) )
789             (dynamic_cast<SPlot2d_ViewWindow*>( window ))->onFitAll();
790           // VSR: there is no 'ResetView' functionality for Plot2d viewer,
791           // so we use 'FitAll' instead.
792 #endif
793 #endif
794 #ifndef DISABLE_OCCVIEWER
795           if ( dynamic_cast<OCCViewer_ViewWindow*>( window ) )
796             (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onResetView();
797 #endif
798         }
799       }
800     }
801   };
802   ProcessVoidEvent( new TEvent() );
803 }
804
805 /*!
806   \brief View operation type.
807   \internal
808 */
809 enum {
810   __ViewTop,          //!< view top side
811   __ViewBottom,       //!< view bottom side
812   __ViewLeft,         //!< view left side
813   __ViewRight,        //!< view right side
814   __ViewFront,        //!< view front side
815   __ViewBack          //!< view back side
816 };
817
818 /*!
819   \brief Change the view of the current view window.
820   \internal
821   \param view view operation type
822 */
823 static void setView( int view )
824 {
825   class TEvent: public SALOME_Event
826   {
827   private:
828     int myView;
829   public:
830     TEvent( int view ) : myView( view ) {}
831     virtual void Execute()
832     {
833       if ( LightApp_Application* anApp = getApplication() ) {
834             SUIT_ViewManager* viewMgr = anApp->activeViewManager();
835             if (!viewMgr) return;
836             SUIT_ViewWindow* window = viewMgr->getActiveView();
837         if ( window ) {
838 #ifndef DISABLE_SALOMEOBJECT
839 #ifndef DISABLE_VTKVIEWER
840           if ( dynamic_cast<SVTK_ViewWindow*>( window ) ) {
841             switch( myView ) {
842             case __ViewTop:
843               (dynamic_cast<SVTK_ViewWindow*>( window ))->onTopView(); break;
844             case __ViewBottom:
845               (dynamic_cast<SVTK_ViewWindow*>( window ))->onBottomView(); break;
846             case __ViewLeft:
847               (dynamic_cast<SVTK_ViewWindow*>( window ))->onLeftView(); break;
848             case __ViewRight:
849               (dynamic_cast<SVTK_ViewWindow*>( window ))->onRightView(); break;
850             case __ViewFront:
851               (dynamic_cast<SVTK_ViewWindow*>( window ))->onFrontView(); break;
852             case __ViewBack:
853               (dynamic_cast<SVTK_ViewWindow*>( window ))->onBackView(); break;
854             default:
855               break;
856             }
857           }
858 #endif
859 #endif
860 #ifndef DISABLE_OCCVIEWER
861           if ( dynamic_cast<OCCViewer_ViewWindow*>( window ) ) {
862             switch( myView ) {
863             case __ViewTop:
864               (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onTopView(); break;
865             case __ViewBottom:
866               (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onBottomView(); break;
867             case __ViewLeft:
868               (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onLeftView(); break;
869             case __ViewRight:
870               (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onRightView(); break;
871             case __ViewFront:
872               (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onFrontView(); break;
873             case __ViewBack:
874               (dynamic_cast<OCCViewer_ViewWindow*>( window ))->onBackView(); break;
875             default:
876               break;
877             }
878           }
879 #endif
880         }
881       }
882     }
883   };
884   ProcessVoidEvent( new TEvent( view ) );
885 }
886
887 /*!
888   \brief Switch current view window to show the top view.
889 */
890 void SALOMEGUI_Swig::ViewTop()
891 {
892   setView( __ViewTop );
893 }
894
895 /*!
896   \brief Switch current view window to show the bottom view
897 */
898 void SALOMEGUI_Swig::ViewBottom()
899 {
900   setView( __ViewBottom );
901 }
902
903 /*!
904   \brief Switch current view window to show the left view
905 */
906 void SALOMEGUI_Swig::ViewLeft()
907 {
908   setView( __ViewLeft );
909 }
910
911 /*!
912   \brief Switch current view window to show the right view
913 */
914 void SALOMEGUI_Swig::ViewRight()
915 {
916   setView( __ViewRight );
917 }
918
919 /*!
920   \brief Switch current view window to show the front view
921 */
922 void SALOMEGUI_Swig::ViewFront()
923 {
924   setView( __ViewFront );
925 }
926
927 /*!
928   \brief Switch current view window to show the back view
929 */
930 void SALOMEGUI_Swig::ViewBack()
931 {
932   setView( __ViewBack );
933 }
934
935 /*
936   \fn bool SALOMEGUI_Swig::getViewParameters()
937   \brief Get camera parameters of the active view.
938
939   NOTE: For the current moment implemented for VTK viewer only.
940
941   \return \c string with the view parameters
942 */
943
944 class TGetViewParameters: public SALOME_Event
945 {
946 public:
947   typedef QString TResult;
948   TResult myResult;
949   TGetViewParameters() : myResult( "" ) {}
950   virtual void Execute() {  
951     if ( LightApp_Application* anApp = getApplication() ) {
952           SUIT_ViewManager* viewMgr = anApp->activeViewManager();
953           if (!viewMgr) return;
954           if ( SUIT_ViewWindow* window = viewMgr->getActiveView() ) {
955 #ifndef DISABLE_VTKVIEWER
956         if ( SVTK_ViewWindow* svtk = dynamic_cast<SVTK_ViewWindow*>( window ) ) {         
957           if ( vtkRenderer* ren = svtk->getRenderer()) {                    
958             if ( vtkCamera* camera = ren->GetActiveCamera() ) {
959               double pos[3], focalPnt[3], viewUp[3], scale[3], parScale;            
960               
961               // save position, focal point, viewUp, scale
962               camera->GetPosition( pos );
963               camera->GetFocalPoint( focalPnt );
964               camera->GetViewUp( viewUp );
965               parScale = camera->GetParallelScale();
966               svtk->GetRenderer()->GetScale( scale );
967
968               myResult += QString("sg.setCameraPosition( %1, %2, %3 )\n").arg(pos[0]).arg(pos[1]).arg(pos[2]);
969               myResult += QString("sg.setCameraFocalPoint( %1, %2, %3 )\n").arg(focalPnt[0]).arg(focalPnt[1]).arg(focalPnt[2]);
970               myResult += QString("sg.setCameraViewUp( %1, %2, %3 )\n").arg(viewUp[0]).arg(viewUp[1]).arg(viewUp[2]);
971               myResult += QString("sg.setViewScale(%1, %2, %3, %4 )\n").arg(parScale).arg(scale[0]).arg(scale[1]).arg(scale[2]);
972             }
973           }
974         }
975 #endif
976       }
977     }
978   }
979 };
980         
981 const char* SALOMEGUI_Swig::getViewParameters() {
982   QString result = ProcessEvent( new TGetViewParameters() );
983   return result.isEmpty() ? 0 : strdup( result.toLatin1().constData() );  
984 }
985
986
987 /*!
988   \brief View parameter type.
989   \internal
990 */
991 enum {
992   __CameraPosition,   //!< position of the active camera
993   __CameraFocalPoint, //!< focal point of the active camera      
994   __CameraViewUp,     //!< view up of the active camera         
995   __ViewScale         //!< scale of the view
996 };
997
998
999 /*!
1000   \brief Change the camera parameters of the current view window.
1001   \internal
1002
1003   NOTE: For the current moment implemented for VTK viewer only.
1004
1005   \param parameter type of the parameter
1006   \param values value of the parameter
1007 */
1008 static void setViewParameter( int parameter, QList<double>& values ) {
1009   class TEvent: public SALOME_Event {
1010   private:
1011     int           myParameter;
1012     QList<double> myValues;
1013   public:
1014     TEvent( int parameter , QList<double>& values ) : myParameter(parameter), myValues( values ) {}
1015
1016     virtual void Execute() {
1017       if ( LightApp_Application* anApp = getApplication() ) {
1018           SUIT_ViewManager* viewMgr = anApp->activeViewManager();
1019           if (!viewMgr) return;
1020           if ( SUIT_ViewWindow* window = viewMgr->getActiveView() ) {
1021 #ifndef DISABLE_VTKVIEWER
1022           if ( SVTK_ViewWindow* svtk = dynamic_cast<SVTK_ViewWindow*>( window ) ) {       
1023             if ( vtkRenderer* ren = svtk->getRenderer()) {                  
1024               if ( vtkCamera* camera = ren->GetActiveCamera() ) {
1025                 switch(myParameter) {       
1026                   case __CameraPosition : {
1027                     if ( myValues.size() == 3 ) {  
1028                       camera->SetPosition( myValues[0], myValues[1], myValues[2] );
1029                     }
1030                     break;
1031                   }
1032                   case __CameraFocalPoint : {
1033                     if ( myValues.size() == 3 ) {  
1034                       camera->SetFocalPoint( myValues[0], myValues[1], myValues[2] );
1035                     }
1036                     break;
1037                   }
1038                   case __CameraViewUp : {
1039                     if ( myValues.size() == 3 ) {  
1040                       camera->SetViewUp( myValues[0], myValues[1], myValues[2] );
1041                     }
1042                     break;
1043                   }
1044                   case __ViewScale : {
1045                     if ( myValues.size() == 4 ) {  
1046                       camera->SetParallelScale( myValues[0] );
1047                       double scale[] = { myValues[1], myValues[2], myValues[3] };
1048                       svtk->GetRenderer()->SetScale( scale );
1049                     }
1050                     break;
1051                   }
1052                   default: break;
1053                 }
1054               }
1055             }
1056             svtk->Repaint();
1057           }
1058 #endif
1059         }
1060       }
1061     }
1062   };
1063   ProcessVoidEvent( new TEvent( parameter, values ) );
1064 }
1065
1066 /*!
1067   \brief Set camera position of the active view .
1068   \param x - X coordinate of the camera
1069   \param y - Y coordinate of the camera
1070   \param z - Z coordinate of the camera
1071 */
1072 void SALOMEGUI_Swig::setCameraPosition( double x, double y, double z ) {
1073   QList<double> lst;
1074   lst.push_back( x );
1075   lst.push_back( y );
1076   lst.push_back( z );
1077   setViewParameter( __CameraPosition, lst );
1078 }
1079
1080 /*!
1081   \brief Set camera focal point of the active view.
1082   \param x - X coordinate of the focal point
1083   \param y - Y coordinate of the focal point
1084   \param z - Z coordinate of the focal point
1085 */
1086 void SALOMEGUI_Swig::setCameraFocalPoint( double x, double y, double z ) {
1087   QList<double> lst;
1088   lst.push_back( x );
1089   lst.push_back( y );
1090   lst.push_back( z );
1091   setViewParameter( __CameraFocalPoint, lst );
1092 }
1093
1094 /*!
1095   \brief Set the view up direction for the camera.
1096   \param x - X component of the direction vector
1097   \param y - Y component of the direction vector
1098   \param z - Z component of the direction vector
1099 */
1100 void SALOMEGUI_Swig::setCameraViewUp( double x, double y, double z ) {
1101   QList<double> lst;
1102   lst.push_back( x );
1103   lst.push_back( y );
1104   lst.push_back( z );
1105   setViewParameter( __CameraViewUp, lst );
1106 }
1107
1108 /*!
1109   \brief Set view scale.
1110   \param parallelScale  - scaling used for a parallel projection.
1111   \param x - X scale
1112   \param y - Y scale
1113   \param z - Z scale
1114 */
1115 void SALOMEGUI_Swig::setViewScale( double parallelScale, double x, double y, double z ) {
1116   QList<double> lst;
1117   lst.push_back( parallelScale );
1118   lst.push_back( x );
1119   lst.push_back( y );
1120   lst.push_back( z );
1121   setViewParameter( __ViewScale, lst );
1122 }
1123
1124