ProgressViewerBar.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/ProgressViewerBar.cpp
22 
23  \brief A class that defines the interface of a qt bar progress viewer.
24 */
25 
26 // Terralib
27 #include "../../../core/translator/Translator.h"
29 #include "ProgressSetValueEvent.h"
30 #include "ProgressResetEvent.h"
31 #include "ProgressViewerBar.h"
32 
33 // Qt
34 #include <QtCore/QCoreApplication>
35 #include <QApplication>
36 
38  : QWidget(parent),
39 
40  m_totalSteps(0),
41  m_currentStep(0),
42  m_propStep(0)
43 {
44  m_progressBar = new QProgressBar(this);
45  m_progressBar->setRange(0, 100);
46  m_progressBar->setTextVisible(false);
47  m_progressBar->setFixedHeight(16);
48 
49  m_button = new QPushButton(this);
50  m_button->setText("...");
51  m_button->setFixedSize(24, 18);
52 
53  m_layout = new QGridLayout(this);
54 
55  m_layout->setContentsMargins(0, 0, 0, 0);
56  m_layout->setHorizontalSpacing(1);
57  m_layout->addWidget(m_progressBar, 0, 0);
58  m_layout->addWidget(m_button, 0, 1);
59 
60  connect(m_button, SIGNAL(released()), this, SLOT(onReleased()));
61 }
62 
64 
66 {
67  m_tasks.insert(std::map<int, te::common::TaskProgress*>::value_type(id, t));
68 }
69 
71 {
72  cancelTask(taskId);
73 
74  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.find(taskId);
75 
76  if(it != m_tasks.end())
77  {
78  m_tasks.erase(it);
79  }
80 
81  if(m_tasks.empty())
82  {
83  m_totalSteps = 0;
84  m_currentStep = 0;
85  m_propStep = 0;
86 
87  m_progressBar->setRange(0, 100);
88 
89  QCoreApplication::postEvent(this, new ProgressResetEvent);
90 
91  QCoreApplication::processEvents();
92  }
93 }
94 
96 {
97  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.find(taskId);
98 
99  if(it != m_tasks.end())
100  {
101  // update total and current values
102  m_totalSteps -= it->second->getTotalSteps();
103  m_currentStep -= it->second->getCurrentStep();
104 
105  if (m_totalSteps > 0)
106  {
107  double aux = static_cast<double> (m_currentStep) / static_cast<double> (m_totalSteps);
108 
109  m_propStep = static_cast<int> (100.0 * aux);
110  }
111  else
112  {
113  m_propStep = 0;
114  }
115  }
116 }
117 
119 {
120  m_totalSteps += m_tasks[taskId]->getTotalSteps();
121 
122  if (m_totalSteps == 0)
123  m_progressBar->setRange(0, 0);
124  else
125  m_progressBar->setRange(0, 100);
126 }
127 
129 {
130  double currentStep = 0;
131 
132  std::map<int, te::common::TaskProgress*>::iterator it;
133 
134  for (it = m_tasks.begin(); it != m_tasks.end(); ++it)
135  currentStep += it->second->getCurrentStep();
136 
137  it = m_tasks.find(taskId);
138 
139  double aux = static_cast<double>(currentStep) / static_cast<double>(m_totalSteps);
140 
141  int val = static_cast<int>(100.0 * aux);
142 
143  if(val != m_propStep && val >= 0.0)
144  {
145  QCoreApplication::postEvent(this, new ProgressSetValueEvent(m_propStep));
146 
147  QCoreApplication::processEvents();
148  }
149 }
150 
152 {
153 }
154 
156 {
157  m_button->setText(value.c_str());
158 }
159 
161 {
162  if(e->type() == ProgressSetValueEvent::type())
163  {
164  m_progressBar->setValue(static_cast<ProgressSetValueEvent*>(e)->m_value);
165  return;
166  }
167 
168  if(e->type() == ProgressResetEvent::type())
169  m_progressBar->reset();
170 }
171 
173 {
174  emit clicked();
175 }
QPushButton * m_button
GUI Objects used to build the custom widget.
void cancelTask(int taskId)
Cancel a task.
std::map< int, te::common::TaskProgress * > m_tasks
Task container.
QGridLayout * m_layout
GUI Objects used to build the custom widget.
The ProgressSetValueEvent is a custom event used to set a new value into a progress bar...
virtual void onReleased()
Used to get the button clicked (internal function).
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
A custom event used to set a new message into a progress bar.
The ProgressResetEvent is a custom event used to reset a progress bar.
int m_propStep
Attribute used to define the proportional step (0-100).
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.
~ProgressViewerBar()
Virtual destructor.
void clicked()
Emit a signal if the button was clicked.
void updateMessage(int taskId)
Update the progress message.
void setButtonText(const std::string &value)
static QEvent::Type type()
Get the custom event type.
void setTotalValues(int taskId)
Set task total steps.
The ProgressResetEvent is a custom event used to reset a progress bar.
ProgressViewerBar(QWidget *parent)
Default constructor.
int m_currentStep
Attribute used to define the current steps of all task.
void removeTask(int taskId)
Removes a task from progress viewer container.
A class that defines the interface of a qt bar progress viewer.
virtual void customEvent(QEvent *e)
Used to receive custom progress events.
QProgressBar * m_progressBar
GUI Objects used to build the custom widget.
The ProgressSetValueEvent is a custom event used to set a new value into a progress bar...
int m_totalSteps
Attribute used to define the total steps of all tasks.
void updateValue(int taskId)
Update the progress evaluation.