insilmaril@435
|
1 |
/****************************************************************************
|
insilmaril@435
|
2 |
**
|
insilmaril@435
|
3 |
** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
|
insilmaril@435
|
4 |
**
|
insilmaril@435
|
5 |
** This file is part of the example classes of the Qt Toolkit.
|
insilmaril@435
|
6 |
**
|
insilmaril@435
|
7 |
** This file may be used under the terms of the GNU General Public
|
insilmaril@435
|
8 |
** License version 2.0 as published by the Free Software Foundation
|
insilmaril@435
|
9 |
** and appearing in the file LICENSE.GPL included in the packaging of
|
insilmaril@435
|
10 |
** this file. Please review the following information to ensure GNU
|
insilmaril@435
|
11 |
** General Public Licensing requirements will be met:
|
insilmaril@435
|
12 |
** http://www.trolltech.com/products/qt/opensource.html
|
insilmaril@435
|
13 |
**
|
insilmaril@435
|
14 |
** If you are unsure which license is appropriate for your use, please
|
insilmaril@435
|
15 |
** review the following information:
|
insilmaril@435
|
16 |
** http://www.trolltech.com/products/qt/licensing.html or contact the
|
insilmaril@435
|
17 |
** sales department at sales@trolltech.com.
|
insilmaril@435
|
18 |
**
|
insilmaril@435
|
19 |
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
insilmaril@435
|
20 |
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
insilmaril@435
|
21 |
**
|
insilmaril@435
|
22 |
****************************************************************************/
|
insilmaril@435
|
23 |
|
insilmaril@435
|
24 |
// highlighting rules have been adapted by Uwe Drechsel to match vym syntax
|
insilmaril@435
|
25 |
|
insilmaril@435
|
26 |
|
insilmaril@435
|
27 |
#include <QtGui>
|
insilmaril@435
|
28 |
|
insilmaril@435
|
29 |
#include "highlighter.h"
|
insilmaril@435
|
30 |
|
insilmaril@435
|
31 |
Highlighter::Highlighter(QTextDocument *parent)
|
insilmaril@435
|
32 |
: QSyntaxHighlighter(parent)
|
insilmaril@435
|
33 |
{
|
insilmaril@435
|
34 |
HighlightingRule rule;
|
insilmaril@435
|
35 |
|
insilmaril@435
|
36 |
keywordFormat.setForeground(Qt::darkBlue);
|
insilmaril@435
|
37 |
keywordFormat.setFontWeight(QFont::Bold);
|
insilmaril@435
|
38 |
QStringList keywordPatterns;
|
insilmaril@447
|
39 |
keywordPatterns << "\\baddBranch\\b"
|
insilmaril@447
|
40 |
<< "\\baddBranchBefore\\b"
|
insilmaril@447
|
41 |
<< "\\baddMapInsert\\b"
|
insilmaril@447
|
42 |
<< "\\baddMapReplace\\b"
|
insilmaril@447
|
43 |
<< "\\bcolorBranch\\b"
|
insilmaril@447
|
44 |
<< "\\bcolorSubtree\\b"
|
insilmaril@502
|
45 |
<< "\\bcopy\\b"
|
insilmaril@447
|
46 |
<< "\\bcut\\b"
|
insilmaril@447
|
47 |
<< "\\bdelete\\b"
|
insilmaril@502
|
48 |
<< "\\bdeleteKeepChilds\\b"
|
insilmaril@502
|
49 |
<< "\\bdeleteChilds\\b"
|
insilmaril@517
|
50 |
<< "\\bexportASCII\\b"
|
insilmaril@517
|
51 |
<< "\\bexportImage\\b"
|
insilmaril@524
|
52 |
<< "\\bexportXHTML\\b"
|
insilmaril@524
|
53 |
<< "\\bexportXML\\b"
|
insilmaril@488
|
54 |
<< "\\bimportDir\\b"
|
insilmaril@447
|
55 |
<< "\\blinkTo\\b"
|
insilmaril@447
|
56 |
<< "\\bloadImage\\b"
|
insilmaril@447
|
57 |
<< "\\bmoveBranchUp\\b"
|
insilmaril@447
|
58 |
<< "\\bmoveBranchDown\\b"
|
insilmaril@447
|
59 |
<< "\\bmove\\b"
|
insilmaril@447
|
60 |
<< "\\bmoveRel\\b"
|
insilmaril@502
|
61 |
<< "\\bnop\\b"
|
insilmaril@435
|
62 |
<< "\\bpaste\\b"
|
insilmaril@502
|
63 |
<< "\\bqa\\b"
|
insilmaril@435
|
64 |
<< "\\bsaveImage\\b"
|
insilmaril@435
|
65 |
<< "\\bscroll\\b"
|
insilmaril@447
|
66 |
<< "\\bselect\\b"
|
insilmaril@447
|
67 |
<< "\\bselectLastBranch\\b"
|
insilmaril@447
|
68 |
<< "\\bselectLastImage\\b"
|
insilmaril@482
|
69 |
<< "\\bsetFrameType\\b"
|
insilmaril@482
|
70 |
<< "\\bsetFramePenColor\\b"
|
insilmaril@482
|
71 |
<< "\\bsetFrameBrushColor\\b"
|
insilmaril@482
|
72 |
<< "\\bsetFramePadding\\b"
|
insilmaril@482
|
73 |
<< "\\bsetFrameBorderWidth\\b"
|
insilmaril@488
|
74 |
<< "\\bsetHideLinkUnselected\\b"
|
insilmaril@435
|
75 |
<< "\\bsetMapAuthor\\b"
|
insilmaril@435
|
76 |
<< "\\bsetMapComment\\b"
|
insilmaril@435
|
77 |
<< "\\bsetMapBackgroundColor\\b"
|
insilmaril@435
|
78 |
<< "\\bsetMapDefLinkColor\\b"
|
insilmaril@435
|
79 |
<< "\\bsetMapDefLinkStyle\\b"
|
insilmaril@435
|
80 |
<< "\\bsetHeading\\b"
|
insilmaril@435
|
81 |
<< "\\bsetHideExport\\b"
|
insilmaril@435
|
82 |
<< "\\bsetIncludeImagesHorizontally\\b"
|
insilmaril@435
|
83 |
<< "\\bsetIncludeImagesVertically\\b"
|
insilmaril@435
|
84 |
<< "\\bsetURL\\b"
|
insilmaril@435
|
85 |
<< "\\bsetVymLink\\b"
|
insilmaril@435
|
86 |
<< "\\bsetFlag\\b"
|
insilmaril@447
|
87 |
<< "\\btoggleFlag\\b"
|
insilmaril@435
|
88 |
<< "\\bunscroll\\b"
|
insilmaril@471
|
89 |
<< "\\bunscrollChilds\\b"
|
insilmaril@435
|
90 |
<< "\\bunsetFlag\\b"
|
insilmaril@435
|
91 |
;
|
insilmaril@435
|
92 |
foreach (QString pattern, keywordPatterns) {
|
insilmaril@435
|
93 |
rule.pattern = QRegExp(pattern);
|
insilmaril@435
|
94 |
rule.format = keywordFormat;
|
insilmaril@435
|
95 |
highlightingRules.append(rule);
|
insilmaril@435
|
96 |
}
|
insilmaril@435
|
97 |
|
insilmaril@435
|
98 |
// QT keywords
|
insilmaril@435
|
99 |
/*
|
insilmaril@435
|
100 |
classFormat.setFontWeight(QFont::Bold);
|
insilmaril@435
|
101 |
classFormat.setForeground(Qt::darkMagenta);
|
insilmaril@435
|
102 |
rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
|
insilmaril@435
|
103 |
rule.format = classFormat;
|
insilmaril@435
|
104 |
highlightingRules.append(rule);
|
insilmaril@435
|
105 |
*/
|
insilmaril@435
|
106 |
|
insilmaril@435
|
107 |
// Single line comments
|
insilmaril@435
|
108 |
singleLineCommentFormat.setForeground(Qt::red);
|
insilmaril@435
|
109 |
rule.pattern = QRegExp("#[^\n]*");
|
insilmaril@435
|
110 |
rule.format = singleLineCommentFormat;
|
insilmaril@435
|
111 |
highlightingRules.append(rule);
|
insilmaril@435
|
112 |
|
insilmaril@447
|
113 |
// multiline comments
|
insilmaril@435
|
114 |
multiLineCommentFormat.setForeground(Qt::red);
|
insilmaril@435
|
115 |
commentStartExpression = QRegExp("/\\*");
|
insilmaril@435
|
116 |
commentEndExpression = QRegExp("\\*/");
|
insilmaril@435
|
117 |
|
insilmaril@435
|
118 |
// Quotations
|
insilmaril@435
|
119 |
quotationFormat.setForeground(Qt::darkGreen);
|
insilmaril@435
|
120 |
rule.pattern = QRegExp("\".*\"");
|
insilmaril@435
|
121 |
rule.format = quotationFormat;
|
insilmaril@435
|
122 |
highlightingRules.append(rule);
|
insilmaril@435
|
123 |
|
insilmaril@435
|
124 |
QStringList valuePatterns;
|
insilmaril@435
|
125 |
valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
|
insilmaril@435
|
126 |
foreach (QString pattern, valuePatterns) {
|
insilmaril@435
|
127 |
rule.pattern = QRegExp(pattern);
|
insilmaril@435
|
128 |
rule.format = quotationFormat;
|
insilmaril@435
|
129 |
highlightingRules.append(rule);
|
insilmaril@435
|
130 |
}
|
insilmaril@435
|
131 |
|
insilmaril@435
|
132 |
|
insilmaril@435
|
133 |
|
insilmaril@435
|
134 |
// Funtions
|
insilmaril@435
|
135 |
/*
|
insilmaril@435
|
136 |
functionFormat.setFontItalic(true);
|
insilmaril@435
|
137 |
functionFormat.setForeground(Qt::blue);
|
insilmaril@435
|
138 |
rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
|
insilmaril@435
|
139 |
rule.format = functionFormat;
|
insilmaril@435
|
140 |
highlightingRules.append(rule);
|
insilmaril@435
|
141 |
*/
|
insilmaril@435
|
142 |
|
insilmaril@435
|
143 |
}
|
insilmaril@435
|
144 |
|
insilmaril@435
|
145 |
void Highlighter::highlightBlock(const QString &text)
|
insilmaril@435
|
146 |
{
|
insilmaril@435
|
147 |
foreach (HighlightingRule rule, highlightingRules) {
|
insilmaril@435
|
148 |
QRegExp expression(rule.pattern);
|
insilmaril@435
|
149 |
int index = text.indexOf(expression);
|
insilmaril@435
|
150 |
while (index >= 0) {
|
insilmaril@435
|
151 |
int length = expression.matchedLength();
|
insilmaril@435
|
152 |
setFormat(index, length, rule.format);
|
insilmaril@435
|
153 |
index = text.indexOf(expression, index + length);
|
insilmaril@435
|
154 |
}
|
insilmaril@435
|
155 |
}
|
insilmaril@435
|
156 |
setCurrentBlockState(0);
|
insilmaril@435
|
157 |
|
insilmaril@435
|
158 |
int startIndex = 0;
|
insilmaril@435
|
159 |
if (previousBlockState() != 1)
|
insilmaril@435
|
160 |
startIndex = text.indexOf(commentStartExpression);
|
insilmaril@435
|
161 |
|
insilmaril@435
|
162 |
while (startIndex >= 0) {
|
insilmaril@435
|
163 |
int endIndex = text.indexOf(commentEndExpression, startIndex);
|
insilmaril@435
|
164 |
int commentLength;
|
insilmaril@435
|
165 |
if (endIndex == -1) {
|
insilmaril@435
|
166 |
setCurrentBlockState(1);
|
insilmaril@435
|
167 |
commentLength = text.length() - startIndex;
|
insilmaril@435
|
168 |
} else {
|
insilmaril@435
|
169 |
commentLength = endIndex - startIndex
|
insilmaril@435
|
170 |
+ commentEndExpression.matchedLength();
|
insilmaril@435
|
171 |
}
|
insilmaril@435
|
172 |
setFormat(startIndex, commentLength, multiLineCommentFormat);
|
insilmaril@435
|
173 |
startIndex = text.indexOf(commentStartExpression,
|
insilmaril@435
|
174 |
startIndex + commentLength);
|
insilmaril@435
|
175 |
}
|
insilmaril@435
|
176 |
}
|