fw4spl
SJobBar.cpp
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 #include "gui/editor/SJobBar.hpp"
8 
9 #include <fwCom/Signal.hxx>
10 #include <fwCom/Slots.hpp>
11 #include <fwCom/Slots.hxx>
12 
13 #include <fwGui/dialog/ProgressDialog.hpp>
14 
15 #include <fwServices/macros.hpp>
16 
17 #include <fwThread/Worker.hxx>
18 
19 namespace gui
20 {
21 namespace editor
22 {
23 
24 fwServicesRegisterMacro( ::fwGui::editor::IDialogEditor, ::gui::editor::SJobBar );
25 
26 static const ::fwCom::Slots::SlotKeyType SHOW_JOB_SLOT = "showJob";
27 static const ::fwCom::Signals::SignalKeyType STARTED_SIGNAL = "started";
28 static const ::fwCom::Signals::SignalKeyType ENDED_SIGNAL = "ended";
29 
30 //-----------------------------------------------------------------------------
31 
32 SJobBar::SJobBar() noexcept
33 {
34  newSlot( SHOW_JOB_SLOT, &SJobBar::showJob, this );
35 
36  m_sigStarted = newSignal< StartedSignalType >( STARTED_SIGNAL );
37  m_sigEnded = newSignal< EndedSignalType >( ENDED_SIGNAL );
38 }
39 
40 //-----------------------------------------------------------------------------
41 
43 {
44 }
45 
46 //-----------------------------------------------------------------------------
47 
49 {
50 }
51 
52 //-----------------------------------------------------------------------------
53 
55 {
56 }
57 
58 //-----------------------------------------------------------------------------
59 
60 void SJobBar::info(std::ostream& _sstream )
61 {
62  _sstream << "Starter editor" << std::endl;
63 }
64 
65 //-----------------------------------------------------------------------------
66 
68 {
69 }
70 
71 //-----------------------------------------------------------------------------
72 
74 {
75 }
76 
77 //-----------------------------------------------------------------------------
78 
80 {
81  ::fwGui::dialog::ProgressDialog::sptr progressDialog = ::fwGui::dialog::ProgressDialog::New();
82  progressDialog->setTitle(iJob->getName());
83 
84  if(!iJob->isCancelable())
85  {
86  progressDialog->hideCancelButton();
87  }
88 
89  iJob->addDoneWorkHook( [ = ](::fwJobs::IJob& job, std::uint64_t oldDoneWork)
90  {
91  std::string msg = (job.getLogs().empty()) ? "" : job.getLogs().back();
92  (*progressDialog)( float(job.getDoneWorkUnits())/job.getTotalWorkUnits(), msg );
93  });
94 
95  iJob->addStateHook( [ = ](::fwJobs::IJob::State state)
96  {
97  if(state == ::fwJobs::IJob::CANCELED || state == ::fwJobs::IJob::FINISHED )
98  {
99  m_sigEnded->emit();
100  m_associatedWorker->postTask< void >( [ = ]
101  {
102  m_progressDialogs.erase(progressDialog);
103  });
104  }
105  else if( state == ::fwJobs::IJob::RUNNING)
106  {
107  m_sigStarted->emit();
108  }
109  });
110 
111  ::fwJobs::IJob::wptr wIJob = iJob;
112  progressDialog->setCancelCallback( [ = ]
113  {
114  ::fwJobs::IJob::sptr job = wIJob.lock();
115  if(job)
116  {
117  job->cancel();
118  }
119  });
120 
121  m_progressDialogs.insert(progressDialog);
122 }
123 
124 //-----------------------------------------------------------------------------
125 
126 } // namespace editor
127 } // namespace gui
This class is an interface for class managing job.
Definition: IJob.hpp:28
GUI_API void updating() override
This method emit a signal.
Definition: SJobBar.cpp:67
virtual FWJOBS_API SharedFuture cancel()
Cancel the current job and call all available cancel callbacks.
Definition: IJob.cpp:87
std::weak_ptr< ::fwJobs::IJob > wptr
Cancel request callback type.
Definition: IJob.hpp:55
The namespace gui contains the basic services to build the application IHM.
virtual GUI_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
Definition: SJobBar.cpp:54
GUI_API void configuring() override
This method is used to configure the service.
Definition: SJobBar.cpp:73
GUI_API SJobBar() noexcept
Constructor. Do nothing.
Definition: SJobBar.cpp:32
std::shared_ptr< ::fwJobs::IJob > sptr
Cancel request callback type.
Definition: IJob.hpp:54
virtual GUI_API ~SJobBar() noexcept
Destructor. Do nothing.
Definition: SJobBar.cpp:42
FWJOBS_API std::uint64_t getDoneWorkUnits() const
Getter on the number of done work units.
Definition: IJob.cpp:71
State
Job&#39;s states.
Definition: IJob.hpp:42
virtual GUI_API void info(std::ostream &_sstream) override
This method gives information about the class. Do nothing.
Definition: SJobBar.cpp:60
Service displaying a progress bar.
Definition: SJobBar.hpp:40
FWJOBS_API Logs getLogs() const
Getter on the log container.
Definition: IJob.cpp:452
std::shared_ptr< ::fwThread::Worker > m_associatedWorker
Associated worker.
Definition: IService.hpp:699
Defines the service interface managing the editor service which create their own container.
virtual GUI_API void starting() override
Initialize the service activity.
Definition: SJobBar.cpp:48
FWJOBS_API std::uint64_t getTotalWorkUnits() const
Getter on the total number of work units.
Definition: IJob.cpp:79
virtual GUI_API void showJob(::fwJobs::IJob::sptr iJob)
showJob slot&#39;s method
Definition: SJobBar.cpp:79