Developer Documentation
renderObjectHighLighter.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: 12439 $ *
45 * $LastChangedBy: moebius $ *
46 * $Date: 2011-09-22 17:55:29 +0200 (Do, 22 Sep 2011) $ *
47 * *
48 \*===========================================================================*/
49 
50 
51 #if QT_VERSION >= 0x050000
52  #include <QtWidgets>
53 #else
54  #include <QtGui>
55 #endif
56 
57 
58 #include "renderObjectHighLighter.hh"
59 
60 RenderObjectHighlighter::RenderObjectHighlighter(QTextDocument *parent)
61  : QSyntaxHighlighter(parent)
62 {
63  init();
64 }
65 
66 RenderObjectHighlighter::RenderObjectHighlighter(QTextEdit *parent)
67 : QSyntaxHighlighter(parent)
68 {
69  init();
70 }
71 
73  // Set the basic format styles
74  vertexShaderFormat_.setBackground(Qt::green);
75  vertexShaderFormat_.setFontWeight(QFont::Bold);
76 
77  vertexShaderStartExpression_ = QRegExp("---------------------vertex-shader--------------------");
78  vertexShaderEndExpression_ = QRegExp("---------------------end-vertex-shader--------------------");
79 
80  geometryShaderFormat_.setBackground(Qt::blue);
81  geometryShaderFormat_.setFontWeight(QFont::Bold);
82 
83  geometryShaderStartExpression_ = QRegExp("---------------------geometry-shader--------------------");;
84  geometryShaderEndExpression_ = QRegExp("---------------------end-geometry-shader--------------------");;
85 
86 
87  fragmentShaderFormat_.setBackground(Qt::red);
88  fragmentShaderFormat_.setFontWeight(QFont::Bold);
89 
90  fragmentShaderStartExpression_ = QRegExp("---------------------fragment-shader--------------------");
91  fragmentShaderEndExpression_ = QRegExp("---------------------end-fragment-shader--------------------");
92 
93  // match whole line containing the define
94  defineFormat_.setForeground(Qt::green);
95  defineFormat_.setFontWeight(QFont::Bold);
96 
97  // Single line comments
98  singleLineCommentFormat_.setForeground(Qt::red);
99 
100  // Set the basic format styles
101  keywordFormat_.setForeground(Qt::darkGreen);
102  keywordFormat_.setFontWeight(QFont::Bold);
103 
104  // Define basic keywords
105  keywordPatterns_ << "main" << "while" << "for" << "if" << "dot" << "sqrt" << "max" << "pow" << "return" << "normalize";
106  keywordPatterns_ << "min" << "clamp" << "step";
107 
108  typeFormat_.setForeground(Qt::darkMagenta);
109  typeFormat_.setFontWeight(QFont::Bold);
110 
111  // Types
112  typePatterns_ << "in" << "out" << "mat3" << "mat4" << "vec2" << "vec3" << "vec4" << "float" << "double" <<"uniform" << "layout" ;
113 
114  update();
115 }
116 
118 
119  highlightingRules_.clear();
120 
121  HighlightingRule rule;
122 
123  // Define rule
124  rule.pattern = QRegExp("#define.*");
125  rule.format = defineFormat_;
126  highlightingRules_.append(rule);
127 
128  // version rule
129  rule.pattern = QRegExp("#version.*");
130  rule.format = defineFormat_;
131  highlightingRules_.append(rule);
132 
133  // Rule for single line comments
134  rule.pattern = QRegExp("//[^\n]*");
135  rule.format = singleLineCommentFormat_;
136  highlightingRules_.append(rule);
137 
138  // Create Rules for keywords
139  foreach (QString pattern, keywordPatterns_) {
140  rule.pattern = QRegExp("\\b" + pattern + "\\b" );
141  rule.format = keywordFormat_;
142  highlightingRules_.append(rule);
143  }
144 
145  // Create Rules for types
146  foreach (QString pattern, typePatterns_ ) {
147  rule.pattern = QRegExp("\\b" + pattern + "\\b" );
148  rule.format = typeFormat_;
149  highlightingRules_.append(rule);
150  }
151 
152 }
153 
154 void RenderObjectHighlighter::highlightBlock(const QString &text)
155 {
156 
157  // Single word highlighting
158  foreach (HighlightingRule rule, highlightingRules_) {
159  QRegExp expression(rule.pattern);
160  int index = text.indexOf(expression);
161  while (index >= 0) {
162  int length = expression.matchedLength();
163  setFormat(index, length, rule.format);
164  index = text.indexOf(expression, index + length);
165  }
166  }
167 
168  // Blockstate -1,0 : nothing
169  // Blockstate 1 : vertexShader Code
170  // Blockstate 2 : geometryShader Code
171  // Blockstate 3 : fragment Shader Code
172 
173 // setCurrentBlockState(0);
174 //
175 // // Vertex shader block
176 // int startIndex = 0;
177 // if (previousBlockState() != 1)
178 // startIndex = text.indexOf(vertexShaderStartExpression_);
179 //
180 // while (startIndex >= 0) {
181 // int endIndex = text.indexOf(vertexShaderEndExpression_, startIndex);
182 // int commentLength;
183 // if (endIndex == -1) {
184 // setCurrentBlockState(1);
185 // commentLength = text.length() - startIndex;
186 // } else {
187 // commentLength = endIndex - startIndex + vertexShaderEndExpression_.matchedLength();
188 // }
189 // setFormat(startIndex, commentLength, vertexShaderFormat_);
190 // startIndex = text.indexOf(vertexShaderStartExpression_, startIndex + commentLength);
191 // }
192 //
193 // // Fragment shader block
194 // startIndex = 0;
195 // if (previousBlockState() != 3)
196 // startIndex = text.indexOf(fragmentShaderStartExpression_);
197 //
198 // while (startIndex >= 0) {
199 // int endIndex = text.indexOf(fragmentShaderEndExpression_, startIndex);
200 // int commentLength;
201 // if (endIndex == -1) {
202 // setCurrentBlockState(3);
203 // commentLength = text.length() - startIndex;
204 // } else {
205 // commentLength = endIndex - startIndex + fragmentShaderEndExpression_.matchedLength();
206 // }
207 // setFormat(startIndex, commentLength, fragmentShaderFormat_);
208 // startIndex = text.indexOf(fragmentShaderStartExpression_, startIndex + commentLength);
209 // }
210 
211 }
void init()
common initializer function called by the constructors
void update()
Updates the highlighter with the current rule set defined in the patterns.