From 0e6b3ef503c54ff17d20ca997eec23e75eda8b87 Mon Sep 17 00:00:00 2001 From: vsr Date: Tue, 19 Jul 2005 11:43:15 +0000 Subject: [PATCH] Add methods to create menu and toolbar actions to be accessed from Python. --- .../SALOME_PYQT_GUI/SALOME_PYQT_Module.cxx | 235 ++++++++----- .../SALOME_PYQT_GUI/SALOME_PYQT_Module.h | 38 ++- src/SALOME_PYQT/SalomePyQt/Makefile.in | 2 +- src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx | 322 ++++++++++++++++++ src/SALOME_PYQT/SalomePyQt/SalomePyQt.h | 36 +- src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip | 28 ++ src/SALOME_PYQT/SalomePyQt/SalomePyQt_v4.sip | 28 ++ 7 files changed, 588 insertions(+), 101 deletions(-) diff --git a/src/SALOME_PYQT/SALOME_PYQT_GUI/SALOME_PYQT_Module.cxx b/src/SALOME_PYQT/SALOME_PYQT_GUI/SALOME_PYQT_Module.cxx index 8696c7524..beb89da22 100644 --- a/src/SALOME_PYQT/SALOME_PYQT_GUI/SALOME_PYQT_Module.cxx +++ b/src/SALOME_PYQT/SALOME_PYQT_GUI/SALOME_PYQT_Module.cxx @@ -15,7 +15,6 @@ #include "STD_TabDesktop.h" #include "SalomeApp_Application.h" #include "SalomeApp_Study.h" -#include "SalomeApp_DataModel.h" #include "QtxWorkstack.h" #include @@ -34,6 +33,8 @@ #include #include +#include + using namespace std; // Default name of the module, should be replaced at the moment @@ -51,18 +52,6 @@ using namespace std; //============================================================================= -//============================================================================= -// The default PyQt module data model. -// Reuses common data model from SalomeApp. -//============================================================================= -class SALOME_PYQT_DataModel: public SalomeApp_DataModel -{ -public: - SALOME_PYQT_DataModel( CAM_Module* theModule ) : SalomeApp_DataModel( theModule ) {} - bool isModified() const { return false; } - bool isSaved() const { return false; } -}; - //============================================================================= // The class for parsing of the XML resource files. // Used for backward compatibility with existing Python modules. @@ -123,6 +112,16 @@ extern "C" { * Static variables definition */ SALOME_PYQT_Module::InterpMap SALOME_PYQT_Module::myInterpMap; +SALOME_PYQT_Module* SALOME_PYQT_Module::myInitModule = 0; + +/*! + * Little trick : provide an access to being activated Python module from outside; + * needed by the SalomePyQt library :( +*/ +SALOME_PYQT_Module* SALOME_PYQT_Module::getInitModule() +{ + return myInitModule; +} /*! * Constructor @@ -147,17 +146,6 @@ SALOME_PYQT_Module::~SALOME_PYQT_Module() delete myXmlHandler; } -/*! - * Creates data model for the module. - * Reimplemented from CAM_Module. - */ -CAM_DataModel* SALOME_PYQT_Module::createDataModel() -{ - // VSR: this is a temporary solution : - // should reuse default data model from SalomeApp - return new SALOME_PYQT_DataModel( this ); -} - /*! * Initialization of the module. * Inherited from CAM_Module. @@ -228,10 +216,6 @@ bool SALOME_PYQT_Module::activateModule( SUIT_Study* theStudy ) if ( !res ) return res; - // activate menus, toolbars, etc - setMenuShown( true ); - setToolShown( true ); - // ActivateReq: request class for internal activate() operation class ActivateReq : public PyInterp_Request { @@ -256,9 +240,10 @@ bool SALOME_PYQT_Module::activateModule( SUIT_Study* theStudy ) // Posting the request PyInterp_Dispatcher::Get()->Exec( new ActivateReq( theStudy, this ) ); - // connect desktop activation signal - connect( application()->desktop(), SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) ); - + // activate menus, toolbars, etc + setMenuShown( true ); + setToolShown( true ); + return true; } @@ -301,9 +286,6 @@ bool SALOME_PYQT_Module::deactivateModule( SUIT_Study* theStudy ) // Posting the request PyInterp_Dispatcher::Get()->Exec( new DeactivateReq( myInterp, theStudy, this ) ); - // disconnect desktop activation signal - disconnect( application()->desktop(), SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) ); - return res; } @@ -354,36 +336,6 @@ void SALOME_PYQT_Module::onGUIEvent() PyInterp_Dispatcher::Get()->Exec( new GUIEvent( myInterp, this, id ) ); } -/*! - * Desktop activation slot. Used for notifying about changing of the active study. - */ -void SALOME_PYQT_Module::onDesktopActivated() -{ - // StudyChangedReq: request class for internal studyChanged() operation - class StudyChangedReq : public PyInterp_Request - { - public: - StudyChangedReq( SUIT_Study* _study, - SALOME_PYQT_Module* _obj ) - : PyInterp_Request( 0, true ), // this request should be processed synchronously (sync == true) - myStudy ( _study ), - myObj ( _obj ) {} - - protected: - virtual void execute() - { - myObj->studyChanged( myStudy ); - } - - private: - SUIT_Study* myStudy; - SALOME_PYQT_Module* myObj; - }; - - // Posting the request - PyInterp_Dispatcher::Get()->Exec( new StudyChangedReq( application()->activeStudy(), this ) ); -} - /*! Context popup menu request. Called when user activates popup menu in some window (view, object browser, etc). @@ -484,6 +436,8 @@ void SALOME_PYQT_Module::init( CAM_Application* app ) if ( !myModule ) return; // Error + myInitModule = this; + #ifdef __CALL_OLD_METHODS__ // call Python module's setWorkspace() method setWorkSpace(); @@ -553,6 +507,7 @@ void SALOME_PYQT_Module::init( CAM_Application* app ) } } } + myInitModule = 0; } /*! @@ -655,7 +610,13 @@ void SALOME_PYQT_Module::studyChanged( SUIT_Study* theStudy ) */ Engines::Component_var SALOME_PYQT_Module::getEngine() const { - Engines::Component_var comp = getApp()->lcc()->FindOrLoad_Component( "FactoryServerPy", name( "" ) ); + Engines::Component_var comp; + // temporary solution + try { + comp = getApp()->lcc()->FindOrLoad_Component( "FactoryServerPy", name( "" ) ); + } + catch (CORBA::Exception&) { + } return comp; } @@ -669,6 +630,37 @@ QString SALOME_PYQT_Module::engineIOR() const return QString( "" ); } +/*! + * Called when study desktop is activated. + * Used for notifying about changing of the active study. + */ +void SALOME_PYQT_Module::studyActivated() +{ + // StudyChangedReq: request class for internal studyChanged() operation + class StudyChangedReq : public PyInterp_Request + { + public: + StudyChangedReq( SUIT_Study* _study, + SALOME_PYQT_Module* _obj ) + : PyInterp_Request( 0, true ), // this request should be processed synchronously (sync == true) + myStudy ( _study ), + myObj ( _obj ) {} + + protected: + virtual void execute() + { + myObj->studyChanged( myStudy ); + } + + private: + SUIT_Study* myStudy; + SALOME_PYQT_Module* myObj; + }; + + // Posting the request + PyInterp_Dispatcher::Get()->Exec( new StudyChangedReq( application()->activeStudy(), this ) ); +} + /*! * Processes context popup menu request * - calls Python module's definePopup(...) method (obsolete function, used for compatibility with old code) @@ -903,6 +895,81 @@ void SALOME_PYQT_Module::addAction( const PyQtGUIAction type, QAction* action ) } } + +/*! + * The next methods just call the parent implementation. + * This is done to open protected methods from CAM_Module class. +*/ +int SALOME_PYQT_Module::createTool( const QString& name ) +{ + return SalomeApp_Module::createTool( name ); +} +int SALOME_PYQT_Module::createTool( const int id, const int tBar, const int idx ) +{ + return SalomeApp_Module::createTool( id, tBar, idx ); +} +int SALOME_PYQT_Module::createTool( const int id, const QString& tBar, const int idx ) +{ + return SalomeApp_Module::createTool( id, tBar, idx ); +} +int SALOME_PYQT_Module::createTool( QAction* a, const int tBar, const int id, const int idx ) +{ + return SalomeApp_Module::createTool( a, tBar, id, idx ); +} +int SALOME_PYQT_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx ) +{ + return SalomeApp_Module::createTool( a, tBar, id, idx ); +} +int SALOME_PYQT_Module::createMenu( const QString& subMenu, const int menu, const int id, const int group, const int idx ) +{ + return SalomeApp_Module::createMenu( subMenu, menu, id, group, idx ); +} +int SALOME_PYQT_Module::createMenu( const QString& subMenu, const QString& menu, const int id, const int group, const int idx ) +{ + return SalomeApp_Module::createMenu( subMenu, menu, id, group, idx ); +} +int SALOME_PYQT_Module::createMenu( const int id, const int menu, const int group, const int idx ) +{ + return SalomeApp_Module::createMenu( id, menu, group, idx ); +} +int SALOME_PYQT_Module::createMenu( const int id, const QString& menu, const int group, const int idx ) +{ + return SalomeApp_Module::createMenu( id, menu, group, idx ); +} +int SALOME_PYQT_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int idx ) +{ + return SalomeApp_Module::createMenu( a, menu, id, group, idx ); +} +int SALOME_PYQT_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int idx ) +{ + return SalomeApp_Module::createMenu( a, menu, id, group, idx ); +} +QAction* SALOME_PYQT_Module::createSeparator() +{ + return SalomeApp_Module::separator(); +} +QAction* SALOME_PYQT_Module::action( const int id ) const +{ + return SalomeApp_Module::action( id ); +} +int SALOME_PYQT_Module::actionId( const QAction* a ) const +{ + return SalomeApp_Module::actionId( a ); +} +QAction* SALOME_PYQT_Module::createAction( const int id, const QString& text, const QString& icon, + const QString& menu, const QString& tip, const int key, + const bool toggle ) +{ + QIconSet anIcon; + if ( !icon.isEmpty() ) { + QPixmap pixmap = getApp()->resourceMgr()->loadPixmap( name(""), tr( icon ) ); + if ( !pixmap.isNull() ) + anIcon = QIconSet( pixmap ); + } + return SalomeApp_Module::createAction( id, text, anIcon, menu, tip, key, getApp()->desktop(), toggle, this, SLOT( onGUIEvent() ) ); +} + + //============================================================================= // SALOME_PYQT_XmlHandler class implementation //============================================================================= @@ -1037,14 +1104,7 @@ void SALOME_PYQT_XmlHandler::createMenu( QDomNode& parentNode, const int parentM QString tooltip = attribute( elem, "tooltip-id" ); QString accel = attribute( elem, "accel-id" ); bool toggle = checkBool( attribute( elem, "toggle-id" ) ); - QString execute = attribute( elem, "execute-action" ); // not used - - QIconSet anIcon; - if ( !icon.isEmpty() ) { - QPixmap pixmap = myModule->getApp()->resourceMgr()->loadPixmap( myModule->name(""), icon ); - if ( !pixmap.isNull() ) - anIcon = QIconSet( pixmap ); - } + ////QString execute = attribute( elem, "execute-action" ); // not used // -1 action ID is not allowed : it means that attribute is missed in the XML file! // also check if the action with given ID is already created @@ -1054,14 +1114,11 @@ void SALOME_PYQT_XmlHandler::createMenu( QDomNode& parentNode, const int parentM // create menu action QAction* action = myModule->createAction( id, // ID tooltip, // tooltip - anIcon, // icon + icon, // icon label, // menu text tooltip, // status-bar text QKeySequence( accel ), // keyboard accelerator - myModule->getApp()->desktop(), // desktop - toggle, // toogled action - myModule, // receiver - SLOT( onGUIEvent() ) ); // slot + toggle ); // toogled action myModule->addAction( SALOME_PYQT_Module::PYQT_ACTION_MENU, action ); myModule->createMenu( action, menuId, -1, 100, pos ); } @@ -1113,14 +1170,7 @@ void SALOME_PYQT_XmlHandler::createToolBar( QDomNode& parentNode ) QString tooltip = attribute( elem, "tooltip-id" ); QString accel = attribute( elem, "accel-id" ); bool toggle = checkBool( attribute( elem, "toggle-id" ) ); - QString execute = attribute( elem, "execute-action" ); // not used - - QIconSet anIcon; - if ( !icon.isEmpty() ) { - QPixmap pixmap = myModule->getApp()->resourceMgr()->loadPixmap( myModule->name(""), icon ); - if ( !pixmap.isNull() ) - anIcon = QIconSet( pixmap ); - } + ////QString execute = attribute( elem, "execute-action" ); // not used // -1 action ID is not allowed : it means that attribute is missed in the XML file! // also check if the action with given ID is already created @@ -1130,14 +1180,11 @@ void SALOME_PYQT_XmlHandler::createToolBar( QDomNode& parentNode ) // create toolbar action QAction* action = myModule->createAction( id, // ID tooltip, // tooltip - anIcon, // icon + icon, // icon label, // menu text tooltip, // status-bar text QKeySequence( accel ), // keyboard accelerator - myModule->getApp()->desktop(), // desktop - toggle, // toogled action - myModule, // receiver - SLOT( onGUIEvent() ) ); // slot + toggle ); // toogled action myModule->addAction( SALOME_PYQT_Module::PYQT_ACTION_TOOLBAL, action ); myModule->createTool( action, tbId, -1, pos ); } @@ -1175,10 +1222,10 @@ void SALOME_PYQT_XmlHandler::insertPopupItems( QDomNode& parentNode, QPopupMenu* int pos = checkInt( attribute( elem, "pos-id" ) ); QString label = attribute( elem, "label-id" ); QString icon = attribute( elem, "icon-id" ); - QString tooltip = attribute( elem, "tooltip-id" ); // not used + /////QString tooltip = attribute( elem, "tooltip-id" ); // not used QString accel = attribute( elem, "accel-id" ); - bool toggle = checkBool( attribute( elem, "toggle-id" ) ); // not used - QString execute = attribute( elem, "execute-action" ); // not used + /////bool toggle = checkBool( attribute( elem, "toggle-id" ) ); // not used + /////QString execute = attribute( elem, "execute-action" ); // not used QIconSet anIcon; if ( !icon.isEmpty() ) { diff --git a/src/SALOME_PYQT/SALOME_PYQT_GUI/SALOME_PYQT_Module.h b/src/SALOME_PYQT/SALOME_PYQT_GUI/SALOME_PYQT_Module.h index 8509bf79c..9474fb0ac 100644 --- a/src/SALOME_PYQT/SALOME_PYQT_GUI/SALOME_PYQT_Module.h +++ b/src/SALOME_PYQT/SALOME_PYQT_GUI/SALOME_PYQT_Module.h @@ -42,6 +42,8 @@ private: SALOME_PYQT_PyInterp* myInterp; /* Python GUI module loaded */ PyObjWrapper myModule; + /* Pytho GUI being initialized (not zero only during the initialization)*/ + static SALOME_PYQT_Module* myInitModule; typedef QPtrList ActionList; @@ -75,10 +77,15 @@ public: Engines::Component_var getEngine() const; /****************************** - * Inherited from CAM_Module + * Inherited from SalomeApp_Module ******************************/ public: + /* little trick : provide an access to being activated Python module from outside; + needed by the SalomePyQt library :( + */ + static SALOME_PYQT_Module* getInitModule(); + /* initialization */ void initialize ( CAM_Application* ); @@ -93,9 +100,32 @@ public: /* get module engine IOR */ virtual QString engineIOR() const; -protected: - /* data model creation */ - CAM_DataModel* createDataModel(); + /* called when study desktop is activated */ + virtual void studyActivated(); + + /* working with toolbars : open protected methods */ + int createTool( const QString& ); + int createTool( const int, const int, const int = -1 ); + int createTool( const int, const QString&, const int = -1 ); + int createTool( QAction*, const int, const int = -1, const int = -1 ); + int createTool( QAction*, const QString&, const int = -1, const int = -1 ); + + /* working with menus : open protected methods */ + int createMenu( const QString&, const int, const int = -1, const int = -1, const int = -1 ); + int createMenu( const QString&, const QString&, const int = -1, const int = -1, const int = -1 ); + int createMenu( const int, const int, const int = -1, const int = -1 ); + int createMenu( const int, const QString&, const int = -1, const int = -1 ); + int createMenu( QAction*, const int, const int = -1, const int = -1, const int = -1 ); + int createMenu( QAction*, const QString&, const int = -1, const int = -1, const int = -1 ); + + /* create separator : open protected method */ + QAction* createSeparator(); + + /* working with actions : open protected methods */ + QAction* action( const int ) const; + int actionId( const QAction* ) const; + QAction* createAction( const int, const QString&, const QString&, const QString&, + const QString&, const int, const bool = false ); public slots: /* activation */ diff --git a/src/SALOME_PYQT/SalomePyQt/Makefile.in b/src/SALOME_PYQT/SalomePyQt/Makefile.in index 5f3ffb6f2..b1be9c4a1 100644 --- a/src/SALOME_PYQT/SalomePyQt/Makefile.in +++ b/src/SALOME_PYQT/SalomePyQt/Makefile.in @@ -101,7 +101,7 @@ LIB_CLIENT_IDL = SALOME_Exception.idl SALOME_GenericObj.idl # Compilation/linkage flags CPPFLAGS += $(QT_INCLUDES) $(PYTHON_INCLUDES) $(SIP_INCLUDES) $(OCC_INCLUDES) $(VTK_INCLUDES) $(OGL_INCLUDES) $(BOOST_CPPFLAGS) -DHAVE_CONFIG_H -I${KERNEL_ROOT_DIR}/include/salome LIBS += $(PYTHON_LIBS) $(SIP_LIBS) $(PYQT_LIBS) $(VTK_LIBS) $(OGL_LIBS) -LDFLAGS += -lsuit -lCAM -lstd -lqtx -lSalomeApp -lSalomeSession -lEvent -lLogWindow -lVTKViewer -lToolsGUI -L${KERNEL_ROOT_DIR}/lib/salome -lSalomeDS +LDFLAGS += -lsuit -lCAM -lstd -lqtx -lSalomeApp -lSalomeSession -lEvent -lLogWindow -lVTKViewer -lToolsGUI -L${KERNEL_ROOT_DIR}/lib/salome -lSalomeDS -lSalomePyQtGUI # Custom build step: generate C++ wrapping according to $(SIP_FILES) diff --git a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx index 7e84a7698..929b3d323 100644 --- a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx +++ b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx @@ -7,10 +7,14 @@ // $Header : $ //============================================================================= +#include "SALOME_PYQT_Module.h" // this include must be first!!! #include "SalomePyQt.h" #include #include +#include +#include +#include #include #include "SALOME_Event.hxx" @@ -888,3 +892,321 @@ bool SalomePyQt::dumpView( const QString& filename ) return ProcessEvent( new TDumpViewEvent( filename ) ); } +/*! + SalomePyQt::createTool + These methods allow operating with the toolbars: + - create a new toolbar or get the existing one (the toolbar name is passed as parameter); + this method returns an id of the toolbar; + - add action with given id (must be created previously) and optional index to the existing toolbar + (toobar is identified either by its id or by its name) + these methods return an id of the action. + If error occurs, the -1 value is returned. +*/ +class CrTool +{ +public: + CrTool( const QString& tBar ) + : myCase( 0 ), myTbName( tBar ) {} + CrTool( const int id, const int tBar, const int idx ) + : myCase( 1 ), myId( id ), myTbId( tBar ), myIndex( idx ) {} + CrTool( const int id, const QString& tBar, const int idx ) + : myCase( 2 ), myId( id ), myTbName( tBar ), myIndex( idx ) {} + CrTool( QAction* action, const int tbId, const int id, const int idx ) + : myCase( 3 ), myAction( action ), myTbId( tbId ), myId( id ), myIndex( idx ) {} + CrTool( QAction* action, const QString& tBar, const int id, const int idx ) + : myCase( 4 ), myAction( action ), myTbName( tBar ), myId( id ), myIndex( idx ) {} + + int execute( SALOME_PYQT_Module* module ) const + { + if ( module ) { + switch ( myCase ) { + case 0: + return module->createTool( myTbName ); + case 1: + return module->createTool( myId, myTbId, myIndex ); + case 2: + return module->createTool( myId, myTbName, myIndex ); + case 3: + return module->createTool( myAction, myTbId, myId, myIndex ); + case 4: + return module->createTool( myAction, myTbName, myId, myIndex ); + } + } + return -1; + } +private: + int myCase; + QString myTbName; + int myTbId; + QAction* myAction; + int myId; + int myIndex; +}; +class TCreateToolEvent: public SALOME_Event { +public: + typedef int TResult; + TResult myResult; + const CrTool& myCrTool; + TCreateToolEvent( const CrTool& crTool ) + : myResult( -1 ), myCrTool( crTool ) {} + virtual void Execute() { + if ( SalomeApp_Application* anApp = getApplication() ) { + SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule(); + if ( !module ) + module = dynamic_cast( anApp->activeModule() ); + myResult = myCrTool.execute( module ); + } + } +}; +// create new toolbar or get existing by name +int SalomePyQt::createTool( const QString& tBar ) +{ + return ProcessEvent( new TCreateToolEvent( CrTool( tBar ) ) ); +} +// add action with id and index to the existing tollbar +int SalomePyQt::createTool( const int id, const int tBar, const int idx ) +{ + return ProcessEvent( new TCreateToolEvent( CrTool( id, tBar, idx ) ) ); +} +// add action with id and index to the existing tollbar +int SalomePyQt::createTool( const int id, const QString& tBar, const int idx ) +{ + return ProcessEvent( new TCreateToolEvent( CrTool( id, tBar, idx ) ) ); +} +// add action with id and index to the existing tollbar +int SalomePyQt::createTool( QAction* a, const int tBar, const int id, const int idx ) +{ + return ProcessEvent( new TCreateToolEvent( CrTool( a, tBar, id, idx ) ) ); +} +// add action with id and index to the existing tollbar +int SalomePyQt::createTool( QAction* a, const QString& tBar, const int id, const int idx ) +{ + return ProcessEvent( new TCreateToolEvent( CrTool( a, tBar, id, idx ) ) ); +} + +/*! + SalomePyQt::createMenu + These methods allow operating with the main menu: + - create a new menu or submenu or get the existing one (the parent menu name or id is passed as parameter, + if it is empty or -1, it means that main menu is created, otherwise submenu is created); + this method returns an id of the menu/submenu; + - add action with given id (must be created previously) and optional index and group number to the existing menu + or submenu (menu name or id us passed as parameter) + these methods return an id of the action. + If error occurs, the -1 value is returned. +*/ +class CrMenu +{ +public: + CrMenu( const QString& subMenu, const int menu, const int group, const int idx ) + : myCase( 0 ), mySubMenuName( subMenu ), myMenuId( menu ), myGroup( group ), myIndex( idx ) {} + CrMenu( const QString& subMenu, const QString& menu, const int group, const int idx ) + : myCase( 1 ), mySubMenuName( subMenu ), myMenuName( menu ), myGroup( group ), myIndex( idx ) {} + CrMenu( const int id, const int menu, const int group, const int idx ) + : myCase( 2 ), myId( id ), myMenuId( menu ), myGroup( group ), myIndex( idx ) {} + CrMenu( const int id, const QString& menu, const int group, const int idx ) + : myCase( 3 ), myId( id ), myMenuName( menu ), myGroup( group ), myIndex( idx ) {} + CrMenu( QAction* action, const int menu, const int id, const int group, const int idx ) + : myCase( 4 ), myAction( action ), myMenuId( menu ), myId( id ), myGroup( group ), myIndex( idx ) {} + CrMenu( QAction* action, const QString& menu, const int id, const int group, const int idx ) + : myCase( 5 ), myAction( action ), myMenuName( menu ), myId( id ), myGroup( group ), myIndex( idx ) {} + + int execute( SALOME_PYQT_Module* module ) const + { + if ( module ) { + switch ( myCase ) { + case 0: + return module->createMenu( mySubMenuName, myMenuId, -1, myGroup, myIndex ); + case 1: + return module->createMenu( mySubMenuName, myMenuName, -1, myGroup, myIndex ); + case 2: + return module->createMenu( myId, myMenuId, myGroup, myIndex ); + case 3: + return module->createMenu( myId, myMenuName, myGroup, myIndex ); + case 4: + return module->createMenu( myAction, myMenuId, myId, myGroup, myIndex ); + case 5: + return module->createMenu( myAction, myMenuName, myId, myGroup, myIndex ); + } + } + return -1; + } +private: + int myCase; + QString myMenuName; + int myMenuId; + QString mySubMenuName; + int myGroup; + QAction* myAction; + int myId; + int myIndex; +}; +class TCreateMenuEvent: public SALOME_Event { +public: + typedef int TResult; + TResult myResult; + const CrMenu& myCrMenu; + TCreateMenuEvent( const CrMenu& crMenu ) + : myResult( -1 ), myCrMenu( crMenu ) {} + virtual void Execute() { + if ( SalomeApp_Application* anApp = getApplication() ) { + SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule(); + if ( !module ) + module = dynamic_cast( anApp->activeModule() ); + myResult = myCrMenu.execute( module ); + } + } +}; +int SalomePyQt::createMenu( const QString& subMenu, const int menu, const int group, const int idx ) +{ + return ProcessEvent( new TCreateMenuEvent( CrMenu( subMenu, menu, group, idx ) ) ); +} + +int SalomePyQt::createMenu( const QString& subMenu, const QString& menu, const int group, const int idx ) +{ + return ProcessEvent( new TCreateMenuEvent( CrMenu( subMenu, menu, group, idx ) ) ); +} + +int SalomePyQt::createMenu( const int id, const int menu, const int group, const int idx ) +{ + return ProcessEvent( new TCreateMenuEvent( CrMenu( id, menu, group, idx ) ) ); +} + +int SalomePyQt::createMenu( const int id, const QString& menu, const int group, const int idx ) +{ + return ProcessEvent( new TCreateMenuEvent( CrMenu( id, menu, group, idx ) ) ); +} + +int SalomePyQt::createMenu( QAction* a, const int menu, const int id, const int group, const int idx ) +{ + return ProcessEvent( new TCreateMenuEvent( CrMenu( a, menu, id, group, idx ) ) ); +} + +int SalomePyQt::createMenu( QAction* a, const QString& menu, const int id, const int group, const int idx ) +{ + return ProcessEvent( new TCreateMenuEvent( CrMenu( a, menu, id, group, idx ) ) ); +} + +/*! + SalomePyQt::createSeparator + Create a separator action which can be then used in the menu or toolbar. +*/ +class TCreateSepEvent: public SALOME_Event { +public: + typedef QAction* TResult; + TResult myResult; + TCreateSepEvent() + : myResult( 0 ) {} + virtual void Execute() { + if ( SalomeApp_Application* anApp = getApplication() ) { + SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule(); + if ( !module ) + module = dynamic_cast( anApp->activeModule() ); + if ( module ) + myResult = module->createSeparator(); + } + } +}; +QAction* SalomePyQt::createSeparator() +{ + return ProcessEvent( new TCreateSepEvent() ); +} + +/*! + SalomePyQt::createAction + Create an action which can be then used in the menu or toolbar: + - id : the unique id action to be registered to; + - menuText : action text which should appear in menu; + - tipText : text which should appear in the tooltip; + - statusText : text which should appear in the status bar when action is activated; + - icon : the name of the icon file (the actual icon file name can be coded in the translation files); + - key : the key accelrator for the action + - toggle : if true the action is checkable +*/ +class TCreateActionEvent: public SALOME_Event { +public: + typedef QAction* TResult; + TResult myResult; + int myId; + QString myMenuText; + QString myTipText; + QString myStatusText; + QString myIcon; + int myKey; + bool myToggle; + TCreateActionEvent( const int id, const QString& menuText, const QString& tipText, + const QString& statusText, const QString& icon, const int key, const bool toggle ) + : myResult( 0 ), myId( id ), myMenuText( menuText ), myTipText( tipText ), + myStatusText( statusText ), myIcon( icon ), myKey( key ), myToggle( toggle ) {} + virtual void Execute() { + if ( SalomeApp_Application* anApp = getApplication() ) { + printf("TCreateActionEvent::Execute() - 1\n"); + SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule(); + printf("TCreateActionEvent::Execute() - 2: module = %d\n",module); + if ( !module ) + module = dynamic_cast( anApp->activeModule() ); + printf("TCreateActionEvent::Execute() - 3: module = %d\n",module); + if ( module ) + myResult = module->createAction( myId, myTipText, myIcon, myMenuText, myStatusText, myKey, myToggle ); + printf("TCreateActionEvent::Execute() - 4: myResult = %d\n",myResult); + } + } +}; +QAction* SalomePyQt::createAction( const int id, const QString& menuText, + const QString& tipText, const QString& statusText, + const QString& icon, const int key, const bool toggle ) +{ + return ProcessEvent( new TCreateActionEvent( id, menuText, tipText, statusText, icon, key, toggle ) ); +} + +/*! + SalomePyQt::action + Get an action by its id. Returns 0 if the action with such id was not registered. +*/ +class TActionEvent: public SALOME_Event { +public: + typedef QAction* TResult; + TResult myResult; + int myId; + TActionEvent( const int id ) + : myResult( 0 ), myId( id ) {} + virtual void Execute() { + if ( SalomeApp_Application* anApp = getApplication() ) { + SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule(); + if ( !module ) + module = dynamic_cast( anApp->activeModule() ); + if ( module ) + myResult = module->action( myId ); + } + } +}; +QAction* SalomePyQt::action( const int id ) +{ + return ProcessEvent( new TActionEvent( id ) ); +} + +/*! + SalomePyQt::actionId + Get an action id. Returns -1 if the action was not registered. +*/ +class TActionIdEvent: public SALOME_Event { +public: + typedef int TResult; + TResult myResult; + const QAction* myAction; + TActionIdEvent( const QAction* action ) + : myResult( -1 ), myAction( action ) {} + virtual void Execute() { + if ( SalomeApp_Application* anApp = getApplication() ) { + SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule(); + if ( !module ) + module = dynamic_cast( anApp->activeModule() ); + if ( module ) + myResult = module->actionId( myAction ); + } + } +}; +int SalomePyQt::actionId( const QAction* a ) +{ + return ProcessEvent( new TActionIdEvent( a ) ); +} diff --git a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.h b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.h index 9eda4b6ae..d15002283 100644 --- a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.h +++ b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.h @@ -10,11 +10,15 @@ #ifndef SALOME_PYQT_H #define SALOME_PYQT_H -#include #include -#include +#include +#include class SalomeApp_SelectionMgr; +class QAction; +class QMenuBar; +class QPopupMenu; +class QWidget; class SALOME_Selection : public QObject { @@ -68,6 +72,34 @@ public: static bool dumpView( const QString& ); + static int createTool( const QString& ); + static int createTool( const int, const int, const int = -1 ); + static int createTool( const int, const QString&, const int = -1 ); + static int createTool( QAction*, const int, const int = -1, const int = -1 ); + static int createTool( QAction*, const QString&, const int = -1, const int = -1 ); + + static int createMenu( const QString&, const int = -1, + const int = -1, const int = -1 ); + static int createMenu( const QString&, const QString& = QString::null, + const int = -1, const int = -1 ); + static int createMenu( const int, const int = -1, + const int = -1, const int = -1 ); + static int createMenu( const int, const QString& = QString::null, + const int = -1, const int = -1 ); + static int createMenu( QAction*, const int, const int = -1, + const int = -1, const int = -1 ); + static int createMenu( QAction*, const QString&, const int = -1, + const int = -1, const int = -1 ); + + static QAction* createSeparator(); + + static QAction* createAction( const int, const QString&, + const QString& = QString::null, const QString& = QString::null, + const QString& = QString::null, const int = 0, const bool = false ); + + static QAction* action( const int ); + static int actionId( const QAction* ); + static void addSetting ( const QString&, const QString&, const double ); static void addSetting ( const QString&, const QString&, const int ); static void addSetting ( const QString&, const QString&, const QString& ); diff --git a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip index a98fbff82..9b4b3ee66 100644 --- a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip +++ b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip @@ -65,6 +65,34 @@ public: static bool dumpView( const QString& ); + static int createTool( const QString& ); + static int createTool( const int, const int, int = -1 ); + static int createTool( const int, const QString&, int = -1 ); + static int createTool( QAction*, const int, int = -1, int = -1 ); + static int createTool( QAction*, const QString&, int = -1, int = -1 ); + + static int createMenu( const QString&, const int, + int = -1, int = -1 ); + static int createMenu( const QString&, const QString&, + int = -1, int = -1 ); + static int createMenu( const int, const int, + int = -1, int = -1 ); + static int createMenu( const int, const QString&, + int = -1, int = -1 ); + static int createMenu( QAction*, const int, int = -1, + int = -1, int = -1 ); + static int createMenu( QAction*, const QString&, int = -1, + int = -1, int = -1 ); + + static QAction* createSeparator(); + + static QAction* createAction( const int, const QString&, + const QString& = QString::null, const QString& = QString::null, + const QString& = QString::null, int = 0, bool = false ); + + static QAction* action( const int ); + static int actionId( const QAction* ); + static void addSetting ( const QString&, const QString&, const double ); static void addSetting ( const QString&, const QString&, const int /Constrained/ ); static void addSetting ( const QString&, const QString&, const QString& ); diff --git a/src/SALOME_PYQT/SalomePyQt/SalomePyQt_v4.sip b/src/SALOME_PYQT/SalomePyQt/SalomePyQt_v4.sip index 3bda8b821..4de783e98 100644 --- a/src/SALOME_PYQT/SalomePyQt/SalomePyQt_v4.sip +++ b/src/SALOME_PYQT/SalomePyQt/SalomePyQt_v4.sip @@ -63,6 +63,34 @@ public: static bool dumpView( const QString& ); + static int createTool( const QString& ); + static int createTool( const int, const int, const int = -1 ); + static int createTool( const int, const QString&, const int = -1 ); + static int createTool( QAction*, const int, const int = -1, const int = -1 ); + static int createTool( QAction*, const QString&, const int = -1, const int = -1 ); + + static int createMenu( const QString&, const int, + const int = -1, const int = -1 ); + static int createMenu( const QString&, const QString&, + const int = -1, const int = -1 ); + static int createMenu( const int, const int, + const int = -1, const int = -1 ); + static int createMenu( const int, const QString&, + const int = -1, const int = -1 ); + static int createMenu( QAction*, const int, const int = -1, + const int = -1, const int = -1 ); + static int createMenu( QAction*, const QString&, const int = -1, + const int = -1, const int = -1 ); + + static QAction* createSeparator(); + + static QAction* createAction( const int, const QString&, + const QString& = QString::null, const QString& = QString::null, + const QString& = QString::null, const int = 0, const bool = false ); + + static QAction* action( const int ); + static int actionId( const QAction* ); + static void addSetting ( const QString&, const QString&, const double ); static void addSetting ( const QString&, const QString&, const int /Constrained/ ); static void addSetting ( const QString&, const QString&, const QString& ); -- 2.30.2