All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ProgressManager.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/ProgressManager.cpp
22 
23  \brief A singleton class used to manage tasks progresses and their viewers.
24 */
25 
26 // TerraLib
27 #include "AbstractProgressViewer.h"
28 #include "ProgressManager.h"
29 #include "TaskProgress.h"
30 
32 {
33  int id = generateViewerId();
34 
35  m_viewers.insert(std::map<int, AbstractProgressViewer*>::value_type(id, apv));
36 
37  // add current tasks into the new viewer
38  std::map<int, TaskProgress*>::iterator it = m_tasks.begin();
39 
40  while(it != m_tasks.end())
41  {
42  apv->addTask(it->second, it->first);
43  ++it;
44  }
45 
46  return id;
47 }
48 
50 {
51  std::map<int, AbstractProgressViewer*>::iterator it = m_viewers.find(viewerId);
52 
53  if(it != m_viewers.end())
54  {
55  m_viewers.erase(it);
56  }
57 }
58 
60 {
61  LockWrite l(this);
62 
63  int id = generateTaskId();
64 
65  m_tasks.insert(std::map<int, TaskProgress*>::value_type(id, tp));
66 
67  // add task in viewers
68  std::map<int, AbstractProgressViewer*>::iterator itV = m_viewers.begin();
69 
70  while(itV != m_viewers.end())
71  {
72  itV->second->addTask(tp, id);
73 
74  ++itV;
75  }
76 
77  return id;
78 }
79 
81 {
82  LockWrite l(this);
83 
84  std::map<int, TaskProgress*>::iterator it = m_tasks.find(taskId);
85 
86  if(it != m_tasks.end())
87  {
88  // remove task from viewers
89  std::map<int, AbstractProgressViewer*>::iterator itV = m_viewers.begin();
90 
91  while(itV != m_viewers.end())
92  {
93  itV->second->removeTask(taskId);
94 
95  ++itV;
96  }
97 
98  m_tasks.erase(it);
99  }
100 }
101 
103 {
104  std::map<int, TaskProgress*>::iterator it = m_tasks.find(taskId);
105 
106  if(it != m_tasks.end())
107  {
108  // remove task from viewers
109  std::map<int, AbstractProgressViewer*>::iterator itV = m_viewers.begin();
110 
111  while(itV != m_viewers.end())
112  {
113  itV->second->cancelTask(taskId);
114 
115  ++itV;
116  }
117  }
118 }
119 
121 {
122  LockWrite l(this);
123 
124  std::vector<TaskProgress*> cancelled;
125  std::map<int, TaskProgress*>::iterator it = m_tasks.begin();
126 
127  while(it != m_tasks.end())
128  {
129  if(it->second->getType() == type)
130  cancelled.push_back(it->second);
131 
132  ++it;
133  }
134 
135  for(std::size_t i = 0; i < cancelled.size(); ++i)
136  cancelled[i]->cancel();
137 }
138 
140 {
141  std::map<int, AbstractProgressViewer*>::iterator itV = m_viewers.begin();
142 
143  while(itV != m_viewers.end())
144  {
145  itV->second->setTotalValues(taskId);
146 
147  ++itV;
148  }
149 }
150 
152 {
153  std::map<int, AbstractProgressViewer*>::iterator itV = m_viewers.begin();
154 
155  while(itV != m_viewers.end())
156  {
157  itV->second->updateValue(taskId);
158 
159  ++itV;
160  }
161 }
162 
164 {
165  std::map<int, AbstractProgressViewer*>::iterator itV = m_viewers.begin();
166 
167  while(itV != m_viewers.end())
168  {
169  itV->second->updateMessage(taskId);
170 
171  ++itV;
172  }
173 }
174 
176 {
177  m_viewers.clear();
178  m_tasks.clear();
179 }
180 
182  : m_taskCounter(0),
183  m_viewerCounter(0)
184 {
185 }
186 
188 {
189  clearAll();
190 }
191 
193 {
194  return m_viewerCounter++;
195 }
196 
198 {
199  return m_taskCounter++;
200 }
201 
This class can be used to inform the progress of a task.
A singleton class used to manage tasks progresses and their viewers.
int addTask(TaskProgress *tp)
Used in TaskProgress constructor, register this task generating a task id.
std::map< int, TaskProgress * > m_tasks
Container with tasks.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
virtual void addTask(TaskProgress *t, int id)=0
Insert a new task in the progress viewer.
void cancelTask(int taskId)
Inform all viewers that a task was canceled.
int generateTaskId()
Used to generate a new task id (use internal counter).
A class that defines the interface of an abstract progress viewer.
void removeViewer(int viewerId)
Dettach a progress viewer.
A class that defines the interface of an abstract progress viewer.
void setTotalValues(int taskId)
Inform all viewers that a task set the total values.
void cancelTasks(unsigned int type)
Cancels the task with the given task type and inform all viewers that a task was canceled.
void removeTask(int taskId)
Used in TaskProgress destructor, remove task from singleton.
std::map< int, AbstractProgressViewer * > m_viewers
Container with viewers.
int addViewer(AbstractProgressViewer *apv)
Attach a progress viewer.
ProgressManager()
Default constructor.
void updateMessage(int taskId)
Inform all viewers that a task set the message.
void clearAll()
Removes references for Viewers and tasks.
int generateViewerId()
Used to generate a new viewer id (use internal counter).
void updateValue(int taskId)
Inform all viewers that a task set the current step.