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  #include <QtWidgets>
45 
46 
47 #include "highLighter.hh"
48 
49  Highlighter::Highlighter(QTextDocument *parent)
50  : QSyntaxHighlighter(parent)
51 {
52  init();
53 }
54 
55 Highlighter::Highlighter(QTextEdit *parent)
56 : QSyntaxHighlighter(parent)
57 {
58  init();
59 }
60 
62  // Set the basic format styles
63  keywordFormat_.setForeground(Qt::darkGreen);
64  keywordFormat_.setFontWeight(QFont::Bold);
65 
66  pluginFormat_.setForeground(Qt::darkBlue);
67  pluginFormat_.setFontWeight(QFont::Bold);
68 
69  functionFormat_.setForeground(Qt::darkYellow);
70  functionFormat_.setFontWeight(QFont::Bold);
71 
72  typeFormat_.setForeground(Qt::darkMagenta);
73  typeFormat_.setFontWeight(QFont::Bold);
74 
75  quotationFormat_.setForeground(Qt::darkRed);
76 
77  listFormat_.setForeground(Qt::darkRed);
78 
79  singleLineCommentFormat_.setForeground(Qt::red);
80  multiLineCommentFormat_.setForeground(Qt::red);
81 
82  commentStartExpression_ = QRegExp("/\\*");
83  commentEndExpression_ = QRegExp("\\*/");
84 
85  // Define basic keywords
86  keywordPatterns_ << "while" << "for" << "print" << "var" << "break" << "if";
87 
88  // Types which are accepted by the scripting system
89  typePatterns_ << "int" << "Matrix4x4" << "QString" << "idList"
90  << "bool" << "Vector" << "Vector4" << "double";
91 
92  update();
93 
94  // classFormat.setFontWeight(QFont::Bold);
95  // classFormat.setForeground(Qt::darkMagenta);
96  // rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
97  // rule.format = classFormat;
98  // highlightingRules.append(rule);
99  //
100 
101 
102 
103  // functionFormat.setFontItalic(true);
104  // functionFormat.setForeground(Qt::blue);
105  // rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
106  // rule.format = functionFormat;
107  // highlightingRules.append(rule);
108  //
109 }
110 
112 
113  highlightingRules_.clear();
114 
115  HighlightingRule rule;
116 
117  // Create Rules for keywords
118  foreach (QString pattern, keywordPatterns_) {
119  rule.pattern = QRegExp("\\b" + pattern + "\\b" );
120  rule.format = keywordFormat_;
121  highlightingRules_.append(rule);
122  }
123 
124  // Create Rules for plugins
125  foreach (QString pattern, pluginPatterns_ ) {
126  rule.pattern = QRegExp("\\b" + pattern + "\\b" );
127  rule.format = pluginFormat_;
128  highlightingRules_.append(rule);
129  }
130 
131  // Create Rules for functions
132  foreach (QString pattern, functionPatterns_ ) {
133  rule.pattern = QRegExp("\\b" + pattern + "\\b" );
134  rule.format = functionFormat_;
135  highlightingRules_.append(rule);
136  }
137 
138  // Create Rules for types
139  foreach (QString pattern, typePatterns_ ) {
140  rule.pattern = QRegExp("\\b" + pattern + "\\b" );
141  rule.format = typeFormat_;
142  highlightingRules_.append(rule);
143  }
144 
145  // Rule for single line comments
146  rule.pattern = QRegExp("//[^\n]*");
147  rule.format = singleLineCommentFormat_;
148  highlightingRules_.append(rule);
149 
150  // Rules for quotations
151  rule.pattern = QRegExp("\".*\"");
152  rule.format = quotationFormat_;
153  highlightingRules_.append(rule);
154 
155  // Rules for Lists
156  rule.pattern = QRegExp("\\[.*\\]");
157  rule.format = listFormat_;
158  highlightingRules_.append(rule);
159 
160 }
161 
162  void Highlighter::highlightBlock(const QString &text)
163 {
164 
165  foreach (HighlightingRule rule, highlightingRules_) {
166  QRegExp expression(rule.pattern);
167  int index = text.indexOf(expression);
168  while (index >= 0) {
169  int length = expression.matchedLength();
170  setFormat(index, length, rule.format);
171  index = text.indexOf(expression, index + length);
172  }
173  }
174  setCurrentBlockState(0);
175 
176  int startIndex = 0;
177  if (previousBlockState() != 1)
178  startIndex = text.indexOf(commentStartExpression_);
179 
180  while (startIndex >= 0) {
181  int endIndex = text.indexOf(commentEndExpression_, startIndex);
182  int commentLength;
183  if (endIndex == -1) {
184  setCurrentBlockState(1);
185  commentLength = text.length() - startIndex;
186  } else {
187  commentLength = endIndex - startIndex + commentEndExpression_.matchedLength();
188  }
189  setFormat(startIndex, commentLength, multiLineCommentFormat_);
190  startIndex = text.indexOf(commentStartExpression_, startIndex + commentLength);
191  }
192 
193 }
void init()
common initializer function called by the constructors
Definition: highLighter.cc:61
void update()
Updates the highlighter with the current rule set defined in the patterns.
Definition: highLighter.cc:111