-
Notifications
You must be signed in to change notification settings - Fork 16
/
highlighter.cpp
57 lines (46 loc) · 1.7 KB
/
highlighter.cpp
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
#include "highlighter.h"
#include <QDebug>
#include <QColor>
Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
QString escaped;
QTextCharFormat strFormat;
strFormat.setFontWeight(QFont::Bold);
strFormat.setForeground(Qt::cyan);
rule.pattern = QRegularExpression(" [0-9]{1,5}.[0-9]{1,2} Mh/s");
rule.format = strFormat;
_highlightingRules.append(rule);
strFormat.setFontWeight(QFont::Bold);
strFormat.setForeground(Qt::red);
rule.pattern = QRegularExpression(" 0.00 Mh");
rule.format = strFormat;
_highlightingRules.append(rule);
escaped = QRegularExpression::escape("**Accepted");
strFormat.setForeground(Qt::green);
rule.pattern = QRegularExpression(escaped);
rule.format = strFormat;
_highlightingRules.append(rule);
escaped = QRegularExpression::escape("**Rejected");
strFormat.setForeground(Qt::red);
rule.pattern = QRegularExpression(escaped);
rule.format = strFormat;
_highlightingRules.append(rule);
escaped = QRegularExpression::escape("(stale)");
strFormat.setForeground(QColor(255, 165, 0)); //orange
rule.pattern = QRegularExpression(escaped);
rule.format = strFormat;
_highlightingRules.append(rule);
}
void Highlighter::highlightBlock(const QString &text)
{
foreach (const HighlightingRule &rule, _highlightingRules) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
setCurrentBlockState(0);
}