Commit d535d6f9 authored by grunau's avatar grunau
Browse files

Highlight matches when filtering log messages

A new class, LogHighlighter, has been added to the application.
Currently it's only responsible for highlighting those parts of log
messages that match the current filter. However, it can be easily
extended to also highlight timestamps, log levels, etc. All in all, this
should make the information the log viewer displays much easier to scan
through.

git-svn-id: https://camplinux.in.tum.de/svn/campvis/branches/gui-redux@518 bb408c1c-ae56-11e1-83d9-df6b3e0c105e
parent 209db5a6
......@@ -33,6 +33,7 @@ SET(CampvisApplicationToBeMocced
gui/qtcolortools.h
gui/qtdatahandle.h
gui/logviewerwidget.h
gui/loghighlighter.h
gui/properties/abstractpropertywidget.h
gui/properties/abstracttransferfunctioneditor.h
gui/properties/boolpropertywidget.h
......
// ================================================================================================
//
// This file is part of the CAMPVis Software Framework.
//
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
// Christian Schulte zu Berge <christian.szb@in.tum.de>
// Chair for Computer Aided Medical Procedures
// Technische Universität München
// Boltzmannstr. 3, 85748 Garching b. München, Germany
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
//
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
//
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ================================================================================================
#include "loghighlighter.h"
#include <QPalette>
namespace campvis {
LogHighlighter::LogHighlighter(QTextEdit* parent)
: QSyntaxHighlighter(parent)
, _filterRegExp(0)
{
QBrush fgBrush = QBrush(Qt::white);
// Firefox's green highlight
QBrush bgBrush = QBrush(QColor(56, 216, 120));
_filterMatchFormat.setBackground(bgBrush);
_filterMatchFormat.setForeground(fgBrush);
}
LogHighlighter::~LogHighlighter() {
setFilterRegExp(0);
}
void LogHighlighter::setFilterRegExp(const QRegExp* filterRegExp) {
if (_filterRegExp != 0) {
delete _filterRegExp;
}
_filterRegExp = filterRegExp;
}
void LogHighlighter::highlightBlock(const QString& text)
{
if (_filterRegExp != 0) {
highlightFilterMatches(text);
}
}
void LogHighlighter::highlightFilterMatches(const QString &text) {
int pos = 0;
while ((pos = _filterRegExp->indexIn(text, pos)) != -1) {
int matchedLength = _filterRegExp->matchedLength();
setFormat(pos, matchedLength, _filterMatchFormat);
pos += matchedLength;
}
}
}
// ================================================================================================
//
// This file is part of the CAMPVis Software Framework.
//
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
// Christian Schulte zu Berge <christian.szb@in.tum.de>
// Chair for Computer Aided Medical Procedures
// Technische Universität München
// Boltzmannstr. 3, 85748 Garching b. München, Germany
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
//
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
//
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ================================================================================================
#ifndef LOGHIGHLIGHTER_H__
#define LOGHIGHLIGHTER_H__
#include <QTextEdit>
#include <QSyntaxHighlighter>
namespace campvis {
class LogHighlighter : public QSyntaxHighlighter {
Q_OBJECT;
public:
/**
* Constructs a LogHighlighter and installs it on parent.
*
* The specified QTextEdit also becomes the owner of the LogHighlighter.
*
* \param parent the QTextEdit to install the LogHighlighter on
*/
LogHighlighter(QTextEdit* parent);
/**
* Destructor.
*/
~LogHighlighter();
/**
* Highlights the given log block.
*
* This function is called when necessary by the rich text engine, i.e. on new/changed log
* messages.
*
* \param text the log block to highlight
*/
void highlightBlock(const QString &text);
/**
* Set the current filter regexp to highlight.
*
* \param filterRegExp the current filter regexp
*/
void setFilterRegExp(const QRegExp* filterRegExp);
private:
/**
* Highlight all matches of the current regexp in the given log block.
*
* \param text the log block to highlight
*/
void highlightFilterMatches(const QString &text);
const QRegExp* _filterRegExp; ///< Current filter regexp
QTextCharFormat _filterMatchFormat; ///< Format for highlighting filter matches
};
}
#endif // LOGHIGHLIGHTER_H__
......@@ -33,6 +33,7 @@ namespace campvis {
LogViewerWidget::LogViewerWidget(QWidget* parent)
: QWidget(parent)
, _filterRegExp(0)
{
setupGUI();
......@@ -72,6 +73,14 @@ namespace campvis {
_logDisplay->setReadOnly(true);
_mainLayout->addWidget(_logDisplay);
// Use the system's default monospace font at the default size in the log viewer
QFont monoFont = QFont("Monospace");
monoFont.setStyleHint(QFont::TypeWriter);
monoFont.setPointSize(QFont().pointSize() + 1);
_logDisplay->document()->setDefaultFont(monoFont);
_logHighlighter = new LogHighlighter(_logDisplay);
connect(_clear_button, SIGNAL(clicked()), this, SLOT(clearMessages()));
connect(_filter_line_edit, SIGNAL(textEdited(const QString&)), this, SLOT(filterLogMessages(const QString&)));
}
......@@ -86,8 +95,15 @@ namespace campvis {
void LogViewerWidget::appendMessage(const QString& message)
{
_logDisplay->append(message);
_logMessages.push_back(message);
displayMessage(message);
}
void LogViewerWidget::displayMessage(const QString& message)
{
if (_filterRegExp == 0 || _filterRegExp->indexIn(message) != -1) {
_logDisplay->append(message);
}
}
void LogViewerWidget::clearMessages()
......@@ -98,16 +114,17 @@ namespace campvis {
void LogViewerWidget::filterLogMessages(const QString& text)
{
QRegExp regexp = QRegExp(text, Qt::CaseInsensitive, QRegExp::FixedString);
if (text.length() == 0) {
_filterRegExp = 0;
} else {
_filterRegExp = new QRegExp(text, Qt::CaseInsensitive, QRegExp::FixedString);
}
_logDisplay->clear();
QTextDocument *logDocument = _logDisplay->document();
QTextCursor cursor = QTextCursor(logDocument);
_logHighlighter->setFilterRegExp(_filterRegExp);
for (std::deque<QString>::iterator it = _logMessages.begin(); it != _logMessages.end(); it++) {
if (regexp.indexIn(*it) != -1) {
cursor.insertText(*it);
cursor.insertText("\n");
}
displayMessage(*it);
}
}
......
......@@ -37,6 +37,7 @@
#include "tbb/mutex.h"
#include "application/tools/bufferinglog.h"
#include "application/gui/loghighlighter.h"
#include <QLabel>
#include <QWidget>
......@@ -80,14 +81,25 @@ namespace campvis {
*/
void setupGUI();
private slots:
public slots:
/**
* Append the given message to the log viewer
*
* This function adds the specified message to the log viewer's message cache and
* displays it.
*
* \param message message to append to the log viewer
*/
void appendMessage(const QString& message);
private slots:
/**
* Display the given message in the log viewer
*
* \param message message to display in the log viewer
*/
void displayMessage(const QString& message);
/**
* Delete all messages from the log viewer
*/
......@@ -109,6 +121,8 @@ namespace campvis {
BufferingLog* _log; ///< Log buffering messages
std::deque<QString> _logMessages; ///< Queue storing a limited number of recent log messages
QTextEdit* _logDisplay; ///< Widget displaying log messages
const QRegExp* _filterRegExp; ///< Current filter regexp
LogHighlighter* _logHighlighter; ///< Highlighter for log messages
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment