All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../../../common/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  : 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  m_dlgProgress->setWindowTitle(parent->windowTitle());
48 
49  connect(m_dlgProgress, SIGNAL(canceled()), this, SLOT(cancel()));
50 }
51 
53 {
54  delete m_dlgProgress;
55 }
56 
58 {
59  m_tasks.insert(std::map<int, te::common::TaskProgress*>::value_type(id, t));
60 
61  updateMessage(-1);
62 }
63 
65 {
66  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.find(taskId);
67 
68  if(it != m_tasks.end())
69  {
70  m_tasks.erase(it);
71  updateMessage(-1);
72  }
73 
74  if(m_tasks.empty())
75  {
76  m_totalSteps = 0;
77  m_currentStep = 0;
78  m_propStep = 0;
79 
80  QCoreApplication::postEvent(this, new ProgressResetEvent);
81 
82  QCoreApplication::processEvents();
83  }
84 }
85 
87 {
88  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.find(taskId);
89 
90  if(it != m_tasks.end())
91  {
92  // update total and current values
93  m_totalSteps -= it->second->getTotalSteps();
94  m_currentStep -= it->second->getCurrentStep();
95 
96  double aux = static_cast<double>(m_currentStep) / static_cast<double>(m_totalSteps);
97 
98  m_propStep = static_cast<int>(100.0 * aux);
99  }
100 }
101 
103 {
104  m_totalSteps += m_tasks[taskId]->getTotalSteps();
105 }
106 
108 {
109  m_dlgProgress->show();
110 
111  m_currentStep++;
112 
113  double aux = static_cast<double>(m_currentStep) / static_cast<double>(m_totalSteps);
114 
115  int val = static_cast<int>(100.0 * aux);
116 
117  if(val != m_propStep && val >= 0.0)
118  {
119  m_propStep = val;
120 
121  QCoreApplication::postEvent(this, new ProgressSetValueEvent(m_propStep));
122 
123  QCoreApplication::processEvents();
124  }
125 }
126 
128 {
129  if(m_tasks.size() == 1)
130  {
131  m_message = m_tasks.begin()->second->getMessage();
132  }
133  else
134  {
135  m_message = TE_TR("Multi Tasks");
136  }
137 
138  QCoreApplication::postEvent(this, new ProgressSetMessageEvent(m_message));
139 }
140 
142 {
143  if(e->type() == ProgressSetValueEvent::type())
144  {
145  m_dlgProgress->setValue(static_cast<ProgressSetValueEvent*>(e)->m_value);
146  }
147  else if(e->type() == ProgressSetMessageEvent::type())
148  {
149  m_dlgProgress->setLabelText(static_cast<ProgressSetMessageEvent*>(e)->m_value.c_str());
150  }
151  else if(e->type() == ProgressResetEvent::type())
152  {
153  m_dlgProgress->reset();
154  }
155 }
156 
158 {
159  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.begin();
160 
161  while(it != m_tasks.end())
162  {
163  it->second->cancel();
164  ++it;
165  }
166 }
void setTotalValues(int taskId)
Set task total steps.
void updateValue(int taskId)
Update the progress evaluation.
virtual ~ProgressViewerDialog()
Virtual destructor.
The ProgressSetValueEvent is a custom event used to set a new value into a progress bar...
ProgressViewerDialog(QWidget *parent)
Default constructor.
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.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
QProgressDialog * m_dlgProgress
GUI Objects used as progress bar dialog.
The ProgressResetEvent is a custom event used to reset a progress bar.
virtual void cancel()
Get the button clicked and cancel ALL tasks.
void cancelTask(int taskId)
Cancel a task.
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.
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.