-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhighlighter.cpp
More file actions
92 lines (80 loc) · 2.76 KB
/
Copy pathhighlighter.cpp
File metadata and controls
92 lines (80 loc) · 2.76 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
#include "highlighter.h"
Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
// first title
setupFormat(firsecTitleFormat,Qt::red,QFont::Bold,PT::FIRST_TITLE);
// second title
setupFormat(firsecTitleFormat,Qt::darkBlue,QFont::Bold,PT::SECOND_TITLE);
// third title
setupFormat(thiforTitleFormat,Qt::blue,QFont::Bold,PT::THIRD_TITLE);
// forth title
setupFormat(thiforTitleFormat,Qt::blue,QFont::Normal,PT::FORTH_TITLE);
// italic
italicFormat.setFontItalic(true);
setupFormat(italicFormat,Qt::darkGray,QFont::Normal,PT::ITALIC);
// bold
setupFormat(boldFormat,Qt::darkGreen,QFont::Bold,PT::BOLD);
// email address
emailFormat.setFontItalic(true);
emailFormat.setFontUnderline(true);
setupFormat(emailFormat,Qt::black,QFont::Normal,PT::EMAIL);
// url link
linkFormat.setFontItalic(true);
linkFormat.setFontUnderline(true);
setupFormat(linkFormat,Qt::darkYellow,QFont::Normal,PT::LINK);
// horizon line
setupFormat(hrFormat,Qt::blue,QFont::Normal,PT::HR);
// string
setupFormat(stringFormat,Qt::darkYellow,QFont::Normal,PT::STRING);
qDebug("rules length %d" , highlightingRules.length());
regQuateStart = regex(PT::QUOTE_START);
regQuateEnd = regex(PT::QUOTE_END);
quoteFormat.setForeground(Qt::green);
quoteFormat.setFontWeight(QFont::Normal);
}
void Highlighter::setupFormat(QTextCharFormat &format, Qt::GlobalColor color,
QFont::Weight fontWeight, const char *pattern)
{
format.setForeground(color);
format.setFontWeight(fontWeight);
rule.format = format;
rule.pattern = boost::regex(pattern);
highlightingRules.append(rule);
}
void Highlighter::highlightBlock(const QString &text)
{
QList<HighlightingRule>::iterator iter;
const char *str;
QByteArray ba = text.toLatin1(); //全部转化为单字节编码
cmatch what;
for (iter = highlightingRules.begin(); iter!=highlightingRules.end(); iter++)
{
str = ba.data();
int index = 0;
while(regex_search(str, what, (*iter).pattern))
{
size_t i = 0;
index = index + what.position(i);
setFormat(index , what.length(i), (*iter).format);
str = str + what.position(i)+what.length(i);
index += what.length(i);
}
}
// match quote block
setCurrentBlockState(STATE_NORMAL);
str = ba.data();
if(regex_search(str, what, regQuateStart))
{
setCurrentBlockState(STATE_QUOTE);
}
if(previousBlockState() == STATE_QUOTE)
{
if(strlen(str) != 0)
setCurrentBlockState(STATE_QUOTE);
}
if(currentBlockState() == STATE_QUOTE)
{
setFormat(0,text.length(), quoteFormat);
}
}