9 #include "fwGuiQt/highlighter/CppHighlighter.hpp" 16 CppHighlighter::CppHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
18 HighlightingRule rule;
20 QTextCharFormat keywordFormat;
21 keywordFormat.setForeground(Qt::darkBlue);
22 keywordFormat.setFontWeight(QFont::Bold);
23 QStringList keywordPatterns;
24 keywordPatterns <<
"char" <<
"class" <<
"const" 25 <<
"double" <<
"enum" <<
"explicit" 26 <<
"friend" <<
"inline" <<
"int" 27 <<
"long" <<
"namespace" <<
"operator" 28 <<
"private" <<
"protected" <<
"public" 29 <<
"short" <<
"signals" <<
"signed" 30 <<
"slots" <<
"static" <<
"struct" 31 <<
"template" <<
"typedef" <<
"typename" 32 <<
"union" <<
"unsigned" <<
"virtual" 33 <<
"void" <<
"volatile";
34 for(
const QString &pattern : keywordPatterns)
36 rule.pattern = QRegExp(
"\\b" + pattern +
"\\b");
37 rule.format = keywordFormat;
38 highlightingRules.append(rule);
41 QTextCharFormat classFormat;
42 classFormat.setFontWeight(QFont::Bold);
43 classFormat.setForeground(Qt::darkMagenta);
44 rule.pattern = QRegExp(
"\\bQ[A-Za-z]+\\b");
45 rule.format = classFormat;
46 highlightingRules.append(rule);
48 QTextCharFormat singleLineCommentFormat;
49 singleLineCommentFormat.setForeground(Qt::red);
50 rule.pattern = QRegExp(
"//[^\n]*");
51 rule.format = singleLineCommentFormat;
52 highlightingRules.append(rule);
54 multiLineCommentFormat.setForeground(Qt::red);
56 QTextCharFormat quotationFormat;
57 quotationFormat.setForeground(Qt::darkGreen);
58 rule.pattern = QRegExp(
"\".*\"");
59 rule.format = quotationFormat;
60 highlightingRules.append(rule);
62 QTextCharFormat functionFormat;
63 functionFormat.setFontItalic(
true);
64 functionFormat.setForeground(Qt::blue);
65 rule.pattern = QRegExp(
"\\b[A-Za-z0-9_]+(?=\\()");
66 rule.format = functionFormat;
67 highlightingRules.append(rule);
69 commentStartExpression = QRegExp(
"/\\*");
70 commentEndExpression = QRegExp(
"\\*/");
75 void CppHighlighter::highlightBlock(
const QString &text)
77 for(
const HighlightingRule &rule : highlightingRules)
79 QRegExp expression(rule.pattern);
80 int index = expression.indexIn(text);
83 int length = expression.matchedLength();
84 setFormat(index, length, rule.format);
85 index = expression.indexIn(text, index + length);
88 setCurrentBlockState(0);
91 if (previousBlockState() != 1)
93 startIndex = commentStartExpression.indexIn(text);
96 while (startIndex >= 0)
98 int endIndex = commentEndExpression.indexIn(text, startIndex);
102 setCurrentBlockState(1);
103 commentLength = text.length() - startIndex;
107 commentLength = endIndex - startIndex
108 + commentEndExpression.matchedLength();
110 setFormat(startIndex, commentLength, multiLineCommentFormat);
111 startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
The namespace fwGuiQt contains classes which provide the implementation of the Gui using Qt library...