author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 15 Mar 2010 12:43:09 +0200 | |
branch | RCL_3 |
changeset 6 | dee5afe5301f |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <QtGui> |
|
43 |
||
44 |
#include "highlighter.h" |
|
45 |
||
46 |
//! [0] |
|
47 |
Highlighter::Highlighter(QTextDocument *parent) |
|
48 |
: QSyntaxHighlighter(parent) |
|
49 |
{ |
|
50 |
HighlightingRule rule; |
|
51 |
||
52 |
keywordFormat.setForeground(Qt::darkBlue); |
|
53 |
keywordFormat.setFontWeight(QFont::Bold); |
|
54 |
QStringList keywordPatterns; |
|
55 |
keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b" |
|
56 |
<< "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b" |
|
57 |
<< "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b" |
|
58 |
<< "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b" |
|
59 |
<< "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b" |
|
60 |
<< "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b" |
|
61 |
<< "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b" |
|
62 |
<< "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b" |
|
63 |
<< "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b" |
|
64 |
<< "\\bvoid\\b" << "\\bvolatile\\b"; |
|
65 |
foreach (const QString &pattern, keywordPatterns) { |
|
66 |
rule.pattern = QRegExp(pattern); |
|
67 |
rule.format = keywordFormat; |
|
68 |
highlightingRules.append(rule); |
|
69 |
//! [0] //! [1] |
|
70 |
} |
|
71 |
//! [1] |
|
72 |
||
73 |
//! [2] |
|
74 |
classFormat.setFontWeight(QFont::Bold); |
|
75 |
classFormat.setForeground(Qt::darkMagenta); |
|
76 |
rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b"); |
|
77 |
rule.format = classFormat; |
|
78 |
highlightingRules.append(rule); |
|
79 |
//! [2] |
|
80 |
||
81 |
//! [3] |
|
82 |
singleLineCommentFormat.setForeground(Qt::red); |
|
83 |
rule.pattern = QRegExp("//[^\n]*"); |
|
84 |
rule.format = singleLineCommentFormat; |
|
85 |
highlightingRules.append(rule); |
|
86 |
||
87 |
multiLineCommentFormat.setForeground(Qt::red); |
|
88 |
//! [3] |
|
89 |
||
90 |
//! [4] |
|
91 |
quotationFormat.setForeground(Qt::darkGreen); |
|
92 |
rule.pattern = QRegExp("\".*\""); |
|
93 |
rule.format = quotationFormat; |
|
94 |
highlightingRules.append(rule); |
|
95 |
//! [4] |
|
96 |
||
97 |
//! [5] |
|
98 |
functionFormat.setFontItalic(true); |
|
99 |
functionFormat.setForeground(Qt::blue); |
|
100 |
rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()"); |
|
101 |
rule.format = functionFormat; |
|
102 |
highlightingRules.append(rule); |
|
103 |
//! [5] |
|
104 |
||
105 |
//! [6] |
|
106 |
commentStartExpression = QRegExp("/\\*"); |
|
107 |
commentEndExpression = QRegExp("\\*/"); |
|
108 |
} |
|
109 |
//! [6] |
|
110 |
||
111 |
//! [7] |
|
112 |
void Highlighter::highlightBlock(const QString &text) |
|
113 |
{ |
|
114 |
foreach (const HighlightingRule &rule, highlightingRules) { |
|
115 |
QRegExp expression(rule.pattern); |
|
116 |
int index = expression.indexIn(text); |
|
117 |
while (index >= 0) { |
|
118 |
int length = expression.matchedLength(); |
|
119 |
setFormat(index, length, rule.format); |
|
120 |
index = expression.indexIn(text, index + length); |
|
121 |
} |
|
122 |
} |
|
123 |
//! [7] //! [8] |
|
124 |
setCurrentBlockState(0); |
|
125 |
//! [8] |
|
126 |
||
127 |
//! [9] |
|
128 |
int startIndex = 0; |
|
129 |
if (previousBlockState() != 1) |
|
130 |
startIndex = commentStartExpression.indexIn(text); |
|
131 |
||
132 |
//! [9] //! [10] |
|
133 |
while (startIndex >= 0) { |
|
134 |
//! [10] //! [11] |
|
135 |
int endIndex = commentEndExpression.indexIn(text, startIndex); |
|
136 |
int commentLength; |
|
137 |
if (endIndex == -1) { |
|
138 |
setCurrentBlockState(1); |
|
139 |
commentLength = text.length() - startIndex; |
|
140 |
} else { |
|
141 |
commentLength = endIndex - startIndex |
|
142 |
+ commentEndExpression.matchedLength(); |
|
143 |
} |
|
144 |
setFormat(startIndex, commentLength, multiLineCommentFormat); |
|
145 |
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength); |
|
146 |
} |
|
147 |
} |
|
148 |
//! [11] |