ProgressViewerDialog.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/qt/widgets/progress/ProgressViewerDialog.cpp
22 
23  \brief A progress dialog.
24 */
25 
26 // Terralib
27 #include "../../../core/translator/Translator.h"
28 #include "ProgressResetEvent.h"
30 #include "ProgressSetValueEvent.h"
31 #include "ProgressViewerDialog.h"
32 
33 // Qt
34 #include <QtCore/QCoreApplication>
35 #include <QApplication>
36 
38  QString title)
39  : m_totalSteps(0), m_currentStep(0), m_propStep(0), m_message("")
40 {
41  m_dlgProgress = new QProgressDialog(parent);
42  m_dlgProgress->setWindowModality(Qt::WindowModal);
43  m_dlgProgress->setRange(0, 100);
44 
45  if(title.isEmpty())
46  m_dlgProgress->setWindowTitle(parent->windowTitle());
47  else
48  m_dlgProgress->setWindowTitle(title);
49 
50  connect(m_dlgProgress, SIGNAL(canceled()), this, SLOT(cancel()));
51 }
52 
54 {
55  delete m_dlgProgress;
56 }
57 
59 {
60  m_tasks.insert(std::map<int, te::common::TaskProgress*>::value_type(id, t));
61 
62  updateMessage(-1);
63 }
64 
66 {
67  cancelTask(taskId);
68 
69  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.find(taskId);
70 
71  if(it != m_tasks.end())
72  {
73  m_tasks.erase(it);
74  updateMessage(-1);
75  }
76 
77  if(m_tasks.empty())
78  {
79  m_totalSteps = 0;
80  m_currentStep = 0;
81  m_propStep = 0;
82 
83  m_dlgProgress->setRange(0, 100);
84 
85  QCoreApplication::postEvent(this, new ProgressResetEvent);
86 
87  QCoreApplication::processEvents();
88  }
89 }
90 
92 {
93  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.find(taskId);
94 
95  if(it != m_tasks.end())
96  {
97  // update total and current values
98  m_totalSteps -= it->second->getTotalSteps();
99  m_currentStep -= it->second->getCurrentStep();
100 
101  if (m_totalSteps > 0)
102  {
103  double aux = static_cast<double> (m_currentStep) / static_cast<double> (m_totalSteps);
104 
105  m_propStep = static_cast<int> (100.0 * aux);
106  }
107  else
108  {
109  m_propStep = 0;
110  }
111  }
112 }
113 
115 {
116  m_totalSteps += m_tasks[taskId]->getTotalSteps();
117 
118  if (m_totalSteps == 0)
119  m_dlgProgress->setRange(0, 0);
120  else
121  m_dlgProgress->setRange(0, 100);
122 }
123 
125 {
126  m_dlgProgress->setWindowTitle(title);
127 }
128 
130 {
131  std::map<int, te::common::TaskProgress*>::iterator it;
132 
133  double currentStep = 0;
134 
135  for (it = m_tasks.begin(); it != m_tasks.end(); ++it)
136  currentStep += it->second->getCurrentStep();
137 
138  it = m_tasks.find(taskId);
139 
140  double aux = static_cast<double>(currentStep) / static_cast<double>(m_totalSteps);
141 
142  int val = static_cast<int>(100.0 * aux);
143 
144  if(val != m_propStep && val >= 0.0)
145  {
146  m_propStep = val;
147 
148  QCoreApplication::postEvent(this, new ProgressSetValueEvent(m_propStep));
149 
150  QCoreApplication::processEvents();
151  }
152 }
153 
155 {
156  if(m_tasks.size() == 1)
157  {
158  m_message = m_tasks.begin()->second->getMessage();
159  }
160  else
161  {
162  m_message = TE_TR("Multi Tasks");
163  }
164 
165  QCoreApplication::postEvent(this, new ProgressSetMessageEvent(m_message));
166 
167  QCoreApplication::processEvents();
168 }
169 
171 {
172  if(e->type() == ProgressSetValueEvent::type())
173  {
174  m_dlgProgress->setValue(static_cast<ProgressSetValueEvent*>(e)->m_value);
175 
176  m_dlgProgress->show();
177  }
178  else if(e->type() == ProgressSetMessageEvent::type())
179  {
180  m_dlgProgress->setLabelText(static_cast<ProgressSetMessageEvent*>(e)->m_value.c_str());
181 
182  m_dlgProgress->show();
183  }
184  else if(e->type() == ProgressResetEvent::type())
185  {
186  m_dlgProgress->reset();
187  }
188 }
189 
191 {
192  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.begin();
193 
194  while(it != m_tasks.end())
195  {
196  it->second->cancel();
197  ++it;
198  }
199 }
void setTotalValues(int taskId)
Set task total steps.
void updateValue(int taskId)
Update the progress evaluation.
void setTitle(QString title)
Set title to dialog.
The ProgressSetValueEvent is a custom event used to set a new value into a progress bar...
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
void addTask(te::common::TaskProgress *t, int id)
Insert a new taks to progress viewer container.
A custom event used to set a new message into a progress bar.
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
QProgressDialog * m_dlgProgress
GUI Objects used as progress bar dialog.
The ProgressResetEvent is a custom event used to reset a progress bar.
std::map< int, te::common::TaskProgress * > m_tasks
Task container.
virtual void cancel()
Get the button clicked and cancel ALL tasks.
int m_totalSteps
Attribute used to define the total steps of all tasks.
void cancelTask(int taskId)
Cancel a task.
ProgressViewerDialog(QWidget *parent, QString title="")
Default constructor.
static QEvent::Type type()
Get the custom event type.
A progress dialog.
virtual void customEvent(QEvent *e)
Used to receive custom progress events.
static QEvent::Type type()
Get the custom event type.
void updateMessage(int taskId)
Update the progress message.
The ProgressResetEvent is a custom event used to reset a progress bar.
int m_propStep
Attribute used to define the proportional step (0-100).
std::string m_message
Attribute used to define dialog message.
void removeTask(int taskId)
Removes a task from progress viewer container.
The ProgressSetMessageEvent is a custom event used to set a new message into a progress bar...
The ProgressSetValueEvent is a custom event used to set a new value into a progress bar...
static QEvent::Type type()
Get the custom event type.