ConsoleProgressViewer.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/ConsoleProgressViewer.cpp
22 
23  \brief A progress viewer implementation for the console.
24 */
25 
26 // TerraLib
27 #include "../../core/translator/Translator.h"
28 #include "ConsoleProgressViewer.h"
29 #include "TaskProgress.h"
30 
31 // STL
32 #include <iostream>
33 
35  : m_totalSteps(0), m_currentStep(0), m_propStep(0)
36 {
37 }
38 
40 
42 {
43  m_tasks.insert(std::map<int, TaskProgress*>::value_type(id, t));
44 
45  updateMessage(-1);
46 }
47 
49 {
50  cancelTask(taskId);
51 
52  std::map<int, TaskProgress*>::iterator it = m_tasks.find(taskId);
53 
54  if(it != m_tasks.end())
55  {
56  m_tasks.erase(it);
57  updateMessage(-1);
58  }
59 
60  if(m_tasks.empty())
61  {
62  m_totalSteps = 0;
63  m_currentStep = 0;
64  m_propStep = 0;
65  }
66 }
67 
69 {
70  std::map<int, TaskProgress*>::iterator it = m_tasks.find(taskId);
71 
72  if(it != m_tasks.end())
73  {
74  //update total and current values
75  m_totalSteps -= it->second->getTotalSteps();
76  m_currentStep -= it->second->getCurrentStep();
77 
78  if (m_totalSteps > 0)
79  {
80  double aux = static_cast<double> (m_currentStep) / static_cast<double> (m_totalSteps);
81 
82  m_propStep = static_cast<int> (100.0 * aux);
83  }
84  else
85  {
86  m_propStep = 0;
87  }
88  }
89 }
90 
92 {
93  m_totalSteps += m_tasks[taskId]->getTotalSteps();
94 }
95 
97 {
98  double currentStep = 0;
99 
100  std::map<int, te::common::TaskProgress*>::iterator it;
101 
102  for (it = m_tasks.begin(); it != m_tasks.end(); ++it)
103  currentStep += it->second->getCurrentStep();
104 
105  double aux = static_cast<double>(currentStep) / static_cast<double>(m_totalSteps);
106 
107  int val = static_cast<int>(100.0 * aux);
108 
109  if((val != m_propStep) && (val >= 0))
110  {
111  m_propStep = val;
112 
113  std::cout << m_message << TE_TR(" - Percent: ") << m_propStep << "%" <<std::endl;
114  }
115 }
116 
118 {
119  if(m_tasks.size() == 1)
120  {
121  m_message = m_tasks.begin()->second->getMessage();
122  }
123  else
124  {
125  m_message = TE_TR("Multi Tasks");
126  }
127 }
128 
This class can be used to inform the progress of a task.
A progress viewer implementation for the console.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
int m_currentStep
Attribute used to define the current steps of all task.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
void updateMessage(int taskId)
Update the progress message.
ConsoleProgressViewer()
Default constructor.
std::string m_message
Attribute used to define the progress message.
void addTask(TaskProgress *t, int id)
Insert a new task in the progress viewer.
void removeTask(int taskId)
Removes a task from progress viewer container.
void cancelTask(int taskId)
Cancel a task.
void setTotalValues(int taskId)
Set task total steps.
std::map< int, TaskProgress * > m_tasks
Task container.
int m_propStep
Attribute used to define the proportional step (0-100).
void updateValue(int taskId)
Update the progress evaluation.
int m_totalSteps
Attribute used to define the total steps of all tasks.
~ConsoleProgressViewer()
Virtual destructor.