OpenFlipperThread.cc 3.3 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
26
27
28
29
30
31
32
33
34
35
//=============================================================================
//
//                               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/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision: 5074 $
//   $Author: wilden $
//   $Date: 2009-02-25 18:27:57 +0100 (Mi, 25. Feb 2009) $
//
//=============================================================================


#include "OpenFlipperThread.hh"
#include <iostream>

Jan Möbius's avatar
Jan Möbius committed
36

37
OpenFlipperThread::OpenFlipperThread( QString _jobId ) :
Jan Möbius's avatar
Jan Möbius committed
38
  job_(0),
39
40
  jobId_(_jobId)
{
Jan Möbius's avatar
Jan Möbius committed
41
  startup_.lock();
42
43
44
45
46
47
48
49
50
  
}

OpenFlipperThread::~OpenFlipperThread() {
  
}

void OpenFlipperThread::run()
{
Jan Möbius's avatar
Jan Möbius committed
51
52
53
  if ( job_ == 0 ) {
    // Create a job wrapper to run a slot from within this thread
    job_ = new OpenFlipperJob();
54

Jan Möbius's avatar
Jan Möbius committed
55
56
57
58
59
60
61
62
63
64
    // Connect the slot which should run in this thread. This has to be a DirectConnection !
    // Otherwisse the slot will run inside its owner context which will be the main loop!!
    connect(job_, SIGNAL(process()),this,SIGNAL(function() ),Qt::DirectConnection);
    
    // Connect the jobs finished function
    connect(job_, SIGNAL(finished()),this,SLOT(slotJobFinished() ) );
    
    // connect the function to start the job
    connect(this,SIGNAL(startProcessingInternal()),job_,SLOT(startJobProcessing()),Qt::QueuedConnection);
  }
65
  
Jan Möbius's avatar
Jan Möbius committed
66
67
68
  std::cerr << "Start Loop" << std::endl;

  startup_.unlock();
69
  
Jan Möbius's avatar
Jan Möbius committed
70
71
  // Start event queue
  exec();
72
  
Jan Möbius's avatar
Jan Möbius committed
73
  std::cerr << "End Loop" << std::endl;
74
75
76
77
78
79
80
81
82
83
}

void OpenFlipperThread::cancel() {
  std::cerr << "Cancel not implemented" << std::endl;
}

void OpenFlipperThread::slotCancel( QString _jobId) {
 if ( _jobId == jobId_ )
  cancel(); 
}
Jan Möbius's avatar
Jan Möbius committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

void OpenFlipperThread::slotJobFinished( ) {
  emit finished( jobId_ );
}


void OpenFlipperThread::startProcessing() {
  std::cerr << "Thread emit startProcessing" << std::endl;
  
  // Wait for thread to come up with event loop
  startup_.lock();
  
  // Tell internal wrapper to start with the processing
  emit startProcessingInternal();
  
  startup_.unlock();
  std::cerr << "Thread emit startProcessing done" << std::endl;
}


void OpenFlipperJob::startJobProcessing() {
  std::cerr << "job emit process" << std::endl;
  // Actually start the process ( This function will block as it uses a direct connection )
  // But it only blocks the current thread.
  emit process(); 
  
  // Tell thread that the job is done.
  emit finished();
  
  std::cerr << "job emit process done" << std::endl;
}