loghighlighter.h 4.07 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-2015, all rights reserved,
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, 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
// 
// ================================================================================================

#ifndef LOGHIGHLIGHTER_H__
#define LOGHIGHLIGHTER_H__

28
#include "application/applicationapi.h"
29
30
31
32
33
#include <QTextEdit>
#include <QSyntaxHighlighter>

namespace campvis {

34
    class CAMPVIS_APPLICATION_API LogHighlighter : public QSyntaxHighlighter {
35

36
        Q_OBJECT
37
38
39
40
41
42
43
44
45
46

    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
         */
47
        explicit LogHighlighter(QTextEdit* parent);
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

        /**
         * 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.
         *
67
68
         * LogHighlighter takes ownership of the given regexp.
         *
69
70
71
72
73
74
75
76
77
78
         * \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
         */
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        void highlightFilterMatches(const QString& text);

        /**
         * Highlight log level information in the given log block.
         *
         * \param text the log block to highlight
         * \param offset the offset at which log level matching should start
         */
        void highlightLogLevel(const QString& text, int offset);

        /**
         * Highlight text matching the provided regexp in the given log block.
         *
         * \param text the log block to highlight
         * \param offset the offset at which matching should start
         * \param regExp the regular expression to match
         * \param format the format used to highlight matches
         * \return end of the matched string (the new offset), or -1 if nothing was matched
         */
        int highlightRegExp(const QString& text, int offset, const QRegExp& regExp, const QTextCharFormat& format);
99

100
101
102
103
104
        const QRegExp* _filterRegExp;                                     ///< Current filter regexp
        const QRegExp* _logLevelRegExp;                                   ///< Regexp matching log level strings
        QTextCharFormat _filterMatchFormat;                               ///< Format for highlighting filter matches
        std::list< std::pair<QRegExp, QTextCharFormat> > _rules;          ///< Set of general highlighting rules
        std::list< std::pair<QString, QTextCharFormat> > _logLevelRules;  ///< Highlighting rules for log levels
105
106
107
108
    };
}

#endif // LOGHIGHLIGHTER_H__