ConsoleWidget.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 "ui_consoleWidget.h"
32 #include <QMessageBox>
33 #include <QtGui/QTextCursor>
34 #include <QtCore/QTimer>
35 
36 namespace rtabmap {
37 
38 ConsoleWidget::ConsoleWidget(QWidget * parent) :
39  QWidget(parent)
40 {
41  _ui = new Ui_consoleWidget();
42  _ui->setupUi(this);
44  _ui->textEdit->document()->setMaximumBlockCount(_ui->spinBox_lines->value());
45  _textCursor = new QTextCursor(_ui->textEdit->document());
46  _ui->textEdit->setFontPointSize(10);
47  QPalette p(_ui->textEdit->palette());
48  p.setColor(QPalette::Base, Qt::black);
49  _ui->textEdit->setPalette(p);
50  _errorMessage = new QMessageBox(QMessageBox::Critical, tr("Fatal error occurred"), "", QMessageBox::Ok, this);
51  _errorMessageMutex.lock();
52  _time.start();
53  _timer.setSingleShot(true);
54  connect(_ui->pushButton_clear, SIGNAL(clicked()), _ui->textEdit, SLOT(clear()));
55  connect(_ui->spinBox_lines, SIGNAL(valueChanged(int)), this, SLOT(updateTextEditBufferSize()));
56  connect(this, SIGNAL(msgReceived(const QString &, int)), this, SLOT(appendMsg(const QString &, int)));
57  connect(&_timer, SIGNAL(timeout()), this, SLOT(flushConsole()));
58 }
59 
61 {
62  delete _ui;
63 }
64 
66 {
67  // WARNING, don't put a log message here! otherwise it could be recursively called.
68  if(anEvent->getClassName().compare("ULogEvent") == 0)
69  {
70  ULogEvent * logEvent = (ULogEvent*)anEvent;
71  _msgListMutex.lock();
72  _msgList.append(QPair<QString, int>(logEvent->getMsg().c_str(), logEvent->getCode()));
73  while(_ui->spinBox_lines->value()>0 && _msgList.size()>_ui->spinBox_lines->value())
74  {
75  _msgList.pop_front();
76  }
77  _msgListMutex.unlock();
78 
79  if(_ui->spinBox_time->value()>0 && _time.restart() < _ui->spinBox_time->value())
80  {
81  if(logEvent->getCode() == ULogger::kFatal)
82  {
83  QMetaObject::invokeMethod(&_timer, "start", Q_ARG(int, 0));
84  }
85  else
86  {
87  QMetaObject::invokeMethod(&_timer, "start", Q_ARG(int, _ui->spinBox_time->value()));
88  }
89  }
90  else
91  {
92  QMetaObject::invokeMethod(&_timer, "start", Q_ARG(int, 0));
93  }
94 
95  if(logEvent->getCode() == ULogger::kFatal)
96  {
97  //This thread will wait until the message box is closed...
98  // Assuming that error messages come from a different thread.
99  _errorMessageMutex.lock();
100  }
101 
102  }
103  return false;
104 }
105 
106 void ConsoleWidget::appendMsg(const QString & msg, int level)
107 {
108  switch(level)
109  {
110  case 0:
111  _ui->textEdit->setTextColor(Qt::darkGreen);
112  break;
113  case 2:
114  _ui->textEdit->setTextColor(Qt::yellow);
115  break;
116  case 3:
117  case 4:
118  _ui->textEdit->setTextColor(Qt::red);
119  break;
120  default:
121  _ui->textEdit->setTextColor(Qt::white);
122  break;
123  }
124  _ui->textEdit->append(msg);
125 
126  if(level == ULogger::kFatal)
127  {
128  _textCursor->endEditBlock();
129  QTextCursor cursor = _ui->textEdit->textCursor();
130  cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
131  _ui->textEdit->setTextCursor(cursor);
132  //The application will exit, so warn the user.
133  _errorMessage->setText(tr("Description:\n\n%1\n\nThe application will now exit...").arg(msg));
134  _errorMessage->exec();
135  _errorMessageMutex.unlock();
136  }
137 }
138 
140 {
141  _msgListMutex.lock();
142  _textCursor->beginEditBlock();
143  for(int i=0; i<_msgList.size(); ++i)
144  {
145  appendMsg(_msgList[i].first, _msgList[i].second);
146  }
147  _textCursor->endEditBlock();
148  _msgList.clear();
149  _msgListMutex.unlock();
150  QTextCursor cursor = _ui->textEdit->textCursor();
151  cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
152  _ui->textEdit->setTextCursor(cursor);
153 }
154 
156 {
157  _ui->textEdit->document()->setMaximumBlockCount(_ui->spinBox_lines->value());
158 }
159 
160 }
int getCode() const
Definition: UEvent.h:74
void appendMsg(const QString &msg, int level=1)
Definition: UEvent.h:57
virtual bool handleEvent(UEvent *anEvent)
ConsoleWidget(QWidget *parent=0)
QMessageBox * _errorMessage
Definition: ConsoleWidget.h:68
Ui_consoleWidget * _ui
Definition: ConsoleWidget.h:67
QTextCursor * _textCursor
Definition: ConsoleWidget.h:73
virtual std::string getClassName() const =0
QList< QPair< QString, int > > _msgList
Definition: ConsoleWidget.h:74
static void addHandler(UEventsHandler *handler)
ULogger class and convenient macros.
void msgReceived(const QString &, int)
const std::string & getMsg() const
Definition: ULogger.h:137


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