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 "LightApp_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 =
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();
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 =
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();
236 SALOMEGUI_Swig::SelectedCount
237 Returns the number of selected objects.
239 class TSelectedCountEvent: public SALOME_Event {
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();
256 int SALOMEGUI_Swig::SelectedCount()
258 return ProcessEvent( new TSelectedCountEvent() );
262 SALOMEGUI_Swig::getSelected
263 Returns the selected object entry by the given index.
265 class TGetSelectedEvent: public SALOME_Event {
267 typedef QString TResult;
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() ) {
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();
293 const char* SALOMEGUI_Swig::getSelected( int index )
295 QString result = ProcessEvent( new TGetSelectedEvent( index ) );
296 return result.isEmpty() ? NULL : strdup(result.latin1());
300 Adds an object with the given entry to the selection.
302 void SALOMEGUI_Swig::AddIObject( const char* theEntry )
304 class TEvent: public SALOME_Event {
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 );
320 ProcessVoidEvent( new TEvent( theEntry ) );
324 Removes the object with the given entry from the selection.
326 void SALOMEGUI_Swig::RemoveIObject( const char* theEntry )
328 class TEvent: public SALOME_Event {
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 );
356 ProcessVoidEvent( new TEvent( theEntry ) );
362 void SALOMEGUI_Swig::ClearIObjects()
364 class TEvent: public SALOME_Event {
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();
376 ProcessVoidEvent( new TEvent() );
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.
384 void SALOMEGUI_Swig::Display( const char* theEntry )
386 class TEvent: public SALOME_Event {
389 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
390 virtual void Execute() {
391 if ( SalomeApp_Application* anApp = getApplication() ) {
392 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
394 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
396 view->Display( view->CreatePrs( myEntry ) );
401 ProcessVoidEvent( new TEvent( theEntry ) );
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.
409 void SALOMEGUI_Swig::DisplayOnly( const char* theEntry )
411 class TEvent: public SALOME_Event {
414 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
415 virtual void Execute() {
416 if ( SalomeApp_Application* anApp = getApplication() ) {
417 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
419 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
421 view->EraseAll( false );
422 view->Display( view->CreatePrs( myEntry ) );
428 ProcessVoidEvent( new TEvent( theEntry ) );
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.
436 void SALOMEGUI_Swig::Erase( const char* theEntry )
438 class TEvent: public SALOME_Event {
441 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
442 virtual void Execute() {
443 if ( SalomeApp_Application* anApp = getApplication() ) {
444 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
446 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
448 view->Erase( view->CreatePrs( myEntry ) );
453 ProcessVoidEvent( new TEvent( theEntry ) );
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.
461 void SALOMEGUI_Swig::DisplayAll()
463 class TEvent: public SALOME_Event {
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() );
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() ) );
484 ProcessVoidEvent( new TEvent() );
488 Erases all objects from the current view window
489 VSR: For the current moment implemented for OCC and VTK viewers only.
491 void SALOMEGUI_Swig::EraseAll()
493 class TEvent: public SALOME_Event {
496 virtual void Execute() {
497 if ( SalomeApp_Application* anApp = getApplication() ) {
498 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
500 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
502 view->EraseAll( false );
507 ProcessVoidEvent( new TEvent() );
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.
514 class TIsInViewerEvent: public SALOME_Event {
517 typedef bool TResult;
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();
524 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
526 SALOME_Prs* aPrs = view->CreatePrs( myEntry );
527 myResult = aPrs->IsNull();
533 bool SALOMEGUI_Swig::IsInCurrentView( const char* theEntry )
535 return ProcessEvent( new TIsInViewerEvent( theEntry ) );