14 QTextCharFormat entityFormat;
15 entityFormat.setForeground(Qt::red);
16 m_formats[Entity] = entityFormat;
18 QTextCharFormat tagFormat;
19 tagFormat.setForeground(Qt::darkMagenta);
20 m_formats[Tag] = tagFormat;
22 QTextCharFormat commentFormat;
23 commentFormat.setForeground(Qt::gray);
24 commentFormat.setFontItalic(
true);
25 m_formats[Comment] = commentFormat;
27 QTextCharFormat attributeFormat;
28 attributeFormat.setForeground(Qt::black);
29 attributeFormat.setFontItalic(
true);
30 m_formats[Attribute] = attributeFormat;
32 QTextCharFormat valueFormat;
33 valueFormat.setForeground(Qt::blue);
34 m_formats[Value] = valueFormat;
36 QTextCharFormat acceleratorFormat;
37 acceleratorFormat.setFontUnderline(
true);
38 m_formats[Accelerator] = acceleratorFormat;
40 QTextCharFormat variableFormat;
41 variableFormat.setForeground(Qt::blue);
42 m_formats[Variable] = variableFormat;
49 static const QLatin1Char tab = QLatin1Char(
'\t');
50 static const QLatin1Char space = QLatin1Char(
' ');
51 static const QLatin1Char amp = QLatin1Char(
'&');
52 static const QLatin1Char endTag = QLatin1Char(
'>');
53 static const QLatin1Char quot = QLatin1Char(
'"');
54 static const QLatin1Char apos = QLatin1Char(
'\'');
55 static const QLatin1Char semicolon = QLatin1Char(
';');
56 static const QLatin1Char equals = QLatin1Char(
'=');
57 static const QLatin1Char percent = QLatin1Char(
'%');
58 static const QLatin1String startComment = QLatin1String(
"<!--");
59 static const QLatin1String endComment = QLatin1String(
"-->");
60 static const QLatin1String endElement = QLatin1String(
"/>");
62 int state = previousBlockState();
63 int len = text.size();
72 QChar ch = text.at(pos);
73 if (ch == QLatin1Char(
'<')) {
74 if (text.mid(pos, 4) == startComment) {
79 while (pos < len && text.at(pos) != space
80 && text.at(pos) != endTag
81 && text.at(pos) != tab
82 && text.mid(pos, 2) != endElement)
84 if (text.mid(pos, 2) == endElement)
86 setFormat(start, pos - start,
91 }
else if (ch == amp && pos + 1 < len) {
93 if (text.at(pos + 1).isLetterOrNumber())
94 setFormat(pos + 1, 1, m_formats[Accelerator]);
99 while (pos + 1 < len && ch != semicolon && ch.isLetterOrNumber())
102 setFormat(start, pos - start + 1, m_formats[Entity]);
103 }
else if (ch == percent) {
106 for (++pos; pos < len && text.at(pos).isDigit(); ++pos) {}
108 if (pos < len && pos == start + 1 && text.at(pos) == QLatin1Char(
'n'))
110 setFormat(start, pos - start, m_formats[Variable]);
120 if (text.mid(pos, 3) == endComment) {
128 setFormat(start, pos - start, m_formats[Comment]);
131 QChar quote = QChar::Null;
133 QChar ch = text.at(pos);
134 if (quote.isNull()) {
136 if (ch == apos || ch == quot) {
138 }
else if (ch == endTag) {
140 setFormat(start, pos - start, m_formats[Tag]);
143 }
else if (text.mid(pos, 2) == endElement) {
145 setFormat(start, pos - start, m_formats[Tag]);
148 }
else if (ch != space && text.at(pos) != tab) {
152 while (pos < len && text.at(pos) != space
153 && text.at(pos) != tab
154 && text.at(pos) != equals)
156 setFormat(start, pos - start, m_formats[Attribute]);
159 }
else if (ch == quote) {
163 setFormat(start, pos - start, m_formats[Value]);
170 setCurrentBlockState(state);
The QTextEdit class provides a widget that is used to edit and display both plain and rich text.