]> SALOME platform Git repositories - modules/gui.git/commitdiff
Salome HOME
Improving SALOME_Event mechanism:
authorsan <san@opencascade.com>
Wed, 3 Dec 2008 12:51:07 +0000 (12:51 +0000)
committersan <san@opencascade.com>
Wed, 3 Dec 2008 12:51:07 +0000 (12:51 +0000)
- 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

12 files changed:
src/Event/Event.pro
src/Event/Makefile.am
src/Event/SALOME_Event.cxx
src/Event/SALOME_Event.h
src/Event/SALOME_EventFilter.cxx [new file with mode: 0755]
src/Event/SALOME_EventFilter.h [new file with mode: 0755]
src/SalomeApp/Makefile.am
src/SalomeApp/SalomeApp.pro
src/SalomeApp/SalomeApp_Application.cxx
src/SalomeApp/SalomeApp_EventFilter.cxx [deleted file]
src/SalomeApp/SalomeApp_EventFilter.h [deleted file]
src/Session/SALOME_Session_Server.cxx

index b28d7bdca55129d1eb621cfc10c6750382d20f88..2cd94efb38a60c2528257fa657df1ad0cb5d1591 100644 (file)
@@ -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
index bf0eb3642984f76d0e7451b4e3c47d9de027571b..d73cf1d73d367f1635c161d8ea03f5957c2e18ac 100755 (executable)
@@ -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)
index 7faf45a7d8c557ffa24e4a54e96b89b8882a81c0..521e363a3fbd6183d92d5302d728f11998c506da 100755 (executable)
@@ -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 );
 }
index 9146e6af8539a225a671b8c71f28de3bdb2d129e..536a516be44f9e29024b9be87cc09ec094e775c7 100644 (file)
@@ -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 (executable)
index 0000000..55027d8
--- /dev/null
@@ -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 <QApplication>
+
+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 (executable)
index 0000000..d671bf1
--- /dev/null
@@ -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 <QObject>
+
+#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
index 33a8c5f4a2f03c2021da2733708f02ab7227255b..ec698b2c3208a00051a1393768ac8d858050d2a5 100755 (executable)
@@ -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)
index 46efb7d64ca643c1432b5d736ee604dd2bad4c01..718ad2e3f73e4b9afcc0356432b624d5ee4e9e77 100644 (file)
@@ -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
index 3ad682944b8b4bb91a5456dd255628d8344eeb72..4a5aba79eb0102bcfeb5479c72e68f845301b5fe 100644 (file)
@@ -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 <QtxTreeView.h>
 
+#include <SALOME_EventFilter.h>
+
 // temporary commented
 //#include <OB_ListItem.h>
 
@@ -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 (executable)
index 02af6ce..0000000
+++ /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 <SALOME_Event.h>
-
-#include <QApplication>
-
-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 (executable)
index dea5cad..0000000
+++ /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 <QObject>
-
-#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
index de4c0822d0a70994b13aaa03b9d40c365a330b6d..90e3c00c933eccbfe8de0b14a5e78786f7641588 100755 (executable)
@@ -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 );