From 1a3e249d182025dca982751273283eeb4479e8c6 Mon Sep 17 00:00:00 2001 From: vsr Date: Wed, 14 Mar 2012 11:38:34 +0000 Subject: [PATCH] 0020136: EDF 933 DEV : Drag&Drop in OB --- .../SALOME_PYQT_ModuleLight.cxx | 210 +++++++++++++++++- .../SALOME_PYQT_ModuleLight.h | 10 + 2 files changed, 219 insertions(+), 1 deletion(-) diff --git a/src/SALOME_PYQT/SALOME_PYQT_GUILight/SALOME_PYQT_ModuleLight.cxx b/src/SALOME_PYQT/SALOME_PYQT_GUILight/SALOME_PYQT_ModuleLight.cxx index 512e2bfec..9dcffd047 100644 --- a/src/SALOME_PYQT/SALOME_PYQT_GUILight/SALOME_PYQT_ModuleLight.cxx +++ b/src/SALOME_PYQT/SALOME_PYQT_GUILight/SALOME_PYQT_ModuleLight.cxx @@ -2621,7 +2621,7 @@ void SALOME_PYQT_ModuleLight::openEvent(QStringList theListOfFiles, bool &opened #if SIP_VERSION < 0x040800 PyObjWrapper sipList( sipBuildResult( 0, "M", theList, sipClass_QStringList) ); #else - PyObjWrapper sipList( sipBuildResult( 0, "D", theList, sipType_QStringList , NULL) ); + PyObjWrapper sipList( sipBuildResult( 0, "D", theList, sipType_QStringList, NULL ) ); #endif if ( PyObject_HasAttrString(myModule , (char*)"openFiles") ) { PyObjWrapper res( PyObject_CallMethod( myModule, (char*)"openFiles", @@ -2918,3 +2918,211 @@ PyObject* SALOME_PYQT_ModuleLight::getPythonModule() { return myModule; } + +bool SALOME_PYQT_ModuleLight::isDraggable( const SUIT_DataObject* what ) const +{ + MESSAGE("SALOME_PYQT_ModuleLight::isDraggable()"); + // perform synchronous request to Python event dispatcher + bool draggable = false; + class IsDraggableEvent: public PyInterp_LockRequest + { + public: + IsDraggableEvent(PyInterp_Interp* _py_interp, + SALOME_PYQT_ModuleLight* _obj, + LightApp_DataObject* _data_object, + bool& _is_draggable ) + : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true) + myObj( _obj ) , + myDataObject( _data_object ), + myIsDraggable( _is_draggable ) {} + protected: + virtual void execute() + { + myIsDraggable = myObj->isDraggableEvent( myDataObject ); + } + + private: + SALOME_PYQT_ModuleLight* myObj; + LightApp_DataObject* myDataObject; + bool& myIsDraggable; + }; + + const LightApp_DataObject* data_object = dynamic_cast( what ); + + // Posting the request only if dispatcher is not busy! + // Executing the request synchronously + if ( !PyInterp_Dispatcher::Get()->IsBusy() ) + PyInterp_Dispatcher::Get()->Exec( new IsDraggableEvent( myInterp, + const_cast( this ), + const_cast( data_object ), + draggable ) ); + return draggable; +} + +bool SALOME_PYQT_ModuleLight::isDraggableEvent( LightApp_DataObject* what ) +{ + MESSAGE("SALOME_PYQT_ModuleLight::isDraggableEvent()"); + + bool draggable = false; + + // Python interpreter should be initialized and Python module should be + // import first + if ( !myInterp || !myModule || !what ) + return draggable; + + if ( PyObject_HasAttrString(myModule , (char*)"isDraggable") ) { + PyObjWrapper res( PyObject_CallMethod( myModule, (char*)"isDraggable", + (char*)"s", what->entry().toLatin1().constData() ) ); + if( !res || !PyBool_Check( res )) { + PyErr_Print(); + draggable = false; + } + else{ + draggable = PyObject_IsTrue( res ); + } + } + + return draggable; +} + +bool SALOME_PYQT_ModuleLight::isDropAccepted( const SUIT_DataObject* where ) const +{ + MESSAGE("SALOME_PYQT_ModuleLight::isDropAccepted()"); + // perform synchronous request to Python event dispatcher + bool dropAccepted = false; + class IsDropAcceptedEvent: public PyInterp_LockRequest + { + public: + IsDropAcceptedEvent(PyInterp_Interp* _py_interp, + SALOME_PYQT_ModuleLight* _obj, + LightApp_DataObject* _data_object, + bool& _is_drop_accepted ) + : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true) + myObj( _obj ) , + myDataObject( _data_object ), + myIsDropAccepted( _is_drop_accepted ) {} + protected: + virtual void execute() + { + myIsDropAccepted = myObj->isDropAcceptedEvent( myDataObject ); + } + + private: + SALOME_PYQT_ModuleLight* myObj; + LightApp_DataObject* myDataObject; + bool& myIsDropAccepted; + }; + + const LightApp_DataObject* data_object = dynamic_cast( where ); + + // Posting the request only if dispatcher is not busy! + // Executing the request synchronously + if ( !PyInterp_Dispatcher::Get()->IsBusy() ) + PyInterp_Dispatcher::Get()->Exec( new IsDropAcceptedEvent( myInterp, + const_cast( this ), + const_cast( data_object ), + dropAccepted ) ); + return dropAccepted; +} + +bool SALOME_PYQT_ModuleLight::isDropAcceptedEvent( LightApp_DataObject* where ) +{ + MESSAGE("SALOME_PYQT_ModuleLight::isDropAcceptedEvent()"); + + bool dropAccepted = false; + + // Python interpreter should be initialized and Python module should be + // import first + if ( !myInterp || !myModule || !where ) + return dropAccepted; + + if ( PyObject_HasAttrString(myModule , (char*)"isDropAccepted") ) { + PyObjWrapper res( PyObject_CallMethod( myModule, (char*)"isDropAccepted", + (char*)"s", where->entry().toLatin1().constData() ) ); + if( !res || !PyBool_Check( res )) { + PyErr_Print(); + dropAccepted = false; + } + else{ + dropAccepted = PyObject_IsTrue( res ); + } + } + + return dropAccepted; +} + +void SALOME_PYQT_ModuleLight::dropObjects( const DataObjectList& what, SUIT_DataObject* where, + const int row, Qt::DropAction action ) +{ + MESSAGE("SALOME_PYQT_ModuleLight::dropObjects()"); + // perform synchronous request to Python event dispatcher + class DropObjectsEvent: public PyInterp_LockRequest + { + public: + DropObjectsEvent(PyInterp_Interp* _py_interp, + SALOME_PYQT_ModuleLight* _obj, + const DataObjectList& _what, + SUIT_DataObject* _where, + const int _row, + Qt::DropAction _action ) + : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true) + myObj( _obj ) , + myWhat( _what ), + myWhere( _where ), + myRow ( _row ), + myAction ( _action ){} + protected: + virtual void execute() + { + myObj->dropObjectsEvent( myWhat, myWhere, myRow, myAction ); + } + + private: + SALOME_PYQT_ModuleLight* myObj; + DataObjectList myWhat; + SUIT_DataObject* myWhere; + int myRow; + Qt::DropAction myAction; + }; + + // Posting the request only if dispatcher is not busy! + // Executing the request synchronously + if ( !PyInterp_Dispatcher::Get()->IsBusy() ) + PyInterp_Dispatcher::Get()->Exec( new DropObjectsEvent( myInterp, this, what, where, row, action ) ); +} + +void SALOME_PYQT_ModuleLight::dropObjectsEvent( const DataObjectList& what, SUIT_DataObject* where, + const int row, Qt::DropAction action ) +{ + MESSAGE("SALOME_PYQT_ModuleLight::dropObjectsEvent()"); + // Python interpreter should be initialized and Python module should be + // import first + if ( !myInterp || !myModule || what.isEmpty() || !where ) + return; + + QStringList* theList = new QStringList(); + + LightApp_DataObject* whereObject = dynamic_cast( where ); + if ( !whereObject ) return; + + for ( int i = 0; i < what.count(); i++ ) { + LightApp_DataObject* dataObject = dynamic_cast( what[i] ); + if ( dataObject ) theList->append( dataObject->entry() ); + } + +#if SIP_VERSION < 0x040800 + PyObjWrapper sipList( sipBuildResult( 0, "M", theList, sipClass_QStringList) ); +#else + PyObjWrapper sipList( sipBuildResult( 0, "D", theList, sipType_QStringList, NULL) ); +#endif + if ( PyObject_HasAttrString(myModule, (char*)"dropObjects") ) { + PyObjWrapper res( PyObject_CallMethod( myModule, (char*)"dropObjects", (char*)"Osii", + sipList.get(), + whereObject->entry().toLatin1().constData(), + row, action ) ); + + if( !res ) { + PyErr_Print(); + } + } +} diff --git a/src/SALOME_PYQT/SALOME_PYQT_GUILight/SALOME_PYQT_ModuleLight.h b/src/SALOME_PYQT/SALOME_PYQT_GUILight/SALOME_PYQT_ModuleLight.h index cf07211b3..6998cb823 100644 --- a/src/SALOME_PYQT/SALOME_PYQT_GUILight/SALOME_PYQT_ModuleLight.h +++ b/src/SALOME_PYQT/SALOME_PYQT_GUILight/SALOME_PYQT_ModuleLight.h @@ -159,6 +159,11 @@ public: /*Access to the underlying Python module object */ PyObject* getPythonModule(); + /*Drag and drop support*/ + virtual bool isDraggable( const SUIT_DataObject* ) const; + virtual bool isDropAccepted( const SUIT_DataObject* ) const; + virtual void dropObjects( const DataObjectList&, SUIT_DataObject*, + const int, Qt::DropAction ); public slots: virtual bool activateModule( SUIT_Study* ); @@ -203,6 +208,11 @@ private: void saveEvent(QStringList& theListOfFiles); void dumpEvent(QStringList& theListOfFiles); void openEvent(QStringList theListOfFiles, bool& opened); + + bool isDraggableEvent( LightApp_DataObject* ); + bool isDropAcceptedEvent( LightApp_DataObject* ); + void dropObjectsEvent( const DataObjectList&, SUIT_DataObject*, + const int, Qt::DropAction ); SALOME_PYQT_DataObjectLight* findObject(const QString& entry); -- 2.39.2