TaskProgress.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/progress/TaskProgress.cpp
22 
23  \brief This class is the implementation of a progress evalution of a task.
24  */
25 
26 // TerraLib
27 #include "ProgressManager.h"
28 #include "ProgressTimer.h"
29 #include "TaskProgress.h"
30 
31 te::common::TaskProgress::TaskProgress(const std::string& message, unsigned int type, int totalSteps)
32  : m_id(-1),
33  m_type(type),
34  m_totalSteps(totalSteps),
35  m_currentStep(0),
36  m_currentPropStep(0),
37  m_message(message),
38  m_hasToUpdate(false),
39  m_isActive(true),
40  m_isMultiThread(false),
41  m_useTimer(false),
42  m_timer(nullptr)
43 {
44  //get task id from progress manager singleton
46 
47  setTotalSteps(totalSteps);
48 }
49 
51 {
53 
54  delete m_timer;
55 }
56 
58 {
59  return m_id;
60 }
61 
63 {
64  return m_type;
65 }
66 
68 {
69  return m_totalSteps;
70 }
71 
73 {
74  if(value < 0)
75  return;
76 
77  m_totalSteps = value;
78 
79  if(m_timer)
80  {
82 
83  // reset timer clock
84  m_timer->start();
85  }
86 
88 }
89 
91 {
92  return m_currentPropStep;
93 }
94 
96 {
97  return m_currentStep;
98 }
99 
101 {
102  if(m_isActive)
103  {
104  m_hasToUpdate = true;
105 
106  if(m_totalSteps > 0)
107  {
108  m_currentStep = value;
109  int val = m_currentStep;
110 
111  double aux = static_cast<double>(m_currentStep) / static_cast<double>(m_totalSteps);
112  val = static_cast<int>(100.0 * aux);
113 
114  if(val > m_currentPropStep)
115  m_currentPropStep = val;
116  else
117  {
118  m_hasToUpdate = false;
119  }
120  }
121 
122  if(m_timer)
123  m_timer->tick();
124 
125  // inform the progress manager singleton that the current value has changed
126  if (m_hasToUpdate)
127  {
129 
130  if (m_timer)
132  }
133  }
134 }
135 
137 {
138  // increment by one current value
139  int val = getCurrentStep();
140 
141  setCurrentStep(++val);
142 }
143 
144 const std::string& te::common::TaskProgress::getMessage() const
145 {
146  return m_message;
147 }
148 
149 void te::common::TaskProgress::setMessage(const std::string& message)
150 {
151  m_message = message;
152 
153  // inform the progress manager singleton that the message has changed
155 }
156 
158 {
159  return m_isActive;
160 }
161 
163 {
164  m_isActive = false;
165 
166  // inform the progress manager singleton that the current task was canceled
168 
169  m_currentStep = 0;
170  m_totalSteps = 0;
171  m_currentPropStep = 0;
172 }
173 
175 {
176  m_isMultiThread = flag;
177 
178  // code restriction, must be fixed
179  if(m_isMultiThread)
180  {
181  delete m_timer;
182  m_timer = nullptr;
183  m_useTimer = false;
184  }
185 }
186 
188 {
189  m_useTimer = flag;
190 
191  if(m_timer == nullptr && m_useTimer)
192  {
193  // code restriction, must be fixed
194  if(!m_isMultiThread)
195  {
197  m_timer->start();
198  }
199  }
200 }
201 
203 {
204  return m_hasToUpdate;
205 }
void useMultiThread(bool flag)
Used to define if task is running in thread mode.
int m_totalSteps
Task total steps.
Definition: TaskProgress.h:179
void setMessage(const std::string &message)
Set the task message.
This class can be used to inform the progress of a task.
int getProportionalValue() const
Get the proportional value (value between 0 and 100).
A singleton class used to manage tasks progresses and their viewers.
void useTimer(bool flag)
Used to define if task use progress timer information.
int addTask(TaskProgress *tp)
Used in TaskProgress constructor, register this task generating a task id.
bool m_isActive
Flag used to indicate the task status.
Definition: TaskProgress.h:184
bool m_isMultiThread
Flag used to indicate the thread mode.
Definition: TaskProgress.h:185
void tick()
Define a new step process evolution.
void cancelTask(int taskId)
Inform all viewers that a task was canceled.
const std::string & getMessage() const
Get the task message.
~TaskProgress()
Destructor.
bool isActive() const
Verify if the task is active.
void setTotalSteps(int value)
Set the task total stepes.
int getId() const
Get the task identification.
TaskProgress(const std::string &message="", unsigned int type=UNDEFINED, int totalSteps=0)
Default constructor.
static ProgressManager & getInstance()
It returns a reference to the singleton instance.
std::string m_message
Task message.
Definition: TaskProgress.h:182
void setTotalValues(int taskId)
Inform all viewers that a task set the total values.
bool m_hasToUpdate
Flag used to indicate the update status.
Definition: TaskProgress.h:183
void cancel()
Cancel task, set the task active FALSE.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
int m_currentStep
Task current step.
Definition: TaskProgress.h:180
int getCurrentStep() const
Get the task current step.
void removeTask(int taskId)
Used in TaskProgress destructor, remove task from singleton.
unsigned int m_type
Task type.
Definition: TaskProgress.h:178
void start()
Start the internal timer.
bool hasToUpdate() const
This function is used when proportional value has changed.
The ProgressTimer is a utility class that can be used to calculate the estimated time to finish a tas...
Definition: ProgressTimer.h:47
ProgressTimer * m_timer
Progress timer instance.
Definition: TaskProgress.h:187
bool m_useTimer
Flag used to indicate the timer status.
Definition: TaskProgress.h:186
unsigned int getType() const
Get the task type.
int m_currentPropStep
Current proportinal step.
Definition: TaskProgress.h:181
void setCurrentStep(int value)
Set the task current step.
std::string getMessage()
Get the information about the evolution of the process.
void updateMessage(int taskId)
Inform all viewers that a task set the message.
int m_id
Task identification.
Definition: TaskProgress.h:177
void setTotalSteps(int totalSteps)
Set the total steps.
The ProgressTimer is a utility class that can be used to calculate the estimated time to finish a tas...
void updateValue(int taskId)
Inform all viewers that a task set the current step.
int getTotalSteps() const
Get the task total stepes.