SeExpr

ExprHighlighter.h
Go to the documentation of this file.
1 /*
2 * Copyright Disney Enterprises, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License
6 * and the following modification to it: Section 6 Trademarks.
7 * deleted and replaced with:
8 *
9 * 6. Trademarks. This License does not grant permission to use the
10 * trade names, trademarks, service marks, or product names of the
11 * Licensor and its affiliates, except as required for reproducing
12 * the content of the NOTICE file.
13 *
14 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * @file ExprHighlighter.h
18 * @brief A Qt syntax highlighter for the SeExpr language
19 * @author aselle
20 */
21 #ifndef _ExprHighlighter_h_
22 #define _ExprHighlighter_h_
23 #include <QSyntaxHighlighter>
24 #include <QPalette>
25 #include <iostream>
26
27 class ExprHighlighter : public QSyntaxHighlighter {
28 struct HighlightingRule {
29 QRegExp pattern;
30 QTextCharFormat format;
31 };
32 QVector<HighlightingRule> highlightingRules;
33 QTextCharFormat singleLineCommentFormat;
34 QTextCharFormat variableFormat;
35 QTextCharFormat numberFormat;
36 QTextCharFormat operatorFormat;
37
38 int lightness;
39
40 public:
41 ExprHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent), lightness(130) { init(); }
42
43 ExprHighlighter(QTextEdit* edit) : QSyntaxHighlighter(edit), lightness(130) { init(); }
44
45 void fixStyle(const QPalette& palette) {
46 lightness = palette.color(QPalette::Base).value() < 127 ? 250 : 130;
47 init();
48 }
49
50 void init() {
51 HighlightingRule rule;
52 highlightingRules.clear();
53
54 // Operator highlighting, disabled for now
55 // operatorFormat.setForeground(QColor::fromHsv(50,128,lightness));
56 // QStringList operatorPatterns;
57 // operatorPatterns<<"(?:->)|(?:[()\\+-/\\*%\\^:\\?\\[\\]])";
58 // foreach (QString pattern,operatorPatterns){
59 // rule.pattern=QRegExp(pattern);
60 // rule.format=operatorFormat;
61 // highlightingRules.append(rule);
62 //}
63
64 numberFormat.setForeground(QColor::fromHsv(180, 204, lightness));
65 rule.pattern = QRegExp("\\b[0-9]*\\.[0-9]*)?|[0-9]+\\b"); // \\b?[^\\$][A-Za-z][A-Za-z0-9]*\\b");
66 rule.format = numberFormat;
67 // highlightingRules.append(rule);
68
69 variableFormat.setForeground(QColor::fromHsv(200, 153, lightness));
70 // variableFormat.setFontWeight(QFont::Bold);
71 rule.pattern = QRegExp("\\$[A-Za-z][A-Za-z0-9]*\\b");
72 rule.format = variableFormat;
73 highlightingRules.append(rule);
74
75 singleLineCommentFormat.setForeground(QColor::fromHsv(210, 128, lightness));
76 rule.pattern = QRegExp("#[^\n]*");
77 rule.format = singleLineCommentFormat;
78 highlightingRules.append(rule);
79 }
80
81 void highlightBlock(const QString& text) {
82 foreach(HighlightingRule rule, highlightingRules) {
83 QRegExp expression(rule.pattern);
84 int index = text.indexOf(expression);
85 while (index >= 0) {
86 int length = expression.matchedLength();
87 setFormat(index, length, rule.format);
88 index = text.indexOf(expression, index + length);
89 }
90 }
91 setCurrentBlockState(0);
92 }
93 };
94 #endif
index
The result is computed int int< br >< div style="margin-left: 40px;"> Picks values randomly between loRange and hiRange based on supplied index(which is automatically hashed). &nbsp
ExprHighlighter::fixStyle
void fixStyle(const QPalette &palette)
Definition: ExprHighlighter.h:45
ExprHighlighter::HighlightingRule::pattern
QRegExp pattern
Definition: ExprHighlighter.h:29
ExprHighlighter::ExprHighlighter
ExprHighlighter(QTextDocument *parent)
Definition: ExprHighlighter.h:41
ExprHighlighter::singleLineCommentFormat
QTextCharFormat singleLineCommentFormat
Definition: ExprHighlighter.h:33
ExprHighlighter::HighlightingRule::format
QTextCharFormat format
Definition: ExprHighlighter.h:30
SeExpr2::length
double length(const Vec3d &v)
Definition: ExprBuiltins.cpp:1062
ExprHighlighter::HighlightingRule
Definition: ExprHighlighter.h:28
ExprHighlighter::ExprHighlighter
ExprHighlighter(QTextEdit *edit)
Definition: ExprHighlighter.h:43
ExprHighlighter::numberFormat
QTextCharFormat numberFormat
Definition: ExprHighlighter.h:35
ExprHighlighter::variableFormat
QTextCharFormat variableFormat
Definition: ExprHighlighter.h:34
ExprHighlighter::operatorFormat
QTextCharFormat operatorFormat
Definition: ExprHighlighter.h:36
ExprHighlighter::highlightBlock
void highlightBlock(const QString &text)
Definition: ExprHighlighter.h:81
ExprHighlighter::init
void init()
Definition: ExprHighlighter.h:50
ExprHighlighter
Definition: ExprHighlighter.h:27
ExprHighlighter::highlightingRules
QVector< HighlightingRule > highlightingRules
Definition: ExprHighlighter.h:32
ExprHighlighter::lightness
int lightness
Definition: ExprHighlighter.h:38
expression
For a multi line expression
Definition: userdoc.txt:551