1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
23 // File : SALOMEGUI_Swig.cxx
24 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
26 #include "SALOMEGUI_Swig.hxx"
28 #include <SUIT_Session.h>
29 #include <SUIT_Desktop.h>
30 #include <SUIT_ViewWindow.h>
31 #include <SUIT_ViewManager.h>
32 #include <SUIT_DataObjectIterator.h>
33 #include <CAM_DataModel.h>
34 #include <SalomeApp_Application.h>
35 #include <SalomeApp_Study.h>
36 #include <SalomeApp_Module.h>
37 #include <SalomeApp_DataObject.h>
38 #include <LightApp_SelectionMgr.h>
39 #include <LightApp_DataOwner.h>
40 #include <SALOME_Prs.h>
41 #include <SOCC_ViewModel.h>
42 #include <SVTK_ViewModel.h>
43 #include <SVTK_ViewWindow.h>
44 #include <SOCC_ViewWindow.h>
45 #include <SPlot2d_ViewWindow.h>
47 #include <SALOME_Event.h>
48 #include <SALOME_ListIO.hxx>
49 #include <SALOME_InteractiveObject.hxx>
50 #include <SALOME_ListIteratorOfListIO.hxx>
54 \brief Python interface module for SALOME GUI.
56 This module provides an access to the SALOME GUI implementing set of functions
57 which can be used from Python. This module is implemented using SWIG wrappings
58 for some GUI functionality:
59 - getActiveStudyId(), getActiveStudyName() : get active study identifier and name
60 - updateObjBrowser() : update contents of the Object Browser
61 - SelectedCount() : get number of currently selected items
62 - getSelected() : get entry of the speicified selected item
63 - ClearIObjects() : clear selection
64 - Display(), DisplayOnly(), Erase() : display/erase objects
67 Instance of this class is created every time "import salome" line is typed
68 - in IAPP embedded Python interpretor (SALOME_Session_Server executable)
69 - in inline Python nodes in Supervisor (in SALOME_Container executable)
70 - in stand-alone Python console outside any executable
72 SALOME GUI (desktop and other objects) is only available in SALOME_Session_Server.
73 It means that it can not be accessed from the external Python console.
78 sg = libSALOME_Swig.SALOMEGUI_Swig()
80 selcount = sg.SelectedCount()
82 sg.Erase( sg.getSelected( 0 ) )
88 --- INTERNAL COMMENTS SECTION ---
90 ASV : 03.12.04 : added checking for NULL GUI objects in almost all methods.
91 In the scope of fixing bug PAL6869.
93 VSR : 19.04.05 : Reimplemented for new SALOME GUI (SUIT-based)
94 All methods are implemeted using Event mechanism.
95 Display/Erase methods use SALOME_Prs/SALOME_View mechanism. It is currently
96 implemented only for OCC and VTK viewers.
100 \brief Get active application object
102 \return active application or 0 if there is no any
104 static SalomeApp_Application* getApplication()
106 if ( SUIT_Session::session() )
107 return dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
112 \brief Get active study object
114 \return active study or 0 if there is no study opened
116 static SalomeApp_Study* getActiveStudy()
118 if ( getApplication() )
119 return dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
126 SALOMEGUI_Swig::SALOMEGUI_Swig()
133 SALOMEGUI_Swig::~SALOMEGUI_Swig()
138 \fn bool SALOMEGUI_Swig::hasDesktop()
139 \brief Check GUI availability.
140 \return \c true if GUI is available
143 class THasDesktopEvent: public SALOME_Event
146 typedef bool TResult;
148 THasDesktopEvent() : myResult( false ) {}
149 virtual void Execute()
151 myResult = (bool)( getApplication() && getApplication()->desktop() );
154 bool SALOMEGUI_Swig::hasDesktop()
156 return ProcessEvent( new THasDesktopEvent() );
160 \brief Update active study's Object Browser.
161 \param updateSelection this parameter is obsolete
163 void SALOMEGUI_Swig::updateObjBrowser( bool /*updateSelection*/ )
165 class TEvent: public SALOME_Event
169 virtual void Execute()
171 if ( SalomeApp_Application* anApp = getApplication() ) {
172 anApp->updateObjectBrowser();
173 anApp->updateActions(); //SRN: added in order to update the toolbar
177 ProcessVoidEvent( new TEvent() );
181 \fn int SALOMEGUI_Swig::getActiveStudyId()
182 \brief Get active study identifier
183 \return active study's ID or 0 if there is no active study
186 class TGetActiveStudyIdEvent: public SALOME_Event
191 TGetActiveStudyIdEvent() : myResult( 0 ) {}
192 virtual void Execute()
194 if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
195 myResult = aStudy->studyDS()->StudyId();
199 int SALOMEGUI_Swig::getActiveStudyId()
201 return ProcessEvent( new TGetActiveStudyIdEvent() );
205 \fn const char* SALOMEGUI_Swig::getActiveStudyName()
206 \brief Get active study name
207 \return active study's name or null string if there is no active study
210 class TGetActiveStudyNameEvent: public SALOME_Event
213 typedef std::string TResult;
215 TGetActiveStudyNameEvent() {}
216 virtual void Execute()
218 if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
219 myResult = aStudy->studyDS()->Name();
223 const char* SALOMEGUI_Swig::getActiveStudyName()
225 std::string result = ProcessEvent( new TGetActiveStudyNameEvent() );
226 return result.empty() ? 0 : result.c_str();
230 \fn const char* SALOMEGUI_Swig::getComponentName( const char* componentUserName )
231 \brief Get name of the component by its title (user name)
232 \param componentUserName component title (user name)
233 \return component name or null string if component title is invalid
237 \fn const char* SALOMEGUI_Swig::getComponentUserName( const char* componentName )
238 \brief Get title (user name) of the component by its name
239 \param componentName component name
240 \return component title or null string if component name is invalid
243 class TGetComponentNameEvent: public SALOME_Event
246 typedef QString TResult;
250 TGetComponentNameEvent( const QString& name, bool isUserName )
251 : myName( name ), myIsUserName( isUserName ) {}
252 virtual void Execute()
254 if ( SalomeApp_Application* app = getApplication() ) {
255 myResult = myIsUserName ? app->moduleTitle( myName ) : app->moduleName( myName );
259 const char* SALOMEGUI_Swig::getComponentName( const char* componentUserName )
261 QString result = ProcessEvent( new TGetComponentNameEvent( componentUserName, false ) );
262 return result.isEmpty() ? 0 : strdup( result.toLatin1().constData() );
264 const char* SALOMEGUI_Swig::getComponentUserName( const char* componentName )
266 QString result = ProcessEvent( new TGetComponentNameEvent( componentName, true ) );
267 return result.isEmpty() ? 0 : strdup( result.toLatin1().constData() );
271 \fn int SALOMEGUI_Swig::SelectedCount()
272 \brief Get number of selected items
273 \return number of selected items in the active study
277 \fn const char* SALOMEGUI_Swig::getSelected( int index )
278 \brief Get entry of the specified selected item
279 \param index selected object index
280 \return selected object entry (null string if index is invalid)
283 class TGetSelectedEvent: public SALOME_Event
286 typedef QStringList TResult;
288 TGetSelectedEvent() {}
289 virtual void Execute()
291 if ( SalomeApp_Application* anApp = getApplication() ) {
292 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
293 LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr();
294 if ( aStudy && aSelMgr ) {
295 SUIT_DataOwnerPtrList aList;
296 aSelMgr->selected( aList );
298 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin();
299 itr != aList.end(); ++itr ) {
300 const LightApp_DataOwner* owner =
301 dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
304 QString entry = owner->entry();
305 if( !myResult.contains( entry ) )
306 myResult.append( entry );
312 int SALOMEGUI_Swig::SelectedCount()
314 QStringList selected = ProcessEvent( new TGetSelectedEvent() );
315 return selected.count();
317 const char* SALOMEGUI_Swig::getSelected( int index )
319 QStringList selected = ProcessEvent( new TGetSelectedEvent() );
320 return index >= 0 && index < selected.count() ?
321 strdup( selected[ index ].toLatin1().constData() ) : 0;
325 \brief Add an object to the current selection.
326 \param theEntry object entry
328 void SALOMEGUI_Swig::AddIObject( const char* theEntry )
330 class TEvent: public SALOME_Event
334 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
335 virtual void Execute()
337 if ( SalomeApp_Application* anApp = getApplication() ) {
338 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
339 LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr();
340 if ( aStudy && aSelMgr ) {
341 SALOME_ListIO anIOList;
342 anIOList.Append( new SALOME_InteractiveObject( myEntry.toLatin1(), "", "" ) );
343 aSelMgr->setSelectedObjects( anIOList, true );
348 ProcessVoidEvent( new TEvent( theEntry ) );
352 \brief Remove the object from the selection.
353 \param theEntry object entry
355 void SALOMEGUI_Swig::RemoveIObject( const char* theEntry )
357 class TEvent: public SALOME_Event
361 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
362 virtual void Execute()
364 if ( SalomeApp_Application* anApp = getApplication() ) {
365 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
366 LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr();
367 if ( aStudy && aSelMgr ) {
368 SALOME_ListIO anIOList;
369 // VSR: temporary solution, until LightApp_SelectionMgr::unsetSelectedObjects() method appears
370 // Lately this should be replaced by the following:
371 // anIOList.Append( new SALOME_InteractiveObject( myEntry, "", "" ) );
372 // aSelMgr->unsetSelectedObjects( anIOList );
373 ///////////////////////////////////////////////
374 aSelMgr->selectedObjects( anIOList );
375 SALOME_ListIteratorOfListIO anIter( anIOList );
376 for( ; anIter.More(); anIter.Next() ) {
377 if ( anIter.Value()->isSame( new SALOME_InteractiveObject( myEntry.toLatin1(), "", "" ) ) ) {
378 anIOList.Remove( anIter );
379 aSelMgr->setSelectedObjects( anIOList, true );
387 ProcessVoidEvent( new TEvent( theEntry ) );
391 \brief Clear selection (unselect all objects).
393 void SALOMEGUI_Swig::ClearIObjects()
395 class TEvent: public SALOME_Event
399 virtual void Execute()
401 if ( SalomeApp_Application* anApp = getApplication() ) {
402 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
403 LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr();
404 if ( aStudy && aSelMgr )
405 aSelMgr->clearSelected();
409 ProcessVoidEvent( new TEvent() );
413 \brief Display an object in the current view window.
415 The presentable object should be previously created and
416 displayed in this viewer.
418 For the current moment implemented for OCC and VTK viewers only.
420 \param theEntry object entry
422 void SALOMEGUI_Swig::Display( const char* theEntry )
424 class TEvent: public SALOME_Event
428 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
429 virtual void Execute() {
430 if ( SalomeApp_Application* anApp = getApplication() ) {
431 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
433 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
435 view->Display( view->CreatePrs( myEntry.toLatin1() ) );
440 ProcessVoidEvent( new TEvent( theEntry ) );
444 \brief Displays an object in the current view window and
445 erases all other ones.
447 The presentable object should be previously created and
448 displayed in this viewer.
450 For the current moment implemented for OCC and VTK viewers only.
452 \param theEntry object entry
454 void SALOMEGUI_Swig::DisplayOnly( const char* theEntry )
456 class TEvent: public SALOME_Event
460 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
461 virtual void Execute()
463 if ( SalomeApp_Application* anApp = getApplication() ) {
464 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
466 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
468 view->EraseAll( false );
469 view->Display( view->CreatePrs( myEntry.toLatin1() ) );
475 ProcessVoidEvent( new TEvent( theEntry ) );
479 \brief Erase an object in the current view window.
481 The presentable object should be previously created and
482 displayed in this viewer.
484 For the current moment implemented for OCC and VTK viewers only.
486 \param theEntry object entry
488 void SALOMEGUI_Swig::Erase( const char* theEntry )
490 class TEvent: public SALOME_Event
494 TEvent( const char* theEntry ) : myEntry( theEntry ) {}
495 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->Erase( view->CreatePrs( myEntry.toLatin1() ) );
507 ProcessVoidEvent( new TEvent( theEntry ) );
511 \brief Display all active module's presentable
512 child objects in the current view window.
514 The presentable objects should be previously created and
515 displayed in this viewer.
517 For the current moment implemented for OCC and VTK viewers only.
519 void SALOMEGUI_Swig::DisplayAll()
521 class TEvent: public SALOME_Event
525 virtual void Execute()
527 if ( SalomeApp_Application* anApp = getApplication() ) {
528 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
529 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
530 SalomeApp_Module* activeModule = dynamic_cast<SalomeApp_Module*>( anApp->activeModule() );
531 if ( study && window && activeModule ) {
532 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
534 for ( SUIT_DataObjectIterator it( activeModule->dataModel()->root(), SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
535 SalomeApp_DataObject* obj = dynamic_cast<SalomeApp_DataObject*>( it.current() );
536 if ( obj && !obj->entry().isEmpty() )
537 view->Display( view->CreatePrs( obj->entry().toLatin1() ) );
544 ProcessVoidEvent( new TEvent() );
548 \brief Erase all objects from the current view window.
550 For the current moment implemented for OCC and VTK viewers only.
552 void SALOMEGUI_Swig::EraseAll()
554 class TEvent: public SALOME_Event
558 virtual void Execute()
560 if ( SalomeApp_Application* anApp = getApplication() ) {
561 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
563 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
565 view->EraseAll( false );
570 ProcessVoidEvent( new TEvent() );
574 \fn bool SALOMEGUI_Swig::IsInCurrentView( const char* theEntry )
575 \brief Check it the object is displayed in the current view window.
577 VSR: For the current moment implemented for OCC and VTK viewers only.
579 \param theEntry object entry
580 \return \c true if the object with given entry is displayed
581 in the current viewer
584 class TIsInViewerEvent: public SALOME_Event
588 typedef bool TResult;
590 TIsInViewerEvent( const char* theEntry ) : myEntry( theEntry ), myResult( false ) {}
591 virtual void Execute()
593 if ( SalomeApp_Application* anApp = getApplication() ) {
594 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
596 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
598 SALOME_Prs* aPrs = view->CreatePrs( myEntry.toLatin1() );
599 myResult = !aPrs->IsNull();
605 bool SALOMEGUI_Swig::IsInCurrentView( const char* theEntry )
607 return ProcessEvent( new TIsInViewerEvent( theEntry ) );
611 \brief Update (repaint) current view window.
613 void SALOMEGUI_Swig::UpdateView()
615 class TEvent: public SALOME_Event
619 virtual void Execute()
621 if ( SalomeApp_Application* anApp = getApplication() ) {
622 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
624 SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
631 ProcessVoidEvent( new TEvent() );
635 \brief Fit current view window to display all its contents.
637 void SALOMEGUI_Swig::FitAll()
639 class TEvent: public SALOME_Event
643 virtual void Execute()
645 if ( SalomeApp_Application* anApp = getApplication() ) {
646 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
648 if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
649 ( dynamic_cast<SVTK_ViewWindow*>( window ) )->onFitAll();
650 else if ( dynamic_cast<SOCC_ViewWindow*>( window ) )
651 ( dynamic_cast<SOCC_ViewWindow*>( window ) )->onFitAll();
652 else if ( dynamic_cast<SPlot2d_ViewWindow*>( window ) )
653 ( dynamic_cast<SPlot2d_ViewWindow*>( window ) )->onFitAll();
658 ProcessVoidEvent( new TEvent() );
662 \brief Reset current view window to the default state.
664 void SALOMEGUI_Swig::ResetView()
666 class TEvent: public SALOME_Event
670 virtual void Execute()
672 if ( SalomeApp_Application* anApp = getApplication() ) {
673 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
675 if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
676 (dynamic_cast<SVTK_ViewWindow*>( window ))->onResetView();
677 else if ( dynamic_cast<SOCC_ViewWindow*>( window ) )
678 (dynamic_cast<SOCC_ViewWindow*>( window ))->onResetView();
679 else if ( dynamic_cast<SPlot2d_ViewWindow*>( window ) )
680 (dynamic_cast<SPlot2d_ViewWindow*>( window ))->onFitAll();
681 // VSR: there is no 'ResetView' functionality for Plot2d viewer,
682 // so we use 'FitAll' instead.
687 ProcessVoidEvent( new TEvent() );
691 \brief View operation type.
695 __ViewTop, //!< view top side
696 __ViewBottom, //!< view bottom side
697 __ViewLeft, //!< view left side
698 __ViewRight, //!< view right side
699 __ViewFront, //!< view front side
700 __ViewBack //!< view back side
704 \brief Change the view of the current view window.
706 \param view view operation type
708 static void setView( int view )
710 class TEvent: public SALOME_Event
715 TEvent( int view ) : myView( view ) {}
716 virtual void Execute()
718 if ( SalomeApp_Application* anApp = getApplication() ) {
719 SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
721 if ( dynamic_cast<SVTK_ViewWindow*>( window ) ) {
724 (dynamic_cast<SVTK_ViewWindow*>( window ))->onTopView(); break;
726 (dynamic_cast<SVTK_ViewWindow*>( window ))->onBottomView(); break;
728 (dynamic_cast<SVTK_ViewWindow*>( window ))->onLeftView(); break;
730 (dynamic_cast<SVTK_ViewWindow*>( window ))->onRightView(); break;
732 (dynamic_cast<SVTK_ViewWindow*>( window ))->onFrontView(); break;
734 (dynamic_cast<SVTK_ViewWindow*>( window ))->onBackView(); break;
739 else if ( dynamic_cast<SOCC_ViewWindow*>( window ) ) {
742 (dynamic_cast<SOCC_ViewWindow*>( window ))->onTopView(); break;
744 (dynamic_cast<SOCC_ViewWindow*>( window ))->onBottomView(); break;
746 (dynamic_cast<SOCC_ViewWindow*>( window ))->onLeftView(); break;
748 (dynamic_cast<SOCC_ViewWindow*>( window ))->onRightView(); break;
750 (dynamic_cast<SOCC_ViewWindow*>( window ))->onFrontView(); break;
752 (dynamic_cast<SOCC_ViewWindow*>( window ))->onBackView(); break;
761 ProcessVoidEvent( new TEvent( view ) );
765 \brief Switch current view window to show the top view.
767 void SALOMEGUI_Swig::ViewTop()
769 setView( __ViewTop );
773 \brief Switch current view window to show the bottom view
775 void SALOMEGUI_Swig::ViewBottom()
777 setView( __ViewBottom );
781 \brief Switch current view window to show the left view
783 void SALOMEGUI_Swig::ViewLeft()
785 setView( __ViewLeft );
789 \brief Switch current view window to show the right view
791 void SALOMEGUI_Swig::ViewRight()
793 setView( __ViewRight );
797 \brief Switch current view window to show the front view
799 void SALOMEGUI_Swig::ViewFront()
801 setView( __ViewFront );
805 \brief Switch current view window to show the back view
807 void SALOMEGUI_Swig::ViewBack()
809 setView( __ViewBack );