fw4spl
ProgressItkToFw.hxx
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #pragma once
8 
9 #ifndef __FWITKIO_HELPER_PROGRESSITKTOFW_HXX__
10 #define __FWITKIO_HELPER_PROGRESSITKTOFW_HXX__
11 
12 #include <fwCore/base.hpp>
13 
14 #include <itkCommand.h>
15 #include <itkEventObject.h>
16 #include <itkLightProcessObject.h>
17 #include <itkSmartPointer.h>
18 
19 #include <limits>
20 #include <sstream>
21 
22 namespace fwItkIO
23 {
24 
25 class LocalCommand : public itk::Command
26 {
27 public:
28 
29  LocalCommand()
30  {
31  }
32 
33  typedef LocalCommand Self;
34  typedef itk::SmartPointer<LocalCommand> Pointer;
35  itkNewMacro(Self);
36 
37  //------------------------------------------------------------------------------
38 
39  void Execute(itk::Object* caller, const itk::EventObject& event) override
40  {
41  itk::LightProcessObject* po = dynamic_cast<itk::LightProcessObject*>(caller);
42  if( !po )
43  {
44  return;
45  }
46  float percent = po->GetProgress();
47  OSLM_TRACE("LocalCommand::Execute '" << m_msg << "' " << percent );
48  m_adviser->notifyProgress( percent, m_msg );
49  }
50 
51  //------------------------------------------------------------------------------
52 
53  void Execute(const itk::Object* caller, const itk::EventObject& event) override
54  {
55  itk::LightProcessObject* po = dynamic_cast<itk::LightProcessObject*>( const_cast<itk::Object*>(caller));
56  Execute(po, event);
57  }
58 
59  std::string m_msg;
60  SPTR(::fwTools::ProgressAdviser) m_adviser;
61 };
62 
63 //------------------------------------------------------------------------------
64 
65 template<typename OBSERVEE >
67  std::string msg) :
68  m_observee( observee),
69  m_obsTag(std::numeric_limits<unsigned long>::max()),
70  m_initialized(false)
71 {
72  typename LocalCommand::Pointer itkCallBack;
73  itkCallBack = LocalCommand::New();
74  itkCallBack->m_msg = msg;
75  itkCallBack->m_adviser = observer;
76  m_obsTag = m_observee->AddObserver(itk::ProgressEvent(), itkCallBack );
77  m_initialized = true;
78 }
79 
80 //------------------------------------------------------------------------------
81 
82 template<typename OBSERVEE >
84 {
85  if( m_initialized)
86  {
87  m_observee->RemoveObserver(m_obsTag);
88  }
89 }
90 
91 }
92 
93 #endif // __FWITKIO_HELPER_PROGRESSITKTOFW_HXX__
#define SPTR(_cls_)
Subclasses Inherited from ProgressAdviser can notify progression This class is used as base class to ...
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
The namespace fwItkIO contains reader, writer and helper for itk image.