Salome HOME
Dialog SetupCurveDlg has been added
[modules/gui.git] / src / PyInterp / PyInterp_Dispatcher.h
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2005  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : PyInterp_Dispatcher.h
8 //  Author : Sergey Anikin, OCC
9 //  Module : SALOME
10 //  $Header$
11
12 #ifndef _PYINTERP_DISPATCHER_H_
13 #define _PYINTERP_DISPATCHER_H_
14
15 #include "PyInterp.h"
16
17 #include <qthread.h>
18 #include <qevent.h>
19
20 #include <list>
21
22 class QObject;
23
24 class PyInterp_base;
25 class PyInterp_Watcher;
26 class PyInterp_Dispatcher;
27
28 //////////////////////////////////////////////////////////
29 // class : PyInterp_Request
30 //////////////////////////////////////////////////////////
31 class PYINTERP_EXPORT PyInterp_Request
32 {
33   friend class PyInterp_Dispatcher;
34
35   PyInterp_Request();
36   PyInterp_Request( const PyInterp_Request& );
37
38 protected:
39   virtual         ~PyInterp_Request() {}; 
40   // protected destructor - to control deletion of requests
41
42 public:
43   PyInterp_Request( QObject* listener, bool sync = false )
44     : myIsSync( sync ), myListener( listener ), myEvent( 0 ) {};
45
46   static void     Destroy( PyInterp_Request* );
47   // Deletes a request
48
49   bool            IsSync() const { return myIsSync; }
50   // Returns true if this request should be processed synchronously,
51   // without putting it to a queue
52
53 protected:
54   virtual void    safeExecute();
55
56   virtual void    execute() = 0;
57   // Should be redefined in successors, contains actual request code
58
59   virtual QEvent* createEvent() const;
60   // This method can be overridden to customize notification event creation
61
62 private:
63   void            process();
64   QObject*        getListener() const { return myListener; }
65   void            setListener( QObject* );
66   QEvent*         getEvent();
67   void            postEvent();
68
69 private:
70   bool            myIsSync;
71   QObject*        myListener;
72   QEvent*         myEvent;
73   QMutex          myMutex;
74 };
75
76 class PYINTERP_EXPORT PyInterp_LockRequest : public PyInterp_Request
77 {
78 public:
79   PyInterp_LockRequest( PyInterp_base* interp, QObject* listener = 0, bool sync = false )
80     : PyInterp_Request( listener, sync ), myInterp( interp ) {}
81
82 protected:
83   PyInterp_base*  getInterp() const { return myInterp; }
84
85   virtual void    safeExecute();
86
87 private:
88   PyInterp_base*  myInterp;
89 };
90
91 //////////////////////////////////////////////////////////
92 // class : PyInterp_Event
93 //////////////////////////////////////////////////////////
94 class PYINTERP_EXPORT PyInterp_Event : public QCustomEvent
95 {
96   PyInterp_Event();
97   PyInterp_Event( const PyInterp_Event& );
98
99 public:
100   enum { NOTIFY = QEvent::User + 5000, OK, ERROR, INCOMPLETE, LAST };
101
102   PyInterp_Event( int type, PyInterp_Request* request )
103     : QCustomEvent( (QEvent::Type)type ), myRequest( request ) {}
104
105   virtual ~PyInterp_Event();
106
107   PyInterp_Request* GetRequest() const { return myRequest; }
108   operator PyInterp_Request*() const { return myRequest; }
109
110 private:
111   PyInterp_Request* myRequest;
112 };
113
114 //////////////////////////////////////////////////////////
115 // class : PyInterp_Dispatcher
116 //////////////////////////////////////////////////////////
117 class PYINTERP_EXPORT PyInterp_Dispatcher : protected QThread
118 {
119   PyInterp_Dispatcher(); // private constructor
120
121 public:
122   static PyInterp_Dispatcher* Get();
123
124   virtual                     ~PyInterp_Dispatcher();
125
126   bool                        IsBusy() const;  
127   void                        Exec( PyInterp_Request* );
128
129 private:
130   virtual void                run();
131   void                        processRequest( PyInterp_Request* );
132   void                        objectDestroyed( const QObject* );
133
134 private:
135   typedef PyInterp_Request*   RequestPtr;
136
137   std::list<RequestPtr>       myQueue;
138   QMutex                      myQueueMutex;
139   PyInterp_Watcher*           myWatcher;
140
141   static PyInterp_Dispatcher* myInstance;
142
143   friend class PyInterp_Watcher;
144 };
145
146 #endif