All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ProgressViewerDialog.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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 "../../../common/Translator.h"
28 #include "ProgressResetEvent.h"
30 #include "ProgressSetValueEvent.h"
31 #include "ProgressViewerDialog.h"
32 
33 // Qt
34 #include <QtCore/QCoreApplication>
35 #include <QtGui/QApplication>
36 
38  : AbstractProgressViewer(),
39  m_totalSteps(0),
40  m_currentStep(0),
41  m_propStep(0),
42  m_message("")
43 {
44  m_dlgProgress = new QProgressDialog(parent);
45  m_dlgProgress->setWindowModality(Qt::NonModal);
46  m_dlgProgress->setRange(0, 100);
47 
48  connect(m_dlgProgress, SIGNAL(canceled()), this, SLOT(cancel()));
49 }
50 
52 {
53  delete m_dlgProgress;
54 }
55 
57 {
58  m_tasks.insert(std::map<int, te::common::TaskProgress*>::value_type(id, t));
59 
60  updateMessage(-1);
61 }
62 
64 {
65  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.find(taskId);
66 
67  if(it != m_tasks.end())
68  {
69  m_tasks.erase(it);
70  updateMessage(-1);
71  }
72 
73  if(m_tasks.empty())
74  {
75  m_totalSteps = 0;
76  m_currentStep = 0;
77  m_propStep = 0;
78 
79  QCoreApplication::postEvent(this, new ProgressResetEvent);
80 
81  QCoreApplication::processEvents();
82  }
83 }
84 
86 {
87  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.find(taskId);
88 
89  if(it != m_tasks.end())
90  {
91  // update total and current values
92  m_totalSteps -= it->second->getTotalSteps();
93  m_currentStep -= it->second->getCurrentStep();
94 
95  double aux = static_cast<double>(m_currentStep) / static_cast<double>(m_totalSteps);
96 
97  m_propStep = static_cast<int>(100.0 * aux);
98  }
99 }
100 
102 {
103  m_totalSteps += m_tasks[taskId]->getTotalSteps();
104 }
105 
107 {
108  m_currentStep++;
109 
110  double aux = static_cast<double>(m_currentStep) / static_cast<double>(m_totalSteps);
111 
112  int val = static_cast<int>(100.0 * aux);
113 
114  if(val != m_propStep && val >= 0.0)
115  {
116  m_propStep = val;
117 
118  QCoreApplication::postEvent(this, new ProgressSetValueEvent(m_propStep));
119 
120  QCoreApplication::processEvents();
121  }
122 }
123 
125 {
126  if(m_tasks.size() == 1)
127  {
128  m_message = m_tasks.begin()->second->getMessage();
129  }
130  else
131  {
132  m_message = TR_QT_WIDGETS("Multi Tasks");
133  }
134 
135  QCoreApplication::postEvent(this, new ProgressSetMessageEvent(m_message));
136 }
137 
139 {
140  if(e->type() == ProgressSetValueEvent::type())
141  {
142  m_dlgProgress->setValue(static_cast<ProgressSetValueEvent*>(e)->m_value);
143  }
144  else if(e->type() == ProgressSetMessageEvent::type())
145  {
146  m_dlgProgress->setLabelText(static_cast<ProgressSetMessageEvent*>(e)->m_value.c_str());
147  }
148  else if(e->type() == ProgressResetEvent::type())
149  {
150  m_dlgProgress->reset();
151  }
152 }
153 
155 {
156  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.begin();
157 
158  while(it != m_tasks.end())
159  {
160  it->second->cancel();
161  ++it;
162  }
163 }
The ProgressSetMessageEvent is a custom event used to set a new message into a progress bar...
void updateValue(int taskId)
Update the progress evaluation.
QProgressDialog * m_dlgProgress
GUI Objects used as progress bar dialog.
static QEvent::Type type()
Get the custom event type.
The ProgressSetValueEvent is a custom event used to set a new value into a progress bar...
virtual ~ProgressViewerDialog()
Virtual destructor.
void addTask(te::common::TaskProgress *t, int id)
Insert a new taks to progress viewer container.
static QEvent::Type type()
Get the custom event type.
virtual void cancel()
Get the button clicked and cancel ALL tasks.
void updateMessage(int taskId)
Update the progress message.
The ProgressResetEvent is a custom event used to reset a progress bar.
virtual void customEvent(QEvent *e)
Used to receive custom progress events.
The ProgressResetEvent is a custom event used to reset a progress bar.
A progress dialog.
void cancelTask(int taskId)
Cancel a task.
#define TR_QT_WIDGETS(message)
It marks a string in order to get translated. This is a special mark used in the TerraLib Qt Widgets ...
Definition: Config.h:60
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.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
void removeTask(int taskId)
Removes a task from progress viewer container.
A custom event used to set a new message into a progress bar.
void setTotalValues(int taskId)
Set task total steps.
ProgressViewerDialog(QWidget *parent)
Default constructor.