Developer Documentation
highLighter.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #if QT_VERSION >= 0x050000
51  #include <QtWidgets>
52 #else
53  #include <QtGui>
54 #endif
55 
56 
57 #include "highLighter.hh"
58 
59  Highlighter::Highlighter(QTextDocument *parent)
60  : QSyntaxHighlighter(parent)
61 {
62  init();
63 }
64 
65 Highlighter::Highlighter(QTextEdit *parent)
66 : QSyntaxHighlighter(parent)
67 {
68  init();
69 }
70 
72  // Set the basic format styles
73  keywordFormat_.setForeground(Qt::darkGreen);
74  keywordFormat_.setFontWeight(QFont::Bold);
75 
76  pluginFormat_.setForeground(Qt::darkBlue);
77  pluginFormat_.setFontWeight(QFont::Bold);
78 
79  functionFormat_.setForeground(Qt::darkYellow);
80  functionFormat_.setFontWeight(QFont::Bold);
81 
82  typeFormat_.setForeground(Qt::darkMagenta);
83  typeFormat_.setFontWeight(QFont::Bold);
84 
85  quotationFormat_.setForeground(Qt::darkRed);
86 
87  listFormat_.setForeground(Qt::darkRed);
88 
89  singleLineCommentFormat_.setForeground(Qt::red);
90  multiLineCommentFormat_.setForeground(Qt::red);
91 
92  commentStartExpression_ = QRegExp("/\\*");
93  commentEndExpression_ = QRegExp("\\*/");
94 
95  // Define basic keywords
96  keywordPatterns_ << "while" << "for" << "print" << "var" << "break" << "if";
97 
98  // Types which are accepted by the scripting system
99  typePatterns_ << "int" << "Matrix4x4" << "QString" << "idList" << "bool" << "Vector" << "double";
100 
101  update();
102 
103  // classFormat.setFontWeight(QFont::Bold);
104  // classFormat.setForeground(Qt::darkMagenta);
105  // rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
106  // rule.format = classFormat;
107  // highlightingRules.append(rule);
108  //
109 
110 
111 
112  // functionFormat.setFontItalic(true);
113  // functionFormat.setForeground(Qt::blue);
114  // rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
115  // rule.format = functionFormat;
116  // highlightingRules.append(rule);
117  //
118 }
119 
121 
122  highlightingRules_.clear();
123 
124  HighlightingRule rule;
125 
126  // Create Rules for keywords
127  foreach (QString pattern, keywordPatterns_) {
128  rule.pattern = QRegExp("\\b" + pattern + "\\b" );
129  rule.format = keywordFormat_;
130  highlightingRules_.append(rule);
131  }
132 
133  // Create Rules for plugins
134  foreach (QString pattern, pluginPatterns_ ) {
135  rule.pattern = QRegExp("\\b" + pattern + "\\b" );
136  rule.format = pluginFormat_;
137  highlightingRules_.append(rule);
138  }
139 
140  // Create Rules for functions
141  foreach (QString pattern, functionPatterns_ ) {
142  rule.pattern = QRegExp("\\b" + pattern + "\\b" );
143  rule.format = functionFormat_;
144  highlightingRules_.append(rule);
145  }
146 
147  // Create Rules for types
148  foreach (QString pattern, typePatterns_ ) {
149  rule.pattern = QRegExp("\\b" + pattern + "\\b" );
150  rule.format = typeFormat_;
151  highlightingRules_.append(rule);
152  }
153 
154  // Rule for single line comments
155  rule.pattern = QRegExp("//[^\n]*");
156  rule.format = singleLineCommentFormat_;
157  highlightingRules_.append(rule);
158 
159  // Rules for quotations
160  rule.pattern = QRegExp("\".*\"");
161  rule.format = quotationFormat_;
162  highlightingRules_.append(rule);
163 
164  // Rules for Lists
165  rule.pattern = QRegExp("\\[.*\\]");
166  rule.format = listFormat_;
167  highlightingRules_.append(rule);
168 
169 }
170 
171  void Highlighter::highlightBlock(const QString &text)
172 {
173 
174  foreach (HighlightingRule rule, highlightingRules_) {
175  QRegExp expression(rule.pattern);
176  int index = text.indexOf(expression);
177  while (index >= 0) {
178  int length = expression.matchedLength();
179  setFormat(index, length, rule.format);
180  index = text.indexOf(expression, index + length);
181  }
182  }
183  setCurrentBlockState(0);
184 
185  int startIndex = 0;
186  if (previousBlockState() != 1)
187  startIndex = text.indexOf(commentStartExpression_);
188 
189  while (startIndex >= 0) {
190  int endIndex = text.indexOf(commentEndExpression_, startIndex);
191  int commentLength;
192  if (endIndex == -1) {
193  setCurrentBlockState(1);
194  commentLength = text.length() - startIndex;
195  } else {
196  commentLength = endIndex - startIndex + commentEndExpression_.matchedLength();
197  }
198  setFormat(startIndex, commentLength, multiLineCommentFormat_);
199  startIndex = text.indexOf(commentStartExpression_, startIndex + commentLength);
200  }
201 
202 }
void init()
common initializer function called by the constructors
Definition: highLighter.cc:71
void update()
Updates the highlighter with the current rule set defined in the patterns.
Definition: highLighter.cc:120