-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcwidgetlogbox.cpp
More file actions
155 lines (112 loc) · 4.59 KB
/
cwidgetlogbox.cpp
File metadata and controls
155 lines (112 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "cwidgetlogbox.h"
CWidgetLogBox::CWidgetLogBox(QWidget *parent) : QWidget(parent)
{
QGridLayout *qGridLayoutMain = new QGridLayout();
//qGridLayoutMain->setSizeConstraint(QLayout::SetFixedSize);
//(0,0) Log Box
qTextEditLog = new QTextEdit(this);
qTextEditLog->setReadOnly(true);
qGridLayoutMain->addWidget(qTextEditLog, 0, 0, 1, 2);
//(8,0) Button Clear log
qPushButtonClearLog = new QPushButton(this);
qPushButtonClearLog->setText("Clear Log");
qGridLayoutMain->addWidget(qPushButtonClearLog, 1, 0, 2, 1);
//(8,2) Button Save log
qPushButtonSaveLog = new QPushButton(this);
qPushButtonSaveLog->setText("Save Log");
qGridLayoutMain->addWidget(qPushButtonSaveLog, 1, 1, 2, 1);
//Finish layout setting
setLayout(qGridLayoutMain);
//Apply High lighter
CHighlighter *mHighlighter = new CHighlighter(qTextEditLog->document());
qTextEditLog->append("[FAIL]:Test");
qTextEditLog->append("[PASS]:Test");
qTextEditLog->append("[ERROR]:Test");
qTextEditLog->append("[WARNING]:Test");
qTextEditLog->append("[STATUS]:Test");
//Connect signal
connect(qPushButtonClearLog, SIGNAL(clicked()), this, SLOT(PublicSlotClearLog()));
connect(qPushButtonSaveLog, SIGNAL(clicked()), this, SLOT(PublicSlotSaveLog()));
}
void CWidgetLogBox::PublicSlotNewLog(QString qStringLog)
{
//Add timestamp
qStringLog = "[" + (QTime::currentTime().toString()) + "] " + qStringLog;
//Add to log box
qTextEditLog->append(qStringLog);
}
void CWidgetLogBox::PublicSlotUpdateLog(QString qStringLog)
{
QTextCursor* qTextCursorLog = new QTextCursor(qTextEditLog->document());
//Move to the end of the document
qTextCursorLog->movePosition(QTextCursor::End, QTextCursor::MoveAnchor, 1);
qTextCursorLog->movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor, 1);
//Change Text
qTextCursorLog->insertText(qStringLog);
}
void CWidgetLogBox::PublicSlotSaveLog(void)
{
QString qStringLog = qTextEditLog->toPlainText();
QString qStringFilename = QFileDialog::getSaveFileName(
this,
"Open Document",
QDir::currentPath(),
"All files (*.*) ;; Document files (*.doc *.rtf);; PNG files (*.png)");
QFile QFileSaved(qStringFilename);
if (!QFileSaved.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream out(&QFileSaved);
out << qStringLog;
}
void CWidgetLogBox::PublicSlotClearLog()
{
qTextEditLog->clear();
}
CHighlighter::CHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule mRule;
//Fail syntax
qTextCharFormatPrefixFailFormat.setForeground(Qt::red);
qTextCharFormatPrefixFailFormat.setFontWeight(QFont::Bold);
mRule.qRegExpPattern = QRegExp("\\bFAIL\\b");
mRule.qTextCharFormat = qTextCharFormatPrefixFailFormat;
qVectorHighlightingRules.append(mRule);
//Pass Syntax
qTextCharFormatPrefixPassFormat.setForeground(Qt::green);
qTextCharFormatPrefixPassFormat.setFontWeight(QFont::Bold);
mRule.qRegExpPattern = QRegExp("\\bPASS\\b");
mRule.qTextCharFormat = qTextCharFormatPrefixPassFormat;
qVectorHighlightingRules.append(mRule);
//Error Syntax
qTextCharFormatPrefixErrorFormat.setForeground(Qt::darkMagenta);
qTextCharFormatPrefixErrorFormat.setFontWeight(QFont::Bold);
mRule.qRegExpPattern = QRegExp("\\bERROR\\b");
mRule.qTextCharFormat = qTextCharFormatPrefixErrorFormat;
qVectorHighlightingRules.append(mRule);
//Warning Syntax
qTextCharFormatPrefixWarningFormat.setForeground(Qt::darkYellow);
qTextCharFormatPrefixWarningFormat.setFontWeight(QFont::Bold);
mRule.qRegExpPattern = QRegExp("\\bWARNING\\b");
mRule.qTextCharFormat = qTextCharFormatPrefixWarningFormat;
qVectorHighlightingRules.append(mRule);
//Status Syntax
qTextCharFormatPrefixStatusFormat.setForeground(Qt::darkBlue);
qTextCharFormatPrefixStatusFormat.setFontWeight(QFont::Bold);
mRule.qRegExpPattern = QRegExp("\\bSTATUS\\b");
mRule.qTextCharFormat = qTextCharFormatPrefixStatusFormat;
qVectorHighlightingRules.append(mRule);
}
void CHighlighter::highlightBlock(const QString &text)
{
foreach (const HighlightingRule &rule, qVectorHighlightingRules) {
QRegExp expression(rule.qRegExpPattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.qTextCharFormat);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
}