1 //=============================================================================
2 // File : SALOME_PYQT_Module.cxx
4 // Author : Vadim SANDLER
6 // Copyright : 2003-2005 CEA/DEN, EDF R&D
8 //=============================================================================
10 #include "SALOME_PYQT_Module.h"
12 #include "PyInterp_Dispatcher.h"
13 #include "SUIT_ResourceMgr.h"
14 #include "STD_MDIDesktop.h"
15 #include "STD_TabDesktop.h"
16 #include "SalomeApp_Application.h"
17 #include "SalomeApp_Study.h"
19 #include "QtxWorkstack.h"
20 #include <SALOME_LifeCycleCORBA.hxx>
21 #include <Container_init_python.hxx>
25 #include <qworkspace.h>
26 #include <qpopupmenu.h>
28 #include "SALOME_PYQT_SipDefs.h"
29 #if defined(SIP_VERS_v4_old) || defined(SIP_VERS_v4_new)
30 #include "sipAPISalomePyQtGUI.h"
32 #include "sipSalomePyQtGUIDeclSalomePyQtGUI.h"
35 #include <sipqtQWidget.h>
36 #include <sipqtQPopupMenu.h>
42 ///////////////////////////////////////////////////////////////////////////////
43 // Default name of the module, replaced at the moment of module creation
44 #define __DEFAULT_NAME__ "SALOME_PYQT_Module"
46 ///////////////////////////////////////////////////////////////////////////////
47 // If __CALL_OLD_METHODS__ macro is not defined the invoking of obsolete Python
48 // module's methods like setSetting(), definePopup(), etc. is blocked.
49 // This macro is defined by default (in Makefile)
50 #ifdef __CALL_OLD_METHODS__
51 const bool IsCallOldMethods = true;
53 const bool IsCallOldMethods = false;
56 ///////////////////////////////////////////////////////////////////////////////
57 // NB: Python requests.
58 // General rule for Python requests created by SALOME_PYQT_Module:
59 // all requests should be executed SYNCHRONOUSLY within the main GUI thread.
60 // However, it is obligatory that ANY Python call is wrapped with a request object,
61 // so that ALL Python API calls are serialized with PyInterp_Dispatcher.
62 ///////////////////////////////////////////////////////////////////////////////
64 //=============================================================================
65 // The class for parsing of the XML resource files.
66 // Used for backward compatibility with existing Python modules.
67 //=============================================================================
68 class SALOME_PYQT_XmlHandler
71 SALOME_PYQT_XmlHandler( SALOME_PYQT_Module* module, const QString& fileName );
73 void createPopup ( QPopupMenu* menu,
74 const QString& context,
75 const QString& parent,
76 const QString& object );
79 void createToolBar ( QDomNode& parentNode );
80 void createMenu ( QDomNode& parentNode,
81 const int parentMenuId = -1 );
83 void insertPopupItems( QDomNode& parentNode,
87 SALOME_PYQT_Module* myModule;
91 //=============================================================================
92 // SALOME_PYQT_Module class implementation (implements CAM_Module API for
93 // all Python-based SALOME module
94 //=============================================================================
96 // While the SalomePyQtGUI library is not imported in Python it's initialization function
97 // should be called manually (and only once) in order to initialize global sip data
98 // and to get C API from sip : sipBuildResult for example
99 #if defined(SIP_STATIC_MODULE)
100 extern "C" void initSalomePyQtGUI();
102 PyMODINIT_FUNC initSalomePyQtGUI();
106 * This function creates an instance of SALOME_PYQT_Module object by request
107 * of and application object when the module is loaded.
110 SALOME_PYQT_EXPORT CAM_Module* createModule() {
111 static bool alreadyInitialized = false;
112 if ( !alreadyInitialized ) {
113 // call only once (see above) !
114 PyEval_RestoreThread( KERNEL_PYTHON::_gtstate );
116 PyEval_ReleaseThread( KERNEL_PYTHON::_gtstate );
117 alreadyInitialized = !alreadyInitialized;
119 return new SALOME_PYQT_Module();
124 * Static variables definition
126 SALOME_PYQT_Module::InterpMap SALOME_PYQT_Module::myInterpMap;
127 SALOME_PYQT_Module* SALOME_PYQT_Module::myInitModule = 0;
130 * Little trick : provide an access to being activated Python module from outside;
131 * needed by the SalomePyQt library :(
133 SALOME_PYQT_Module* SALOME_PYQT_Module::getInitModule()
141 SALOME_PYQT_Module::SALOME_PYQT_Module() :
142 SalomeApp_Module( __DEFAULT_NAME__ ), myModule( 0 ), myXmlHandler ( 0 )
144 myMenuActionList.setAutoDelete( false );
145 myPopupActionList.setAutoDelete( false );
146 myToolbarActionList.setAutoDelete( false );
152 SALOME_PYQT_Module::~SALOME_PYQT_Module()
154 myMenuActionList.clear();
155 myPopupActionList.clear();
156 myToolbarActionList.clear();
162 * Initialization of the module.
163 * Inherited from CAM_Module.
165 * This method is used for creation of the menus, toolbars and other staff.
166 * There are two ways:
167 * - for obsolete modules this method first tries to read <module>_<language>.xml
168 * resource file which contains a menu, toolbars and popup menus description.
169 * - new modules can create menus by by calling the corresponding methods of SalomePyQt
170 * Python API in the Python module's initialize() method which is called from here.
171 * NOTE: if postponed modules loading is not used, the active study might be not defined
172 * yet at this stage, so initialize() method should not perform any study-based initialization.
174 void SALOME_PYQT_Module::initialize( CAM_Application* app )
176 SalomeApp_Module::initialize( app );
178 // Try to get XML resource file name
179 SUIT_ResourceMgr* aResMgr = getApp()->resourceMgr();
180 QString aLang = aResMgr->stringValue( "language", "language", QString::null );
181 if ( aLang.isEmpty() ) aLang = QString( "en" );
182 QString aName = name( "" );
183 QString aFileName = aName + "_" + aLang + ".xml";
184 aFileName = aResMgr->path( "resources", aName, aFileName );
186 // parse XML file if it is found and create actions
187 if ( !myXmlHandler && !aFileName.isEmpty() ) {
188 myXmlHandler = new SALOME_PYQT_XmlHandler( this, aFileName );
189 myXmlHandler->createActions();
192 // perform internal initialization and call module's initialize() method
193 // InitializeReq: request class for internal init() operation
194 class InitializeReq : public PyInterp_Request
197 InitializeReq( CAM_Application* _app,
198 SALOME_PYQT_Module* _obj )
199 : PyInterp_Request( 0, true ), // this request should be processed synchronously (sync == true)
204 virtual void execute()
206 myObj->init( myApp );
210 CAM_Application* myApp;
211 SALOME_PYQT_Module* myObj;
214 // Posting the request
215 PyInterp_Dispatcher::Get()->Exec( new InitializeReq( app, this ) );
219 * Activation of the module.
220 * Inherited from CAM_Module.
222 bool SALOME_PYQT_Module::activateModule( SUIT_Study* theStudy )
224 MESSAGE( "SALOME_PYQT_Module::activateModule" );
226 bool res = SalomeApp_Module::activateModule( theStudy );
231 // ActivateReq: request class for internal activate() operation
232 class ActivateReq : public PyInterp_Request
235 ActivateReq( SUIT_Study* _study,
236 SALOME_PYQT_Module* _obj )
237 : PyInterp_Request( 0, true ), // this request should be processed synchronously (sync == true)
242 virtual void execute()
244 myObj->activate( myStudy );
249 SALOME_PYQT_Module* myObj;
252 // Posting the request
253 PyInterp_Dispatcher::Get()->Exec( new ActivateReq( theStudy, this ) );
255 // activate menus, toolbars, etc
256 setMenuShown( true );
257 setToolShown( true );
263 * Deactivation of the module.
264 * Inherited from CAM_Module.
266 bool SALOME_PYQT_Module::deactivateModule( SUIT_Study* theStudy )
268 MESSAGE( "SALOME_PYQT_Module::deactivateModule" );
270 bool res = SalomeApp_Module::deactivateModule( theStudy );
272 // deactivate menus, toolbars, etc
273 setMenuShown( false );
274 setToolShown( false );
276 // DeactivateReq: request class for internal deactivate() operation
277 class DeactivateReq : public PyInterp_LockRequest
280 DeactivateReq( PyInterp_base* _py_interp,
282 SALOME_PYQT_Module* _obj )
283 : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
288 virtual void execute()
290 myObj->deactivate( myStudy );
295 SALOME_PYQT_Module* myObj;
298 // Posting the request
299 PyInterp_Dispatcher::Get()->Exec( new DeactivateReq( myInterp, theStudy, this ) );
305 * Processes GUI action (from main menu, toolbar or context popup menu)
307 void SALOME_PYQT_Module::onGUIEvent()
310 const QObject* obj = sender();
311 if ( !obj || !obj->inherits( "QAction" ) )
313 QAction* action = (QAction*)obj;
316 int id = actionId( action );
317 MESSAGE( "SALOME_PYQT_Module::onGUIEvent: id = " << id );
319 // perform synchronous request to Python event dispatcher
320 class GUIEvent : public PyInterp_LockRequest
323 GUIEvent( PyInterp_base* _py_interp,
324 SALOME_PYQT_Module* _obj,
326 : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
331 virtual void execute()
333 myObj->guiEvent( myId );
338 SALOME_PYQT_Module* myObj;
341 // Posting the request
342 PyInterp_Dispatcher::Get()->Exec( new GUIEvent( myInterp, this, id ) );
346 * Processes GUI action (from context popup menu, only for XML-based actions!)
348 void SALOME_PYQT_Module::onGUIEvent( int id )
350 // perform synchronous request to Python event dispatcher
351 class GUIEvent : public PyInterp_LockRequest
354 GUIEvent( PyInterp_base* _py_interp,
355 SALOME_PYQT_Module* _obj,
357 : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
362 virtual void execute()
364 myObj->guiEvent( myId );
369 SALOME_PYQT_Module* myObj;
372 // Posting the request
373 PyInterp_Dispatcher::Get()->Exec( new GUIEvent( myInterp, this, id ) );
377 Context popup menu request.
378 Called when user activates popup menu in some window (view, object browser, etc).
380 void SALOME_PYQT_Module::contextMenuPopup( const QString& theContext, QPopupMenu* thePopupMenu, QString& /*title*/ )
382 MESSAGE( "SALOME_PYQT_Module::contextMenuPopup : " << theContext.latin1() );
383 // perform synchronous request to Python event dispatcher
384 class PopupMenuEvent : public PyInterp_LockRequest
387 PopupMenuEvent( PyInterp_base* _py_interp,
388 SALOME_PYQT_Module* _obj,
389 const QString& _context,
391 : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
392 myContext( _context ),
397 virtual void execute()
399 myObj->contextMenu( myContext, myPopup );
403 SALOME_PYQT_Module* myObj;
408 // Posting the request only if dispatcher is not busy!
409 // Executing the request synchronously
410 if ( !PyInterp_Dispatcher::Get()->IsBusy() )
411 PyInterp_Dispatcher::Get()->Exec( new PopupMenuEvent( myInterp, this, theContext, thePopupMenu ) );
415 * Defines the dockable window associated with the module.
416 * To fill the list of windows the correspondind Python module's windows()
417 * method is called from SALOME_PYQT_Module::init() method.
418 * By default, ObjectBrowser, PythonConsole and LogWindow are provided.
420 void SALOME_PYQT_Module::windows( QMap<int, int>& mappa ) const
422 // First clear the output parameters
423 QMap<int, int>::ConstIterator it;
424 for ( it = myWindowsMap.begin(); it != myWindowsMap.end(); ++it ) {
425 mappa[ it.key() ] = it.data();
430 * Defines the compatible views which should be opened on module activation.
431 * To fill the list of views the correspondind Python module's views()
432 * method is called from SALOME_PYQT_Module::init() method.
433 * By default, the list is empty.
435 void SALOME_PYQT_Module::viewManagers( QStringList& listik ) const
437 for ( QStringList::ConstIterator it = myViewMgrList.begin(); it != myViewMgrList.end(); ++it ) {
438 listik.append( *it );
443 * Performs internal initialization
444 * - initializes/gets the Python interpreter (one per study)
445 * - imports the Python module
446 * - passes the workspace widget to the Python module
447 * - calls Python module's initialize() method
448 * - calls Python module's windows() method
449 * - calls Python module's views() method
451 void SALOME_PYQT_Module::init( CAM_Application* app )
453 // reset interpreter to NULL
457 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( app );
461 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
464 int aStudyId = aStudy ? aStudy->studyDS()->StudyId() : 0;
466 // initialize Python subinterpreter (on per study) and put it in <myInterp> variable
467 initInterp( aStudyId );
471 // import Python GUI module
478 if ( IsCallOldMethods ) { // __CALL_OLD_METHODS__
479 // call Python module's setWorkspace() method
481 } //__CALL_OLD_METHODS__
483 // then call Python module's initialize() method
484 // ... first get python lock
485 PyLockWrapper aLock = myInterp->GetLockWrapper();
486 // ... (the Python module is already imported)
487 // ... finally call Python module's initialize() method
488 PyObjWrapper res( PyObject_CallMethod( myModule, "initialize", "" ) );
490 // VSR: this method may not be implemented in Python module
494 // get the windows list from the Python module by calling windows() method
495 // ... first put default values
496 myWindowsMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::DockLeft );
497 myWindowsMap.insert( SalomeApp_Application::WT_PyConsole, Qt::DockBottom );
498 // VSR: LogWindow is not yet implemented
499 // myWindowsMap.insert( SalomeApp_Application::WT_LogWindow, Qt::DockBottom );
501 PyObjWrapper res1( PyObject_CallMethod( myModule, "windows", "" ) );
503 // VSR: this method may not be implemented in Python module
507 myWindowsMap.clear();
508 if ( PyDict_Check( res1 ) ) {
512 while ( PyDict_Next( res1, &pos, &key, &value ) ) {
513 // parse the return value
514 // it should be a map: {integer:integer}
516 if( key && PyInt_Check( key ) && value && PyInt_Check( value ) ) {
517 aKey = PyInt_AsLong( key );
518 aValue = PyInt_AsLong( value );
519 myWindowsMap[ aKey ] = aValue;
524 // get the windows list from the Python module by calling views() method
525 PyObjWrapper res2( PyObject_CallMethod( myModule, "views", "" ) );
527 // VSR: this method may not be implemented in Python module
531 // parse the return value
532 // result can be one string...
533 if ( PyString_Check( res2 ) ) {
534 myViewMgrList.append( PyString_AsString( res2 ) );
536 // ... or list of strings
537 else if ( PyList_Check( res2 ) ) {
538 int size = PyList_Size( res2 );
539 for ( int i = 0; i < size; i++ ) {
540 PyObject* value = PyList_GetItem( res2, i );
541 if( value && PyString_Check( value ) ) {
542 myViewMgrList.append( PyString_AsString( value ) );
551 * Performs internal activation:
552 * - initializes/gets the Python interpreter (one per study)
553 * - imports the Python GUI module
554 * - calls Python module's setSettings() method (obsolete function, used for compatibility with old code)
555 * or activate() method (for new modules)
557 void SALOME_PYQT_Module::activate( SUIT_Study* theStudy )
560 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( theStudy );
561 int aStudyId = aStudy ? aStudy->studyDS()->StudyId() : 0;
563 // initialize Python subinterpreter (on per study) and put it in <myInterp> variable
564 initInterp( aStudyId );
568 // import Python GUI module
574 PyLockWrapper aLock = myInterp->GetLockWrapper();
576 if ( IsCallOldMethods ) { //__CALL_OLD_METHODS__
577 // call Python module's setSettings() method (obsolete)
578 PyObjWrapper res( PyObject_CallMethod( myModule, "setSettings", "" ) );
580 // VSR: this method may not be implemented in Python module
583 } //__CALL_OLD_METHODS__
585 // call Python module's activate() method (for the new modules)
586 PyObjWrapper res1( PyObject_CallMethod( myModule, "activate", "" ) );
588 // VSR: this method may not be implemented in Python module
594 * Performs internal deactivation:
595 * - calls Python module's deactivate() method
597 void SALOME_PYQT_Module::deactivate( SUIT_Study* theStudy )
599 // check if the subinterpreter is initialized and Python module is imported
600 if ( !myInterp || !myModule ) {
601 // Error! Python subinterpreter should be initialized and module should be imported first!
604 // then call Python module's deactivate() method
605 PyObjWrapper res( PyObject_CallMethod( myModule, "deactivate", "" ) );
607 // VSR: this method may not be implemented in Python module
613 * Called when active the study is actived (user brings its desktop to top)
614 * - initializes/gets the Python interpreter (one per study)
615 * - imports the Python GUI module
616 * - calls Python module's activeStudyChanged() method
618 void SALOME_PYQT_Module::studyChanged( SUIT_Study* theStudy )
621 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( theStudy );
622 int aStudyId = aStudy ? aStudy->studyDS()->StudyId() : 0;
624 // initialize Python subinterpreter (on per study) and put it in <myInterp> variable
625 initInterp( aStudyId );
629 // import Python GUI module
635 PyLockWrapper aLock = myInterp->GetLockWrapper();
637 // call Python module's activeStudyChanged() method
638 PyObjWrapper res( PyObject_CallMethod( myModule, "activeStudyChanged", "i", aStudyId ) );
640 // VSR: this method may not be implemented in Python module
646 * Get module engine, returns nil var if engine is not found in LifeCycleCORBA
648 Engines::Component_var SALOME_PYQT_Module::getEngine() const
650 Engines::Component_var comp;
651 // temporary solution
653 comp = getApp()->lcc()->FindOrLoad_Component( "FactoryServerPy", name( "" ) );
655 catch (CORBA::Exception&) {
661 * Get module engine IOR, returns empty string if engine is not found in LifeCycleCORBA
663 QString SALOME_PYQT_Module::engineIOR() const
665 if ( !CORBA::is_nil( getEngine() ) )
666 return QString( getApp()->orb()->object_to_string( getEngine() ) );
667 return QString( "" );
671 * Called when study desktop is activated.
672 * Used for notifying about changing of the active study.
674 void SALOME_PYQT_Module::studyActivated()
676 // StudyChangedReq: request class for internal studyChanged() operation
677 class StudyChangedReq : public PyInterp_Request
680 StudyChangedReq( SUIT_Study* _study,
681 SALOME_PYQT_Module* _obj )
682 : PyInterp_Request( 0, true ), // this request should be processed synchronously (sync == true)
687 virtual void execute()
689 myObj->studyChanged( myStudy );
694 SALOME_PYQT_Module* myObj;
697 // Posting the request
698 PyInterp_Dispatcher::Get()->Exec( new StudyChangedReq( application()->activeStudy(), this ) );
702 * Processes context popup menu request
703 * - calls Python module's definePopup(...) method (obsolete function, used for compatibility with old code)
704 * to define the popup menu context
705 * - parses XML resourses file (if exists) and fills the popup menu with the items)
706 * - calls Python module's customPopup(...) method (obsolete function, used for compatibility with old code)
707 * to allow module to customize the popup menu
708 * - for new modules calls createPopupMenu() function to allow the modules to build the popup menu
709 * by using insertItem(...) Qt functions.
711 void SALOME_PYQT_Module::contextMenu( const QString& theContext, QPopupMenu* thePopupMenu )
713 // Python interpreter should be initialized and Python module should be
715 if ( !myInterp || !myModule )
718 QString aContext( theContext ), aObject( "" ), aParent( "" );
720 if ( IsCallOldMethods ) { //__CALL_OLD_METHODS__
721 // call definePopup() Python module's function
722 // this is obsolete function, used only for compatibility reasons
723 PyObjWrapper res(PyObject_CallMethod( myModule,
728 aParent.latin1() ) );
730 // VSR: this method may not be implemented in Python module
734 // parse return value
736 if( PyArg_ParseTuple( res, "sss", &co, &ob, &pa ) ) {
742 } //__CALL_OLD_METHODS__
744 // first try to create menu via XML parser:
745 // we create popup menus without help of QtxPopupMgr
747 myXmlHandler->createPopup( thePopupMenu, aContext, aParent, aObject );
749 PyObjWrapper sipPopup( sipBuildResult( 0, "M", thePopupMenu, sipClass_QPopupMenu ) );
751 // then call Python module's createPopupMenu() method (for new modules)
752 PyObjWrapper res1( PyObject_CallMethod( myModule,
756 aContext.latin1() ) );
758 // VSR: this method may not be implemented in Python module
762 if ( IsCallOldMethods ) { //__CALL_OLD_METHODS__
763 // call customPopup() Python module's function
764 // this is obsolete function, used only for compatibility reasons
765 PyObjWrapper res2( PyObject_CallMethod( myModule,
771 aParent.latin1() ) );
773 // VSR: this method may not be implemented in Python module
776 } //__CALL_OLD_METHODS__
780 * Processes GUI event
781 * - calls Python module's OnGUIEvent() method
783 void SALOME_PYQT_Module::guiEvent( const int theId )
785 // Python interpreter should be initialized and Python module should be
787 if ( !myInterp || !myModule )
790 PyObjWrapper res( PyObject_CallMethod( myModule, "OnGUIEvent", "i", theId ) );
798 * Initialises python subinterpreter (one per study)
800 void SALOME_PYQT_Module::initInterp( int theStudyId )
804 // Error! Study Id must not be 0!
808 // try to find the subinterpreter
809 if( myInterpMap.find( theStudyId ) != myInterpMap.end() ) {
811 myInterp = myInterpMap[ theStudyId ];
814 // not found - create a new one!
815 ///////////////////////////////////////////////////////////////////
816 // Attention: the creation of Python interpretor must be protected
817 // by a C++ Lock because of C threads
818 ///////////////////////////////////////////////////////////////////
819 myInterp = new SALOME_PYQT_PyInterp();
820 myInterp->initialize();
821 myInterpMap[ theStudyId ] = myInterp;
823 // import 'salome' module and call 'salome_init' method;
824 // do it only once on interpreter creation
825 // ... first get python lock
826 PyLockWrapper aLock = myInterp->GetLockWrapper();
827 // ... then import a module
828 PyObjWrapper aMod = PyImport_ImportModule( "salome" );
834 // ... then call a method
835 PyObjWrapper aRes( PyObject_CallMethod( aMod, "salome_init", "" ) );
844 * Imports Python GUI module and remember the reference to the module
845 * !!! initInterp() should be called first!!!
847 void SALOME_PYQT_Module::importModule()
849 // check if the subinterpreter is initialized
851 // Error! Python subinterpreter should be initialized first!
855 // import Python GUI module and puts it in <myModule> attribute
856 // ... first get python lock
857 PyLockWrapper aLock = myInterp->GetLockWrapper();
858 // ... then import a module
859 QString aMod = QString( name("") ) + "GUI";
860 myModule = PyImport_ImportModule( (char*)( aMod.latin1() ) );
869 * Calls <module>.setWorkSpace() method with PyQt QWidget object to use with
871 * !!! initInterp() and importModule() should be called first!!!
873 void SALOME_PYQT_Module::setWorkSpace()
875 // check if the subinterpreter is initialized and Python module is imported
876 if ( !myInterp || !myModule ) {
877 // Error! Python subinterpreter should be initialized and module should be imported first!
881 // call setWorkspace() method
882 // ... first get python lock
883 PyLockWrapper aLock = myInterp->GetLockWrapper();
885 // ... then try to import SalomePyQt module. If it's not possible don't go on.
886 PyObjWrapper aQtModule( PyImport_ImportModule( "SalomePyQt" ) );
893 if ( IsCallOldMethods ) { //__CALL_OLD_METHODS__
894 // ... then get workspace object
895 QWidget* aWorkspace = 0;
896 if ( getApp()->desktop()->inherits( "STD_MDIDesktop" ) ) {
897 STD_MDIDesktop* aDesktop = dynamic_cast<STD_MDIDesktop*>( getApp()->desktop() );
899 aWorkspace = aDesktop->workspace();
901 else if ( getApp()->desktop()->inherits( "STD_TabDesktop" ) ) {
902 STD_TabDesktop* aDesktop = dynamic_cast<STD_TabDesktop*>( getApp()->desktop() );
904 aWorkspace = aDesktop->workstack();
906 PyObjWrapper pyws( sipBuildResult( 0, "M", aWorkspace, sipClass_QWidget ) );
907 // ... and finally call Python module's setWorkspace() method (obsolete)
908 PyObjWrapper res( PyObject_CallMethod( myModule, "setWorkSpace", "O", pyws.get() ) );
910 // VSR: this method may not be implemented in Python module
914 } //__CALL_OLD_METHODS__
918 * Adds an action into private action list [internal usage]
920 void SALOME_PYQT_Module::addAction( const PyQtGUIAction type, QAction* action )
923 case PYQT_ACTION_MENU:
924 myMenuActionList.append( action );
926 case PYQT_ACTION_TOOLBAL:
927 myToolbarActionList.append( action );
929 case PYQT_ACTION_POPUP:
930 myPopupActionList.append( action );
937 * The next methods just call the parent implementation.
938 * This is done to open protected methods from CAM_Module class.
940 int SALOME_PYQT_Module::createTool( const QString& name )
942 return SalomeApp_Module::createTool( name );
944 int SALOME_PYQT_Module::createTool( const int id, const int tBar, const int idx )
946 return SalomeApp_Module::createTool( id, tBar, idx );
948 int SALOME_PYQT_Module::createTool( const int id, const QString& tBar, const int idx )
950 return SalomeApp_Module::createTool( id, tBar, idx );
952 int SALOME_PYQT_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
954 return SalomeApp_Module::createTool( a, tBar, id, idx );
956 int SALOME_PYQT_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
958 return SalomeApp_Module::createTool( a, tBar, id, idx );
960 int SALOME_PYQT_Module::createMenu( const QString& subMenu, const int menu, const int id, const int group, const int idx )
962 return SalomeApp_Module::createMenu( subMenu, menu, id, group, idx );
964 int SALOME_PYQT_Module::createMenu( const QString& subMenu, const QString& menu, const int id, const int group, const int idx )
966 return SalomeApp_Module::createMenu( subMenu, menu, id, group, idx );
968 int SALOME_PYQT_Module::createMenu( const int id, const int menu, const int group, const int idx )
970 return SalomeApp_Module::createMenu( id, menu, group, idx );
972 int SALOME_PYQT_Module::createMenu( const int id, const QString& menu, const int group, const int idx )
974 return SalomeApp_Module::createMenu( id, menu, group, idx );
976 int SALOME_PYQT_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int idx )
978 return SalomeApp_Module::createMenu( a, menu, id, group, idx );
980 int SALOME_PYQT_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int idx )
982 return SalomeApp_Module::createMenu( a, menu, id, group, idx );
984 QAction* SALOME_PYQT_Module::createSeparator()
986 return SalomeApp_Module::separator();
988 QAction* SALOME_PYQT_Module::action( const int id ) const
990 QAction* a = SalomeApp_Module::action( id );
991 if ( !a ) // try own action map for menu items
992 a = SalomeApp_Module::action( id + PYQT_ACTION_MENU );
993 if ( !a ) // try own action map for toolbar items
994 a = SalomeApp_Module::action( id + PYQT_ACTION_TOOLBAL );
995 if ( !a ) // try own action map for popup items
996 a = SalomeApp_Module::action( id + PYQT_ACTION_POPUP );
999 int SALOME_PYQT_Module::actionId( const QAction* a ) const
1001 int id = SalomeApp_Module::actionId( a );
1002 if ( myMenuActionList.contains( a ) ) // check own action map for menu items
1003 id -= PYQT_ACTION_MENU;
1004 if ( myToolbarActionList.contains( a ) ) // check own action map for toolbar items
1005 id -= PYQT_ACTION_TOOLBAL;
1006 if ( myPopupActionList.contains( a ) ) // check own action map for popup items
1007 id -= PYQT_ACTION_POPUP;
1010 QAction* SALOME_PYQT_Module::createAction( const int id, const QString& text, const QString& icon,
1011 const QString& menu, const QString& tip, const int key,
1015 if ( !icon.isEmpty() ) {
1016 QPixmap pixmap = getApp()->resourceMgr()->loadPixmap( name(""), tr( icon ) );
1017 if ( !pixmap.isNull() )
1018 anIcon = QIconSet( pixmap );
1020 return SalomeApp_Module::createAction( id, text, anIcon, menu, tip, key, getApp()->desktop(), toggle, this, SLOT( onGUIEvent() ) );
1024 //=============================================================================
1025 // SALOME_PYQT_XmlHandler class implementation
1026 //=============================================================================
1028 // gets an tag name for the dom element [ static ]
1029 // returns an empty string if the element does not have tag name
1030 static QString tagName( const QDomElement& element ) {
1031 return element.tagName().stripWhiteSpace();
1034 // gets an attribute by it's name for the dom element [ static ]
1035 // returns an empty string if the element does not have such attribute
1036 static QString attribute( const QDomElement& element, const QString& attName ) {
1037 return element.attribute( attName ).stripWhiteSpace();
1040 // checks the given value for the boolean value [ static ]
1041 // returns TRUE if string is "true", "yes" or "1"
1042 static bool checkBool( const QString& value ) {
1043 return ( value == "true" || value == "yes" || value == "1" );
1046 // checks the given value for the integer value [ static ]
1047 // returns -1 if item is empty or presents and invalid number
1048 static int checkInt( const QString& value )
1050 return value.isEmpty() ? -1 : value.toInt();
1056 SALOME_PYQT_XmlHandler::SALOME_PYQT_XmlHandler( SALOME_PYQT_Module* module, const QString& fileName )
1057 : myModule( module )
1059 QFile aFile( fileName );
1060 if ( !aFile.open( IO_ReadOnly ) )
1062 if ( !myDoc.setContent( &aFile ) ) {
1070 Called by SALOME_PYQT_Module::initialize() in order to create actions
1071 (menus, toolbars, popup menus)
1073 void SALOME_PYQT_XmlHandler::createActions()
1075 // get document element
1076 QDomElement aDocElem = myDoc.documentElement();
1078 // get main menu actions
1079 QDomNodeList aMenuList = aDocElem.elementsByTagName( "menu-item" );
1080 for ( int i = 0; i < aMenuList.count(); i++ ) {
1081 QDomNode n = aMenuList.item( i );
1085 // create toolbars actions
1086 QDomNodeList aToolsList = aDocElem.elementsByTagName( "toolbar" );
1087 for ( int i = 0; i < aToolsList.count(); i++ ) {
1088 QDomNode n = aToolsList.item( i );
1094 * Creates popup menu
1096 void SALOME_PYQT_XmlHandler::createPopup( QPopupMenu* menu,
1097 const QString& context,
1098 const QString& parent,
1099 const QString& object )
1101 // get document element
1102 QDomElement aDocElem = myDoc.documentElement();
1104 // get popup menus actions
1105 QDomNodeList aPopupList = aDocElem.elementsByTagName( "popupmenu" );
1106 for ( int i = 0; i < aPopupList.count(); i++ ) {
1107 QDomNode n = aPopupList.item( i );
1108 if ( !n.isNull() && n.isElement() ) {
1109 QDomElement e = n.toElement();
1110 QString lab = attribute( e, "label-id" );
1111 QString ctx = attribute( e, "context-id" );
1112 QString prt = attribute( e, "parent-id" );
1113 QString obj = attribute( e, "object-id" );
1114 if ( ctx == context && prt == parent && obj == object ) {
1115 insertPopupItems( n, menu );
1123 Create main menu with child actions
1125 void SALOME_PYQT_XmlHandler::createMenu( QDomNode& parentNode, const int parentMenuId )
1130 if ( parentNode.isNull() )
1133 QDomElement parentElement = parentNode.toElement();
1134 if ( !parentElement.isNull() ) {
1135 QString plabel = attribute( parentElement, "label-id" );
1136 int pid = checkInt( attribute( parentElement, "item-id" ) );
1137 int ppos = checkInt( attribute( parentElement, "pos-id" ) );
1138 if ( !plabel.isEmpty() ) {
1140 int menuId = myModule->createMenu( plabel, // label
1141 parentMenuId, // parent menu ID, should be -1 for main menu
1145 QDomNode node = parentNode.firstChild();
1146 while ( !node.isNull() ) {
1147 if ( node.isElement() ) {
1148 QDomElement elem = node.toElement();
1149 QString aTagName = tagName( elem );
1150 if ( aTagName == "popup-item" ) {
1151 int id = checkInt( attribute( elem, "item-id" ) );
1152 int pos = checkInt( attribute( elem, "pos-id" ) );
1153 QString label = attribute( elem, "label-id" );
1154 QString icon = attribute( elem, "icon-id" );
1155 QString tooltip = attribute( elem, "tooltip-id" );
1156 QString accel = attribute( elem, "accel-id" );
1157 bool toggle = checkBool( attribute( elem, "toggle-id" ) );
1158 ////QString execute = attribute( elem, "execute-action" ); // not used
1160 // -1 action ID is not allowed : it means that <item-id> attribute is missed in the XML file!
1161 // also check if the action with given ID is already created
1162 if ( id != -1 && !myModule->action( SALOME_PYQT_Module::PYQT_ACTION_MENU + id ) ) {
1163 // little trick to have several actions with same ID for menus and toolbars
1164 id = SALOME_PYQT_Module::PYQT_ACTION_MENU + id;
1165 // create menu action
1166 QAction* action = myModule->createAction( id, // ID
1170 tooltip, // status-bar text
1171 QKeySequence( accel ), // keyboard accelerator
1172 toggle ); // toogled action
1173 myModule->addAction( SALOME_PYQT_Module::PYQT_ACTION_MENU, action );
1174 myModule->createMenu( action, menuId, -1, 80, pos );
1177 else if ( aTagName == "submenu" ) {
1179 createMenu( node, menuId );
1181 else if ( aTagName == "separator" ) {
1182 // create menu separator
1183 int pos = checkInt( attribute( elem, "pos-id" ) );
1184 QAction* action = myModule->createSeparator();
1185 myModule->createMenu( action, menuId, -1, 80, pos );
1188 node = node.nextSibling();
1195 Create a toolbar with child actions
1197 void SALOME_PYQT_XmlHandler::createToolBar( QDomNode& parentNode )
1202 if ( parentNode.isNull() )
1205 QDomElement parentElement = parentNode.toElement();
1206 if ( !parentElement.isNull() ) {
1207 QString aLabel = attribute( parentElement, "label-id" );
1208 if ( !aLabel.isEmpty() ) {
1210 int tbId = myModule->createTool( aLabel );
1211 QDomNode node = parentNode.firstChild();
1212 while ( !node.isNull() ) {
1213 if ( node.isElement() ) {
1214 QDomElement elem = node.toElement();
1215 QString aTagName = tagName( elem );
1216 if ( aTagName == "toolbutton-item" ) {
1217 int id = checkInt( attribute( elem, "item-id" ) );
1218 int pos = checkInt( attribute( elem, "pos-id" ) );
1219 QString label = attribute( elem, "label-id" );
1220 QString icon = attribute( elem, "icon-id" );
1221 QString tooltip = attribute( elem, "tooltip-id" );
1222 QString accel = attribute( elem, "accel-id" );
1223 bool toggle = checkBool( attribute( elem, "toggle-id" ) );
1224 ////QString execute = attribute( elem, "execute-action" ); // not used
1226 // -1 action ID is not allowed : it means that <item-id> attribute is missed in the XML file!
1227 // also check if the action with given ID is already created
1228 if ( id != -1 && !myModule->action( SALOME_PYQT_Module::PYQT_ACTION_TOOLBAL + id ) ) {
1229 // little trick to have several actions with same ID for menus and toolbars
1230 id = SALOME_PYQT_Module::PYQT_ACTION_TOOLBAL + id;
1231 // create toolbar action
1232 QAction* action = myModule->createAction( id, // ID
1236 tooltip, // status-bar text
1237 QKeySequence( accel ), // keyboard accelerator
1238 toggle ); // toogled action
1239 myModule->addAction( SALOME_PYQT_Module::PYQT_ACTION_TOOLBAL, action );
1240 myModule->createTool( action, tbId, -1, pos );
1243 else if ( aTagName == "separatorTB" ) {
1244 // create toolbar separator
1245 int pos = checkInt( attribute( elem, "pos-id" ) );
1246 QAction* action = myModule->createSeparator();
1247 myModule->createTool( action, tbId, -1, pos );
1250 node = node.nextSibling();
1256 void SALOME_PYQT_XmlHandler::insertPopupItems( QDomNode& parentNode, QPopupMenu* menu )
1261 if ( parentNode.isNull() )
1264 // we create popup menus without help of QtxPopupMgr
1265 QDomNode node = parentNode.firstChild();
1266 while ( !node.isNull() ) {
1267 if ( node.isElement() ) {
1268 QDomElement elem = node.toElement();
1269 QString aTagName = tagName( elem );
1270 if ( aTagName == "popup-item" ) {
1271 // insert a command item
1272 int id = checkInt( attribute( elem, "item-id" ) );
1273 int pos = checkInt( attribute( elem, "pos-id" ) );
1274 QString label = attribute( elem, "label-id" );
1275 QString icon = attribute( elem, "icon-id" );
1276 /////QString tooltip = attribute( elem, "tooltip-id" ); // not used
1277 QString accel = attribute( elem, "accel-id" );
1278 /////bool toggle = checkBool( attribute( elem, "toggle-id" ) ); // not used
1279 /////QString execute = attribute( elem, "execute-action" ); // not used
1282 if ( !icon.isEmpty() ) {
1283 QPixmap pixmap = myModule->getApp()->resourceMgr()->loadPixmap( myModule->name(""), icon );
1284 if ( !pixmap.isNull() )
1285 anIcon = QIconSet( pixmap );
1288 // -1 action ID is not allowed : it means that <item-id> attribute is missed in the XML file!
1289 // also check if the action with given ID is already created
1291 menu->insertItem( anIcon, label, myModule, SLOT( onGUIEvent(int) ), QKeySequence( accel ), id, pos );
1294 else if ( aTagName == "submenu" ) {
1296 int id = checkInt( attribute( elem, "item-id" ) );
1297 int pos = checkInt( attribute( elem, "pos-id" ) );
1298 QString label = attribute( elem, "label-id" );
1299 QString icon = attribute( elem, "icon-id" );
1302 if ( !icon.isEmpty() ) {
1303 QPixmap pixmap = myModule->getApp()->resourceMgr()->loadPixmap( myModule->name(""), icon );
1304 if ( !pixmap.isNull() )
1305 anIcon = QIconSet( pixmap );
1308 QPopupMenu* newPopup = new QPopupMenu( menu, label );
1309 menu->insertItem( anIcon, label, newPopup, id, pos );
1310 insertPopupItems( node, newPopup );
1312 else if ( aTagName == "separator" ) {
1313 // create menu separator
1314 int pos = checkInt( attribute( elem, "pos-id" ) );
1315 menu->insertSeparator( pos );
1318 node = node.nextSibling();