author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 08 Apr 2010 14:19:33 +0300 | |
branch | RCL_3 |
changeset 8 | 3f74d0d4af4c |
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 Qt Designer 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 "csshighlighter_p.h" |
|
43 |
||
44 |
QT_BEGIN_NAMESPACE |
|
45 |
||
46 |
namespace qdesigner_internal { |
|
47 |
||
48 |
CssHighlighter::CssHighlighter(QTextDocument *document) |
|
49 |
: QSyntaxHighlighter(document) |
|
50 |
{ |
|
51 |
} |
|
52 |
||
53 |
void CssHighlighter::highlightBlock(const QString& text) |
|
54 |
{ |
|
55 |
enum Token { ALNUM, LBRACE, RBRACE, COLON, SEMICOLON, COMMA, QUOTE, SLASH, STAR }; |
|
56 |
static const int transitions[10][9] = { |
|
57 |
{ Selector, Property, Selector, Pseudo, Property, Selector, Quote, MaybeComment, Selector }, // Selector |
|
58 |
{ Property, Property, Selector, Value, Property, Property, Quote, MaybeComment, Property }, // Property |
|
59 |
{ Value, Property, Selector, Value, Property, Value, Quote, MaybeComment, Value }, // Value |
|
60 |
{ Pseudo1, Property, Selector, Pseudo2, Selector, Selector, Quote, MaybeComment, Pseudo }, // Pseudo |
|
61 |
{ Pseudo1, Property, Selector, Pseudo, Selector, Selector, Quote, MaybeComment, Pseudo1 }, // Pseudo1 |
|
62 |
{ Pseudo2, Property, Selector, Pseudo, Selector, Selector, Quote, MaybeComment, Pseudo2 }, // Pseudo2 |
|
63 |
{ Quote, Quote, Quote, Quote, Quote, Quote, -1, Quote, Quote }, // Quote |
|
64 |
{ -1, -1, -1, -1, -1, -1, -1, -1, Comment }, // MaybeComment |
|
65 |
{ Comment, Comment, Comment, Comment, Comment, Comment, Comment, Comment, MaybeCommentEnd }, // Comment |
|
66 |
{ Comment, Comment, Comment, Comment, Comment, Comment, Comment, -1, MaybeCommentEnd } // MaybeCommentEnd |
|
67 |
}; |
|
68 |
||
69 |
int lastIndex = 0; |
|
70 |
bool lastWasSlash = false; |
|
71 |
int state = previousBlockState(), save_state; |
|
72 |
if (state == -1) { |
|
73 |
// As long as the text is empty, leave the state undetermined |
|
74 |
if (text.isEmpty()) { |
|
75 |
setCurrentBlockState(-1); |
|
76 |
return; |
|
77 |
} |
|
78 |
// The initial state is based on the precense of a : and the absense of a {. |
|
79 |
// This is because Qt style sheets support both a full stylesheet as well as |
|
80 |
// an inline form with just properties. |
|
81 |
state = save_state = (text.indexOf(QLatin1Char(':')) > -1 && |
|
82 |
text.indexOf(QLatin1Char('{')) == -1) ? Property : Selector; |
|
83 |
} else { |
|
84 |
save_state = state>>16; |
|
85 |
state &= 0x00ff; |
|
86 |
} |
|
87 |
||
88 |
if (state == MaybeCommentEnd) { |
|
89 |
state = Comment; |
|
90 |
} else if (state == MaybeComment) { |
|
91 |
state = save_state; |
|
92 |
} |
|
93 |
||
94 |
for (int i = 0; i < text.length(); i++) { |
|
95 |
int token = ALNUM; |
|
96 |
const QChar c = text.at(i); |
|
97 |
const char a = c.toAscii(); |
|
98 |
||
99 |
if (state == Quote) { |
|
100 |
if (a == '\\') { |
|
101 |
lastWasSlash = true; |
|
102 |
} else { |
|
103 |
if (a == '\"' && !lastWasSlash) { |
|
104 |
token = QUOTE; |
|
105 |
} |
|
106 |
lastWasSlash = false; |
|
107 |
} |
|
108 |
} else { |
|
109 |
switch (a) { |
|
110 |
case '{': token = LBRACE; break; |
|
111 |
case '}': token = RBRACE; break; |
|
112 |
case ':': token = COLON; break; |
|
113 |
case ';': token = SEMICOLON; break; |
|
114 |
case ',': token = COMMA; break; |
|
115 |
case '\"': token = QUOTE; break; |
|
116 |
case '/': token = SLASH; break; |
|
117 |
case '*': token = STAR; break; |
|
118 |
default: break; |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
int new_state = transitions[state][token]; |
|
123 |
||
124 |
if (new_state != state) { |
|
125 |
bool include_token = new_state == MaybeCommentEnd || (state == MaybeCommentEnd && new_state!= Comment) |
|
126 |
|| state == Quote; |
|
127 |
highlight(text, lastIndex, i-lastIndex+include_token, state); |
|
128 |
||
129 |
if (new_state == Comment) { |
|
130 |
lastIndex = i-1; // include the slash and star |
|
131 |
} else { |
|
132 |
lastIndex = i + ((token == ALNUM || new_state == Quote) ? 0 : 1); |
|
133 |
} |
|
134 |
} |
|
135 |
||
136 |
if (new_state == -1) { |
|
137 |
state = save_state; |
|
138 |
} else if (state <= Pseudo2) { |
|
139 |
save_state = state; |
|
140 |
state = new_state; |
|
141 |
} else { |
|
142 |
state = new_state; |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
highlight(text, lastIndex, text.length() - lastIndex, state); |
|
147 |
setCurrentBlockState(state + (save_state<<16)); |
|
148 |
} |
|
149 |
||
150 |
void CssHighlighter::highlight(const QString &text, int start, int length, int state) |
|
151 |
{ |
|
152 |
if (start >= text.length() || length <= 0) |
|
153 |
return; |
|
154 |
||
155 |
QTextCharFormat format; |
|
156 |
||
157 |
switch (state) { |
|
158 |
case Selector: |
|
159 |
setFormat(start, length, Qt::darkRed); |
|
160 |
break; |
|
161 |
case Property: |
|
162 |
setFormat(start, length, Qt::blue); |
|
163 |
break; |
|
164 |
case Value: |
|
165 |
setFormat(start, length, Qt::black); |
|
166 |
break; |
|
167 |
case Pseudo1: |
|
168 |
setFormat(start, length, Qt::darkRed); |
|
169 |
break; |
|
170 |
case Pseudo2: |
|
171 |
setFormat(start, length, Qt::darkRed); |
|
172 |
break; |
|
173 |
case Quote: |
|
174 |
setFormat(start, length, Qt::darkMagenta); |
|
175 |
break; |
|
176 |
case Comment: |
|
177 |
case MaybeCommentEnd: |
|
178 |
format.setForeground(Qt::darkGreen); |
|
179 |
setFormat(start, length, format); |
|
180 |
break; |
|
181 |
default: |
|
182 |
break; |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
} // namespace qdesigner_internal |
|
187 |
||
188 |
QT_END_NAMESPACE |