Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "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 #define OLD_TIME 1000 //ms
00037 #define MAXIMUM_ITEMS 100
00038
00039 namespace rtabmap {
00040
00041 ConsoleWidget::ConsoleWidget(QWidget * parent) :
00042 QWidget(parent)
00043 {
00044 _ui = new Ui_consoleWidget();
00045 _ui->setupUi(this);
00046 UEventsManager::addHandler(this);
00047 _ui->textEdit->document()->setMaximumBlockCount(MAXIMUM_ITEMS);
00048 _textCursor = new QTextCursor(_ui->textEdit->document());
00049 _ui->textEdit->setFontPointSize(10);
00050 QPalette p(_ui->textEdit->palette());
00051 p.setColor(QPalette::Base, Qt::black);
00052 _ui->textEdit->setPalette(p);
00053 _errorMessage = new QMessageBox(QMessageBox::Critical, tr("Fatal error occurred"), "", QMessageBox::Ok, this);
00054 _errorMessageMutex.lock();
00055 _time.start();
00056 _timer.setSingleShot(true);
00057 connect(_ui->pushButton_clear, SIGNAL(clicked()), _ui->textEdit, SLOT(clear()));
00058 connect(this, SIGNAL(msgReceived(const QString &, int)), this, SLOT(appendMsg(const QString &, int)));
00059 connect(&_timer, SIGNAL(timeout()), this, SLOT(flushConsole()));
00060 }
00061
00062 ConsoleWidget::~ConsoleWidget()
00063 {
00064 delete _ui;
00065 }
00066
00067 void ConsoleWidget::handleEvent(UEvent * anEvent)
00068 {
00069
00070 if(anEvent->getClassName().compare("ULogEvent") == 0)
00071 {
00072 ULogEvent * logEvent = (ULogEvent*)anEvent;
00073 _msgListMutex.lock();
00074 _msgList.append(QPair<QString, int>(logEvent->getMsg().c_str(), logEvent->getCode()));
00075 if(_msgList.size()>MAXIMUM_ITEMS)
00076 {
00077 _msgList.pop_front();
00078 }
00079 _msgListMutex.unlock();
00080
00081 if(_time.restart() < OLD_TIME)
00082 {
00083 if(logEvent->getCode() == ULogger::kFatal)
00084 {
00085 QMetaObject::invokeMethod(&_timer, "start", Q_ARG(int, 0));
00086 }
00087 else
00088 {
00089 QMetaObject::invokeMethod(&_timer, "start", Q_ARG(int, OLD_TIME));
00090 }
00091 }
00092 else
00093 {
00094 QMetaObject::invokeMethod(&_timer, "start", Q_ARG(int, 0));
00095 }
00096
00097 if(logEvent->getCode() == ULogger::kFatal)
00098 {
00099
00100
00101 _errorMessageMutex.lock();
00102 }
00103
00104 }
00105 }
00106
00107 void ConsoleWidget::appendMsg(const QString & msg, int level)
00108 {
00109 switch(level)
00110 {
00111 case 0:
00112 _ui->textEdit->setTextColor(Qt::darkGreen);
00113 break;
00114 case 2:
00115 _ui->textEdit->setTextColor(Qt::yellow);
00116 break;
00117 case 3:
00118 case 4:
00119 _ui->textEdit->setTextColor(Qt::red);
00120 break;
00121 default:
00122 _ui->textEdit->setTextColor(Qt::white);
00123 break;
00124 }
00125 _ui->textEdit->append(msg);
00126
00127 if(level == ULogger::kFatal)
00128 {
00129 _textCursor->endEditBlock();
00130 QTextCursor cursor = _ui->textEdit->textCursor();
00131 cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
00132 _ui->textEdit->setTextCursor(cursor);
00133
00134 _errorMessage->setText(tr("Description:\n\n%1\n\nThe application will now exit...").arg(msg));
00135 _errorMessage->exec();
00136 _errorMessageMutex.unlock();
00137 }
00138 }
00139
00140 void ConsoleWidget::flushConsole()
00141 {
00142 _msgListMutex.lock();
00143 _textCursor->beginEditBlock();
00144 for(int i=0; i<_msgList.size(); ++i)
00145 {
00146 appendMsg(_msgList[i].first, _msgList[i].second);
00147 }
00148 _textCursor->endEditBlock();
00149 _msgList.clear();
00150 _msgListMutex.unlock();
00151 QTextCursor cursor = _ui->textEdit->textCursor();
00152 cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
00153 _ui->textEdit->setTextCursor(cursor);
00154 }
00155
00156 }