fw4spl
CppHighlighter.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 <QtGui>
8 
9 #include "fwGuiQt/highlighter/CppHighlighter.hpp"
10 
11 namespace fwGuiQt
12 {
13 namespace highlighter
14 {
15 
16 CppHighlighter::CppHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
17 {
18  HighlightingRule rule;
19 
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)
35  {
36  rule.pattern = QRegExp("\\b" + pattern + "\\b");
37  rule.format = keywordFormat;
38  highlightingRules.append(rule);
39  }
40 
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);
47 
48  QTextCharFormat singleLineCommentFormat;
49  singleLineCommentFormat.setForeground(Qt::red);
50  rule.pattern = QRegExp("//[^\n]*");
51  rule.format = singleLineCommentFormat;
52  highlightingRules.append(rule);
53 
54  multiLineCommentFormat.setForeground(Qt::red);
55 
56  QTextCharFormat quotationFormat;
57  quotationFormat.setForeground(Qt::darkGreen);
58  rule.pattern = QRegExp("\".*\"");
59  rule.format = quotationFormat;
60  highlightingRules.append(rule);
61 
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);
68 
69  commentStartExpression = QRegExp("/\\*");
70  commentEndExpression = QRegExp("\\*/");
71 }
72 
73 //------------------------------------------------------------------------------
74 
75 void CppHighlighter::highlightBlock(const QString &text)
76 {
77  for(const HighlightingRule &rule : highlightingRules)
78  {
79  QRegExp expression(rule.pattern);
80  int index = expression.indexIn(text);
81  while (index >= 0)
82  {
83  int length = expression.matchedLength();
84  setFormat(index, length, rule.format);
85  index = expression.indexIn(text, index + length);
86  }
87  }
88  setCurrentBlockState(0);
89 
90  int startIndex = 0;
91  if (previousBlockState() != 1)
92  {
93  startIndex = commentStartExpression.indexIn(text);
94  }
95 
96  while (startIndex >= 0)
97  {
98  int endIndex = commentEndExpression.indexIn(text, startIndex);
99  int commentLength;
100  if (endIndex == -1)
101  {
102  setCurrentBlockState(1);
103  commentLength = text.length() - startIndex;
104  }
105  else
106  {
107  commentLength = endIndex - startIndex
108  + commentEndExpression.matchedLength();
109  }
110  setFormat(startIndex, commentLength, multiLineCommentFormat);
111  startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
112  }
113 }
114 
115 //------------------------------------------------------------------------------
116 
117 } //namespace fwGuiQt
118 } //namespace highlighter
119 
The namespace fwGuiQt contains classes which provide the implementation of the Gui using Qt library...
Definition: WindowLevel.hpp:32