-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move LogHighlighter from Gui to Core (KDAB#155)
move loghighlighter to allow use in Gui and Core classes
- Loading branch information
Showing
4 changed files
with
87 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
This file is part of Knut. | ||
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
SPDX-License-Identifier: GPL-3.0-only | ||
Contact KDAB at <info@kdab.com> for commercial licensing options. | ||
*/ | ||
|
||
#include "loghighlighter.h" | ||
|
||
#include <QTextCharFormat> | ||
#include <QTextDocument> | ||
|
||
namespace Core { | ||
|
||
LogHighlighter::LogHighlighter(QTextDocument *parent) | ||
: QSyntaxHighlighter(parent) | ||
{ | ||
auto getFormat = [](const QColor &color, const QColor &background = {}) { | ||
QTextCharFormat format; | ||
format.setForeground(color); | ||
if (background.isValid()) | ||
format.setBackground(background); | ||
return format; | ||
}; | ||
m_rules = { | ||
{"[trace]", getFormat(QColor(128, 128, 128))}, | ||
{"[debug]", getFormat(Qt::cyan)}, | ||
{"[info]", getFormat(Qt::green)}, | ||
{"[warning]", getFormat(QColor(255, 220, 0))}, | ||
{"[error]", getFormat(Qt::red)}, | ||
{"[critical]", getFormat(Qt::white, Qt::red)}, | ||
}; | ||
} | ||
|
||
void LogHighlighter::highlightBlock(const QString &text) | ||
{ | ||
for (const auto &rule : std::as_const(m_rules)) { | ||
if (int start = text.indexOf(rule.m_keyword); start != -1) | ||
setFormat(start + 1, rule.m_keyword.length() - 2, rule.m_format); | ||
} | ||
} | ||
|
||
} // namespace Core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
This file is part of Knut. | ||
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
SPDX-License-Identifier: GPL-3.0-only | ||
Contact KDAB at <info@kdab.com> for commercial licensing options. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <QSyntaxHighlighter> | ||
|
||
class QTextDocument; | ||
class QTextCharFormat; | ||
|
||
namespace Core { | ||
|
||
class LogHighlighter : public QSyntaxHighlighter | ||
{ | ||
public: | ||
explicit LogHighlighter(QTextDocument *parent = nullptr); | ||
|
||
protected: | ||
void highlightBlock(const QString &text) override; | ||
|
||
private: | ||
struct HighlightingRule | ||
{ | ||
QString m_keyword; | ||
QTextCharFormat m_format; | ||
}; | ||
QList<HighlightingRule> m_rules; | ||
}; | ||
|
||
} // namespace Core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters