Skip to content

Commit

Permalink
fix: move LogHighlighter from Gui to Core (KDAB#155)
Browse files Browse the repository at this point in the history
move loghighlighter to allow use in Gui and Core classes
  • Loading branch information
smnppKDAB authored Aug 29, 2024
1 parent 98918ab commit 223f682
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 42 deletions.
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ set(PROJECT_SOURCES
lsp_utils.cpp
logger.h
logger.cpp
loghighlighter.cpp
loghighlighter.h
mark_p.h
mark.h
mark.cpp
Expand Down
46 changes: 46 additions & 0 deletions src/core/loghighlighter.cpp
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
37 changes: 37 additions & 0 deletions src/core/loghighlighter.h
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
44 changes: 2 additions & 42 deletions src/gui/logpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

#include "logpanel.h"
#include "core/loghighlighter.h"
#include "guisettings.h"
#include "utils/log.h"

Expand All @@ -26,47 +27,6 @@

namespace Gui {

class LogHighlighter : public QSyntaxHighlighter
{
public:
explicit LogHighlighter(QTextDocument *parent = nullptr)
: 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)},
};
}

protected:
void highlightBlock(const QString &text) override
{
for (const auto &rule : std::as_const(m_rules)) {
if (int start = text.indexOf(rule.keyword); start != -1)
setFormat(start + 1, rule.keyword.length() - 2, rule.format);
}
}

private:
struct HighlightingRule
{
QString keyword;
QTextCharFormat format;
};
QList<HighlightingRule> m_rules;
};

LogPanel::LogPanel(QWidget *parent)
: QPlainTextEdit(parent)
, m_toolBar(new QWidget)
Expand All @@ -79,7 +39,7 @@ LogPanel::LogPanel(QWidget *parent)
logger->sinks().push_back(std::make_shared<spdlog::sinks::qt_sink_mt>(this, "appendPlainText"));

// Setup text edit
new LogHighlighter(document());
new Core::LogHighlighter(document());
setWordWrapMode(QTextOption::NoWrap);
GuiSettings::setupTextEdit(this);

Expand Down

0 comments on commit 223f682

Please sign in to comment.