ConsoleWidget.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/ConsoleWidget.h"
00029 #include "ui_consoleWidget.h"
00030 #include <rtabmap/utilite/ULogger.h>
00031 #include <rtabmap/utilite/UEventsManager.h>
00032 #include <QMessageBox>
00033 #include <QtGui/QTextCursor>
00034 #include <QtCore/QTimer>
00035 
00036 namespace rtabmap {
00037 
00038 ConsoleWidget::ConsoleWidget(QWidget * parent) :
00039         QWidget(parent)
00040 {
00041         _ui = new Ui_consoleWidget();
00042         _ui->setupUi(this);
00043         UEventsManager::addHandler(this);
00044         _ui->textEdit->document()->setMaximumBlockCount(_ui->spinBox_lines->value());
00045         _textCursor = new QTextCursor(_ui->textEdit->document());
00046         _ui->textEdit->setFontPointSize(10);
00047         QPalette p(_ui->textEdit->palette());
00048         p.setColor(QPalette::Base, Qt::black);
00049         _ui->textEdit->setPalette(p);
00050         _errorMessage = new QMessageBox(QMessageBox::Critical, tr("Fatal error occurred"), "", QMessageBox::Ok, this);
00051         _errorMessageMutex.lock();
00052         _time.start();
00053         _timer.setSingleShot(true);
00054         connect(_ui->pushButton_clear, SIGNAL(clicked()), _ui->textEdit, SLOT(clear()));
00055         connect(_ui->spinBox_lines, SIGNAL(valueChanged(int)), this, SLOT(updateTextEditBufferSize()));
00056         connect(this, SIGNAL(msgReceived(const QString &, int)), this, SLOT(appendMsg(const QString &, int)));
00057         connect(&_timer, SIGNAL(timeout()), this, SLOT(flushConsole()));
00058 }
00059 
00060 ConsoleWidget::~ConsoleWidget()
00061 {
00062         delete _ui;
00063 }
00064 
00065 bool ConsoleWidget::handleEvent(UEvent * anEvent)
00066 {
00067         // WARNING, don't put a log message here! otherwise it could be recursively called.
00068         if(anEvent->getClassName().compare("ULogEvent") == 0)
00069         {
00070                 ULogEvent * logEvent = (ULogEvent*)anEvent;
00071                 _msgListMutex.lock();
00072                 _msgList.append(QPair<QString, int>(logEvent->getMsg().c_str(), logEvent->getCode()));
00073                 while(_ui->spinBox_lines->value()>0 && _msgList.size()>_ui->spinBox_lines->value())
00074                 {
00075                         _msgList.pop_front();
00076                 }
00077                 _msgListMutex.unlock();
00078 
00079                 if(_ui->spinBox_time->value()>0 && _time.restart() < _ui->spinBox_time->value())
00080                 {
00081                         if(logEvent->getCode() == ULogger::kFatal)
00082                         {
00083                                 QMetaObject::invokeMethod(&_timer, "start", Q_ARG(int, 0));
00084                         }
00085                         else
00086                         {
00087                                 QMetaObject::invokeMethod(&_timer, "start", Q_ARG(int, _ui->spinBox_time->value()));
00088                         }
00089                 }
00090                 else
00091                 {
00092                         QMetaObject::invokeMethod(&_timer, "start", Q_ARG(int, 0));
00093                 }
00094 
00095                 if(logEvent->getCode() == ULogger::kFatal)
00096                 {
00097                         //This thread will wait until the message box is closed...
00098                         // Assuming that error messages come from a different thread.
00099                         _errorMessageMutex.lock();
00100                 }
00101 
00102         }
00103         return false;
00104 }
00105 
00106 void ConsoleWidget::appendMsg(const QString & msg, int level)
00107 {
00108         switch(level)
00109         {
00110         case 0:
00111                 _ui->textEdit->setTextColor(Qt::darkGreen);
00112                 break;
00113         case 2:
00114                 _ui->textEdit->setTextColor(Qt::yellow);
00115                 break;
00116         case 3:
00117         case 4:
00118                 _ui->textEdit->setTextColor(Qt::red);
00119                 break;
00120         default:
00121                 _ui->textEdit->setTextColor(Qt::white);
00122                 break;
00123         }
00124         _ui->textEdit->append(msg);
00125 
00126         if(level == ULogger::kFatal)
00127         {
00128                 _textCursor->endEditBlock();
00129                 QTextCursor cursor = _ui->textEdit->textCursor();
00130                 cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
00131                 _ui->textEdit->setTextCursor(cursor);
00132                 //The application will exit, so warn the user.
00133                 _errorMessage->setText(tr("Description:\n\n%1\n\nThe application will now exit...").arg(msg));
00134                 _errorMessage->exec();
00135                 _errorMessageMutex.unlock();
00136         }
00137 }
00138 
00139 void ConsoleWidget::flushConsole()
00140 {
00141         _msgListMutex.lock();
00142         _textCursor->beginEditBlock();
00143         for(int i=0; i<_msgList.size(); ++i)
00144         {
00145                 appendMsg(_msgList[i].first, _msgList[i].second);
00146         }
00147         _textCursor->endEditBlock();
00148         _msgList.clear();
00149         _msgListMutex.unlock();
00150         QTextCursor cursor = _ui->textEdit->textCursor();
00151         cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
00152         _ui->textEdit->setTextCursor(cursor);
00153 }
00154 
00155 void ConsoleWidget::updateTextEditBufferSize()
00156 {
00157         _ui->textEdit->document()->setMaximumBlockCount(_ui->spinBox_lines->value());
00158 }
00159 
00160 }


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