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 anApp->updateObjectBrowser();
141 anApp->updateActions(); //SRN: added in order to update the toolbar
145 ProcessVoidEvent( new TEvent() );
149 SALOMEGUI_Swig::getActiveStudyId
150 Returns active study's ID or 0 if there is no active study.
152 class TGetActiveStudyIdEvent: public SALOME_Event {
156 TGetActiveStudyIdEvent() : myResult( 0 ) {}
157 virtual void Execute() {
158 if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
159 myResult = aStudy->studyDS()->StudyId();
163 int SALOMEGUI_Swig::getActiveStudyId()
165 return ProcessEvent( new TGetActiveStudyIdEvent() );
169 SALOMEGUI_Swig::getActiveStudyName
170 Returns active study's name or NULL if there is no active study.
172 class TGetActiveStudyNameEvent: public SALOME_Event {
174 typedef string TResult;
176 TGetActiveStudyNameEvent() {}
177 virtual void Execute() {
178 if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
179 myResult = aStudy->studyDS()->Name();
183 const char* SALOMEGUI_Swig::getActiveStudyName()
185 string result = ProcessEvent( new TGetActiveStudyNameEvent() );
186 return result.empty() ? NULL : result.c_str();
190 SALOMEGUI_Swig::getComponentName
191 Returns the name of the component by its user name.
193 const char* SALOMEGUI_Swig::getComponentName( const char* componentUserName )
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();
212 SALOMEGUI_Swig::getComponentUserName
213 Returns the user name of the component by its name.
215 const char* SALOMEGUI_Swig::getComponentUserName( const char* componentName )
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();
234 SALOMEGUI_Swig::SelectedCount
235 Returns the number of selected objects.
237 class TSelectedCountEvent: public SALOME_Event {
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();
254 int SALOMEGUI_Swig::SelectedCount()
256 return ProcessEvent( new TSelectedCountEvent() );
260 SALOMEGUI_Swig::getSelected
261 Returns the selected object entry by the given index.
263 class TGetSelectedEvent: public SALOME_Event {
265 typedef QString TResult;
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() ) {
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();
291 const char* SALOMEGUI_Swig::getSelected( int index )
293 QString result = ProcessEvent( new TGetSelectedEvent( index ) );
294 return result.isEmpty() ? NULL : strdup(result.latin1());
298 Adds an object with the given entry to the selection.
300 void SALOMEGUI_Swig::AddIObject( const char* theEntry )
302 class TEvent: public SALOME_Event {
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 );
318 ProcessVoidEvent( new TEvent( theEntry ) );
322 Removes the object with the given entry from the selection.
324 void SALOMEGUI_Swig::RemoveIObject( const char* theEntry )
326 class TEvent: public SALOME_Event {
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 );
354 ProcessVoidEvent( new TEvent( theEntry ) );
360 void SALOMEGUI_Swig::ClearIObjects()
362 class TEvent: public SALOME_Event {
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();
374 ProcessVoidEvent( new TEvent() );
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.
382 void SALOMEGUI_Swig::Display( const char* theEntry )
384 class TEvent: public SALOME_Event {
387 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
388 virtual void Execute() {
389 if ( SalomeApp_Application* anApp = getApplication() ) {
390 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
392 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
394 view->Display( view->CreatePrs( myEntry ) );
399 ProcessVoidEvent( new TEvent( theEntry ) );
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.
407 void SALOMEGUI_Swig::DisplayOnly( const char* theEntry )
409 class TEvent: public SALOME_Event {
412 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
413 virtual void Execute() {
414 if ( SalomeApp_Application* anApp = getApplication() ) {
415 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
417 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
419 view->EraseAll( false );
420 view->Display( view->CreatePrs( myEntry ) );
426 ProcessVoidEvent( new TEvent( theEntry ) );
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.
434 void SALOMEGUI_Swig::Erase( const char* theEntry )
436 class TEvent: public SALOME_Event {
439 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
440 virtual void Execute() {
441 if ( SalomeApp_Application* anApp = getApplication() ) {
442 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
444 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
446 view->Erase( view->CreatePrs( myEntry ) );
451 ProcessVoidEvent( new TEvent( theEntry ) );
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.
459 void SALOMEGUI_Swig::DisplayAll()
461 class TEvent: public SALOME_Event {
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() );
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() ) );
482 ProcessVoidEvent( new TEvent() );
486 Erases all objects from the current view window
487 VSR: For the current moment implemented for OCC and VTK viewers only.
489 void SALOMEGUI_Swig::EraseAll()
491 class TEvent: public SALOME_Event {
494 virtual void Execute() {
495 if ( SalomeApp_Application* anApp = getApplication() ) {
496 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
498 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
500 view->EraseAll( false );
505 ProcessVoidEvent( new TEvent() );
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.
512 class TIsInViewerEvent: public SALOME_Event {
515 typedef bool TResult;
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();
522 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
524 SALOME_Prs* aPrs = view->CreatePrs( myEntry );
525 myResult = aPrs->IsNull();
531 bool SALOMEGUI_Swig::IsInCurrentView( const char* theEntry )
533 return ProcessEvent( new TIsInViewerEvent( theEntry ) );