Salome HOME
Rolling back incorrect integration
[modules/gui.git] / src / SALOME_SWIG / SALOMEGUI_Swig.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  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. 
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOMEGUI_Swig.cxx
25 //  Author : Vadim SANDLER
26 //  Module : SALOME
27 //  $Header$
28
29 #include "SALOMEGUI_Swig.hxx"
30
31 #include "SUIT_Session.h"
32 #include "SUIT_Desktop.h"
33 #include "SUIT_DataObjectIterator.h"
34 #include "OB_Browser.h"
35 #include "SalomeApp_Application.h"
36 #include "SalomeApp_Study.h"
37 #include "SalomeApp_Module.h"
38 #include "SalomeApp_DataObject.h"
39 #include "SalomeApp_SelectionMgr.h"
40 #include "SALOME_Prs.h"
41 #include "SOCC_ViewModel.h"
42 #include "SVTK_ViewModel.h"
43
44 #include "SALOME_Event.hxx"
45 #include "SALOME_ListIO.hxx"
46 #include "SALOME_InteractiveObject.hxx"
47 #include "SALOME_ListIteratorOfListIO.hxx"
48
49 //#include "utilities.h"
50
51 #include <SALOMEconfig.h>
52 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
53
54 using namespace std;
55
56 //////////////////////////////////////////////////////////////////////////////
57 // asv : 3.12.04 : added checking for NULL GUI objects in almost all methods.
58 // In the scope of fixing bug PAL6869.
59 //////////////////////////////////////////////////////////////////////////////
60 // (PR : modify comments)
61 // Instance of this class is created every time "import salome" line is typed 
62 // - in IAPP embedded Python interpretor  (SALOME_Session_Server executable),
63 // - in inline Python nodes in Supervisor (in SALOME_Container executable),
64 // - in stand-alone Python console outside any executable.
65 // SALOME GUI(desktop and other objects) is only available in SALOME_Session_Server
66 //////////////////////////////////////////////////////////////////////////////
67 // VSR : 19.04.05 : Reimplemented for new SALOME GUI (SUIT-based)
68 // All methods are implemeted using Event mechanism.
69 // Display/Erase methods use SALOME_Prs/SALOME_View mechanism. It is currently
70 // implemented only for OCC and VTK viewers.
71 //////////////////////////////////////////////////////////////////////////////
72
73 /*!
74   getApplication()
75   Returns active application object [ static ]
76 */
77 static SalomeApp_Application* getApplication() {
78   if ( SUIT_Session::session() )
79     return dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
80   return NULL;
81 }
82
83 /*!
84   getActiveStudy()
85   Gets active study or 0 if there is no study opened [ static ]
86 */
87 static SalomeApp_Study* getActiveStudy()
88 {
89   if ( getApplication() )
90     return dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
91   return 0;
92 }
93
94 /*!
95   SALOMEGUI_Swig::SALOMEGUI_Swig
96   Constructor
97 */
98 SALOMEGUI_Swig::SALOMEGUI_Swig()
99 {
100 }
101
102 /*!
103   SALOMEGUI_Swig::~SALOMEGUI_Swig
104   Destructor
105 */
106 SALOMEGUI_Swig::~SALOMEGUI_Swig()
107 {
108 }
109
110 /*!
111   SALOMEGUI_Swig::hasDesktop
112   Returns TRUE if GUI is available.
113 */
114 class THasDesktopEvent: public SALOME_Event {
115 public:
116   typedef bool TResult;
117   TResult myResult;
118   THasDesktopEvent() : myResult( false ) {}
119   virtual void Execute() {
120     myResult = (bool)( getApplication() && getApplication()->desktop() );
121   }
122 };
123 bool SALOMEGUI_Swig::hasDesktop()
124 {
125   return ProcessEvent( new THasDesktopEvent() );
126 }
127
128 /*!
129   SALOMEGUI_Swig::updateObjBrowser
130   Updates active study's Object Browser.
131   VSR: updateSelection parameter is currently not used. Will be implemented or removed lately.
132 */
133 void SALOMEGUI_Swig::updateObjBrowser( bool /*updateSelection*/ )
134 {
135   class TEvent: public SALOME_Event {
136   public:
137     TEvent() {}
138     virtual void Execute() {
139       if ( SalomeApp_Application* anApp = getApplication() ) {
140         anApp->updateObjectBrowser();
141         anApp->updateActions(); //SRN: added in order to update the toolbar
142       }
143     }
144   };
145   ProcessVoidEvent( new TEvent() );
146 }
147
148 /*!
149   SALOMEGUI_Swig::getActiveStudyId
150   Returns active study's ID or 0 if there is no active study.
151 */
152 class TGetActiveStudyIdEvent: public SALOME_Event {
153 public:
154   typedef int TResult;
155   TResult myResult;
156   TGetActiveStudyIdEvent() : myResult( 0 ) {}
157   virtual void Execute() {
158     if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
159       myResult = aStudy->studyDS()->StudyId();
160     }
161   }
162 };
163 int SALOMEGUI_Swig::getActiveStudyId()
164 {
165   return ProcessEvent( new TGetActiveStudyIdEvent() );
166 }
167
168 /*!
169   SALOMEGUI_Swig::getActiveStudyName
170   Returns active study's name or NULL if there is no active study.
171 */
172 class TGetActiveStudyNameEvent: public SALOME_Event {
173 public:
174   typedef string TResult;
175   TResult myResult;
176   TGetActiveStudyNameEvent() {}
177   virtual void Execute() {
178     if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
179       myResult = aStudy->studyDS()->Name();
180     }
181   }
182 };
183 const char* SALOMEGUI_Swig::getActiveStudyName()
184 {
185   string result = ProcessEvent( new TGetActiveStudyNameEvent() );
186   return result.empty() ? NULL : result.c_str();
187 }
188
189 /*!
190   SALOMEGUI_Swig::getComponentName
191   Returns the name of the component by its user name.
192 */
193 const char* SALOMEGUI_Swig::getComponentName( const char* componentUserName )
194 {
195   if ( SalomeApp_Application* anApp = getApplication() ) { 
196     CORBA::Object_var anObject = anApp->namingService()->Resolve("/Kernel/ModulCatalog");
197     if ( !CORBA::is_nil( anObject ) ) {
198       SALOME_ModuleCatalog::ModuleCatalog_var aCatalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow( anObject );
199       SALOME_ModuleCatalog::ListOfIAPP_Affich_var aModules = aCatalogue->GetComponentIconeList();
200       for ( unsigned int ind = 0; ind < aModules->length(); ind++ ) {
201         string aModuleName     = CORBA::string_dup( aModules[ ind ].modulename ) ;
202         string aModuleUserName = CORBA::string_dup( aModules[ ind ].moduleusername ) ;
203         if ( componentUserName == aModuleUserName )
204           return aModuleName.c_str();
205       }
206     }
207   }
208   return 0;
209 }
210
211 /*!
212   SALOMEGUI_Swig::getComponentUserName
213   Returns the user name of the component by its name.
214 */
215 const char* SALOMEGUI_Swig::getComponentUserName( const char* componentName )
216 {
217   if ( SalomeApp_Application* anApp = getApplication() ) { 
218     CORBA::Object_var anObject = anApp->namingService()->Resolve("/Kernel/ModulCatalog");
219     if ( !CORBA::is_nil( anObject ) ) {
220       SALOME_ModuleCatalog::ModuleCatalog_var aCatalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow( anObject );
221       SALOME_ModuleCatalog::ListOfIAPP_Affich_var aModules = aCatalogue->GetComponentIconeList();
222       for ( unsigned int ind = 0; ind < aModules->length(); ind++ ) {
223         string aModuleName     = CORBA::string_dup( aModules[ ind ].modulename ) ;
224         string aModuleUserName = CORBA::string_dup( aModules[ ind ].moduleusername ) ;
225         if ( componentName == aModuleName )
226           return aModuleUserName.c_str();
227       }
228     }
229   }
230   return 0;
231 }
232
233 /*!
234   SALOMEGUI_Swig::SelectedCount
235   Returns the number of selected objects.
236 */
237 class TSelectedCountEvent: public SALOME_Event {
238 public:
239   typedef int TResult;
240   TResult myResult;
241   TSelectedCountEvent() : myResult( 0 ) {}
242   virtual void Execute() {
243     if ( SalomeApp_Application* anApp = getApplication() ) {
244       SalomeApp_Study*        aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
245       SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
246       if ( aStudy && aSelMgr ) {
247         SALOME_ListIO anIOList;
248         aSelMgr->selectedObjects( anIOList );
249         myResult = anIOList.Extent();
250       }
251     }
252   }
253 };
254 int SALOMEGUI_Swig::SelectedCount()
255 {
256   return ProcessEvent( new TSelectedCountEvent() );
257 }
258
259 /*!
260   SALOMEGUI_Swig::getSelected
261   Returns the selected object entry by the given index.
262 */
263 class TGetSelectedEvent: public SALOME_Event {
264 public:
265   typedef QString TResult;
266   TResult myResult;
267   int     myIndex;
268   TGetSelectedEvent( int theIndex ) : myIndex( theIndex ) {}
269   virtual void Execute() {
270     if ( SalomeApp_Application* anApp = getApplication() ) {
271       SalomeApp_Study*        aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
272       SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
273       if ( aStudy && aSelMgr ) {
274         SALOME_ListIO anIOList;
275         aSelMgr->selectedObjects( anIOList );
276         if ( myIndex < anIOList.Extent() ) {
277           int index = 0;
278           SALOME_ListIteratorOfListIO anIter( anIOList );
279           for( ; anIter.More(); anIter.Next(), index++ ) {
280             Handle(SALOME_InteractiveObject) anIO = anIter.Value();
281             if ( myIndex == index ) {
282               myResult = anIO->getEntry();
283               return;
284             }
285           }
286         }
287       }
288     }
289   }
290 };
291 const char* SALOMEGUI_Swig::getSelected( int index )
292 {
293   QString result = ProcessEvent( new TGetSelectedEvent( index ) );
294   return result.isEmpty() ? NULL : strdup(result.latin1());
295 }
296
297 /*!
298   Adds an object with the given entry to the selection.
299 */
300 void SALOMEGUI_Swig::AddIObject( const char* theEntry )
301 {
302   class TEvent: public SALOME_Event {
303   public:
304     QString myEntry;
305     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
306     virtual void Execute() {
307       if ( SalomeApp_Application* anApp = getApplication() ) {
308         SalomeApp_Study*        aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
309         SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
310         if ( aStudy && aSelMgr ) {
311           SALOME_ListIO anIOList;
312           anIOList.Append( new SALOME_InteractiveObject( myEntry, "", "" ) );
313           aSelMgr->setSelectedObjects( anIOList, true );
314         }
315       }
316     }
317   };
318   ProcessVoidEvent( new TEvent( theEntry ) );
319 }
320
321 /*!
322   Removes the object with the given entry from the selection.
323 */
324 void SALOMEGUI_Swig::RemoveIObject( const char* theEntry )
325 {
326   class TEvent: public SALOME_Event {
327   public:
328     QString myEntry;
329     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
330     virtual void Execute() {
331       if ( SalomeApp_Application* anApp = getApplication() ) {
332         SalomeApp_Study*        aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
333         SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
334         if ( aStudy && aSelMgr ) {
335           SALOME_ListIO anIOList;
336           // VSR: temporary solution, until SalomeApp_SelectionMgr::unsetSelectedObjects() method appears
337           // Lately this should be replaced by the following:
338           // anIOList.Append( new SALOME_InteractiveObject( myEntry, "", "" ) );
339           // aSelMgr->unsetSelectedObjects( anIOList );
340           ///////////////////////////////////////////////
341           aSelMgr->selectedObjects( anIOList );
342           SALOME_ListIteratorOfListIO anIter( anIOList );
343           for( ; anIter.More(); anIter.Next() ) {
344             if ( anIter.Value()->isSame( new SALOME_InteractiveObject( myEntry, "", "" ) ) ) { 
345               anIOList.Remove( anIter );
346               aSelMgr->setSelectedObjects( anIOList, true );
347               return;
348             }
349           }
350         }
351       }
352     }
353   };
354   ProcessVoidEvent( new TEvent( theEntry ) );
355 }
356
357 /*!
358   Clears selection.
359 */
360 void SALOMEGUI_Swig::ClearIObjects()
361 {
362   class TEvent: public SALOME_Event {
363   public:
364     TEvent() {}
365     virtual void Execute() {
366       if ( SalomeApp_Application* anApp = getApplication() ) {
367         SalomeApp_Study*        aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
368         SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
369         if ( aStudy && aSelMgr )
370           aSelMgr->clearSelected();
371       }
372     }
373   };
374   ProcessVoidEvent( new TEvent() );
375 }
376
377 /*!
378   Displays an object in the current view window
379   (the presentable object should be previously created and displayed in this viewer).
380   VSR: For the current moment implemented for OCC and VTK viewers only.
381 */              
382 void SALOMEGUI_Swig::Display( const char* theEntry )
383 {
384   class TEvent: public SALOME_Event {
385     QString myEntry;
386   public:
387     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
388     virtual void Execute() {
389       if ( SalomeApp_Application* anApp = getApplication() ) {
390         SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
391         if ( window ) {
392           SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
393           if ( view )
394             view->Display( view->CreatePrs( myEntry ) );
395         }
396       }
397     }
398   };
399   ProcessVoidEvent( new TEvent( theEntry ) );
400 }
401
402 /*!
403   Displays an object in the current view window and erases all other
404   (the presentable object should be previously created and displayed in this viewer).
405   VSR: For the current moment implemented for OCC and VTK viewers only.
406 */
407 void SALOMEGUI_Swig::DisplayOnly( const char* theEntry )
408 {
409   class TEvent: public SALOME_Event {
410     QString myEntry;
411   public:
412     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
413     virtual void Execute() {
414       if ( SalomeApp_Application* anApp = getApplication() ) {
415         SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
416         if ( window ) {
417           SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
418           if ( view ) {
419             view->EraseAll( false );
420             view->Display( view->CreatePrs( myEntry ) );
421           }
422         }
423       }
424     }
425   };
426   ProcessVoidEvent( new TEvent( theEntry ) );
427 }
428
429 /*!
430   Erases an object in the current view window
431   (the presentable object should be previously created and displayed in this viewer).
432   VSR: For the current moment implemented for OCC and VTK viewers only.
433 */              
434 void SALOMEGUI_Swig::Erase( const char* theEntry )
435 {
436   class TEvent: public SALOME_Event {
437     QString myEntry;
438   public:
439     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
440     virtual void Execute() {
441       if ( SalomeApp_Application* anApp = getApplication() ) {
442         SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
443         if ( window ) {
444           SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
445           if ( view )
446             view->Erase( view->CreatePrs( myEntry ) );
447         }
448       }
449     }
450   };
451   ProcessVoidEvent( new TEvent( theEntry ) );
452 }
453
454 /*!
455   Displays all active module's child objects in the current view window
456   (the presentable objects should be previously created and displayed in this viewer).
457   VSR: For the current moment implemented for OCC and VTK viewers only.
458 */
459 void SALOMEGUI_Swig::DisplayAll()
460 {
461   class TEvent: public SALOME_Event {
462   public:
463     TEvent() {}
464     virtual void Execute() {
465       if ( SalomeApp_Application* anApp = getApplication() ) {
466         SalomeApp_Study*  study        = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
467         SUIT_ViewWindow*  window       = anApp->desktop()->activeWindow();
468         SalomeApp_Module* activeModule = dynamic_cast<SalomeApp_Module*>( anApp->activeModule() );
469         if ( study && window && activeModule ) {
470           SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
471           if ( view ) {
472             for ( SUIT_DataObjectIterator it( activeModule->dataModel()->root(), SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
473               SalomeApp_DataObject* obj = dynamic_cast<SalomeApp_DataObject*>( it.current() );
474               if ( obj && !obj->entry().isEmpty() )
475                 view->Display( view->CreatePrs( obj->entry() ) );
476             }
477           }
478         }
479       }
480     }
481   };
482   ProcessVoidEvent( new TEvent() );
483 }
484
485 /*!
486   Erases all objects from the current view window
487   VSR: For the current moment implemented for OCC and VTK viewers only.
488 */
489 void SALOMEGUI_Swig::EraseAll()
490 {
491   class TEvent: public SALOME_Event {
492   public:
493     TEvent() {}
494     virtual void Execute() {
495       if ( SalomeApp_Application* anApp = getApplication() ) {
496         SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
497         if ( window ) {
498           SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
499           if ( view )
500             view->EraseAll( false );
501         }
502       }
503     }
504   };
505   ProcessVoidEvent( new TEvent() );
506 }
507
508 /*!
509   Returns TRUE if the object with given entry is in the current viewer.
510   VSR: For the current moment implemented for OCC and VTK viewers only.
511 */
512 class TIsInViewerEvent: public SALOME_Event {
513   QString myEntry;
514 public:
515   typedef bool TResult;
516   TResult myResult;
517   TIsInViewerEvent( const char* theEntry ) : myEntry( theEntry ), myResult( false ) {}
518   virtual void Execute() {
519     if ( SalomeApp_Application* anApp = getApplication() ) {
520       SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
521       if ( window ) {
522         SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
523         if ( view ) {
524           SALOME_Prs* aPrs = view->CreatePrs( myEntry );
525           myResult = aPrs->IsNull();
526         }
527       }
528     }
529   }
530 };
531 bool SALOMEGUI_Swig::IsInCurrentView( const char* theEntry )
532 {
533   return ProcessEvent( new TIsInViewerEvent( theEntry ) );
534 }