fw4spl
PythonHighlighter.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwGuiQt/highlighter/PythonHighlighter.hpp"
8 
9 #include <QtGui>
10 
11 namespace fwGuiQt
12 {
13 namespace highlighter
14 {
15 
16 PythonHighlighter::PythonHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
17 {
18  // http://diotavelli.net/PyQtWiki/Python%20syntax%20highlighting
19  HighlightingRule rule;
20 
21  // Python keywords
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";
32  rule.nth = 0;
33  for(const QString &pattern : keywordPatterns)
34  {
35  rule.pattern = QRegExp( "\\b" + pattern + "\\b");
36  rule.format = keywordFormat;
37  highlightingRules.append(rule);
38  }
39 
40  // Python operators
41  QTextCharFormat operatorsFormat;
42  operatorsFormat.setForeground(Qt::red);
43  QStringList operatorsPatterns;
44  operatorsPatterns << "="
45  // Comparison
46  << "==" << "!=" << "<" << "<=" << ">" << ">="
47  // Arithmetic
48  << "\\+" << "-" << "\\*" << "/" << "//" << "\\%" << "\\*\\*"
49  // In-place
50  << "\\+=" << "-=" << "\\*=" << "/=" << "\\%="
51  // Bitwise
52  << "\\^" << "\\|" << "\\&" << "\\~" << ">>" << "<<";
53  rule.nth = 0;
54  for(const QString &pattern : operatorsPatterns)
55  {
56  rule.pattern = QRegExp( pattern );
57  rule.format = operatorsFormat;
58  highlightingRules.append(rule);
59  }
60 
61  // Python braces
62  QTextCharFormat bracesFormat;
63  bracesFormat.setForeground(Qt::darkGray);
64  QStringList bracesPatterns;
65  bracesPatterns << "\\{" << "\\}" << "\\(" << "\\)" << "\\[" << "\\]";
66  rule.nth = 0;
67  for(const QString &pattern : bracesPatterns)
68  {
69  rule.pattern = QRegExp( pattern );
70  rule.format = bracesFormat;
71  highlightingRules.append(rule);
72  }
73 
74  // 'class' followed by an identifier
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;
80  rule.nth = 1;
81  highlightingRules.append(rule);
82 
83  // 'def' followed by an identifier
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;
89  rule.nth = 1;
90  highlightingRules.append(rule);
91 
92  // Python self
93  QTextCharFormat selfFormat;
94  selfFormat.setFontItalic(true);
95  selfFormat.setForeground(Qt::black);
96  rule.pattern = QRegExp("\\bself\\b");
97  rule.format = selfFormat;
98  rule.nth = 0;
99  highlightingRules.append(rule);
100 
101  // Double-quoted string, possibly containing escape sequences : "[^"\\]*(\\.[^"\\]*)*"
102  QTextCharFormat stringFormat;
103  stringFormat.setForeground(Qt::darkMagenta);
104  rule.pattern = QRegExp("\".*\"");
105  rule.format = stringFormat;
106  rule.nth = 0;
107  highlightingRules.append(rule);
108  // Single-quoted string, possibly containing escape sequences : '[^'\\]*(\\.[^'\\]*)*'
109  rule.pattern = QRegExp("'.*'");
110  highlightingRules.append(rule);
111 
112  // Python comment (single line)
113  QTextCharFormat singleLineCommentFormat;
114  singleLineCommentFormat.setFontItalic(true);
115  singleLineCommentFormat.setForeground(Qt::darkGreen);
116  rule.pattern = QRegExp("#[^\\n]*");
117  rule.format = singleLineCommentFormat;
118  rule.nth = 0;
119  highlightingRules.append(rule);
120 
121  // Numeric literals
122  QTextCharFormat numericFormat;
123  QColor col;
124  col.setNamedColor("brown");
125  numericFormat.setForeground(col);
126  rule.format = numericFormat;
127  rule.nth = 0;
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);
134 }
135 
136 //------------------------------------------------------------------------------
137 
138 void PythonHighlighter::highlightBlock(const QString &text)
139 {
140  for(const HighlightingRule &rule : highlightingRules)
141  {
142  QRegExp expression(rule.pattern);
143  int index = expression.indexIn(text, 0);
144  while (index >= 0)
145  {
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);
150  }
151  }
152  setCurrentBlockState(0);
153 }
154 
155 //------------------------------------------------------------------------------
156 
157 } //namespace fwGuiQt
158 } //namespace highlighter
The namespace fwGuiQt contains classes which provide the implementation of the Gui using Qt library...
Definition: WindowLevel.hpp:32