ProgressViewerTaskWidget.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/ProgressViewerTaskWidget.cpp
22 
23 \brief A progress dialog.
24 */
25 
26 // Terralib
27 #include "../../../core/translator/Translator.h"
29 #include "ProgressResetEvent.h"
31 #include "ProgressSetValueEvent.h"
32 
33 
34 // Qt
35 #include <QtCore/QCoreApplication>
36 #include <QApplication>
37 #include <QStyle>
38 
40  QWidget* parent, bool hideToolBar, std::string message)
41  : QWidget(parent),
42  m_taskId(-1),
43  m_totalSteps(0),
44  m_currentStep(0),
45  m_propStep(0),
46  m_message(message),
47  m_task(nullptr)
48 {
49  // build custom widget
50  m_mainGridLayout = new QGridLayout(this);
51  m_mainGridLayout->setContentsMargins(1, 1, 1, 1);
52  m_mainGridLayout->setHorizontalSpacing(2);
53 
54  m_button = new QToolButton(this);
55  m_button->setIcon(m_button->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
56 
57  m_label = new QLabel(this);
58 
59  if (hideToolBar)
60  {
61  m_mainGridLayout->addWidget(m_button, 0, 0);
62  m_mainGridLayout->addWidget(m_label, 0, 1);
63 
64  m_progressBar = nullptr;
65  }
66  else
67  {
68  m_progressBar = new QProgressBar(this);
69 
70  m_mainGridLayout->addWidget(m_button, 1, 0);
71  m_mainGridLayout->addWidget(m_progressBar, 1, 1);
72  m_mainGridLayout->addWidget(m_label, 0, 0, 1, 2);
73 
74  // set default range
75  m_progressBar->setRange(0, 100);
76  }
77 
78  m_button->setEnabled(false);
79 
80  if (!m_message.empty())
81  m_label->setText(QString::fromUtf8(m_message.c_str()));
82 
83  // connect signal cancel
84  connect(m_button, SIGNAL(released()), this, SLOT(cancel()));
85 }
86 
88  default;
89 
91  te::common::TaskProgress* /*t*/, int /*id*/)
92 {
93 }
94 
96 {
97  cancelTask(taskId);
98 
99  if (m_task && m_task->getId() == taskId)
100  {
101 
102  m_task = nullptr;
103  m_button->setEnabled(false);
104 
105  if (m_progressBar != nullptr)
106  {
107  m_progressBar->setRange(0, 100);
108  }
109 
110  QCoreApplication::postEvent(this, new ProgressResetEvent);
111 
112  QCoreApplication::processEvents();
113  }
114 }
115 
117 {
118  if (m_task && m_task->getId() == taskId)
119  {
120  m_totalSteps = 0;
121  m_currentStep = 0;
122  m_propStep = 0;
123  }
124 }
125 
127 {
128  if (m_task && m_task->getId() == taskId)
129  {
131 
132  if (m_totalSteps == 0)
133  m_progressBar->setRange(0, 0);
134  else
135  m_progressBar->setRange(0, 100);
136  }
137 }
138 
140 {
141  double currentStep = 0;
142 
143  if (m_task && m_task->getId() == taskId)
144  {
145  currentStep += m_task->getCurrentStep();
146 
147  double aux = static_cast<double>(currentStep) / static_cast<double>(m_totalSteps);
148 
149  int val = static_cast<int>(100.0 * aux);
150 
151  if (val != m_propStep && val >= 0.0)
152  {
153  m_propStep = val;
154 
155  QCoreApplication::postEvent(this, new ProgressSetValueEvent(m_propStep));
156 
157  QCoreApplication::processEvents();
158  }
159  }
160 }
161 
163 {
164  if (m_task && m_task->getId() == taskId)
165  {
167 
168  QCoreApplication::postEvent(this, new ProgressSetMessageEvent(m_message));
169 
170  QCoreApplication::processEvents();
171  }
172 }
173 
175 {
176  m_task = task;
177  m_taskId = task->getId();
178 
179  m_button->setEnabled(true);
180 }
181 
183 {
184  m_message = message;
185 
186  if (m_label != nullptr)
187  {
188  m_label->setText(QString(m_message.c_str()));
189  }
190 }
191 
193 {
194  if(e->type() == ProgressSetValueEvent::type())
195  {
196  if (m_progressBar)
197  m_progressBar->setValue(static_cast<ProgressSetValueEvent*>(e)->m_value);
198  }
199  else if(e->type() == ProgressResetEvent::type())
200  {
201  if (m_progressBar)
202  m_progressBar->reset();
203  }
204  else if (e->type() == ProgressSetMessageEvent::type())
205  {
206  m_label->setText(static_cast<ProgressSetMessageEvent*>(e)->m_value.c_str());
207  return;
208  }
209 }
210 
212 {
213  if (m_task)
214  m_task->cancel();
215 }
void setTask(te::common::TaskProgress *task)
int m_totalSteps
Attribute used to define the total steps of all tasks.
int m_currentStep
Attribute used to define the current steps of all task.
int m_propStep
Attribute used to define the proportional step (0-100).
ProgressViewerTaskWidget(QWidget *parent, bool hideToolBar, std::string message="")
Default constructor.
void setTotalValues(int taskId)
Set task total steps.
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
A custom event used to set a new message into a progress bar.
const std::string & getMessage() const
Get the task message.
The ProgressResetEvent is a custom event used to reset a progress bar.
virtual ~ProgressViewerTaskWidget()
Virtual destructor.
QGridLayout * m_mainGridLayout
GUI Objects used to build the custom widget.
int getId() const
Get the task identification.
void cancel()
Cancel task, set the task active FALSE.
static QEvent::Type type()
Get the custom event type.
int getCurrentStep() const
Get the task current step.
void updateValue(int taskId)
Update the progress evaluation.
QLabel * m_label
GUI Objects used to build the custom widget.
static QEvent::Type type()
Get the custom event type.
QToolButton * m_button
GUI Objects used to build the custom widget.
void addTask(te::common::TaskProgress *t, int id)
Insert a new taks to progress viewer container.
A progress dialog.
The ProgressResetEvent is a custom event used to reset a progress bar.
void removeTask(int taskId)
Removes a task from progress viewer container.
te::common::TaskProgress * m_task
Task object.
std::string m_message
Attribute used to define dialog message.
QProgressBar * m_progressBar
GUI Objects used to build the custom widget.
The ProgressSetMessageEvent is a custom event used to set a new message into a progress bar...
virtual void customEvent(QEvent *e)
Used to receive custom progress events.
void updateMessage(int taskId)
Update the progress message.
virtual void cancel()
Get the button clicked and cancel ALL tasks.
int getTotalSteps() const
Get the task total stepes.
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.