Salome HOME
0c8e1764d402e06687cce089b7a51669728ea718
[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 "LightApp_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 =
199         SALOME_ModuleCatalog::ModuleCatalog::_narrow( anObject );
200       SALOME_ModuleCatalog::ListOfIAPP_Affich_var aModules = aCatalogue->GetComponentIconeList();
201       for ( unsigned int ind = 0; ind < aModules->length(); ind++ ) {
202         CORBA::String_var aModuleName     = aModules[ ind ].modulename;
203         CORBA::String_var aModuleUserName = aModules[ ind ].moduleusername;
204         if ( strcmp(componentUserName, aModuleUserName.in()) == 0 )
205           return aModuleName._retn();
206       }
207     }
208   }
209   return 0;
210 }
211
212 /*!
213   SALOMEGUI_Swig::getComponentUserName
214   Returns the user name of the component by its name.
215 */
216 const char* SALOMEGUI_Swig::getComponentUserName( const char* componentName )
217 {
218   if ( SalomeApp_Application* anApp = getApplication() ) { 
219     CORBA::Object_var anObject = anApp->namingService()->Resolve("/Kernel/ModulCatalog");
220     if ( !CORBA::is_nil( anObject ) ) {
221       SALOME_ModuleCatalog::ModuleCatalog_var aCatalogue =
222         SALOME_ModuleCatalog::ModuleCatalog::_narrow( anObject );
223       SALOME_ModuleCatalog::ListOfIAPP_Affich_var aModules = aCatalogue->GetComponentIconeList();
224       for ( unsigned int ind = 0; ind < aModules->length(); ind++ ) {
225         CORBA::String_var aModuleName     = aModules[ ind ].modulename;
226         CORBA::String_var aModuleUserName = aModules[ ind ].moduleusername;
227         if ( strcmp(componentName, aModuleName.in()) == 0 )
228           return aModuleUserName._retn();
229       }
230     }
231   }
232   return 0;
233 }
234
235 /*!
236   SALOMEGUI_Swig::SelectedCount
237   Returns the number of selected objects.
238 */
239 class TSelectedCountEvent: public SALOME_Event {
240 public:
241   typedef int TResult;
242   TResult myResult;
243   TSelectedCountEvent() : myResult( 0 ) {}
244   virtual void Execute() {
245     if ( SalomeApp_Application* anApp = getApplication() ) {
246       SalomeApp_Study*       aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
247       LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
248       if ( aStudy && aSelMgr ) {
249         SALOME_ListIO anIOList;
250         aSelMgr->selectedObjects( anIOList );
251         myResult = anIOList.Extent();
252       }
253     }
254   }
255 };
256 int SALOMEGUI_Swig::SelectedCount()
257 {
258   return ProcessEvent( new TSelectedCountEvent() );
259 }
260
261 /*!
262   SALOMEGUI_Swig::getSelected
263   Returns the selected object entry by the given index.
264 */
265 class TGetSelectedEvent: public SALOME_Event {
266 public:
267   typedef QString TResult;
268   TResult myResult;
269   int     myIndex;
270   TGetSelectedEvent( int theIndex ) : myIndex( theIndex ) {}
271   virtual void Execute() {
272     if ( SalomeApp_Application* anApp = getApplication() ) {
273       SalomeApp_Study*       aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
274       LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
275       if ( aStudy && aSelMgr ) {
276         SALOME_ListIO anIOList;
277         aSelMgr->selectedObjects( anIOList );
278         if ( myIndex < anIOList.Extent() ) {
279           int index = 0;
280           SALOME_ListIteratorOfListIO anIter( anIOList );
281           for( ; anIter.More(); anIter.Next(), index++ ) {
282             Handle(SALOME_InteractiveObject) anIO = anIter.Value();
283             if ( myIndex == index ) {
284               myResult = anIO->getEntry();
285               return;
286             }
287           }
288         }
289       }
290     }
291   }
292 };
293 const char* SALOMEGUI_Swig::getSelected( int index )
294 {
295   QString result = ProcessEvent( new TGetSelectedEvent( index ) );
296   return result.isEmpty() ? NULL : strdup(result.latin1());
297 }
298
299 /*!
300   Adds an object with the given entry to the selection.
301 */
302 void SALOMEGUI_Swig::AddIObject( const char* theEntry )
303 {
304   class TEvent: public SALOME_Event {
305   public:
306     QString myEntry;
307     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
308     virtual void Execute() {
309       if ( SalomeApp_Application* anApp = getApplication() ) {
310         SalomeApp_Study*       aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
311         LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
312         if ( aStudy && aSelMgr ) {
313           SALOME_ListIO anIOList;
314           anIOList.Append( new SALOME_InteractiveObject( myEntry, "", "" ) );
315           aSelMgr->setSelectedObjects( anIOList, true );
316         }
317       }
318     }
319   };
320   ProcessVoidEvent( new TEvent( theEntry ) );
321 }
322
323 /*!
324   Removes the object with the given entry from the selection.
325 */
326 void SALOMEGUI_Swig::RemoveIObject( const char* theEntry )
327 {
328   class TEvent: public SALOME_Event {
329   public:
330     QString myEntry;
331     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
332     virtual void Execute() {
333       if ( SalomeApp_Application* anApp = getApplication() ) {
334         SalomeApp_Study*       aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
335         LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
336         if ( aStudy && aSelMgr ) {
337           SALOME_ListIO anIOList;
338           // VSR: temporary solution, until LightApp_SelectionMgr::unsetSelectedObjects() method appears
339           // Lately this should be replaced by the following:
340           // anIOList.Append( new SALOME_InteractiveObject( myEntry, "", "" ) );
341           // aSelMgr->unsetSelectedObjects( anIOList );
342           ///////////////////////////////////////////////
343           aSelMgr->selectedObjects( anIOList );
344           SALOME_ListIteratorOfListIO anIter( anIOList );
345           for( ; anIter.More(); anIter.Next() ) {
346             if ( anIter.Value()->isSame( new SALOME_InteractiveObject( myEntry, "", "" ) ) ) { 
347               anIOList.Remove( anIter );
348               aSelMgr->setSelectedObjects( anIOList, true );
349               return;
350             }
351           }
352         }
353       }
354     }
355   };
356   ProcessVoidEvent( new TEvent( theEntry ) );
357 }
358
359 /*!
360   Clears selection.
361 */
362 void SALOMEGUI_Swig::ClearIObjects()
363 {
364   class TEvent: public SALOME_Event {
365   public:
366     TEvent() {}
367     virtual void Execute() {
368       if ( SalomeApp_Application* anApp = getApplication() ) {
369         SalomeApp_Study*       aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
370         LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
371         if ( aStudy && aSelMgr )
372           aSelMgr->clearSelected();
373       }
374     }
375   };
376   ProcessVoidEvent( new TEvent() );
377 }
378
379 /*!
380   Displays an object in the current view window
381   (the presentable object should be previously created and displayed in this viewer).
382   VSR: For the current moment implemented for OCC and VTK viewers only.
383 */              
384 void SALOMEGUI_Swig::Display( const char* theEntry )
385 {
386   class TEvent: public SALOME_Event {
387     QString myEntry;
388   public:
389     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
390     virtual void Execute() {
391       if ( SalomeApp_Application* anApp = getApplication() ) {
392         SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
393         if ( window ) {
394           SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
395           if ( view )
396             view->Display( view->CreatePrs( myEntry ) );
397         }
398       }
399     }
400   };
401   ProcessVoidEvent( new TEvent( theEntry ) );
402 }
403
404 /*!
405   Displays an object in the current view window and erases all other
406   (the presentable object should be previously created and displayed in this viewer).
407   VSR: For the current moment implemented for OCC and VTK viewers only.
408 */
409 void SALOMEGUI_Swig::DisplayOnly( const char* theEntry )
410 {
411   class TEvent: public SALOME_Event {
412     QString myEntry;
413   public:
414     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
415     virtual void Execute() {
416       if ( SalomeApp_Application* anApp = getApplication() ) {
417         SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
418         if ( window ) {
419           SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
420           if ( view ) {
421             view->EraseAll( false );
422             view->Display( view->CreatePrs( myEntry ) );
423           }
424         }
425       }
426     }
427   };
428   ProcessVoidEvent( new TEvent( theEntry ) );
429 }
430
431 /*!
432   Erases an object in the current view window
433   (the presentable object should be previously created and displayed in this viewer).
434   VSR: For the current moment implemented for OCC and VTK viewers only.
435 */              
436 void SALOMEGUI_Swig::Erase( const char* theEntry )
437 {
438   class TEvent: public SALOME_Event {
439     QString myEntry;
440   public:
441     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
442     virtual void Execute() {
443       if ( SalomeApp_Application* anApp = getApplication() ) {
444         SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
445         if ( window ) {
446           SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
447           if ( view )
448             view->Erase( view->CreatePrs( myEntry ) );
449         }
450       }
451     }
452   };
453   ProcessVoidEvent( new TEvent( theEntry ) );
454 }
455
456 /*!
457   Displays all active module's child objects in the current view window
458   (the presentable objects should be previously created and displayed in this viewer).
459   VSR: For the current moment implemented for OCC and VTK viewers only.
460 */
461 void SALOMEGUI_Swig::DisplayAll()
462 {
463   class TEvent: public SALOME_Event {
464   public:
465     TEvent() {}
466     virtual void Execute() {
467       if ( SalomeApp_Application* anApp = getApplication() ) {
468         SalomeApp_Study*  study        = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
469         SUIT_ViewWindow*  window       = anApp->desktop()->activeWindow();
470         SalomeApp_Module* activeModule = dynamic_cast<SalomeApp_Module*>( anApp->activeModule() );
471         if ( study && window && activeModule ) {
472           SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
473           if ( view ) {
474             for ( SUIT_DataObjectIterator it( activeModule->dataModel()->root(), SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
475               SalomeApp_DataObject* obj = dynamic_cast<SalomeApp_DataObject*>( it.current() );
476               if ( obj && !obj->entry().isEmpty() )
477                 view->Display( view->CreatePrs( obj->entry() ) );
478             }
479           }
480         }
481       }
482     }
483   };
484   ProcessVoidEvent( new TEvent() );
485 }
486
487 /*!
488   Erases all objects from the current view window
489   VSR: For the current moment implemented for OCC and VTK viewers only.
490 */
491 void SALOMEGUI_Swig::EraseAll()
492 {
493   class TEvent: public SALOME_Event {
494   public:
495     TEvent() {}
496     virtual void Execute() {
497       if ( SalomeApp_Application* anApp = getApplication() ) {
498         SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
499         if ( window ) {
500           SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
501           if ( view )
502             view->EraseAll( false );
503         }
504       }
505     }
506   };
507   ProcessVoidEvent( new TEvent() );
508 }
509
510 /*!
511   Returns TRUE if the object with given entry is in the current viewer.
512   VSR: For the current moment implemented for OCC and VTK viewers only.
513 */
514 class TIsInViewerEvent: public SALOME_Event {
515   QString myEntry;
516 public:
517   typedef bool TResult;
518   TResult myResult;
519   TIsInViewerEvent( const char* theEntry ) : myEntry( theEntry ), myResult( false ) {}
520   virtual void Execute() {
521     if ( SalomeApp_Application* anApp = getApplication() ) {
522       SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
523       if ( window ) {
524         SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
525         if ( view ) {
526           SALOME_Prs* aPrs = view->CreatePrs( myEntry );
527           myResult = aPrs->IsNull();
528         }
529       }
530     }
531   }
532 };
533 bool SALOMEGUI_Swig::IsInCurrentView( const char* theEntry )
534 {
535   return ProcessEvent( new TIsInViewerEvent( theEntry ) );
536 }