1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 //=============================================================================
20 // File : SALOME_PYQT_Module.cxx
22 // Author : Vadim SANDLER
24 // Copyright : 2003-2005 CEA/DEN, EDF R&D
26 //=============================================================================
28 #include "SALOME_PYQT_Module.h"
30 #include "PyInterp_Dispatcher.h"
31 #include "SUIT_ResourceMgr.h"
32 #include "STD_MDIDesktop.h"
33 #include "STD_TabDesktop.h"
34 #include "SalomeApp_Application.h"
35 #include "SalomeApp_Study.h"
37 #include "QtxWorkstack.h"
38 #include <SALOME_LifeCycleCORBA.hxx>
39 #include <Container_init_python.hxx>
43 #include <qworkspace.h>
44 #include <qpopupmenu.h>
46 #include "SALOME_PYQT_SipDefs.h"
47 #if defined(SIP_VERS_v4_old) || defined(SIP_VERS_v4_new)
48 #include "sipAPISalomePyQtGUI.h"
50 #include "sipSalomePyQtGUIDeclSalomePyQtGUI.h"
53 #include <sipqtQWidget.h>
54 #include <sipqtQPopupMenu.h>
60 ///////////////////////////////////////////////////////////////////////////////
61 // Default name of the module, replaced at the moment of module creation
62 #define __DEFAULT_NAME__ "SALOME_PYQT_Module"
64 ///////////////////////////////////////////////////////////////////////////////
65 // If __CALL_OLD_METHODS__ macro is not defined the invoking of obsolete Python
66 // module's methods like setSetting(), definePopup(), etc. is blocked.
67 // This macro is defined by default (in Makefile)
68 #ifdef __CALL_OLD_METHODS__
69 const bool IsCallOldMethods = true;
71 const bool IsCallOldMethods = false;
74 ///////////////////////////////////////////////////////////////////////////////
75 // NB: Python requests.
76 // General rule for Python requests created by SALOME_PYQT_Module:
77 // all requests should be executed SYNCHRONOUSLY within the main GUI thread.
78 // However, it is obligatory that ANY Python call is wrapped with a request object,
79 // so that ALL Python API calls are serialized with PyInterp_Dispatcher.
80 ///////////////////////////////////////////////////////////////////////////////
82 //=============================================================================
83 // The class for parsing of the XML resource files.
84 // Used for backward compatibility with existing Python modules.
85 //=============================================================================
86 class SALOME_PYQT_XmlHandler
89 SALOME_PYQT_XmlHandler( SALOME_PYQT_Module* module, const QString& fileName );
91 void createPopup ( QPopupMenu* menu,
92 const QString& context,
93 const QString& parent,
94 const QString& object );
97 void createToolBar ( QDomNode& parentNode );
98 void createMenu ( QDomNode& parentNode,
99 const int parentMenuId = -1 );
101 void insertPopupItems( QDomNode& parentNode,
105 SALOME_PYQT_Module* myModule;
109 //=============================================================================
110 // SALOME_PYQT_Module class implementation (implements CAM_Module API for
111 // all Python-based SALOME module
112 //=============================================================================
114 // While the SalomePyQtGUI library is not imported in Python it's initialization function
115 // should be called manually (and only once) in order to initialize global sip data
116 // and to get C API from sip : sipBuildResult for example
117 #if defined(SIP_STATIC_MODULE)
118 extern "C" void initSalomePyQtGUI();
120 PyMODINIT_FUNC initSalomePyQtGUI();
124 * This function creates an instance of SALOME_PYQT_Module object by request
125 * of and application object when the module is loaded.
128 SALOME_PYQT_EXPORT CAM_Module* createModule() {
129 static bool alreadyInitialized = false;
130 if ( !alreadyInitialized ) {
131 // call only once (see above) !
132 PyEval_RestoreThread( KERNEL_PYTHON::_gtstate );
134 PyEval_ReleaseThread( KERNEL_PYTHON::_gtstate );
135 alreadyInitialized = !alreadyInitialized;
137 return new SALOME_PYQT_Module();
142 * Static variables definition
144 SALOME_PYQT_Module::InterpMap SALOME_PYQT_Module::myInterpMap;
145 SALOME_PYQT_Module* SALOME_PYQT_Module::myInitModule = 0;
148 * Little trick : provide an access to being activated Python module from outside;
149 * needed by the SalomePyQt library :(
151 SALOME_PYQT_Module* SALOME_PYQT_Module::getInitModule()
159 SALOME_PYQT_Module::SALOME_PYQT_Module() :
160 SalomeApp_Module( __DEFAULT_NAME__ ), myModule( 0 ), myXmlHandler ( 0 )
162 myMenuActionList.setAutoDelete( false );
163 myPopupActionList.setAutoDelete( false );
164 myToolbarActionList.setAutoDelete( false );
170 SALOME_PYQT_Module::~SALOME_PYQT_Module()
172 myMenuActionList.clear();
173 myPopupActionList.clear();
174 myToolbarActionList.clear();
180 * Initialization of the module.
181 * Inherited from CAM_Module.
183 * This method is used for creation of the menus, toolbars and other staff.
184 * There are two ways:
185 * - for obsolete modules this method first tries to read <module>_<language>.xml
186 * resource file which contains a menu, toolbars and popup menus description.
187 * - new modules can create menus by by calling the corresponding methods of SalomePyQt
188 * Python API in the Python module's initialize() method which is called from here.
189 * NOTE: if postponed modules loading is not used, the active study might be not defined
190 * yet at this stage, so initialize() method should not perform any study-based initialization.
192 void SALOME_PYQT_Module::initialize( CAM_Application* app )
194 SalomeApp_Module::initialize( app );
196 // Try to get XML resource file name
197 SUIT_ResourceMgr* aResMgr = getApp()->resourceMgr();
198 QString aLang = aResMgr->stringValue( "language", "language", QString::null );
199 if ( aLang.isEmpty() ) aLang = QString( "en" );
200 QString aName = name( "" );
201 QString aFileName = aName + "_" + aLang + ".xml";
202 aFileName = aResMgr->path( "resources", aName, aFileName );
204 // parse XML file if it is found and create actions
205 if ( !myXmlHandler && !aFileName.isEmpty() ) {
206 myXmlHandler = new SALOME_PYQT_XmlHandler( this, aFileName );
207 myXmlHandler->createActions();
210 // perform internal initialization and call module's initialize() method
211 // InitializeReq: request class for internal init() operation
212 class InitializeReq : public PyInterp_Request
215 InitializeReq( CAM_Application* _app,
216 SALOME_PYQT_Module* _obj )
217 : PyInterp_Request( 0, true ), // this request should be processed synchronously (sync == true)
222 virtual void execute()
224 myObj->init( myApp );
228 CAM_Application* myApp;
229 SALOME_PYQT_Module* myObj;
232 // Posting the request
233 PyInterp_Dispatcher::Get()->Exec( new InitializeReq( app, this ) );
237 * Activation of the module.
238 * Inherited from CAM_Module.
240 bool SALOME_PYQT_Module::activateModule( SUIT_Study* theStudy )
242 MESSAGE( "SALOME_PYQT_Module::activateModule" );
244 bool res = SalomeApp_Module::activateModule( theStudy );
249 // ActivateReq: request class for internal activate() operation
250 class ActivateReq : public PyInterp_Request
253 ActivateReq( SUIT_Study* _study,
254 SALOME_PYQT_Module* _obj )
255 : PyInterp_Request( 0, true ), // this request should be processed synchronously (sync == true)
260 virtual void execute()
262 myObj->activate( myStudy );
267 SALOME_PYQT_Module* myObj;
270 // Posting the request
271 PyInterp_Dispatcher::Get()->Exec( new ActivateReq( theStudy, this ) );
273 // activate menus, toolbars, etc
274 setMenuShown( true );
275 setToolShown( true );
281 * Deactivation of the module.
282 * Inherited from CAM_Module.
284 bool SALOME_PYQT_Module::deactivateModule( SUIT_Study* theStudy )
286 MESSAGE( "SALOME_PYQT_Module::deactivateModule" );
288 bool res = SalomeApp_Module::deactivateModule( theStudy );
290 // deactivate menus, toolbars, etc
291 setMenuShown( false );
292 setToolShown( false );
294 // DeactivateReq: request class for internal deactivate() operation
295 class DeactivateReq : public PyInterp_LockRequest
298 DeactivateReq( PyInterp_base* _py_interp,
300 SALOME_PYQT_Module* _obj )
301 : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
306 virtual void execute()
308 myObj->deactivate( myStudy );
313 SALOME_PYQT_Module* myObj;
316 // Posting the request
317 PyInterp_Dispatcher::Get()->Exec( new DeactivateReq( myInterp, theStudy, this ) );
323 * Processes GUI action (from main menu, toolbar or context popup menu)
325 void SALOME_PYQT_Module::onGUIEvent()
328 const QObject* obj = sender();
329 if ( !obj || !obj->inherits( "QAction" ) )
331 QAction* action = (QAction*)obj;
334 int id = actionId( action );
335 MESSAGE( "SALOME_PYQT_Module::onGUIEvent: id = " << id );
337 // perform synchronous request to Python event dispatcher
338 class GUIEvent : public PyInterp_LockRequest
341 GUIEvent( PyInterp_base* _py_interp,
342 SALOME_PYQT_Module* _obj,
344 : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
349 virtual void execute()
351 myObj->guiEvent( myId );
356 SALOME_PYQT_Module* myObj;
359 // Posting the request
360 PyInterp_Dispatcher::Get()->Exec( new GUIEvent( myInterp, this, id ) );
364 * Processes GUI action (from context popup menu, only for XML-based actions!)
366 void SALOME_PYQT_Module::onGUIEvent( int id )
368 // perform synchronous request to Python event dispatcher
369 class GUIEvent : public PyInterp_LockRequest
372 GUIEvent( PyInterp_base* _py_interp,
373 SALOME_PYQT_Module* _obj,
375 : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
380 virtual void execute()
382 myObj->guiEvent( myId );
387 SALOME_PYQT_Module* myObj;
390 // Posting the request
391 PyInterp_Dispatcher::Get()->Exec( new GUIEvent( myInterp, this, id ) );
395 Context popup menu request.
396 Called when user activates popup menu in some window (view, object browser, etc).
398 void SALOME_PYQT_Module::contextMenuPopup( const QString& theContext, QPopupMenu* thePopupMenu, QString& /*title*/ )
400 MESSAGE( "SALOME_PYQT_Module::contextMenuPopup : " << theContext.latin1() );
401 // perform synchronous request to Python event dispatcher
402 class PopupMenuEvent : public PyInterp_LockRequest
405 PopupMenuEvent( PyInterp_base* _py_interp,
406 SALOME_PYQT_Module* _obj,
407 const QString& _context,
409 : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
410 myContext( _context ),
415 virtual void execute()
417 myObj->contextMenu( myContext, myPopup );
421 SALOME_PYQT_Module* myObj;
426 // Posting the request only if dispatcher is not busy!
427 // Executing the request synchronously
428 if ( !PyInterp_Dispatcher::Get()->IsBusy() )
429 PyInterp_Dispatcher::Get()->Exec( new PopupMenuEvent( myInterp, this, theContext, thePopupMenu ) );
433 * Defines the dockable window associated with the module.
434 * To fill the list of windows the correspondind Python module's windows()
435 * method is called from SALOME_PYQT_Module::init() method.
436 * By default, ObjectBrowser, PythonConsole and LogWindow are provided.
438 void SALOME_PYQT_Module::windows( QMap<int, int>& mappa ) const
440 // First clear the output parameters
441 QMap<int, int>::ConstIterator it;
442 for ( it = myWindowsMap.begin(); it != myWindowsMap.end(); ++it ) {
443 mappa[ it.key() ] = it.data();
448 * Defines the compatible views which should be opened on module activation.
449 * To fill the list of views the correspondind Python module's views()
450 * method is called from SALOME_PYQT_Module::init() method.
451 * By default, the list is empty.
453 void SALOME_PYQT_Module::viewManagers( QStringList& listik ) const
455 for ( QStringList::ConstIterator it = myViewMgrList.begin(); it != myViewMgrList.end(); ++it ) {
456 listik.append( *it );
461 * Performs internal initialization
462 * - initializes/gets the Python interpreter (one per study)
463 * - imports the Python module
464 * - passes the workspace widget to the Python module
465 * - calls Python module's initialize() method
466 * - calls Python module's windows() method
467 * - calls Python module's views() method
469 void SALOME_PYQT_Module::init( CAM_Application* app )
471 // reset interpreter to NULL
475 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( app );
479 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
482 int aStudyId = aStudy ? aStudy->studyDS()->StudyId() : 0;
484 // initialize Python subinterpreter (on per study) and put it in <myInterp> variable
485 initInterp( aStudyId );
489 // import Python GUI module
496 if ( IsCallOldMethods ) { // __CALL_OLD_METHODS__
497 // call Python module's setWorkspace() method
499 } //__CALL_OLD_METHODS__
501 // then call Python module's initialize() method
502 // ... first get python lock
503 PyLockWrapper aLock = myInterp->GetLockWrapper();
504 // ... (the Python module is already imported)
505 // ... finally call Python module's initialize() method
506 PyObjWrapper res( PyObject_CallMethod( myModule, "initialize", "" ) );
508 // VSR: this method may not be implemented in Python module
513 // get the windows list from the Python module by calling windows() method
514 // ... first put default values
515 myWindowsMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::DockLeft );
516 myWindowsMap.insert( SalomeApp_Application::WT_PyConsole, Qt::DockBottom );
517 // VSR: LogWindow is not yet implemented
518 // myWindowsMap.insert( SalomeApp_Application::WT_LogWindow, Qt::DockBottom );
520 PyObjWrapper res1( PyObject_CallMethod( myModule, "windows", "" ) );
522 // VSR: this method may not be implemented in Python module
527 myWindowsMap.clear();
528 if ( PyDict_Check( res1 ) ) {
532 while ( PyDict_Next( res1, &pos, &key, &value ) ) {
533 // parse the return value
534 // it should be a map: {integer:integer}
536 if( key && PyInt_Check( key ) && value && PyInt_Check( value ) ) {
537 aKey = PyInt_AsLong( key );
538 aValue = PyInt_AsLong( value );
539 myWindowsMap[ aKey ] = aValue;
544 // get the windows list from the Python module by calling views() method
545 PyObjWrapper res2( PyObject_CallMethod( myModule, "views", "" ) );
547 // VSR: this method may not be implemented in Python module
552 // parse the return value
553 // result can be one string...
554 if ( PyString_Check( res2 ) ) {
555 myViewMgrList.append( PyString_AsString( res2 ) );
557 // ... or list of strings
558 else if ( PyList_Check( res2 ) ) {
559 int size = PyList_Size( res2 );
560 for ( int i = 0; i < size; i++ ) {
561 PyObject* value = PyList_GetItem( res2, i );
562 if( value && PyString_Check( value ) ) {
563 myViewMgrList.append( PyString_AsString( value ) );
572 * Performs internal activation:
573 * - initializes/gets the Python interpreter (one per study)
574 * - imports the Python GUI module
575 * - calls Python module's setSettings() method (obsolete function, used for compatibility with old code)
576 * or activate() method (for new modules)
578 void SALOME_PYQT_Module::activate( SUIT_Study* theStudy )
581 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( theStudy );
582 int aStudyId = aStudy ? aStudy->studyDS()->StudyId() : 0;
584 // initialize Python subinterpreter (on per study) and put it in <myInterp> variable
585 initInterp( aStudyId );
589 // import Python GUI module
595 PyLockWrapper aLock = myInterp->GetLockWrapper();
597 if ( IsCallOldMethods ) { //__CALL_OLD_METHODS__
598 // call Python module's setSettings() method (obsolete)
599 PyObjWrapper res( PyObject_CallMethod( myModule, "setSettings", "" ) );
601 // VSR: this method may not be implemented in Python module
605 } //__CALL_OLD_METHODS__
607 // call Python module's activate() method (for the new modules)
608 PyObjWrapper res1( PyObject_CallMethod( myModule, "activate", "" ) );
610 // VSR: this method may not be implemented in Python module
617 * Performs internal deactivation:
618 * - calls Python module's deactivate() method
620 void SALOME_PYQT_Module::deactivate( SUIT_Study* theStudy )
622 // check if the subinterpreter is initialized and Python module is imported
623 if ( !myInterp || !myModule ) {
624 // Error! Python subinterpreter should be initialized and module should be imported first!
627 // then call Python module's deactivate() method
628 PyObjWrapper res( PyObject_CallMethod( myModule, "deactivate", "" ) );
630 // VSR: this method may not be implemented in Python module
637 * Called when active the study is actived (user brings its desktop to top)
638 * - initializes/gets the Python interpreter (one per study)
639 * - imports the Python GUI module
640 * - calls Python module's activeStudyChanged() method
642 void SALOME_PYQT_Module::studyChanged( SUIT_Study* theStudy )
645 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( theStudy );
646 int aStudyId = aStudy ? aStudy->studyDS()->StudyId() : 0;
648 // initialize Python subinterpreter (on per study) and put it in <myInterp> variable
649 initInterp( aStudyId );
653 // import Python GUI module
659 PyLockWrapper aLock = myInterp->GetLockWrapper();
661 // call Python module's activeStudyChanged() method
662 PyObjWrapper res( PyObject_CallMethod( myModule, "activeStudyChanged", "i", aStudyId ) );
664 // VSR: this method may not be implemented in Python module
671 * Get module engine, returns nil var if engine is not found in LifeCycleCORBA
673 Engines::Component_var SALOME_PYQT_Module::getEngine() const
675 Engines::Component_var comp;
676 // temporary solution
678 comp = getApp()->lcc()->FindOrLoad_Component( "FactoryServerPy", name( "" ) );
680 catch (CORBA::Exception&) {
686 * Get module engine IOR, returns empty string if engine is not found in LifeCycleCORBA
688 QString SALOME_PYQT_Module::engineIOR() const
690 if ( !CORBA::is_nil( getEngine() ) )
691 return QString( getApp()->orb()->object_to_string( getEngine() ) );
692 return QString( "" );
696 * Called when study desktop is activated.
697 * Used for notifying about changing of the active study.
699 void SALOME_PYQT_Module::studyActivated()
701 // StudyChangedReq: request class for internal studyChanged() operation
702 class StudyChangedReq : public PyInterp_Request
705 StudyChangedReq( SUIT_Study* _study,
706 SALOME_PYQT_Module* _obj )
707 : PyInterp_Request( 0, true ), // this request should be processed synchronously (sync == true)
712 virtual void execute()
714 myObj->studyChanged( myStudy );
719 SALOME_PYQT_Module* myObj;
722 // Posting the request
723 PyInterp_Dispatcher::Get()->Exec( new StudyChangedReq( application()->activeStudy(), this ) );
727 * Processes context popup menu request
728 * - calls Python module's definePopup(...) method (obsolete function, used for compatibility with old code)
729 * to define the popup menu context
730 * - parses XML resourses file (if exists) and fills the popup menu with the items)
731 * - calls Python module's customPopup(...) method (obsolete function, used for compatibility with old code)
732 * to allow module to customize the popup menu
733 * - for new modules calls createPopupMenu() function to allow the modules to build the popup menu
734 * by using insertItem(...) Qt functions.
736 void SALOME_PYQT_Module::contextMenu( const QString& theContext, QPopupMenu* thePopupMenu )
738 // Python interpreter should be initialized and Python module should be
740 if ( !myInterp || !myModule )
743 QString aContext( theContext ), aObject( "" ), aParent( "" );
745 if ( IsCallOldMethods ) { //__CALL_OLD_METHODS__
746 // call definePopup() Python module's function
747 // this is obsolete function, used only for compatibility reasons
748 PyObjWrapper res(PyObject_CallMethod( myModule,
753 aParent.latin1() ) );
755 // VSR: this method may not be implemented in Python module
760 // parse return value
762 if( PyArg_ParseTuple( res, "sss", &co, &ob, &pa ) ) {
768 } //__CALL_OLD_METHODS__
770 // first try to create menu via XML parser:
771 // we create popup menus without help of QtxPopupMgr
773 myXmlHandler->createPopup( thePopupMenu, aContext, aParent, aObject );
775 PyObjWrapper sipPopup( sipBuildResult( 0, "M", thePopupMenu, sipClass_QPopupMenu ) );
777 // then call Python module's createPopupMenu() method (for new modules)
778 PyObjWrapper res1( PyObject_CallMethod( myModule,
782 aContext.latin1() ) );
784 // VSR: this method may not be implemented in Python module
789 if ( IsCallOldMethods ) { //__CALL_OLD_METHODS__
790 // call customPopup() Python module's function
791 // this is obsolete function, used only for compatibility reasons
792 PyObjWrapper res2( PyObject_CallMethod( myModule,
798 aParent.latin1() ) );
800 // VSR: this method may not be implemented in Python module
804 } //__CALL_OLD_METHODS__
808 * Processes GUI event
809 * - calls Python module's OnGUIEvent() method
811 void SALOME_PYQT_Module::guiEvent( const int theId )
813 // Python interpreter should be initialized and Python module should be
815 if ( !myInterp || !myModule )
818 PyObjWrapper res( PyObject_CallMethod( myModule, "OnGUIEvent", "i", theId ) );
820 // VSR: this method may not be implemented in Python module
827 * Initialises python subinterpreter (one per study)
829 void SALOME_PYQT_Module::initInterp( int theStudyId )
833 // Error! Study Id must not be 0!
837 // try to find the subinterpreter
838 if( myInterpMap.find( theStudyId ) != myInterpMap.end() ) {
840 myInterp = myInterpMap[ theStudyId ];
843 // not found - create a new one!
844 ///////////////////////////////////////////////////////////////////
845 // Attention: the creation of Python interpretor must be protected
846 // by a C++ Lock because of C threads
847 ///////////////////////////////////////////////////////////////////
848 myInterp = new SALOME_PYQT_PyInterp();
849 myInterp->initialize();
850 myInterpMap[ theStudyId ] = myInterp;
852 // import 'salome' module and call 'salome_init' method;
853 // do it only once on interpreter creation
854 // ... first get python lock
855 PyLockWrapper aLock = myInterp->GetLockWrapper();
856 // ... then import a module
857 PyObjWrapper aMod = PyImport_ImportModule( "salome" );
863 // ... then call a method
864 PyObjWrapper aRes( PyObject_CallMethod( aMod, "salome_init", "" ) );
873 * Imports Python GUI module and remember the reference to the module
874 * !!! initInterp() should be called first!!!
876 void SALOME_PYQT_Module::importModule()
878 // check if the subinterpreter is initialized
880 // Error! Python subinterpreter should be initialized first!
884 // import Python GUI module and puts it in <myModule> attribute
885 // ... first get python lock
886 PyLockWrapper aLock = myInterp->GetLockWrapper();
887 // ... then import a module
888 QString aMod = QString( name("") ) + "GUI";
889 myModule = PyImport_ImportModule( (char*)( aMod.latin1() ) );
898 * Calls <module>.setWorkSpace() method with PyQt QWidget object to use with
900 * !!! initInterp() and importModule() should be called first!!!
902 void SALOME_PYQT_Module::setWorkSpace()
904 // check if the subinterpreter is initialized and Python module is imported
905 if ( !myInterp || !myModule ) {
906 // Error! Python subinterpreter should be initialized and module should be imported first!
910 // call setWorkspace() method
911 // ... first get python lock
912 PyLockWrapper aLock = myInterp->GetLockWrapper();
914 // ... then try to import SalomePyQt module. If it's not possible don't go on.
915 PyObjWrapper aQtModule( PyImport_ImportModule( "SalomePyQt" ) );
922 if ( IsCallOldMethods ) { //__CALL_OLD_METHODS__
923 // ... then get workspace object
924 QWidget* aWorkspace = 0;
925 if ( getApp()->desktop()->inherits( "STD_MDIDesktop" ) ) {
926 STD_MDIDesktop* aDesktop = dynamic_cast<STD_MDIDesktop*>( getApp()->desktop() );
928 aWorkspace = aDesktop->workspace();
930 else if ( getApp()->desktop()->inherits( "STD_TabDesktop" ) ) {
931 STD_TabDesktop* aDesktop = dynamic_cast<STD_TabDesktop*>( getApp()->desktop() );
933 aWorkspace = aDesktop->workstack();
935 PyObjWrapper pyws( sipBuildResult( 0, "M", aWorkspace, sipClass_QWidget ) );
936 // ... and finally call Python module's setWorkspace() method (obsolete)
937 PyObjWrapper res( PyObject_CallMethod( myModule, "setWorkSpace", "O", pyws.get() ) );
939 // VSR: this method may not be implemented in Python module
943 } //__CALL_OLD_METHODS__
947 * Adds an action into private action list [internal usage]
949 void SALOME_PYQT_Module::addAction( const PyQtGUIAction type, QAction* action )
952 case PYQT_ACTION_MENU:
953 myMenuActionList.append( action );
955 case PYQT_ACTION_TOOLBAL:
956 myToolbarActionList.append( action );
958 case PYQT_ACTION_POPUP:
959 myPopupActionList.append( action );
966 * The next methods just call the parent implementation.
967 * This is done to open protected methods from CAM_Module class.
969 int SALOME_PYQT_Module::createTool( const QString& name )
971 return SalomeApp_Module::createTool( name );
973 int SALOME_PYQT_Module::createTool( const int id, const int tBar, const int idx )
975 return SalomeApp_Module::createTool( id, tBar, idx );
977 int SALOME_PYQT_Module::createTool( const int id, const QString& tBar, const int idx )
979 return SalomeApp_Module::createTool( id, tBar, idx );
981 int SALOME_PYQT_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
983 return SalomeApp_Module::createTool( a, tBar, id, idx );
985 int SALOME_PYQT_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
987 return SalomeApp_Module::createTool( a, tBar, id, idx );
989 int SALOME_PYQT_Module::createMenu( const QString& subMenu, const int menu, const int id, const int group, const int idx )
991 return SalomeApp_Module::createMenu( subMenu, menu, id, group, idx );
993 int SALOME_PYQT_Module::createMenu( const QString& subMenu, const QString& menu, const int id, const int group, const int idx )
995 return SalomeApp_Module::createMenu( subMenu, menu, id, group, idx );
997 int SALOME_PYQT_Module::createMenu( const int id, const int menu, const int group, const int idx )
999 return SalomeApp_Module::createMenu( id, menu, group, idx );
1001 int SALOME_PYQT_Module::createMenu( const int id, const QString& menu, const int group, const int idx )
1003 return SalomeApp_Module::createMenu( id, menu, group, idx );
1005 int SALOME_PYQT_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int idx )
1007 return SalomeApp_Module::createMenu( a, menu, id, group, idx );
1009 int SALOME_PYQT_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int idx )
1011 return SalomeApp_Module::createMenu( a, menu, id, group, idx );
1013 QAction* SALOME_PYQT_Module::createSeparator()
1015 return SalomeApp_Module::separator();
1017 QAction* SALOME_PYQT_Module::action( const int id ) const
1019 QAction* a = SalomeApp_Module::action( id );
1020 if ( !a ) // try own action map for menu items
1021 a = SalomeApp_Module::action( id + PYQT_ACTION_MENU );
1022 if ( !a ) // try own action map for toolbar items
1023 a = SalomeApp_Module::action( id + PYQT_ACTION_TOOLBAL );
1024 if ( !a ) // try own action map for popup items
1025 a = SalomeApp_Module::action( id + PYQT_ACTION_POPUP );
1028 int SALOME_PYQT_Module::actionId( const QAction* a ) const
1030 int id = SalomeApp_Module::actionId( a );
1031 if ( myMenuActionList.contains( a ) ) // check own action map for menu items
1032 id -= PYQT_ACTION_MENU;
1033 if ( myToolbarActionList.contains( a ) ) // check own action map for toolbar items
1034 id -= PYQT_ACTION_TOOLBAL;
1035 if ( myPopupActionList.contains( a ) ) // check own action map for popup items
1036 id -= PYQT_ACTION_POPUP;
1039 QAction* SALOME_PYQT_Module::createAction( const int id, const QString& text, const QString& icon,
1040 const QString& menu, const QString& tip, const int key,
1044 if ( !icon.isEmpty() ) {
1045 QPixmap pixmap = getApp()->resourceMgr()->loadPixmap( name(""), tr( icon ) );
1046 if ( !pixmap.isNull() )
1047 anIcon = QIconSet( pixmap );
1049 return SalomeApp_Module::createAction( id, text, anIcon, menu, tip, key, getApp()->desktop(), toggle, this, SLOT( onGUIEvent() ) );
1053 //=============================================================================
1054 // SALOME_PYQT_XmlHandler class implementation
1055 //=============================================================================
1057 // gets an tag name for the dom element [ static ]
1058 // returns an empty string if the element does not have tag name
1059 static QString tagName( const QDomElement& element ) {
1060 return element.tagName().stripWhiteSpace();
1063 // gets an attribute by it's name for the dom element [ static ]
1064 // returns an empty string if the element does not have such attribute
1065 static QString attribute( const QDomElement& element, const QString& attName ) {
1066 return element.attribute( attName ).stripWhiteSpace();
1069 // checks the given value for the boolean value [ static ]
1070 // returns TRUE if string is "true", "yes" or "1"
1071 static bool checkBool( const QString& value ) {
1072 return ( value == "true" || value == "yes" || value == "1" );
1075 // checks the given value for the integer value [ static ]
1076 // returns -1 if item is empty or presents and invalid number
1077 static int checkInt( const QString& value )
1079 return value.isEmpty() ? -1 : value.toInt();
1085 SALOME_PYQT_XmlHandler::SALOME_PYQT_XmlHandler( SALOME_PYQT_Module* module, const QString& fileName )
1086 : myModule( module )
1088 QFile aFile( fileName );
1089 if ( !aFile.open( IO_ReadOnly ) )
1091 if ( !myDoc.setContent( &aFile ) ) {
1099 Called by SALOME_PYQT_Module::initialize() in order to create actions
1100 (menus, toolbars, popup menus)
1102 void SALOME_PYQT_XmlHandler::createActions()
1104 // get document element
1105 QDomElement aDocElem = myDoc.documentElement();
1107 // get main menu actions
1108 QDomNodeList aMenuList = aDocElem.elementsByTagName( "menu-item" );
1109 for ( int i = 0; i < aMenuList.count(); i++ ) {
1110 QDomNode n = aMenuList.item( i );
1114 // create toolbars actions
1115 QDomNodeList aToolsList = aDocElem.elementsByTagName( "toolbar" );
1116 for ( int i = 0; i < aToolsList.count(); i++ ) {
1117 QDomNode n = aToolsList.item( i );
1123 * Creates popup menu
1125 void SALOME_PYQT_XmlHandler::createPopup( QPopupMenu* menu,
1126 const QString& context,
1127 const QString& parent,
1128 const QString& object )
1130 // get document element
1131 QDomElement aDocElem = myDoc.documentElement();
1133 // get popup menus actions
1134 QDomNodeList aPopupList = aDocElem.elementsByTagName( "popupmenu" );
1135 for ( int i = 0; i < aPopupList.count(); i++ ) {
1136 QDomNode n = aPopupList.item( i );
1137 if ( !n.isNull() && n.isElement() ) {
1138 QDomElement e = n.toElement();
1139 QString lab = attribute( e, "label-id" );
1140 QString ctx = attribute( e, "context-id" );
1141 QString prt = attribute( e, "parent-id" );
1142 QString obj = attribute( e, "object-id" );
1143 if ( ctx == context && prt == parent && obj == object ) {
1144 insertPopupItems( n, menu );
1152 Create main menu with child actions
1154 void SALOME_PYQT_XmlHandler::createMenu( QDomNode& parentNode, const int parentMenuId )
1159 if ( parentNode.isNull() )
1162 QDomElement parentElement = parentNode.toElement();
1163 if ( !parentElement.isNull() ) {
1164 QString plabel = attribute( parentElement, "label-id" );
1165 int pid = checkInt( attribute( parentElement, "item-id" ) );
1166 int ppos = checkInt( attribute( parentElement, "pos-id" ) );
1167 if ( !plabel.isEmpty() ) {
1169 int menuId = myModule->createMenu( plabel, // label
1170 parentMenuId, // parent menu ID, should be -1 for main menu
1174 QDomNode node = parentNode.firstChild();
1175 while ( !node.isNull() ) {
1176 if ( node.isElement() ) {
1177 QDomElement elem = node.toElement();
1178 QString aTagName = tagName( elem );
1179 if ( aTagName == "popup-item" ) {
1180 int id = checkInt( attribute( elem, "item-id" ) );
1181 int pos = checkInt( attribute( elem, "pos-id" ) );
1182 QString label = attribute( elem, "label-id" );
1183 QString icon = attribute( elem, "icon-id" );
1184 QString tooltip = attribute( elem, "tooltip-id" );
1185 QString accel = attribute( elem, "accel-id" );
1186 bool toggle = checkBool( attribute( elem, "toggle-id" ) );
1187 ////QString execute = attribute( elem, "execute-action" ); // not used
1189 // -1 action ID is not allowed : it means that <item-id> attribute is missed in the XML file!
1190 // also check if the action with given ID is already created
1191 if ( id != -1 && !myModule->action( SALOME_PYQT_Module::PYQT_ACTION_MENU + id ) ) {
1192 // little trick to have several actions with same ID for menus and toolbars
1193 id = SALOME_PYQT_Module::PYQT_ACTION_MENU + id;
1194 // create menu action
1195 QAction* action = myModule->createAction( id, // ID
1199 tooltip, // status-bar text
1200 QKeySequence( accel ), // keyboard accelerator
1201 toggle ); // toogled action
1202 myModule->addAction( SALOME_PYQT_Module::PYQT_ACTION_MENU, action );
1203 myModule->createMenu( action, menuId, -1, 80, pos );
1206 else if ( aTagName == "submenu" ) {
1208 createMenu( node, menuId );
1210 else if ( aTagName == "separator" ) {
1211 // create menu separator
1212 int pos = checkInt( attribute( elem, "pos-id" ) );
1213 QAction* action = myModule->createSeparator();
1214 myModule->createMenu( action, menuId, -1, 80, pos );
1217 node = node.nextSibling();
1224 Create a toolbar with child actions
1226 void SALOME_PYQT_XmlHandler::createToolBar( QDomNode& parentNode )
1231 if ( parentNode.isNull() )
1234 QDomElement parentElement = parentNode.toElement();
1235 if ( !parentElement.isNull() ) {
1236 QString aLabel = attribute( parentElement, "label-id" );
1237 if ( !aLabel.isEmpty() ) {
1239 int tbId = myModule->createTool( aLabel );
1240 QDomNode node = parentNode.firstChild();
1241 while ( !node.isNull() ) {
1242 if ( node.isElement() ) {
1243 QDomElement elem = node.toElement();
1244 QString aTagName = tagName( elem );
1245 if ( aTagName == "toolbutton-item" ) {
1246 int id = checkInt( attribute( elem, "item-id" ) );
1247 int pos = checkInt( attribute( elem, "pos-id" ) );
1248 QString label = attribute( elem, "label-id" );
1249 QString icon = attribute( elem, "icon-id" );
1250 QString tooltip = attribute( elem, "tooltip-id" );
1251 QString accel = attribute( elem, "accel-id" );
1252 bool toggle = checkBool( attribute( elem, "toggle-id" ) );
1253 ////QString execute = attribute( elem, "execute-action" ); // not used
1255 // -1 action ID is not allowed : it means that <item-id> attribute is missed in the XML file!
1256 // also check if the action with given ID is already created
1257 if ( id != -1 && !myModule->action( SALOME_PYQT_Module::PYQT_ACTION_TOOLBAL + id ) ) {
1258 // little trick to have several actions with same ID for menus and toolbars
1259 id = SALOME_PYQT_Module::PYQT_ACTION_TOOLBAL + id;
1260 // create toolbar action
1261 QAction* action = myModule->createAction( id, // ID
1265 tooltip, // status-bar text
1266 QKeySequence( accel ), // keyboard accelerator
1267 toggle ); // toogled action
1268 myModule->addAction( SALOME_PYQT_Module::PYQT_ACTION_TOOLBAL, action );
1269 myModule->createTool( action, tbId, -1, pos );
1272 else if ( aTagName == "separatorTB" ) {
1273 // create toolbar separator
1274 int pos = checkInt( attribute( elem, "pos-id" ) );
1275 QAction* action = myModule->createSeparator();
1276 myModule->createTool( action, tbId, -1, pos );
1279 node = node.nextSibling();
1285 void SALOME_PYQT_XmlHandler::insertPopupItems( QDomNode& parentNode, QPopupMenu* menu )
1290 if ( parentNode.isNull() )
1293 // we create popup menus without help of QtxPopupMgr
1294 QDomNode node = parentNode.firstChild();
1295 while ( !node.isNull() ) {
1296 if ( node.isElement() ) {
1297 QDomElement elem = node.toElement();
1298 QString aTagName = tagName( elem );
1299 if ( aTagName == "popup-item" ) {
1300 // insert a command item
1301 int id = checkInt( attribute( elem, "item-id" ) );
1302 int pos = checkInt( attribute( elem, "pos-id" ) );
1303 QString label = attribute( elem, "label-id" );
1304 QString icon = attribute( elem, "icon-id" );
1305 /////QString tooltip = attribute( elem, "tooltip-id" ); // not used
1306 QString accel = attribute( elem, "accel-id" );
1307 /////bool toggle = checkBool( attribute( elem, "toggle-id" ) ); // not used
1308 /////QString execute = attribute( elem, "execute-action" ); // not used
1311 if ( !icon.isEmpty() ) {
1312 QPixmap pixmap = myModule->getApp()->resourceMgr()->loadPixmap( myModule->name(""), icon );
1313 if ( !pixmap.isNull() )
1314 anIcon = QIconSet( pixmap );
1317 // -1 action ID is not allowed : it means that <item-id> attribute is missed in the XML file!
1318 // also check if the action with given ID is already created
1320 menu->insertItem( anIcon, label, myModule, SLOT( onGUIEvent(int) ), QKeySequence( accel ), id, pos );
1323 else if ( aTagName == "submenu" ) {
1325 int id = checkInt( attribute( elem, "item-id" ) );
1326 int pos = checkInt( attribute( elem, "pos-id" ) );
1327 QString label = attribute( elem, "label-id" );
1328 QString icon = attribute( elem, "icon-id" );
1331 if ( !icon.isEmpty() ) {
1332 QPixmap pixmap = myModule->getApp()->resourceMgr()->loadPixmap( myModule->name(""), icon );
1333 if ( !pixmap.isNull() )
1334 anIcon = QIconSet( pixmap );
1337 QPopupMenu* newPopup = new QPopupMenu( menu, label );
1338 menu->insertItem( anIcon, label, newPopup, id, pos );
1339 insertPopupItems( node, newPopup );
1341 else if ( aTagName == "separator" ) {
1342 // create menu separator
1343 int pos = checkInt( attribute( elem, "pos-id" ) );
1344 menu->insertSeparator( pos );
1347 node = node.nextSibling();