Last change
on this file since 1194 was 1110, checked in by s10k, 6 years ago |
slightly modifications to allow subclassing, added Q_OBJECT macro, since usually all classes derived from QObject should use it
|
File size:
1.2 KB
|
Line | |
---|
1 | #include "highlighter.h"
|
---|
2 | #include <QRegExp>
|
---|
3 | #include <QTextCharFormat>
|
---|
4 | #include <QTextDocument>
|
---|
5 |
|
---|
6 | Highlighter::Highlighter(QTextDocument *parent)
|
---|
7 | : QSyntaxHighlighter(parent)
|
---|
8 | {
|
---|
9 | HighlightingRule rule;
|
---|
10 |
|
---|
11 | //numbers
|
---|
12 | rule.pattern = QRegExp("([-0-9.]+)(?!([^\"]*\"[\\s]*\\:))");
|
---|
13 | rule.format.setForeground(QColor(255,192,85));
|
---|
14 | rules.append(rule);
|
---|
15 |
|
---|
16 | //key
|
---|
17 | rule.pattern = QRegExp ("(\"[^\"]*\")");
|
---|
18 | rule.format.setForeground(QColor(145,145,192));
|
---|
19 | rules.append(rule);
|
---|
20 |
|
---|
21 | //value
|
---|
22 | rule.pattern = QRegExp(":\\s*([\"](?:[^\"])*[\"])");
|
---|
23 | rule.format.setForeground(QColor(145,145,192));
|
---|
24 | rules.append(rule);
|
---|
25 |
|
---|
26 | //reserved words
|
---|
27 | rule.pattern = QRegExp("(true|false|null)(?!\"[^\"]*\")");
|
---|
28 | rule.format.setForeground(QColor(0,0,255));
|
---|
29 | rules.append(rule);
|
---|
30 | }
|
---|
31 |
|
---|
32 | void Highlighter::highlightBlock(const QString &text)
|
---|
33 | {
|
---|
34 | foreach (const HighlightingRule &rule, rules) {
|
---|
35 | QRegExp expression(rule.pattern);
|
---|
36 | int index = expression.indexIn(text);
|
---|
37 |
|
---|
38 | while (index >= 0) {
|
---|
39 | index = expression.pos(1);
|
---|
40 | int length = expression.cap(1).length();
|
---|
41 | setFormat(index, length, rule.format);
|
---|
42 | index = expression.indexIn(text, index + length);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.