ProgressViewerWidget.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/ProgressViewerWidget.cpp
22 
23  \brief A class that defines the interface of a qt widget to group a set of ProgressWidgetItem.
24 */
25 
26 // Terralib
28 #include "ProgressViewerWidget.h"
29 #include "ProgressWidgetItem.h"
31 
32 // Qt
33 #include <QtCore/QCoreApplication>
34 #include <QApplication>
35 #include <QCloseEvent>
36 #include <QScrollBar>
37 
39  : QDialog(parent, Qt::Tool)
40 
41 {
42  //create custom widget
43  m_MainLayout = new QGridLayout(this);
44  m_MainLayout->setContentsMargins(1, 1, 1, 1);
45 
46  m_scroll = new QScrollArea(this);
47  m_scroll->setBackgroundRole(QPalette::Light);
48  m_MainLayout->addWidget(m_scroll);
49  m_scroll->setWidgetResizable(true);
50 
51  m_widget = new QWidget(m_scroll);
52  m_scroll->setWidget(m_widget);
53 
54  m_widgetLayout = new QGridLayout(m_widget);
55  m_widgetLayout->setContentsMargins(1, 1, 1, 1);
56  m_widgetLayout->setVerticalSpacing(1);
57  m_widgetLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
58 }
59 
61 
63 {
64  m_tasks.insert(std::map<int, te::common::TaskProgress*>::value_type(id, t));
65 
66  QCoreApplication::postEvent(this, new CreateProgressWidgetItemEvent(t->getMessage().c_str(), id, t->getTotalSteps()));
67 
68  QCoreApplication::processEvents();
69 }
70 
72 {
73  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.find(taskId);
74 
75  if(it != m_tasks.end())
76  m_tasks.erase(it);
77 
78  QCoreApplication::postEvent(this, new RemoveProgressWidgetItemEvent(taskId));
79 
80  QCoreApplication::processEvents();
81 }
82 
84 {
85  removeTask(taskId);
86 }
87 
89 {
90  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.find(taskId);
91 
92  if (it != m_tasks.end())
93  {
94  std::map<int, te::qt::widgets::ProgressWidgetItem*>::iterator itItem = m_items.find(taskId);
95 
96  if (itItem != m_items.end())
97  {
98  itItem->second->setTotalValues(it->second->getTotalSteps());
99  }
100  }
101 }
102 
104 {
105  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.find(taskId);
106 
107  if(it != m_tasks.end())
108  {
109  std::map<int, te::qt::widgets::ProgressWidgetItem*>::iterator itItem = m_items.find(taskId);
110 
111  if(itItem != m_items.end())
112  {
113  itItem->second->setValue(it->second->getProportionalValue());
114  }
115  }
116 }
117 
119 {
120  std::map<int, te::common::TaskProgress*>::iterator it = m_tasks.find(taskId);
121 
122  if(it != m_tasks.end())
123  {
124  std::map<int, te::qt::widgets::ProgressWidgetItem*>::iterator itItem = m_items.find(taskId);
125 
126  if(itItem != m_items.end())
127  {
128  itItem->second->setLabel(it->second->getMessage());
129  }
130  }
131 }
132 
134 {
135  if(e->type() == CreateProgressWidgetItemEvent::type())
136  {
137  CreateProgressWidgetItemEvent* createEvent = static_cast<CreateProgressWidgetItemEvent*>(e);
138 
139  ProgressWidgetItem* item = new ProgressWidgetItem(m_widget, createEvent->m_taskId, createEvent->m_totalSteps, createEvent->m_label);
140 
141  m_items.insert(std::map<int, ProgressWidgetItem*>::value_type(createEvent->m_taskId, item));
142 
143  connect(item, SIGNAL(taskCanceled(int)), this, SLOT(cancel(int)));
144 
145  m_widgetLayout->addWidget(item);
146 
147  item->show();
148 
149  return;
150  }
151 
152  if(e->type() == RemoveProgressWidgetItemEvent::type())
153  {
154  RemoveProgressWidgetItemEvent* removeEvent = static_cast<RemoveProgressWidgetItemEvent*>(e);
155 
156  std::map<int, te::qt::widgets::ProgressWidgetItem*>::iterator itItem = m_items.find(removeEvent->m_taskId);
157  if(itItem == m_items.end())
158  return;
159 
160  delete itItem->second;
161  m_items.erase(itItem);
162  }
163 }
164 
166 {
167  std::map<int, te::common::TaskProgress*>::iterator itTask = m_tasks.find(id);
168 
169  if(itTask != m_tasks.end())
170  {
171  itTask->second->cancel();
172  }
173 }
174 
virtual void cancelTask(int taskId)
Cancel a task.
virtual void updateMessage(int taskId)
Update the progress message.
The CreateProgressWidgetItemEvent is a custom event used to create a progress widget item...
The RemoveProgressWidgetItemEvent is a custom event used to remove a progress widget item...
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
virtual void removeTask(int taskId)
Removes a task from progress viewer container.
const std::string & getMessage() const
Get the task message.
virtual void updateValue(int taskId)
Update the progress evaluation.
A class that defines the interface of a qt widget to group a set of ProgressWidgetItem.
Custom widget used to represent a single task.
The CreateProgressWidgetItemEvent is a custom event used to create a progress widget item...
static QEvent::Type type()
Get the custom event type.
ProgressViewerWidget(QWidget *parent)
Default constructor.
int m_totalSteps
The total steps of progress widget that will be created.
int m_taskId
The task id associated with the progress widget that will be removed.
static QEvent::Type type()
Get the custom event type.
virtual void addTask(te::common::TaskProgress *t, int id)
Insert a new taks to progress viewer container.
The RemoveProgressWidgetItem is a custom event used to remove a progress widget item.
void cancel(int id)
This slot is connect to all cancel buttons of each progress items.
std::map< int, te::common::TaskProgress * > m_tasks
Task container.
QGridLayout * m_widgetLayout
GUI Objects used to build the custom widget.
QGridLayout * m_MainLayout
GUI Objects used to build the custom widget.
int m_taskId
The task id associated with the progress widget that will be created.
QWidget * m_widget
GUI Objects used to build the custom widget.
QScrollArea * m_scroll
GUI Objects used to build the custom widget.
void customEvent(QEvent *e)
Used to receive custom progress events.
virtual void setTotalValues(int taskId)
Set task total steps.
virtual ~ProgressViewerWidget()
Virtual destructor.
std::map< int, ProgressWidgetItem * > m_items
Custom widget progress item container.
int getTotalSteps() const
Get the task total stepes.
Custom widget used to represent a single task.