OpenFlipperThread.hh 8.42 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  OpenFlipper 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 3 of the License, or
//  (at your option) any later version.
//
//  OpenFlipper 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 OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
26
27
28
//   $Revision$
//   $Author$
//   $Date$
29
30
31
32
33
34
35
36
//
//=============================================================================


#ifndef OPENFLIPPERTHREAD_HH
#define OPENFLIPPERTHREAD_HH

#include <QThread>
Jan Möbius's avatar
Jan Möbius committed
37
#include <QMutex>
38
39
40

#include <OpenFlipper/common/GlobalDefines.hh>

41
42
43
44
45
46
47
48
/**
\class OpenFlipperThread

    Instanciate this class in order to provide a thread
    to a plugin. Unless you don't need a specialized
    run() or cancel() function, it won't be necessary to
    reimplement this class. Just connect the signals
    for the thread to work properly.
Mike Kremer's avatar
Mike Kremer committed
49
50
51
52
53
54
55
56
        
    Note: Updating GUI elements of the main window from
    within a thread which is not the main thread can
    cause unexpected crashes to OpenFlipper. This introduces
    some major limitations to the usage of threads
    in plugins. These include avoiding the creation
    of new objects and generally every function that
    produces logging messages.
57

Mike Kremer's avatar
Mike Kremer committed
58
    The following code fragment shows a simple example
59
60
61
    of how to use this class from within a plugin:

\code
Mike Kremer's avatar
Mike Kremer committed
62
void MyPlugin::launchThread() {
63

Mike Kremer's avatar
Mike Kremer committed
64
    OpenFlipperThread* myThread = new OpenFlipperThread("MyPluginsThread");
65

Mike Kremer's avatar
Mike Kremer committed
66
67
    // Connect the appropriate signals
    connect(myThread, SIGNAL(function()), this, SLOT(myPluginsThreadFunction()), Qt::DirectConnection);
68

Mike Kremer's avatar
Mike Kremer committed
69
70
71
    // Tell core about my thread
    // Note: The last parameter determines whether the thread should be blocking
    emit startJob( "MyPluginsThread", "Thread Description" , 0 , 100 , true);
72

Mike Kremer's avatar
Mike Kremer committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    // Start internal QThread
    myThread->start();

    // Start actual processing of job
    myThread->startProcessing();

}

void MyPlugin::myPluginsThreadFunction() {
   
   emit setJobState("MyPluginsThread", 0);
   
   // Do something...
   for(int i = 0; i < 100; ++i) {
   
       // Do something...
       
       // Update process' progress bar status
       emit setJobState("MyPluginsThread", i);
   }
   
   emit finishJob("MyPluginsThread");
}
96
97
98
\endcode
*/

Jan Möbius's avatar
Jan Möbius committed
99
100
class OpenFlipperJob;

101
102
103
104
105
106
107
108
class DLLEXPORT OpenFlipperThread : public QThread
{
  Q_OBJECT
  
  public:
    OpenFlipperThread( QString _jobId );
    ~OpenFlipperThread();
    
Jan Möbius's avatar
Jan Möbius committed
109
110
111
112
113
114
  //===========================================================================
  /** @name Advanced job processing
  * @{ */
  //===========================================================================
  public:
    
115
116
    /** \brief Main processing
    *
117
118
    * Either reimplement this function or connect the function()
    * signal.
119
120
121
    */
    virtual void run();
    
Jan Möbius's avatar
Jan Möbius committed
122
123
124
125
126
127
128
    /** \brief Cancel the job
    *
    * Reimplement this function to correctly terminate your job.
    * If your job is able to terminate correctly then you can reimplement this function
    * and stop your process.
    */
    virtual void cancel();   
129
130
131
132
133
134
135
136
137
    
  public slots:
    /** Call this slot with the correct job id to abort processing
    * This directly calls cancel()
    * This is only usefull when you derived from this class, as other jobs might not react on this slot
    */
    void slotCancel( QString _jobId);
    
  signals:
Jan Möbius's avatar
Jan Möbius committed
138
139
140
141
142
    /** \brief Tell core about job state
    *
    * Emit this signal to tell the core about your job status. You have to create a new run function for
    * this. In simple mode, this signal is not emitted at all!
    */
143
144
    void state( QString _jobId, int _state );
    
Jan Möbius's avatar
Jan Möbius committed
145
  /** @} */
146
    
Jan Möbius's avatar
Jan Möbius committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    
  //===========================================================================
  /** @name Simple job processing
  * @{ */
  //===========================================================================
    
  signals:
    /**  \brief job function
    *
    * If you do not specialize the OpenFlipperThread, The function connected to this 
    * signal will be used as the processing function for your thread. When you call
    * OpenFlipper::startProcessing(); \n
    *
    * If you want to provide more feedback (% of runtime) or cancel the job,
    * you have to derive from OpenFlipperThread and emit the right signals.
    * 
    * You have connect one of your slot using \n
164
165
    * connect( OpenFlipperThread ,SIGNAL(function()),YourPlugin,SLOT(YourSlot(),Qt::DirectConnection) );\n
    * The default implementation will call your slot inside the given thread and the core will still respond.
Jan Möbius's avatar
Jan Möbius committed
166
167
168
    * However you should only use this function if you Dont know how long your job takes. Or when
    * you just want to keep the core responding.\n
    * Otherwise you should reimplement the run(), and cancel() function of OpenFlipperThread and emit the state signal.
169
170
    */
    void function();
Jan Möbius's avatar
Jan Möbius committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    
    /** \brief job done
    *
    * This signal is emitted if your job has finished and should be used to tell the core that your job has finished.
    *
    */
    void finished( QString _jobId );
    
    
    
  public slots:
    /** \brief start processing
    *
    * This function will start the actuall processing of a job.
    */
    void startProcessing();
    
  /** @} */
    
    
  private slots:
    /** \brief job has finished
    * 
    * Called by the job wrapper, when the job is done.
    */
    void slotJobFinished();
  
  signals:
    /** \brief start processing of a function
    *
    * (INTERNAL!) \n
    * This signal is used to start the process execution through the wrapper
    */
    void startProcessingInternal();
    
  private: 
    /** \brief Internal job wrapper
    *
    * (INTERNAL!) \n
    * This wrapper is used to start slots inside the event queue of the current thread.
    * All slots defined in the QThread object live in the callers thread(The main thread).
    * All slots inside the wrapper live in the thread created by QThread. and therefore
    * do not lock the main event loop
    */
    OpenFlipperJob* job_;
    
    /** \brief Id of the current job
    *
    * This holds the id of the current job. Most functions only react if the correct id is passed
    * to the function.
    */
    QString jobId_;
    
    QMutex startup_;

    
};

229

Jan Möbius's avatar
Jan Möbius committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// ================================================================================
// Job wrapper
// ================================================================================

/** \brief Internal Job execution object
*
* This class is used to start a process within a thread. The thread object itself
* lives in the event queue of the object that crreated the thread. So every signal
* or slot within the qthread object will be dispatched in the core event loop and
* therefore lock the gui. 
* Objects created in the qthread will run in the threads event queue as well as their
* signals and slots. So qthread uses this object to actually run arbitrary threads 
* inside the threads event queue.
*/
class DLLEXPORT OpenFlipperJob : public QObject
{
  Q_OBJECT
  
  public:
    OpenFlipperJob() {}
Jan Möbius's avatar
Jan Möbius committed
250
    ~OpenFlipperJob();
Jan Möbius's avatar
Jan Möbius committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  
  signals:
    /** \brief connection to actual processing function
    *
    * This signal has to be connected via a Qt::DirectConnection to an
    * arbitrary slot. This slot will be executed in the local event queue.
    * If this object is created in a QThread, than the slot will be run 
    * inside this thread.
    */
    void process();
    
    /** \brief Job done
    *
    * This signal is emitted, when the job has finished
    */
    void finished();
    
  public slots:
    /** \brief slot to start processing
    *
    * If this slot is called, the slot connected to process() will be executed
    * in the current thread.
    */
    void startJobProcessing();
275
276
277
278
279
280
    
};



#endif //OPENFLIPPERTHREAD_HH