ProgressDialog.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
29 #include <QLayout>
30 #include <QProgressBar>
31 #include <QTextEdit>
32 #include <QLabel>
33 #include <QPushButton>
34 #include <QtGui/QCloseEvent>
35 #include <QCheckBox>
36 #include <QtCore/QTimer>
37 #include <QtCore/QTime>
38 #include <QScrollBar>
40 
41 namespace rtabmap {
42 
43 ProgressDialog::ProgressDialog(QWidget *parent, Qt::WindowFlags flags) :
44  QDialog(parent, flags),
45  _delayedClosingTime(1),
46  _canceled(false)
47 {
48  _text = new QLabel(this);
49  _text->setWordWrap(true);
50  _progressBar = new QProgressBar(this);
51  _progressBar->setMaximum(1);
52  _detailedText = new QTextEdit(this);
53  _detailedText->setReadOnly(true);
54  _detailedText->setLineWrapMode(QTextEdit::NoWrap);
55  _closeButton = new QPushButton(this);
56  _closeButton->setText("Close");
57  _cancelButton = new QPushButton(this);
58  _cancelButton->setText("Cancel");
59  _cancelButton-> setVisible(false);
60  _closeWhenDoneCheckBox = new QCheckBox(this);
61  _closeWhenDoneCheckBox->setChecked(true);
62  _closeWhenDoneCheckBox->setText("Close when done.");
63  _endMessage = "Finished!";
64  this->clear();
65  connect(_closeButton, SIGNAL(clicked()), this, SLOT(close()));
66  connect(_cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
67 
68  QVBoxLayout * layout = new QVBoxLayout(this);
69  layout->addWidget(_text);
70  layout->addWidget(_progressBar);
71  layout->addWidget(_detailedText);
72  QHBoxLayout * hLayout = new QHBoxLayout();
73  layout->addLayout(hLayout);
74  hLayout->addWidget(_closeWhenDoneCheckBox);
75  hLayout->addStretch();
76  hLayout->addWidget(_cancelButton);
77  hLayout->addWidget(_closeButton);
78  this->setLayout(layout);
79 
80  this->setModal(true);
81 }
82 
84 {
85 
86 }
87 
88 void ProgressDialog::setAutoClose(bool on, int delayedClosingTimeSec)
89 {
90  if(delayedClosingTimeSec >= 0)
91  {
92  _delayedClosingTime = delayedClosingTimeSec;
93  }
94  _closeWhenDoneCheckBox->setChecked(on);
95 }
96 
98 {
99  _cancelButton->setVisible(visible);
100 }
101 
102 void ProgressDialog::appendText(const QString & text, const QColor & color)
103 {
104  UDEBUG(text.toStdString().c_str());
105  _text->setText(text);
106  QString html = tr("<html><font color=\"#999999\">%1 </font><font color=\"%2\">%3</font></html>").arg(QTime::currentTime().toString("HH:mm:ss")).arg(color.name()).arg(text);
107  _detailedText->append(html);
108  _detailedText->ensureCursorVisible();
109  _detailedText->horizontalScrollBar()->setSliderPosition(0);
110  _detailedText->verticalScrollBar()->setSliderPosition(_detailedText->verticalScrollBar()->maximum());
111 }
113 {
114  _progressBar->setValue(value);
115  if(value == _progressBar->maximum())
116  {
117  _text->setText(_endMessage);
118  _closeButton->setEnabled(true);
119  if(_closeWhenDoneCheckBox->isChecked() && _delayedClosingTime == 0)
120  {
121  this->close();
122  }
123  else if(_closeWhenDoneCheckBox->isChecked())
124  {
125  QTimer::singleShot(_delayedClosingTime*1000, this, SLOT(closeDialog()));
126  }
127  }
128 }
130 {
131  return _progressBar->maximum();
132 }
134 {
135  _progressBar->setMaximum(steps);
136 }
137 
139 {
140  //incremental progress bar (if we don't know how many items will be added)
141  if(_progressBar->value() >= _progressBar->maximum()-steps)
142  {
143  _progressBar->setMaximum(_progressBar->maximum()+steps+1);
144  }
145  _progressBar->setValue(_progressBar->value()+steps);
146 }
147 
149 {
150  _text->clear();
151  _detailedText->clear();
152  resetProgress();
153 }
154 
156 {
157  _progressBar->reset();
158  _closeButton->setEnabled(false);
159  _canceled = false;
160 }
161 
163 {
164  if(_closeWhenDoneCheckBox->isChecked())
165  {
166  close();
167  }
168 }
169 
170 void ProgressDialog::closeEvent(QCloseEvent *event)
171 {
172  if(_progressBar->value() == _progressBar->maximum())
173  {
174  _canceled = false;
175  event->accept();
176  }
177  else
178  {
179  event->ignore();
180  }
181 }
182 
184 {
185  _canceled = true;
186  Q_EMIT canceled();
187 }
188 
189 }
void setAutoClose(bool on, int delayedClosingTimeMsec=-1)
void incrementStep(int steps=1)
QPushButton * _cancelButton
void setCancelButtonVisible(bool visible)
ProgressDialog(QWidget *parent=0, Qt::WindowFlags flags=0)
QProgressBar * _progressBar
QCheckBox * _closeWhenDoneCheckBox
void setMaximumSteps(int steps)
#define false
Definition: ConvertUTF.c:56
virtual void closeEvent(QCloseEvent *event)
#define UDEBUG(...)
ULogger class and convenient macros.
void appendText(const QString &text, const QColor &color=Qt::black)
QPushButton * _closeButton


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:41:32