From: san Date: Wed, 3 Dec 2008 12:51:07 +0000 (+0000) Subject: Improving SALOME_Event mechanism: X-Git-Tag: PARAVIS_TEST~3 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=c8c635371e5436b94afc48c72e031f09951a8cd9;p=modules%2Fgui.git Improving SALOME_Event mechanism: - Session thread ID is initialized automatically, no explicit call to GetSessionThread() is needed - SalomeApp_EventFilter moved to Event library and can be reused in any SALOME-based application that uses SALOME_Event - Some diagnostic output added to SALOME_Event --- diff --git a/src/Event/Event.pro b/src/Event/Event.pro index b28d7bdca..2cd94efb3 100644 --- a/src/Event/Event.pro +++ b/src/Event/Event.pro @@ -14,9 +14,11 @@ win32:DEFINES += WIN32 DEFINES += EVENT_EXPORTS HEADERS = Event.h -HEADERS += SALOME_Event.h +HEADERS += SALOME_Event.h +HEADERS += SALOME_EventFilter.h -SOURCES = SALOME_Event.cxx +SOURCES = SALOME_Event.cxx +SOURCES += SALOME_EventFilter.cxx includes.files = $$HEADERS includes.path = ../../include diff --git a/src/Event/Makefile.am b/src/Event/Makefile.am index bf0eb3642..d73cf1d73 100755 --- a/src/Event/Makefile.am +++ b/src/Event/Makefile.am @@ -32,9 +32,12 @@ lib_LTLIBRARIES = libEvent.la salomeinclude_HEADERS = \ Event.h \ - SALOME_Event.h + SALOME_Event.h \ + SALOME_EventFilter.h -dist_libEvent_la_SOURCES = SALOME_Event.cxx +dist_libEvent_la_SOURCES = \ + SALOME_Event.cxx \ + SALOME_EventFilter.cxx libEvent_la_CPPFLAGS = $(QT_INCLUDES) libEvent_la_LDFLAGS = $(QT_MT_LIBS) diff --git a/src/Event/SALOME_Event.cxx b/src/Event/SALOME_Event.cxx index 7faf45a7d..521e363a3 100755 --- a/src/Event/SALOME_Event.cxx +++ b/src/Event/SALOME_Event.cxx @@ -41,6 +41,49 @@ static DWORD myThread; static pthread_t myThread; #endif +/*! + \class InitEvent + \brief Helper event class responsible for initializing SALOME_Event + mechanism by the main thread ID + */ +class InitEvent : public SALOME_Event +{ +public: + InitEvent(); + virtual ~InitEvent(); + virtual void Execute(); +}; + +/*! + \brief Constructor, initializes the event mechanism by the current thread ID. + It is asssumed to be the main thread ID, so be careful! +*/ +InitEvent::InitEvent() +{ + GetSessionThread(); +} + +/*! + \brief Destructor, does nothing. +*/ +InitEvent::~InitEvent() +{ +} + +/*! + \brief Nothing to be executed for this kind of event. +*/ +void InitEvent::Execute() +{ +} + +// NOTE: Here the SALOME event mechanism is initalized by the +// current thread ID that is always assumed to be the main thread ID. +// This should be revised as soon as the application library is no longer +// linked against the Event library (i.e. this static object is not created or created +// outside the main thread). +static InitEvent myInitEvent; + /*! \class SALOME_CustomEvent \brief Generic event class for user-defined events @@ -172,12 +215,36 @@ SALOME_Event::~SALOME_Event(){ delete mySemaphore; } +/*! + \brief This method should be called by the main GUI thread + in order to execute the code specific for this event and finally + to inform the calling thread that the event + has been processed waking it up with help of the semaphore . + */ +void SALOME_Event::ExecutePostedEvent() +{ + // Diagnose incorrect usage of SALOME_Event API + if ( !IsSessionThread() ){ + qWarning( "SALOME_Event::ExecutePostedEvent() is called from a secondary thread that might mean an error in application logic!" ); + } + // Actual execution specific for particular kind of event + Execute(); + // Signal the calling thread that the event has been processed + processed(); +} + /*! \brief Post the event and wait for its completion. + process() should be called from a secondary thread only. \sa processed() */ void SALOME_Event::process() { + // Diagnose incorrect usage of SALOME_Event API + if ( IsSessionThread() ){ + qWarning( "SALOME_Event::process() is called from the main GUI thread that might mean an error in application logic!" ); + } + QApplication::postEvent( qApp, new SALOME_CustomEvent( SALOME_EVENT, (void*)this ) ); mySemaphore->acquire( 1 ); } diff --git a/src/Event/SALOME_Event.h b/src/Event/SALOME_Event.h index 9146e6af8..536a516be 100644 --- a/src/Event/SALOME_Event.h +++ b/src/Event/SALOME_Event.h @@ -53,6 +53,7 @@ public: SALOME_Event(); virtual ~SALOME_Event(); + void ExecutePostedEvent(); virtual void Execute() = 0; static bool IsSessionThread(); @@ -63,7 +64,6 @@ protected: friend class SalomeApp_EventFilter; static void GetSessionThread(); - friend int main(int, char **); private: QSemaphore* mySemaphore; //!< internal semaphore diff --git a/src/Event/SALOME_EventFilter.cxx b/src/Event/SALOME_EventFilter.cxx new file mode 100755 index 000000000..55027d863 --- /dev/null +++ b/src/Event/SALOME_EventFilter.cxx @@ -0,0 +1,78 @@ +// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include "SALOME_EventFilter.h" +#include "SALOME_Event.h" + +#include + +SALOME_EventFilter* SALOME_EventFilter::myFilter = NULL; + +/*!Constructor.*/ +SALOME_EventFilter::SALOME_EventFilter() +: QObject() +{ + /* VSR 13/01/03 : installing global event filter for the application */ + qApp->installEventFilter( this ); +} + +/*!Destructor.*/ +SALOME_EventFilter::~SALOME_EventFilter() +{ + qApp->removeEventFilter( this ); +} + +/*! + Custom event filter +*/ +bool SALOME_EventFilter::eventFilter( QObject* o, QEvent* e ) +{ + if ( e->type() == SALOME_EVENT ) + { + SALOME_Event* aSE = (SALOME_Event*)((SALOME_CustomEvent*)e)->data(); + processEvent(aSE); + ((SALOME_CustomEvent*)e)->setData( 0 ); + return true; + } + return QObject::eventFilter( o, e ); +} + +/*!Process event.*/ +void SALOME_EventFilter::processEvent( SALOME_Event* theEvent ) +{ + if(theEvent) + theEvent->ExecutePostedEvent(); +} + +/*!Create new instance of SALOME_EventFilter*/ +void SALOME_EventFilter::Init() +{ + if( myFilter==NULL ) + myFilter = new SALOME_EventFilter(); +} + +/*!Destroy filter.*/ +void SALOME_EventFilter::Destroy() +{ + if( myFilter ) + { + delete myFilter; + myFilter = NULL; + } +} diff --git a/src/Event/SALOME_EventFilter.h b/src/Event/SALOME_EventFilter.h new file mode 100755 index 000000000..d671bf194 --- /dev/null +++ b/src/Event/SALOME_EventFilter.h @@ -0,0 +1,60 @@ +// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +#ifndef SALOME_EVENTFILTER_H +#define SALOME_EVENTFILTER_H + +#include "Event.h" +#include + +#if defined WIN32 +#pragma warning( disable: 4251 ) +#endif + +class SALOME_Event; + +/*! + Event filter class for QApplication object that handles custom events posted by SALOME_Event objects. + It assumes that such custom events are alwys posted, not sent. + This event filter can be installed by any application that intends to use SALOME_Event mechanism asynchronously. + This class replaced SalomeApp_EventFilter. +*/ +class EVENT_EXPORT SALOME_EventFilter: public QObject +{ +public: + static void Init(); + static void Destroy(); + +protected: + SALOME_EventFilter(); + virtual ~SALOME_EventFilter(); + +private: + /*! global event filter for qapplication */ + virtual bool eventFilter( QObject* o, QEvent* e ); + void processEvent( SALOME_Event* ); + +private: + static SALOME_EventFilter* myFilter; +}; + +#if defined WIN32 +#pragma warning( default: 4251 ) +#endif + +#endif diff --git a/src/SalomeApp/Makefile.am b/src/SalomeApp/Makefile.am index 33a8c5f4a..ec698b2c3 100755 --- a/src/SalomeApp/Makefile.am +++ b/src/SalomeApp/Makefile.am @@ -40,7 +40,6 @@ salomeinclude_HEADERS = \ SalomeApp_Module.h \ SalomeApp_Study.h \ SalomeApp_ExceptionHandler.h \ - SalomeApp_EventFilter.h \ SalomeApp_PyInterp.h \ SalomeApp_Tools.h \ SalomeApp_ImportOperation.h \ @@ -59,7 +58,6 @@ dist_libSalomeApp_la_SOURCES = \ SalomeApp_LoadStudiesDlg.cxx \ SalomeApp_Study.cxx \ SalomeApp_ExceptionHandler.cxx \ - SalomeApp_EventFilter.cxx \ SalomeApp_PyInterp.cxx \ SalomeApp_Tools.cxx \ SalomeApp_ImportOperation.cxx \ @@ -109,4 +107,4 @@ libSalomeApp_la_LIBADD = $(KERNEL_LDFLAGS) -lOpUtil -lSALOMELocalTrace -lSalome ../SVTK/libSVTK.la ../SOCC/libSOCC.la ../PyInterp/libPyInterp.la \ ../PyConsole/libPyConsole.la ../LogWindow/libLogWindow.la \ ../LightApp/libLightApp.la ../TOOLSGUI/libToolsGUI.la ../Session/libSalomeSession.la \ - ../CASCatch/libCASCatch.la $(CAS_KERNEL) + ../Event/libEvent.la ../CASCatch/libCASCatch.la $(CAS_KERNEL) diff --git a/src/SalomeApp/SalomeApp.pro b/src/SalomeApp/SalomeApp.pro index 46efb7d64..718ad2e3f 100644 --- a/src/SalomeApp/SalomeApp.pro +++ b/src/SalomeApp/SalomeApp.pro @@ -50,7 +50,6 @@ HEADERS += SalomeApp_LoadStudiesDlg.h HEADERS += SalomeApp_Module.h HEADERS += SalomeApp_Study.h HEADERS += SalomeApp_ExceptionHandler.h -HEADERS += SalomeApp_EventFilter.h HEADERS += SalomeApp_Tools.h HEADERS += SalomeApp_ImportOperation.h HEADERS += SalomeApp_Filter.h @@ -66,7 +65,6 @@ SOURCES += SalomeApp_DataObject.cxx SOURCES += SalomeApp_LoadStudiesDlg.cxx SOURCES += SalomeApp_Study.cxx SOURCES += SalomeApp_ExceptionHandler.cxx -SOURCES += SalomeApp_EventFilter.cxx SOURCES += SalomeApp_PyInterp.cxx SOURCES += SalomeApp_Tools.cxx SOURCES += SalomeApp_ImportOperation.cxx diff --git a/src/SalomeApp/SalomeApp_Application.cxx b/src/SalomeApp/SalomeApp_Application.cxx index 3ad682944..4a5aba79e 100644 --- a/src/SalomeApp/SalomeApp_Application.cxx +++ b/src/SalomeApp/SalomeApp_Application.cxx @@ -26,7 +26,6 @@ #include "SalomeApp_Study.h" #include "SalomeApp_DataModel.h" #include "SalomeApp_DataObject.h" -#include "SalomeApp_EventFilter.h" #include "SalomeApp_VisualState.h" #include "SalomeApp_StudyPropertiesDlg.h" #include "SalomeApp_LoadStudiesDlg.h" @@ -52,6 +51,8 @@ #include +#include + // temporary commented //#include @@ -146,7 +147,7 @@ SalomeApp_Application::SalomeApp_Application() SalomeApp_Application::~SalomeApp_Application() { // Do not destroy. It's a singleton ! - //SalomeApp_EventFilter::Destroy(); + //SALOME_EventFilter::Destroy(); } /*!Start application.*/ @@ -154,7 +155,7 @@ void SalomeApp_Application::start() { LightApp_Application::start(); - SalomeApp_EventFilter::Init(); + SALOME_EventFilter::Init(); static bool isFirst = true; if ( isFirst ) { diff --git a/src/SalomeApp/SalomeApp_EventFilter.cxx b/src/SalomeApp/SalomeApp_EventFilter.cxx deleted file mode 100755 index 02af6ce0a..000000000 --- a/src/SalomeApp/SalomeApp_EventFilter.cxx +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 of the License. -// -// This library is distributed in the hope that it will be useful -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com -// - -#include "SalomeApp_EventFilter.h" -#include - -#include - -SalomeApp_EventFilter* SalomeApp_EventFilter::myFilter = NULL; - -/*!Constructor.*/ -SalomeApp_EventFilter::SalomeApp_EventFilter() -: QObject() -{ - /* VSR 13/01/03 : installing global event filter for the application */ - qApp->installEventFilter( this ); -} - -/*!Destructor.*/ -SalomeApp_EventFilter::~SalomeApp_EventFilter() -{ - qApp->removeEventFilter( this ); -} - -/*! - Custom event filter -*/ -bool SalomeApp_EventFilter::eventFilter( QObject* o, QEvent* e ) -{ - if ( e->type() == SALOME_EVENT ) - { - SALOME_Event* aSE = (SALOME_Event*)((SALOME_CustomEvent*)e)->data(); - processEvent(aSE); - ((SALOME_CustomEvent*)e)->setData( 0 ); - return true; - } - return QObject::eventFilter( o, e ); -} - -/*!Process event.*/ -void SalomeApp_EventFilter::processEvent( SALOME_Event* theEvent ) -{ - if(theEvent){ - theEvent->Execute(); - // Signal the calling thread that the event has been processed - theEvent->processed(); - } -} - -/*!Create new instance of SalomeApp_EventFilter*/ -void SalomeApp_EventFilter::Init() -{ - if( myFilter==NULL ) - myFilter = new SalomeApp_EventFilter(); -} - -/*!Destroy filter.*/ -void SalomeApp_EventFilter::Destroy() -{ - if( myFilter ) - { - delete myFilter; - myFilter = NULL; - } -} diff --git a/src/SalomeApp/SalomeApp_EventFilter.h b/src/SalomeApp/SalomeApp_EventFilter.h deleted file mode 100755 index dea5cad73..000000000 --- a/src/SalomeApp/SalomeApp_EventFilter.h +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 of the License. -// -// This library is distributed in the hope that it will be useful -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com -// -#ifndef SALOMEAPP_EVENTFILTER_H -#define SALOMEAPP_EVENTFILTER_H - -#include "SalomeApp.h" -#include - -#if defined WIN32 -#pragma warning( disable: 4251 ) -#endif - -class SALOME_Event; - -/*! - Class provide event filter. -*/ -class SALOMEAPP_EXPORT SalomeApp_EventFilter: public QObject -{ -public: - static void Init(); - static void Destroy(); - -protected: - SalomeApp_EventFilter(); - virtual ~SalomeApp_EventFilter(); - -private: - /*! global event filter for qapplication */ - virtual bool eventFilter( QObject* o, QEvent* e ); - void processEvent( SALOME_Event* ); - -private: - static SalomeApp_EventFilter* myFilter; -}; - -#if defined WIN32 -#pragma warning( default: 4251 ) -#endif - -#endif diff --git a/src/Session/SALOME_Session_Server.cxx b/src/Session/SALOME_Session_Server.cxx index de4c0822d..90e3c00c9 100755 --- a/src/Session/SALOME_Session_Server.cxx +++ b/src/Session/SALOME_Session_Server.cxx @@ -411,9 +411,6 @@ int main( int argc, char **argv ) int orbArgc = 1; orb = init( orbArgc, argv ); - // ...install SALOME thread event handler - SALOME_Event::GetSessionThread(); - CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" ); poa = PortableServer::POA::_narrow( obj );