logviewerwidget.cpp 5.26 KB
Newer Older
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2013, all rights reserved,
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, Germany
10
// 
11
12
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22
23
24
25
26
27
28
29
30
// 
// ================================================================================================

#include "logviewerwidget.h"

namespace campvis {

    LogViewerWidget::LogViewerWidget(QWidget* parent)
        : QWidget(parent)
31
        , _filterRegExp(0)
32
33
34
    {
        setupGUI();

35
        _log = new BufferingLog(100, this);
36
        _log->addCat("", true, tgt::Debug);
37

38
39
        connect(_log, SIGNAL(s_messageAppended(const QString&, int)),
                this, SLOT(appendMessage(const QString&, int)));
40
41
42
    }

    LogViewerWidget::~LogViewerWidget() {
43
44
        delete _logHighlighter;
        delete _controls_layout;
45
46
47
48
49
    }

    void LogViewerWidget::setupGUI() {
        setWindowTitle(tr("Log Viewer"));

50
        _mainLayout = new QVBoxLayout(this);
51
52
53
54

        _controls_layout = new QHBoxLayout();
        _mainLayout->addLayout(_controls_layout);

55
        _filter_label = new QLabel(tr("&Filter:"), this);
56
57
        _controls_layout->addWidget(_filter_label);

58
        _filter_line_edit = new QLineEdit(this);
59
60
61
62
        _filter_line_edit->setPlaceholderText(tr("Start typing here to filter log messages"));

        _filter_label->setBuddy(_filter_line_edit);
        _controls_layout->addWidget(_filter_line_edit);
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

        _cbLogLevel = new QComboBox(this);
        _cbLogLevel->addItem("Debug",   static_cast<int>(tgt::Debug));
        _cbLogLevel->addItem("Info",    static_cast<int>(tgt::Info));
        _cbLogLevel->addItem("Warning", static_cast<int>(tgt::Warning));
        _cbLogLevel->addItem("Error",   static_cast<int>(tgt::Error));
        _cbLogLevel->addItem("Fatal",   static_cast<int>(tgt::Fatal));
        _cbLogLevel->setCurrentIndex(1);

        QLabel* lblLogLevel = new QLabel("Minimum Log Level:");
        lblLogLevel->setBuddy(_cbLogLevel);

        _controls_layout->addWidget(lblLogLevel);
        _controls_layout->addWidget(_cbLogLevel);

78
79
        _controls_layout->addStretch();

80
        _clear_button = new QPushButton(tr("&Clear"), this);
81
82
        _controls_layout->addWidget(_clear_button);

83
        _logDisplay = new QTextEdit(this);
84
85
86
        _logDisplay->setReadOnly(true);
        _mainLayout->addWidget(_logDisplay);

87
88
89
90
91
92
93
94
        // 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);

grunau's avatar
grunau committed
95
96
        connect(_clear_button, SIGNAL(clicked()), this, SLOT(clearMessages()));
        connect(_filter_line_edit, SIGNAL(textEdited(const QString&)), this, SLOT(filterLogMessages(const QString&)));
97
        connect(_cbLogLevel, SIGNAL(currentIndexChanged(int)), this, SLOT(onLogLevelChanged(int)));
98
99
100
101
102
103
104
105
106
107
    }

    void LogViewerWidget::init() {
        LogMgr.addLog(_log);
    }

    void LogViewerWidget::deinit() {
        LogMgr.removeLog(_log);
    }

108
    void LogViewerWidget::appendMessage(const QString& message, int level)
grunau's avatar
grunau committed
109
    {
110
111
112
        LogEntry entry = { message, level };
        _logMessages.push_back(entry);
        displayMessage(entry);
113
114
    }

115
    void LogViewerWidget::displayMessage(const LogViewerWidget::LogEntry& message)
116
    {
117
118
119
120
        int filterLevel = _cbLogLevel->itemData(_cbLogLevel->currentIndex()).toInt();
        
        if ((message._level >= filterLevel) && (_filterRegExp == 0 || _filterRegExp->indexIn(message._message) != -1)) {
            _logDisplay->append(message._message);
121
        }
grunau's avatar
grunau committed
122
123
124
125
126
127
128
129
130
131
    }

    void LogViewerWidget::clearMessages()
    {
        _logDisplay->clear();
        _logMessages.clear();
    }

    void LogViewerWidget::filterLogMessages(const QString& text)
    {
132
133
134
135
136
137
        if (text.length() == 0) {
            _filterRegExp = 0;
        } else {
            _filterRegExp = new QRegExp(text, Qt::CaseInsensitive, QRegExp::FixedString);
        }

grunau's avatar
grunau committed
138
        _logDisplay->clear();
139
        _logHighlighter->setFilterRegExp(_filterRegExp);
grunau's avatar
grunau committed
140

141
        for (std::deque<LogEntry>::iterator it = _logMessages.begin(); it != _logMessages.end(); ++it) {
142
            displayMessage(*it);
grunau's avatar
grunau committed
143
144
145
        }
    }

146
147
148
149
    void LogViewerWidget::onLogLevelChanged(int) {
        filterLogMessages(_filter_line_edit->text());
    }

150
}