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
27class ExprHighlighter : public QSyntaxHighlighter {
29 QRegExp pattern;
30 QTextCharFormat format;
31 };
32 QVector<HighlightingRule> highlightingRules;
33 QTextCharFormat singleLineCommentFormat;
34 QTextCharFormat variableFormat;
35 QTextCharFormat numberFormat;
36 QTextCharFormat operatorFormat;
37
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() {
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");
73 highlightingRules.append(rule);
74
75 singleLineCommentFormat.setForeground(QColor::fromHsv(210, 128, lightness));
76 rule.pattern = QRegExp("#[^\n]*");
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
void fixStyle(const QPalette &palette)
QTextCharFormat numberFormat
QTextCharFormat operatorFormat
ExprHighlighter(QTextDocument *parent)
QTextCharFormat variableFormat
void highlightBlock(const QString &text)
ExprHighlighter(QTextEdit *edit)
QVector< HighlightingRule > highlightingRules
QTextCharFormat singleLineCommentFormat
For a multi line expression
Definition userdoc.txt:551
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