1 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SALOMEGUI_Swig.cxx
25 // Author : Vadim SANDLER
29 #include "SALOMEGUI_Swig.hxx"
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"
44 #include "SALOME_Event.hxx"
45 #include "SALOME_ListIO.hxx"
46 #include "SALOME_InteractiveObject.hxx"
47 #include "SALOME_ListIteratorOfListIO.hxx"
49 //#include "utilities.h"
51 #include <SALOMEconfig.h>
52 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
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 //////////////////////////////////////////////////////////////////////////////
75 Returns active application object [ static ]
77 static SalomeApp_Application* getApplication() {
78 if ( SUIT_Session::session() )
79 return dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
85 Gets active study or 0 if there is no study opened [ static ]
87 static SalomeApp_Study* getActiveStudy()
89 if ( getApplication() )
90 return dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
95 SALOMEGUI_Swig::SALOMEGUI_Swig
98 SALOMEGUI_Swig::SALOMEGUI_Swig()
103 SALOMEGUI_Swig::~SALOMEGUI_Swig
106 SALOMEGUI_Swig::~SALOMEGUI_Swig()
111 SALOMEGUI_Swig::hasDesktop
112 Returns TRUE if GUI is available.
114 class THasDesktopEvent: public SALOME_Event {
116 typedef bool TResult;
118 THasDesktopEvent() : myResult( false ) {}
119 virtual void Execute() {
120 myResult = (bool)( getApplication() && getApplication()->desktop() );
123 bool SALOMEGUI_Swig::hasDesktop()
125 return ProcessEvent( new THasDesktopEvent() );
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.
133 void SALOMEGUI_Swig::updateObjBrowser( bool /*updateSelection*/ )
135 class TEvent: public SALOME_Event {
138 virtual void Execute() {
139 if ( SalomeApp_Application* anApp = getApplication() ) {
140 OB_Browser* browser = anApp->objectBrowser();
142 browser->updateTree();
146 ProcessVoidEvent( new TEvent() );
150 SALOMEGUI_Swig::getActiveStudyId
151 Returns active study's ID or 0 if there is no active study.
153 class TGetActiveStudyIdEvent: public SALOME_Event {
157 TGetActiveStudyIdEvent() : myResult( 0 ) {}
158 virtual void Execute() {
159 if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
160 myResult = aStudy->studyDS()->StudyId();
164 int SALOMEGUI_Swig::getActiveStudyId()
166 return ProcessEvent( new TGetActiveStudyIdEvent() );
170 SALOMEGUI_Swig::getActiveStudyName
171 Returns active study's name or NULL if there is no active study.
173 class TGetActiveStudyNameEvent: public SALOME_Event {
175 typedef string TResult;
177 TGetActiveStudyNameEvent() {}
178 virtual void Execute() {
179 if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
180 myResult = aStudy->studyDS()->Name();
184 const char* SALOMEGUI_Swig::getActiveStudyName()
186 string result = ProcessEvent( new TGetActiveStudyNameEvent() );
187 return result.empty() ? NULL : result.c_str();
191 SALOMEGUI_Swig::getComponentName
192 Returns the name of the component by its user name.
194 const char* SALOMEGUI_Swig::getComponentName( const char* componentUserName )
196 if ( SalomeApp_Application* anApp = getApplication() ) {
197 CORBA::Object_var anObject = anApp->namingService()->Resolve("/Kernel/ModulCatalog");
198 if ( !CORBA::is_nil( anObject ) ) {
199 SALOME_ModuleCatalog::ModuleCatalog_var aCatalogue = 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 string aModuleName = CORBA::string_dup( aModules[ ind ].modulename ) ;
203 string aModuleUserName = CORBA::string_dup( aModules[ ind ].moduleusername ) ;
204 if ( componentUserName == aModuleUserName )
205 return aModuleName.c_str();
213 SALOMEGUI_Swig::getComponentUserName
214 Returns the user name of the component by its name.
216 const char* SALOMEGUI_Swig::getComponentUserName( const char* componentName )
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 = SALOME_ModuleCatalog::ModuleCatalog::_narrow( anObject );
222 SALOME_ModuleCatalog::ListOfIAPP_Affich_var aModules = aCatalogue->GetComponentIconeList();
223 for ( unsigned int ind = 0; ind < aModules->length(); ind++ ) {
224 string aModuleName = CORBA::string_dup( aModules[ ind ].modulename ) ;
225 string aModuleUserName = CORBA::string_dup( aModules[ ind ].moduleusername ) ;
226 if ( componentName == aModuleName )
227 return aModuleUserName.c_str();
235 SALOMEGUI_Swig::SelectedCount
236 Returns the number of selected objects.
238 class TSelectedCountEvent: public SALOME_Event {
242 TSelectedCountEvent() : myResult( 0 ) {}
243 virtual void Execute() {
244 if ( SalomeApp_Application* anApp = getApplication() ) {
245 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
246 SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr();
247 if ( aStudy && aSelMgr ) {
248 SALOME_ListIO anIOList;
249 aSelMgr->selectedObjects( anIOList );
250 myResult = anIOList.Extent();
255 int SALOMEGUI_Swig::SelectedCount()
257 return ProcessEvent( new TSelectedCountEvent() );
261 SALOMEGUI_Swig::getSelected
262 Returns the selected object entry by the given index.
264 class TGetSelectedEvent: public SALOME_Event {
266 typedef QString TResult;
269 TGetSelectedEvent( int theIndex ) : myIndex( theIndex ) {}
270 virtual void Execute() {
271 if ( SalomeApp_Application* anApp = getApplication() ) {
272 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
273 SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr();
274 if ( aStudy && aSelMgr ) {
275 SALOME_ListIO anIOList;
276 aSelMgr->selectedObjects( anIOList );
277 if ( myIndex < anIOList.Extent() ) {
279 SALOME_ListIteratorOfListIO anIter( anIOList );
280 for( ; anIter.More(); anIter.Next(), index++ ) {
281 Handle(SALOME_InteractiveObject) anIO = anIter.Value();
282 if ( myIndex == index ) {
283 myResult = anIO->getEntry();
292 const char* SALOMEGUI_Swig::getSelected( int index )
294 QString result = ProcessEvent( new TGetSelectedEvent( index ) );
295 return result.isEmpty() ? NULL : result.latin1();
299 Adds an object with the given entry to the selection.
301 void SALOMEGUI_Swig::AddIObject( const char* theEntry )
303 class TEvent: public SALOME_Event {
306 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
307 virtual void Execute() {
308 if ( SalomeApp_Application* anApp = getApplication() ) {
309 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
310 SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr();
311 if ( aStudy && aSelMgr ) {
312 SALOME_ListIO anIOList;
313 anIOList.Append( new SALOME_InteractiveObject( myEntry, "", "" ) );
314 aSelMgr->setSelectedObjects( anIOList, true );
319 ProcessVoidEvent( new TEvent( theEntry ) );
323 Removes the object with the given entry from the selection.
325 void SALOMEGUI_Swig::RemoveIObject( const char* theEntry )
327 class TEvent: public SALOME_Event {
330 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
331 virtual void Execute() {
332 if ( SalomeApp_Application* anApp = getApplication() ) {
333 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
334 SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr();
335 if ( aStudy && aSelMgr ) {
336 SALOME_ListIO anIOList;
337 // VSR: temporary solution, until SalomeApp_SelectionMgr::unsetSelectedObjects() method appears
338 // Lately this should be replaced by the following:
339 // anIOList.Append( new SALOME_InteractiveObject( myEntry, "", "" ) );
340 // aSelMgr->unsetSelectedObjects( anIOList );
341 ///////////////////////////////////////////////
342 aSelMgr->selectedObjects( anIOList );
343 SALOME_ListIteratorOfListIO anIter( anIOList );
344 for( ; anIter.More(); anIter.Next() ) {
345 if ( anIter.Value()->isSame( new SALOME_InteractiveObject( myEntry, "", "" ) ) ) {
346 anIOList.Remove( anIter );
347 aSelMgr->setSelectedObjects( anIOList, true );
355 ProcessVoidEvent( new TEvent( theEntry ) );
361 void SALOMEGUI_Swig::ClearIObjects()
363 class TEvent: public SALOME_Event {
366 virtual void Execute() {
367 if ( SalomeApp_Application* anApp = getApplication() ) {
368 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
369 SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr();
370 if ( aStudy && aSelMgr )
371 aSelMgr->clearSelected();
375 ProcessVoidEvent( new TEvent() );
379 Displays an object in the current view window
380 (the presentable object should be previously created and displayed in this viewer).
381 VSR: For the current moment implemented for OCC and VTK viewers only.
383 void SALOMEGUI_Swig::Display( const char* theEntry )
385 class TEvent: public SALOME_Event {
388 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
389 virtual void Execute() {
390 if ( SalomeApp_Application* anApp = getApplication() ) {
391 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
393 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
395 view->Display( view->CreatePrs( myEntry ) );
400 ProcessVoidEvent( new TEvent( theEntry ) );
404 Displays an object in the current view window and erases all other
405 (the presentable object should be previously created and displayed in this viewer).
406 VSR: For the current moment implemented for OCC and VTK viewers only.
408 void SALOMEGUI_Swig::DisplayOnly( const char* theEntry )
410 class TEvent: public SALOME_Event {
413 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
414 virtual void Execute() {
415 if ( SalomeApp_Application* anApp = getApplication() ) {
416 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
418 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
420 view->EraseAll( false );
421 view->Display( view->CreatePrs( myEntry ) );
427 ProcessVoidEvent( new TEvent( theEntry ) );
431 Erases an object in the current view window
432 (the presentable object should be previously created and displayed in this viewer).
433 VSR: For the current moment implemented for OCC and VTK viewers only.
435 void SALOMEGUI_Swig::Erase( const char* theEntry )
437 class TEvent: public SALOME_Event {
440 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
441 virtual void Execute() {
442 if ( SalomeApp_Application* anApp = getApplication() ) {
443 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
445 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
447 view->Erase( view->CreatePrs( myEntry ) );
452 ProcessVoidEvent( new TEvent( theEntry ) );
456 Displays all active module's child objects in the current view window
457 (the presentable objects should be previously created and displayed in this viewer).
458 VSR: For the current moment implemented for OCC and VTK viewers only.
460 void SALOMEGUI_Swig::DisplayAll()
462 class TEvent: public SALOME_Event {
465 virtual void Execute() {
466 if ( SalomeApp_Application* anApp = getApplication() ) {
467 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
468 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
469 SalomeApp_Module* activeModule = dynamic_cast<SalomeApp_Module*>( anApp->activeModule() );
470 if ( study && window && activeModule ) {
471 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
473 for ( SUIT_DataObjectIterator it( activeModule->dataModel()->root(), SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
474 SalomeApp_DataObject* obj = dynamic_cast<SalomeApp_DataObject*>( it.current() );
475 if ( obj && !obj->entry().isEmpty() )
476 view->Display( view->CreatePrs( obj->entry() ) );
483 ProcessVoidEvent( new TEvent() );
487 Erases all objects from the current view window
488 VSR: For the current moment implemented for OCC and VTK viewers only.
490 void SALOMEGUI_Swig::EraseAll()
492 class TEvent: public SALOME_Event {
495 virtual void Execute() {
496 if ( SalomeApp_Application* anApp = getApplication() ) {
497 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
499 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
501 view->EraseAll( false );
506 ProcessVoidEvent( new TEvent() );
510 Returns TRUE if the object with given entry is in the current viewer.
511 VSR: For the current moment implemented for OCC and VTK viewers only.
513 class TIsInViewerEvent: public SALOME_Event {
516 typedef bool TResult;
518 TIsInViewerEvent( const char* theEntry ) : myEntry( theEntry ), myResult( false ) {}
519 virtual void Execute() {
520 if ( SalomeApp_Application* anApp = getApplication() ) {
521 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
523 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
525 SALOME_Prs* aPrs = view->CreatePrs( myEntry );
526 myResult = aPrs->IsNull();
532 bool SALOMEGUI_Swig::IsInCurrentView( const char* theEntry )
534 return ProcessEvent( new TIsInViewerEvent( theEntry ) );