Developer Documentation
Loading...
Searching...
No Matches
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
47RenderObjectHighlighter::RenderObjectHighlighter(QTextDocument *parent)
48 : QSyntaxHighlighter(parent)
49{
50 init();
51}
52
53RenderObjectHighlighter::RenderObjectHighlighter(QTextEdit *parent)
54: QSyntaxHighlighter(parent)
55{
56 init();
57}
58
60 // Set the basic format styles
61 vertexShaderFormat_.setBackground(Qt::green);
62 vertexShaderFormat_.setFontWeight(QFont::Bold);
63
64 vertexShaderStartExpression_ = QRegularExpression("---------------------vertex-shader--------------------");
65 vertexShaderEndExpression_ = QRegularExpression("---------------------end-vertex-shader--------------------");
66
67 geometryShaderFormat_.setBackground(Qt::blue);
68 geometryShaderFormat_.setFontWeight(QFont::Bold);
69
70 geometryShaderStartExpression_ = QRegularExpression("---------------------geometry-shader--------------------");;
71 geometryShaderEndExpression_ = QRegularExpression("---------------------end-geometry-shader--------------------");;
72
73
74 fragmentShaderFormat_.setBackground(Qt::red);
75 fragmentShaderFormat_.setFontWeight(QFont::Bold);
76
77 fragmentShaderStartExpression_ = QRegularExpression("---------------------fragment-shader--------------------");
78 fragmentShaderEndExpression_ = QRegularExpression("---------------------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
105
106 highlightingRules_.clear();
107
108 HighlightingRule rule;
109
110 // Define rule
111 rule.pattern = QRegularExpression("(#define|#ifdef|#else|#endif|#ifndef).*");
112 rule.format = defineFormat_;
113 highlightingRules_.append(rule);
114
115 // version rule
116 rule.pattern = QRegularExpression("#version.*");
117 rule.format = defineFormat_;
118 highlightingRules_.append(rule);
119
120 // Rule for single line comments
121 rule.pattern = QRegularExpression("//[^\n]*");
122 rule.format = singleLineCommentFormat_;
123 highlightingRules_.append(rule);
124
125 // Create Rules for keywords
126 foreach (QString pattern, keywordPatterns_) {
127 rule.pattern = QRegularExpression("\\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 = QRegularExpression("\\b" + pattern + "\\b" );
135 rule.format = typeFormat_;
136 highlightingRules_.append(rule);
137 }
138
139}
140
141void RenderObjectHighlighter::highlightBlock(const QString &text)
142{
143
144 // Single word highlighting
145 foreach (HighlightingRule rule, highlightingRules_) {
146 QRegularExpression expression(rule.pattern);
147 QRegularExpressionMatch match = expression.match(text);
148 int index = match.capturedStart();
149 while (index >= 0) {
150 int length = match.capturedLength();
151 setFormat(index, length, rule.format);
152 match = expression.match(text, index + length);
153 index = match.capturedStart();
154 }
155 }
156
157 // Blockstate -1,0 : nothing
158 // Blockstate 1 : vertexShader Code
159 // Blockstate 2 : geometryShader Code
160 // Blockstate 3 : fragment Shader Code
161
162// setCurrentBlockState(0);
163//
164// // Vertex shader block
165// int startIndex = 0;
166// if (previousBlockState() != 1)
167// startIndex = text.indexOf(vertexShaderStartExpression_);
168//
169// while (startIndex >= 0) {
170// int endIndex = text.indexOf(vertexShaderEndExpression_, startIndex);
171// int commentLength;
172// if (endIndex == -1) {
173// setCurrentBlockState(1);
174// commentLength = text.length() - startIndex;
175// } else {
176// commentLength = endIndex - startIndex + vertexShaderEndExpression_.matchedLength();
177// }
178// setFormat(startIndex, commentLength, vertexShaderFormat_);
179// startIndex = text.indexOf(vertexShaderStartExpression_, startIndex + commentLength);
180// }
181//
182// // Fragment shader block
183// startIndex = 0;
184// if (previousBlockState() != 3)
185// startIndex = text.indexOf(fragmentShaderStartExpression_);
186//
187// while (startIndex >= 0) {
188// int endIndex = text.indexOf(fragmentShaderEndExpression_, startIndex);
189// int commentLength;
190// if (endIndex == -1) {
191// setCurrentBlockState(3);
192// commentLength = text.length() - startIndex;
193// } else {
194// commentLength = endIndex - startIndex + fragmentShaderEndExpression_.matchedLength();
195// }
196// setFormat(startIndex, commentLength, fragmentShaderFormat_);
197// startIndex = text.indexOf(fragmentShaderStartExpression_, startIndex + commentLength);
198// }
199
200}
Container to describe a highlighting rule. Based on a regular expression, a relevant match # and the ...
void init()
common initializer function called by the constructors
void update()
Updates the highlighter with the current rule set defined in the patterns.