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