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