]> Creatis software - clitk.git/blob - vv/vvThreadedFilter.cxx
put a filter in a thread. Can be canceled.
[clitk.git] / vv / vvThreadedFilter.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ======================================================================-====*/
18
19 // vv
20 #include "vvThreadedFilter.h"
21 #include "vvProgressDialog.h"
22
23 // Qt
24 #include <QMessageBox>
25
26 //------------------------------------------------------------------------------
27 vvThreadedFilter::vvThreadedFilter():
28   QThread()
29 {
30   m_Filter = NULL;
31 }
32 //------------------------------------------------------------------------------
33
34
35 //------------------------------------------------------------------------------
36 vvThreadedFilter::~vvThreadedFilter()
37 {
38 }
39 //------------------------------------------------------------------------------
40
41
42 //------------------------------------------------------------------------------
43 void vvThreadedFilter::SetFilter(clitk::ImageToImageGenericFilterBase * f)
44 {
45   m_Filter = f;
46 }
47 //------------------------------------------------------------------------------
48
49
50 //------------------------------------------------------------------------------
51 void vvThreadedFilter::Update()
52 {
53   assert(m_Filter != NULL);
54
55   // Show a progress bar while computing
56   vvProgressDialog progress("Computing ...",100);
57   progress.SetCancelButtonEnabled(true);
58   connect(&progress, SIGNAL(rejected()), this, SLOT(reject()));
59   this->start();
60   this->setTerminationEnabled(true);
61   std::string temp;
62   while (this->isRunning()) {
63     m_FilterBase = m_Filter->GetFilterBase(); // get filterbase is only set after Update
64     if (m_FilterBase != NULL) {
65       //      m_FilterBase->StopOnErrorOff(); // filter can be interrupted
66       progress.SetProgress(m_FilterBase->GetCurrentStepNumber(), 
67                            m_FilterBase->GetNumberOfSteps());
68       if (temp != m_FilterBase->GetCurrentStepName()) {
69         progress.AddToText(m_FilterBase->GetCurrentStepName());
70       }
71       temp = m_FilterBase->GetCurrentStepName();
72     }
73     this->wait(200); // in milisecond
74     qApp->processEvents();
75   }
76 }
77 //------------------------------------------------------------------------------
78
79
80 //------------------------------------------------------------------------------
81 void vvThreadedFilter::run()
82 {
83   assert(m_Filter != NULL);
84   m_Filter->Update();
85 }
86 //------------------------------------------------------------------------------
87
88
89 //------------------------------------------------------------------------------
90 void vvThreadedFilter::reject()
91 {
92   // First, say the filter it must stop
93   if (m_FilterBase != NULL) {
94     m_FilterBase->SetMustStop(true);
95   }
96   // Indicate to the user it will stop
97   QApplication::restoreOverrideCursor();
98   QMessageBox::information(new QWidget, tr("Error"), m_FilterBase->GetLastError().c_str());  
99   // Quit the thread (is it needed ?)
100   this->quit();
101   emit ThreadInterrupted();
102 }
103 //------------------------------------------------------------------------------
104
105
106
107