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  _errorMessageMutex.unlock();
64 }
65 
67 {
68  // WARNING, don't put a log message here! otherwise it could be recursively called.
69  if(anEvent->getClassName().compare("ULogEvent") == 0)
70  {
71  ULogEvent * logEvent = (ULogEvent*)anEvent;
72  _msgListMutex.lock();
73  _msgList.append(QPair<QString, int>(logEvent->getMsg().c_str(), logEvent->getCode()));
74  while(_ui->spinBox_lines->value()>0 && _msgList.size()>_ui->spinBox_lines->value())
75  {
76  _msgList.pop_front();
77  }
78  _msgListMutex.unlock();
79 
80  if(_ui->spinBox_time->value()>0 && _time.restart() < _ui->spinBox_time->value())
81  {
82  if(logEvent->getCode() == ULogger::kFatal)
83  {
84  QMetaObject::invokeMethod(&_timer, "start", Q_ARG(int, 0));
85  }
86  else
87  {
88  QMetaObject::invokeMethod(&_timer, "start", Q_ARG(int, _ui->spinBox_time->value()));
89  }
90  }
91  else
92  {
93  QMetaObject::invokeMethod(&_timer, "start", Q_ARG(int, 0));
94  }
95 
96  if(logEvent->getCode() == ULogger::kFatal)
97  {
98  //This thread will wait until the message box is closed...
99  // Assuming that error messages come from a different thread.
100  _errorMessageMutex.lock();
101  }
102 
103  }
104  return false;
105 }
106 
107 void ConsoleWidget::appendMsg(const QString & msg, int level)
108 {
109  switch(level)
110  {
111  case 0:
112  _ui->textEdit->setTextColor(Qt::darkGreen);
113  break;
114  case 2:
115  _ui->textEdit->setTextColor(Qt::yellow);
116  break;
117  case 3:
118  case 4:
119  _ui->textEdit->setTextColor(Qt::red);
120  break;
121  default:
122  _ui->textEdit->setTextColor(Qt::white);
123  break;
124  }
125  _ui->textEdit->append(msg);
126 
127  if(level == ULogger::kFatal)
128  {
129  _textCursor->endEditBlock();
130  QTextCursor cursor = _ui->textEdit->textCursor();
131  cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
132  _ui->textEdit->setTextCursor(cursor);
133  //The application will exit, so warn the user.
134  _errorMessage->setText(tr("Description:\n\n%1\n\nThe application will now exit...").arg(msg));
135  _errorMessage->exec();
136  _errorMessageMutex.unlock();
137  }
138 }
139 
141 {
142  _msgListMutex.lock();
143  _textCursor->beginEditBlock();
144  for(int i=0; i<_msgList.size(); ++i)
145  {
146  appendMsg(_msgList[i].first, _msgList[i].second);
147  }
148  _textCursor->endEditBlock();
149  _msgList.clear();
150  _msgListMutex.unlock();
151  QTextCursor cursor = _ui->textEdit->textCursor();
152  cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
153  _ui->textEdit->setTextCursor(cursor);
154 }
155 
157 {
158  _ui->textEdit->document()->setMaximumBlockCount(_ui->spinBox_lines->value());
159 }
160 
161 }
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 Mon Dec 14 2020 03:34:58