ProgressDialog.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007     * Redistributions of source code must retain the above copyright
00008       notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright
00010       notice, this list of conditions and the following disclaimer in the
00011       documentation and/or other materials provided with the distribution.
00012     * Neither the name of the Universite de Sherbrooke nor the
00013       names of its contributors may be used to endorse or promote products
00014       derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
00020 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 */
00027 
00028 #include "rtabmap/gui/ProgressDialog.h"
00029 #include <QLayout>
00030 #include <QProgressBar>
00031 #include <QTextEdit>
00032 #include <QLabel>
00033 #include <QPushButton>
00034 #include <QtGui/QCloseEvent>
00035 #include <QCheckBox>
00036 #include <QtCore/QTimer>
00037 #include <QtCore/QTime>
00038 #include <QScrollBar>
00039 #include "rtabmap/utilite/ULogger.h"
00040 
00041 namespace rtabmap {
00042 
00043 ProgressDialog::ProgressDialog(QWidget *parent, Qt::WindowFlags flags) :
00044                 QDialog(parent, flags),
00045                 _delayedClosingTime(1),
00046                 _canceled(false)
00047 {
00048         _text = new QLabel(this);
00049         _text->setWordWrap(true);
00050         _progressBar = new QProgressBar(this);
00051         _progressBar->setMaximum(1);
00052         _detailedText  = new QTextEdit(this);
00053         _detailedText->setReadOnly(true);
00054         _detailedText->setLineWrapMode(QTextEdit::NoWrap);
00055         _closeButton = new QPushButton(this);
00056         _closeButton->setText("Close");
00057         _cancelButton = new QPushButton(this);
00058         _cancelButton->setText("Cancel");
00059         _cancelButton-> setVisible(false);
00060         _closeWhenDoneCheckBox = new QCheckBox(this);
00061         _closeWhenDoneCheckBox->setChecked(true);
00062         _closeWhenDoneCheckBox->setText("Close when done.");
00063         _endMessage = "Finished!";
00064         this->clear();
00065         connect(_closeButton, SIGNAL(clicked()), this, SLOT(close()));
00066         connect(_cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
00067 
00068         QVBoxLayout * layout = new QVBoxLayout(this);
00069         layout->addWidget(_text);
00070         layout->addWidget(_progressBar);
00071         layout->addWidget(_detailedText);
00072         QHBoxLayout * hLayout = new QHBoxLayout();
00073         layout->addLayout(hLayout);
00074         hLayout->addWidget(_closeWhenDoneCheckBox);
00075         hLayout->addStretch();
00076         hLayout->addWidget(_cancelButton);
00077         hLayout->addWidget(_closeButton);
00078         this->setLayout(layout);
00079 
00080         this->setModal(true);
00081 }
00082 
00083 ProgressDialog::~ProgressDialog()
00084 {
00085 
00086 }
00087 
00088 void ProgressDialog::setAutoClose(bool on, int delayedClosingTimeSec)
00089 {
00090         if(delayedClosingTimeSec >= 0)
00091         {
00092                 _delayedClosingTime = delayedClosingTimeSec;
00093         }
00094         _closeWhenDoneCheckBox->setChecked(on);
00095 }
00096 
00097 void ProgressDialog::setCancelButtonVisible(bool visible)
00098 {
00099         _cancelButton->setVisible(visible);
00100 }
00101 
00102 void ProgressDialog::appendText(const QString & text, const QColor & color)
00103 {
00104         UDEBUG(text.toStdString().c_str());
00105         _text->setText(text);
00106         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);
00107         _detailedText->append(html);
00108         _detailedText->ensureCursorVisible();
00109         _detailedText->horizontalScrollBar()->setSliderPosition(0);
00110         _detailedText->verticalScrollBar()->setSliderPosition(_detailedText->verticalScrollBar()->maximum());
00111 }
00112 void ProgressDialog::setValue(int value)
00113 {
00114         _progressBar->setValue(value);
00115         if(value == _progressBar->maximum())
00116         {
00117                 _text->setText(_endMessage);
00118                 _closeButton->setEnabled(true);
00119                 if(_closeWhenDoneCheckBox->isChecked() && _delayedClosingTime == 0)
00120                 {
00121                         this->close();
00122                 }
00123                 else if(_closeWhenDoneCheckBox->isChecked())
00124                 {
00125                         QTimer::singleShot(_delayedClosingTime*1000, this, SLOT(closeDialog()));
00126                 }
00127         }
00128 }
00129 int ProgressDialog::maximumSteps() const
00130 {
00131         return _progressBar->maximum();
00132 }
00133 void ProgressDialog::setMaximumSteps(int steps)
00134 {
00135         _progressBar->setMaximum(steps);
00136 }
00137 
00138 void ProgressDialog::incrementStep(int steps)
00139 {
00140         //incremental progress bar (if we don't know how many items will be added)
00141         if(_progressBar->value() >= _progressBar->maximum()-steps)
00142         {
00143                 _progressBar->setMaximum(_progressBar->maximum()+steps+1);
00144         }
00145         _progressBar->setValue(_progressBar->value()+steps);
00146 }
00147 
00148 void ProgressDialog::clear()
00149 {
00150         _text->clear();
00151         _detailedText->clear();
00152         resetProgress();
00153 }
00154 
00155 void ProgressDialog::resetProgress()
00156 {
00157         _progressBar->reset();
00158         _closeButton->setEnabled(false);
00159         _canceled = false;
00160 }
00161 
00162 void ProgressDialog::closeDialog()
00163 {
00164         if(_closeWhenDoneCheckBox->isChecked())
00165         {
00166                 close();
00167         }
00168 }
00169 
00170 void ProgressDialog::closeEvent(QCloseEvent *event)
00171 {
00172         if(_progressBar->value() == _progressBar->maximum())
00173         {
00174                 _canceled = false;
00175                 event->accept();
00176         }
00177         else
00178         {
00179                 event->ignore();
00180         }
00181 }
00182 
00183 void ProgressDialog::cancel()
00184 {
00185         _canceled = true;
00186         Q_EMIT canceled();
00187 }
00188 
00189 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:21