-
Notifications
You must be signed in to change notification settings - Fork 20
/
phonetichighlighter.cpp
49 lines (31 loc) · 1.2 KB
/
phonetichighlighter.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
#include "phonetichighlighter.h"
PhoneticHighlighter::PhoneticHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
{
QString MatchExp = "\\{(\\s*?.*?)*?\\}";
PhonemeFormat.setForeground(Qt::magenta);
PhonemeFormat.setFontWeight(QFont::Bold);
PhonemeExp = QRegularExpression(MatchExp);
QString SingleExp = "@.\\S*";
SinglePhonemeExp = QRegularExpression(SingleExp);
QString LongExp = "\\b\\w{23,}";
TooLongExp = QRegularExpression(LongExp);
ErrorFormat = PhonemeFormat;
ErrorFormat.setForeground(Qt::red);
ErrorFormat.setBackground(Qt::black);
}
void PhoneticHighlighter::highlightBlock(const QString &text)
{
// Phoneme
HighlightRegex(text,PhonemeExp,PhonemeFormat);
HighlightRegex(text,SinglePhonemeExp,PhonemeFormat);
// Error
HighlightRegex(text,TooLongExp,ErrorFormat);
}
void PhoneticHighlighter::HighlightRegex(const QString& Text,const QRegularExpression &Reg, const QTextCharFormat &Fmt)
{
QRegularExpressionMatchIterator MatchIter = Reg.globalMatch(Text);
while (MatchIter.hasNext()) {
QRegularExpressionMatch match = MatchIter.next();
setFormat(match.capturedStart(), match.capturedLength(), Fmt);
}
}