7 #include "fwGuiQt/highlighter/PythonHighlighter.hpp" 16 PythonHighlighter::PythonHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
19 HighlightingRule rule;
22 QTextCharFormat keywordFormat;
23 keywordFormat.setForeground(Qt::darkBlue);
24 keywordFormat.setFontWeight(QFont::Bold);
25 QStringList keywordPatterns;
26 keywordPatterns <<
"and" <<
"assert" <<
"break" <<
"class" <<
"continue" <<
"def" 27 <<
"del" <<
"elif" <<
"else" <<
"except" <<
"exec" <<
"finally" 28 <<
"for" <<
"from" <<
"global" <<
"if" <<
"import" <<
"in" 29 <<
"is" <<
"lambda" <<
"not" <<
"or" <<
"pass" <<
"print" 30 <<
"raise" <<
"return" <<
"try" <<
"while" <<
"yield" 31 <<
"None" <<
"True" <<
"False";
33 for(
const QString &pattern : keywordPatterns)
35 rule.pattern = QRegExp(
"\\b" + pattern +
"\\b");
36 rule.format = keywordFormat;
37 highlightingRules.append(rule);
41 QTextCharFormat operatorsFormat;
42 operatorsFormat.setForeground(Qt::red);
43 QStringList operatorsPatterns;
44 operatorsPatterns <<
"=" 46 <<
"==" <<
"!=" <<
"<" <<
"<=" <<
">" <<
">=" 48 <<
"\\+" <<
"-" <<
"\\*" <<
"/" <<
"//" <<
"\\%" <<
"\\*\\*" 50 <<
"\\+=" <<
"-=" <<
"\\*=" <<
"/=" <<
"\\%=" 52 <<
"\\^" <<
"\\|" <<
"\\&" <<
"\\~" <<
">>" <<
"<<";
54 for(
const QString &pattern : operatorsPatterns)
56 rule.pattern = QRegExp( pattern );
57 rule.format = operatorsFormat;
58 highlightingRules.append(rule);
62 QTextCharFormat bracesFormat;
63 bracesFormat.setForeground(Qt::darkGray);
64 QStringList bracesPatterns;
65 bracesPatterns <<
"\\{" <<
"\\}" <<
"\\(" <<
"\\)" <<
"\\[" <<
"\\]";
67 for(
const QString &pattern : bracesPatterns)
69 rule.pattern = QRegExp( pattern );
70 rule.format = bracesFormat;
71 highlightingRules.append(rule);
75 QTextCharFormat classFormat;
76 classFormat.setFontWeight(QFont::Bold);
77 classFormat.setForeground(Qt::darkMagenta);
78 rule.pattern = QRegExp(
"\\bclass\\b\\s*(\\w+)");
79 rule.format = classFormat;
81 highlightingRules.append(rule);
84 QTextCharFormat defFormat;
85 defFormat.setFontWeight(QFont::Bold);
86 defFormat.setForeground(Qt::darkMagenta);
87 rule.pattern = QRegExp(
"\\bdef\\b\\s*(\\w+)");
88 rule.format = defFormat;
90 highlightingRules.append(rule);
93 QTextCharFormat selfFormat;
94 selfFormat.setFontItalic(
true);
95 selfFormat.setForeground(Qt::black);
96 rule.pattern = QRegExp(
"\\bself\\b");
97 rule.format = selfFormat;
99 highlightingRules.append(rule);
102 QTextCharFormat stringFormat;
103 stringFormat.setForeground(Qt::darkMagenta);
104 rule.pattern = QRegExp(
"\".*\"");
105 rule.format = stringFormat;
107 highlightingRules.append(rule);
109 rule.pattern = QRegExp(
"'.*'");
110 highlightingRules.append(rule);
113 QTextCharFormat singleLineCommentFormat;
114 singleLineCommentFormat.setFontItalic(
true);
115 singleLineCommentFormat.setForeground(Qt::darkGreen);
116 rule.pattern = QRegExp(
"#[^\\n]*");
117 rule.format = singleLineCommentFormat;
119 highlightingRules.append(rule);
122 QTextCharFormat numericFormat;
124 col.setNamedColor(
"brown");
125 numericFormat.setForeground(col);
126 rule.format = numericFormat;
128 rule.pattern = QRegExp(
"\\b[+-]?[0-9]+[lL]?\\b");
129 highlightingRules.append(rule);
130 rule.pattern = QRegExp(
"\\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\b");
131 highlightingRules.append(rule);
132 rule.pattern = QRegExp(
"\\b[+-]?[0-9]+(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b");
133 highlightingRules.append(rule);
138 void PythonHighlighter::highlightBlock(
const QString &text)
140 for(
const HighlightingRule &rule : highlightingRules)
142 QRegExp expression(rule.pattern);
143 int index = expression.indexIn(text, 0);
146 index = expression.pos(rule.nth);
147 int length = expression.cap(rule.nth).length();
148 setFormat(index, length, rule.format);
149 index = expression.indexIn(text, index + length);
152 setCurrentBlockState(0);
The namespace fwGuiQt contains classes which provide the implementation of the Gui using Qt library...