CppHighlighter.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::
17 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
18 * @date 2014
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "CppHighlighter.h"
24 
25 namespace armarx
26 {
27 
28 
29  CppHighlighter::CppHighlighter(QTextDocument* parent)
30  : QSyntaxHighlighter(parent)
31  {
32  HighlightingRule rule;
33 
34  keywordFormat.setForeground(Qt::darkBlue);
35  keywordFormat.setFontWeight(QFont::Bold);
36  QStringList keywordPatterns;
37  keywordPatterns << "\\bfor\\b" << "\\bwhile\\b" << "\\bif\\b" << "\\bchar\\b"
38  << "\\bclass\\b" << "\\bconst\\b" << "\\breturn\\b"
39  << "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
40  << "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
41  << "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
42  << "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
43  << "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
44  << "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
45  << "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
46  << "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
47  << "\\bvoid\\b" << "\\bvolatile\\b";
48  foreach (const QString& pattern, keywordPatterns)
49  {
50  rule.pattern = QRegExp(pattern);
51  rule.format = keywordFormat;
52  highlightingRules.append(rule);
53  }
54 
55  classFormat.setFontWeight(QFont::Bold);
56  classFormat.setForeground(Qt::darkMagenta);
57  rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
58  rule.format = classFormat;
59  highlightingRules.append(rule);
60 
61  singleLineCommentFormat.setForeground(Qt::red);
62  rule.pattern = QRegExp("//[^\n]*");
63  rule.format = singleLineCommentFormat;
64  highlightingRules.append(rule);
65 
66  multiLineCommentFormat.setForeground(Qt::red);
67 
68  quotationFormat.setForeground(Qt::darkGreen);
69  rule.pattern = QRegExp("\".*\"");
70  rule.format = quotationFormat;
71  highlightingRules.append(rule);
72 
73  functionFormat.setFontItalic(true);
74  functionFormat.setForeground(Qt::blue);
75  rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
76  rule.format = functionFormat;
77  highlightingRules.append(rule);
78 
79  commentStartExpression = QRegExp("/\\*");
80  commentEndExpression = QRegExp("\\*/");
81  }
82 
83  void CppHighlighter::highlightBlock(const QString& text)
84  {
85  foreach (const HighlightingRule& rule, highlightingRules)
86  {
87  QRegExp expression(rule.pattern);
88  int index = expression.indexIn(text);
89 
90  while (index >= 0)
91  {
92  int length = expression.matchedLength();
93  setFormat(index, length, rule.format);
94  index = expression.indexIn(text, index + length);
95  }
96  }
97  setCurrentBlockState(0);
98 
99  int startIndex = 0;
100 
101  if (previousBlockState() != 1)
102  {
103  startIndex = commentStartExpression.indexIn(text);
104  }
105 
106  while (startIndex >= 0)
107  {
108  int endIndex = commentEndExpression.indexIn(text, startIndex);
109  int commentLength;
110 
111  if (endIndex == -1)
112  {
113  setCurrentBlockState(1);
114  commentLength = text.length() - startIndex;
115  }
116  else
117  {
118  commentLength = endIndex - startIndex
119  + commentEndExpression.matchedLength();
120  }
121 
122  setFormat(startIndex, commentLength, multiLineCommentFormat);
123  startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
124  }
125  }
126 } // namespace armarx
CppHighlighter.h
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::red
QColor red()
Definition: StyleSheets.h:76
armarx::CppHighlighter::CppHighlighter
CppHighlighter(QTextDocument *parent)
Definition: CppHighlighter.cpp:29
armarx::CppHighlighter::highlightBlock
void highlightBlock(const QString &text) override
Definition: CppHighlighter.cpp:83
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28