SILICON
Loading...
Searching...
No Matches
logSideView.cpp
Go to the documentation of this file.
1#include "logSideView.hpp"
2
3#include <QEvent>
4#include <QFont>
5#include <QFrame>
6#include <QRegularExpression>
7#include <QSignalBlocker>
8#include <QSizePolicy>
9#include <QTextBlock>
10#include <QTextCharFormat>
11#include <QTextCursor>
12#include <QTextEdit>
13#include <QVBoxLayout>
14
15#include <ui/shell/theme.hpp>
16
17
18namespace SILICON::ui {
19
20namespace {
21
22QString getLevel(const QString& line)
23{
24 static const QRegularExpression levelPattern(R"(^\‍[[^\‍]]+\‍]\s+\‍[([^\‍]]+)\‍])",
25 QRegularExpression::CaseInsensitiveOption);
26
27 const auto match = levelPattern.match(line);
28
29 if (match.hasMatch())
30 return match.captured(1).toUpper();
31
32 return "INFO";
33}
34
35bool isBold(const QString& level)
36{
37 return (level != "TRACE") && (level != "DEBUG") && (level != "INFO");
38}
39
40QColor levelColor(const QString& level)
41{
42 if (level == "TRACE")
44 if (level == "DEBUG")
46 if (level == "INFO")
48 if (level == "WARNING")
50 if (level == "ERROR")
52 if (level == "FATAL" || level == "CRITICAL")
54
56}
57
58QTextCharFormat formatForLine(const QString& line)
59{
60 QTextCharFormat format;
61 const QString level = getLevel(line);
62
63 format.setForeground(levelColor(level));
64
65 if (isBold(level))
66 format.setFontWeight(QFont::Bold);
67
68 return format;
69}
70
71} // namespace
72
73LogSideView::LogSideView(QWidget* parent) : QWidget(parent)
74{
75 auto* layout = new QVBoxLayout(this);
76 layout->setContentsMargins(10, 10, 10, 0);
77
78 logOutput = new QTextEdit(this);
79 logOutput->setReadOnly(true);
80 logOutput->setUndoRedoEnabled(false);
81 logOutput->setFrameStyle(QFrame::NoFrame);
82 logOutput->document()->setMaximumBlockCount(5000);
83 logOutput->document()->setDocumentMargin(6);
84 logOutput->setProperty("class", "mono");
85 logOutput->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
86 layout->addWidget(logOutput);
87}
88
90{
91 return {260, 220};
92}
93
95{
96 return {180, 120};
97}
98
99void LogSideView::appendLine(const QString& line)
100{
101 QTextCursor cursor(logOutput->document());
102 cursor.movePosition(QTextCursor::End);
103 cursor.insertText(line, formatForLine(line));
104 cursor.insertBlock();
105
106 logOutput->setTextCursor(cursor);
107 logOutput->ensureCursorVisible();
108}
109
111{
112 logOutput->clear();
113}
114
115void LogSideView::changeEvent(QEvent* event)
116{
117 QWidget::changeEvent(event);
118
119 if (event->type() == QEvent::PaletteChange
120 || event->type() == QEvent::ApplicationPaletteChange
121 || event->type() == QEvent::StyleChange) {
123 }
124}
125
127{
128 const QSignalBlocker blocker(logOutput);
129 QTextCursor cursor(logOutput->document());
130
131 for (QTextBlock block = logOutput->document()->begin(); block.isValid();
132 block = block.next()) {
133 const QString line = block.text();
134 if (line.isEmpty())
135 continue;
136
137 cursor.setPosition(block.position());
138 cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
139 cursor.setCharFormat(formatForLine(line));
140 }
141
142 logOutput->viewport()->update();
143}
144
145} // namespace SILICON::ui
void changeEvent(QEvent *event) override
LogSideView(QWidget *parent=nullptr)
void appendLine(const QString &line)
QSize minimumSizeHint() const override
QSize sizeHint() const override
static const QColor & getColor(theme::ColorToken token)
Definition theme.hpp:92