All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TaskProgress.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 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(0)
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  {
81  m_timer->setTotalSteps(m_totalSteps);
82 
83  // reset timer clock
84  m_timer->start();
85  }
86 
87  te::common::ProgressManager::getInstance().setTotalValues(m_id);
88 }
89 
91 {
92  return m_currentPropStep;
93 }
94 
96 {
97  return m_currentStep;
98 }
99 
101 {
102  if(m_isActive)
103  {
104  m_currentStep = value;
105 
106  double aux = static_cast<double>(m_currentStep) / static_cast<double>(m_totalSteps);
107 
108  int val = static_cast<int>(100.0 * aux);
109 
110  if(val > m_currentPropStep)
111  {
112  m_currentPropStep = val;
113  m_hasToUpdate = true;
114  }
115  else
116  {
117  m_hasToUpdate = false;
118  }
119 
120  if(m_timer)
121  {
122  m_timer->tick();
123 
124  setMessage(m_timer->getMessage());
125  }
126 
127  // inform the progress manager singleton that the current value has changed
128  te::common::ProgressManager::getInstance().updateValue(m_id);
129  }
130 }
131 
133 {
134  // increment by one current value
135  int val = getCurrentStep();
136 
137  setCurrentStep(++val);
138 }
139 
140 const std::string& te::common::TaskProgress::getMessage() const
141 {
142  return m_message;
143 }
144 
145 void te::common::TaskProgress::setMessage(const std::string& message)
146 {
147  m_message = message;
148 
149  // inform the progress manager singleton that the message has changed
150  te::common::ProgressManager::getInstance().updateMessage(m_id);
151 }
152 
154 {
155  return m_isActive;
156 }
157 
159 {
160  m_isActive = false;
161 
162  // inform the progress manager singleton that the current task was canceled
163  te::common::ProgressManager::getInstance().cancelTask(m_id);
164 }
165 
167 {
168  m_isMultiThread = flag;
169 
170  // code restriction, must be fixed
171  if(m_isMultiThread)
172  {
173  delete m_timer;
174  m_timer = 0;
175  m_useTimer = false;
176  }
177 }
178 
180 {
181  m_useTimer = flag;
182 
183  if(m_timer == 0 && m_useTimer)
184  {
185  // code restriction, must be fixed
186  if(!m_isMultiThread)
187  {
188  m_timer = new ProgressTimer(getTotalSteps());
189  m_timer->start();
190  }
191  }
192 }
193 
195 {
196  return m_hasToUpdate;
197 }
bool isActive() const
Verify if the task is active.
This class can be used to inform the progress of a task.
void useMultiThread(bool flag)
Used to define if task is running in thread mode.
int getCurrentStep() const
Get the task current step.
void setTotalSteps(int value)
Set the task total stepes.
A singleton class used to manage tasks progresses and their viewers.
int getProportionalValue() const
Get the proportional value (value between 0 and 100).
int getTotalSteps() const
Get the task total stepes.
void cancel()
Cancel task, set the task active FALSE.
void setCurrentStep(int value)
Set the task current step.
The ProgressTimer is a utility class that can be used to calculate the estimated time to finish a tas...
Definition: ProgressTimer.h:47
unsigned int getType() const
Get the task type.
int getId() const
Get the task identification.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
const std::string & getMessage() const
Get the task message.
The ProgressTimer is a utility class that can be used to calculate the estimated time to finish a tas...
bool hasToUpdate() const
This function is used when proportional value has changed.
void useTimer(bool flag)
Used to define if task use progress timer information.
void setMessage(const std::string &message)
Set the task message.
static T & getInstance()
It returns a reference to the singleton instance.
Definition: Singleton.h:120
~TaskProgress()
Destructor.
int m_id
Task identification.
Definition: TaskProgress.h:177
TaskProgress(const std::string &message="", unsigned int type=UNDEFINED, int totalSteps=0)
Default constructor.