/
ProgressAccumulator.h
179 lines (151 loc) · 5.53 KB
/
ProgressAccumulator.h
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
///////////////////////////////////////////////////////////////////////////////////////////////////
// Project: Espina Volume Editor
// Author: Félix de las Pozas Alvarez
//
// File: ProgressAccumulator.h
// Purpose: total progress reporter for a filter or multi-filter process
// Notes: some parts "borrowed" (that is, blatantly stolen) from qtITK example by Luis Ibañez and
// AllPurposeProgressAccumulator from itksnap. thanks.
///////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _PROGRESS_ACCUMULATOR_H_
#define _PROGRESS_ACCUMULATOR_H_
// itk includes
#include <itkObject.h>
#include <itkObjectFactory.h>
#include <itkCommand.h>
#include <itkProcessObject.h>
// vtk includes
#include <vtkSmartPointer.h>
#include <vtkCallbackCommand.h>
#include <vtkObject.h>
// qt includes
#include <QApplication>
#include <QProgressBar>
#include <QLabel>
#include <QObject>
// c++ includes
#include <map>
#include <string>
///////////////////////////////////////////////////////////////////////////////////////////////////
// ProgressAccumulator class
//
class ProgressAccumulator
{
public:
enum class callerType: char { VTK, ITK, None };
struct observerTags
{
callerType type;
unsigned long tagStart;
unsigned long tagEnd;
unsigned long tagProgress;
std::string text;
double weight;
observerTags()
: type{callerType::None}, tagStart{0}, tagEnd{0}, tagProgress{0}, text{}, weight{0} {}
};
// command class invoked
using ITKCommandType = itk::MemberCommand<ProgressAccumulator>;
using VTKCommandType = vtkCallbackCommand;
/** \brief ProgressAccumulator class constructor.
*
*/
ProgressAccumulator();
/** \brief ProgressAccumulator class destructor.
*
*/
~ProgressAccumulator();
/** \brief Sets the progress bar that this class will use.
* \param[in] bar progress bar used by accumulator.
* \param[in] label label of the bar.
*
*/
void SetProgressBar(QProgressBar *bar);
/** \brief Observe a ITK process (add as a observer of that process).
* \param[in] filter filter to observe.
* \param[in] text text of the filter.
* \param[in] weight weight of the filter in the process.
*
*/
void Observe(itk::Object *filter, const std::string &text, const double weight);
/** \brief Observe a VTK process (add as a observer of that process).
* \param[in] filter filter to observe.
* \param[in] text text of the filter.
* \param[in] weight weight of the filter in the process.
*
*/
void Observe(vtkObject *filter, const std::string &text, const double weight);
/** \brief Ignore a ITK process (remove from the list of observers of that object.
* \param[in] filter filter to ignore progress.
*
*/
void Ignore(itk::Object *filter);
/** \brief Ignore a VTK process (remove from the list of observers of that object.
* \param[in] filter filter to ignore progress.
*
*/
void Ignore(vtkObject *filter);
/** \brief Ignore all registered processes.
*
*/
void IgnoreAll();
/** \brief Set accumulated progress to 0.
*
*/
void Reset();
/** \brief Set the name and progress value manually.
* \param[in] text text of the filter.
* \param[in] value progress value.
* \param[in] calledFromThread true if this is being called from a thread and false otherwise.
*
*/
void ManualSet(const std::string &text, const int value = 0, bool calledFromThread = false);
/** \brief Manually update the progress value.
* \param[in] value progress value.
* \param[in] calledFromThread true if this is being called from a thread and false otherwise.
*
*/
void ManualUpdate(const int value, bool calledFromThread = false);
/** \brief Manually reset progress value.
* \param[in] calledFromThread true if this is being called from a thread and false otherwise.
*
*/
void ManualReset(bool calledFromThread = false);
private:
/** \brief Helper method to manage a ITK process event.
* \param[in] caller pointer to caller object.
* \param[in] event event object.
*
*/
void ITKProcessEvent(itk::Object *caller, const itk::EventObject &event);
/** \brief Helper method to manage a VTK process event.
* \param[in] caller pointer to caller object.
* \param[in] eid event id.
* \param[in] clientdata caller additional data.
* \param[in] calldata event additional data.
*
*/
static void VTKProcessEvent(vtkObject *caller, unsigned long eid, void *clientdata, void *calldata);
/** \brief Progress event callback.
* \param[in] caller pointer to caller object.
* \param[in] value progress value.
*
*/
void CallbackProgress(void *caller, const double value);
/** \brief Progress start callback.
* \param[in] caller pointer to caller object.
*
*/
void CallbackStart(void *caller);
/** \brief Progress end callback.
* \param[in] caller pointer to caller object.
*
*/
void CallbackEnd(void *caller);
itk::SmartPointer<ITKCommandType> m_ITKCommand; /** itk command. */
vtkSmartPointer<VTKCommandType> m_VTKCommand; /** vtk command. */
std::map<void *, observerTags*> m_observed; /** observed processes. */
double m_progress; /** accumulated progress. */
QProgressBar *m_progressBar; /** QProgress bar showing the progress. */
};
#endif // _PROGRESS_ACCUMULATOR_H_