SILICON
Toggle main menu visibility
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
18
namespace
SILICON::ui
{
19
20
namespace
{
21
22
QString 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
35
bool
isBold(
const
QString& level)
36
{
37
return
(level !=
"TRACE"
) && (level !=
"DEBUG"
) && (level !=
"INFO"
);
38
}
39
40
QColor levelColor(
const
QString& level)
41
{
42
if
(level ==
"TRACE"
)
43
return
ThemeEngine::getColor
(
theme::ColorToken::Background
).darker(160);
44
if
(level ==
"DEBUG"
)
45
return
ThemeEngine::getColor
(
theme::ColorToken::Blue
);
46
if
(level ==
"INFO"
)
47
return
ThemeEngine::getColor
(
theme::ColorToken::Ink
).lighter(145);
48
if
(level ==
"WARNING"
)
49
return
ThemeEngine::getColor
(
theme::ColorToken::Orange
);
50
if
(level ==
"ERROR"
)
51
return
ThemeEngine::getColor
(
theme::ColorToken::Violet
);
52
if
(level ==
"FATAL"
|| level ==
"CRITICAL"
)
53
return
ThemeEngine::getColor
(
theme::ColorToken::Violet
).darker(150);
54
55
return
ThemeEngine::getColor
(
theme::ColorToken::Ink
);
56
}
57
58
QTextCharFormat 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
73
LogSideView::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
89
QSize
LogSideView::sizeHint
()
const
90
{
91
return
{260, 220};
92
}
93
94
QSize
LogSideView::minimumSizeHint
()
const
95
{
96
return
{180, 120};
97
}
98
99
void
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
110
void
LogSideView::clear
()
111
{
112
logOutput
->clear();
113
}
114
115
void
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) {
122
repaintLogText
();
123
}
124
}
125
126
void
LogSideView::repaintLogText
()
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
SILICON::ui::LogSideView::changeEvent
void changeEvent(QEvent *event) override
Definition
logSideView.cpp:115
SILICON::ui::LogSideView::LogSideView
LogSideView(QWidget *parent=nullptr)
Definition
logSideView.cpp:73
SILICON::ui::LogSideView::appendLine
void appendLine(const QString &line)
Definition
logSideView.cpp:99
SILICON::ui::LogSideView::minimumSizeHint
QSize minimumSizeHint() const override
Definition
logSideView.cpp:94
SILICON::ui::LogSideView::sizeHint
QSize sizeHint() const override
Definition
logSideView.cpp:89
SILICON::ui::LogSideView::repaintLogText
void repaintLogText()
Definition
logSideView.cpp:126
SILICON::ui::LogSideView::clear
void clear()
Definition
logSideView.cpp:110
SILICON::ui::LogSideView::logOutput
QTextEdit * logOutput
Definition
logSideView.hpp:28
SILICON::ui::ThemeEngine::getColor
static const QColor & getColor(theme::ColorToken token)
Definition
theme.hpp:92
logSideView.hpp
SILICON::ui::theme::ColorToken::Ink
@ Ink
Definition
theme.hpp:40
SILICON::ui::theme::ColorToken::Violet
@ Violet
Definition
theme.hpp:38
SILICON::ui::theme::ColorToken::Orange
@ Orange
Definition
theme.hpp:34
SILICON::ui::theme::ColorToken::Blue
@ Blue
Definition
theme.hpp:36
SILICON::ui::theme::ColorToken::Background
@ Background
Definition
theme.hpp:41
SILICON::ui
Definition
actionGroups.hpp:14
theme.hpp
src
ui
shell
logging
logSideView.cpp
Generated by
1.17.0