ProgressWidgetItem.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/ProgressWidgetItem.cpp
22 
23  \brief Custom widget used to represent a single task.
24 */
25 
26 // TerraLib
27 #include "ProgressResetEvent.h"
29 #include "ProgressSetValueEvent.h"
30 #include "ProgressWidgetItem.h"
31 
32 // Qt
33 #include <QtCore/QCoreApplication>
34 #include <QSizePolicy>
35 #include <QStyle>
36 
37 te::qt::widgets::ProgressWidgetItem::ProgressWidgetItem(QWidget* parent, int taskId, int totalSteps, QString label)
38  : QWidget(parent),
39  m_taskId(taskId),
40  m_totalSteps(0),
41  m_currentStep(0),
42  m_propStep(0)
43 {
44  // build custom widget
45  m_mainGridLayout = new QGridLayout(this);
46  m_mainGridLayout->setContentsMargins(1, 1, 1, 1);
47  m_mainGridLayout->setHorizontalSpacing(2);
48 
49  m_frame = new QFrame(this);
50  m_frame->setFrameShape(QFrame::StyledPanel);
51  m_frame->setFrameShadow(QFrame::Sunken);
52 
53  m_mainGridLayout->addWidget(m_frame);
54 
55  m_frameGridLayout = new QGridLayout(m_frame);
56  m_frameGridLayout->setContentsMargins(1, 1, 1, 1);
57  m_frameGridLayout->setVerticalSpacing(1);
58 
59  m_label = new QLabel(m_frame);
60  m_label->setText(label);
61 
62  m_progressBar = new QProgressBar(m_frame);
63 
64  m_button = new QCommandLinkButton(m_frame);
65  m_button->setIcon(m_button->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
66  m_button->setFixedHeight(23);
67  m_button->setFixedWidth(23);
68 
69  m_frameGridLayout->addWidget(m_label, 0, 0, 1, 2);
70  m_frameGridLayout->addWidget(m_progressBar, 1, 1);
71  m_frameGridLayout->addWidget(m_button, 1, 0);
72 
73  // set default range
74  if (totalSteps != 0)
75  m_progressBar->setRange(0, 100);
76  else
77  m_progressBar->setRange(0, 0);
78 
79  // connect signal cancel
80  connect(m_button, SIGNAL(released()), this, SLOT(cancel()));
81 
82  this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
83 }
84 
86 
88 {
89  m_currentStep = step;
90 
91  if (m_currentStep != m_propStep && m_currentStep >= 0.0)
92  {
93  //fix progress bar range
94  if (m_progressBar->maximum() != 100)
95  m_progressBar->setRange(0, 100);
96 
98 
99  QCoreApplication::postEvent(this, new ProgressSetValueEvent(m_propStep));
100 
101  QCoreApplication::processEvents();
102  }
103 }
104 
106 {
107  m_totalSteps = values;
108 
109  if (m_totalSteps == 0)
110  m_progressBar->setRange(0, 0);
111  else
112  m_progressBar->setRange(0, 100);
113 }
114 
115 void te::qt::widgets::ProgressWidgetItem::setLabel(const std::string& message)
116 {
117  QCoreApplication::postEvent(this, new ProgressSetMessageEvent(message));
118 
119  QCoreApplication::processEvents();
120 }
121 
123 {
124  QCoreApplication::postEvent(this, new ProgressResetEvent);
125 
126  QCoreApplication::processEvents();
127 }
128 
130 {
131  emit taskCanceled(m_taskId);
132 }
133 
135 {
137  {
138  if(m_progressBar->minimum() == 0 && m_progressBar->maximum() == 0)
139  return;
140 
141  m_progressBar->setValue(static_cast<te::qt::widgets::ProgressSetValueEvent*>(e)->m_value);
142 
143  return;
144  }
145 
146  if(e->type() == ProgressSetMessageEvent::type())
147  {
148  m_label->setText(static_cast<ProgressSetMessageEvent*>(e)->m_value.c_str());
149  return;
150  }
151 
152  if(e->type() == ProgressResetEvent::type())
153  m_progressBar->reset();
154 }
int m_propStep
Attribute used to define the proportional step (0-100).
virtual void customEvent(QEvent *e)
Used to receive custom progress events.
QProgressBar * m_progressBar
GUI Objects used to build the custom widget.
QGridLayout * m_frameGridLayout
GUI Objects used to build the custom widget.
int m_totalSteps
Attribute used to define the total steps of all tasks.
QCommandLinkButton * m_button
GUI Objects used to build the custom widget.
The ProgressSetValueEvent is a custom event used to set a new value into a progress bar...
QFrame * m_frame
GUI Objects used to build the custom widget.
A custom event used to set a new message into a progress bar.
virtual void setValue(int step)
Set the current value in progress bar.
The ProgressResetEvent is a custom event used to reset a progress bar.
void setTotalValues(int values)
Set task total steps.
QLabel * m_label
GUI Objects used to build the custom widget.
void taskCanceled(int id)
Inform that a task was canceled.
static QEvent::Type type()
Get the custom event type.
ProgressWidgetItem(QWidget *parent, int taskId, int totalSteps, QString label)
Default constructor.
QGridLayout * m_mainGridLayout
GUI Objects used to build the custom widget.
virtual void reset()
Reset the progress bar.
~ProgressWidgetItem()
Virtual destructor.
static QEvent::Type type()
Get the custom event type.
virtual void setLabel(const std::string &message)
Set the progress label information.
The ProgressResetEvent is a custom event used to reset a progress bar.
virtual void cancel()
Get the button clicked and cancel ALL tasks.
The ProgressSetMessageEvent is 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.
The ProgressSetValueEvent is a custom event used to set a new value into a progress bar...
Custom widget used to represent a single task.
static QEvent::Type type()
Get the custom event type.