-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlighter.cpp
63 lines (54 loc) · 1.83 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
58
59
60
61
62
63
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** Copyright (C) 2010 Interhost AS - www.interhost.no
** Distributed under the terms of the GNU General Public License v3
**
****************************************************************************/
#include <QtGui>
#include "highlighter.h"
Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
format.setForeground(Qt::red);
format.setFontWeight(QFont::Bold);
matchWords = 0;
}
void Highlighter::highlightBlock(const QString &text, const QString &pattern, const QTextCharFormat & highlightFormat)
{
QRegExp re(pattern, Qt::CaseInsensitive);
int index = re.indexIn(text);
while (index >= 0) {
int length = re.matchedLength();
setFormat(index, length, highlightFormat);
index = re.indexIn(text, index + length);
}
}
void Highlighter::highlightBlock(const QString &text)
{
if (matchWords != 0) {
QStringList stringList = matchWords->stringList();
foreach (const QString &pattern, stringList) {
highlightBlock(text, pattern, format);
}
// Tone down likely RTF-encoding
QTextCharFormat codeFormat;
codeFormat.setForeground(Qt::lightGray);
highlightBlock(text, "\\\\[a-z0-9\\\\]+", codeFormat);
highlightBlock(text, "\\{.*\\}", codeFormat);
highlightBlock(text, "\\}", codeFormat);
}
}
void Highlighter::setModel(QStringListModel * newList)
{
if (matchWords != 0) {
matchWords->disconnect(this);
}
matchWords = newList;
connect(matchWords, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(rehighlight()));
}
void Highlighter::setColor(const QColor &color)
{
format.setForeground(color);
rehighlight();
}