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 <QtGui/QSyntaxHighlighter>
24 #include <QtGui/QPalette>
25 #include <iostream>
26 
27 class ExprHighlighter : public QSyntaxHighlighter
28 {
30  QRegExp pattern;
31  QTextCharFormat format;
32  };
33  QVector<HighlightingRule> highlightingRules;
34  QTextCharFormat singleLineCommentFormat;
35  QTextCharFormat variableFormat;
36  QTextCharFormat numberFormat;
37  QTextCharFormat operatorFormat;
38 
39  int lightness;
40 
41 public:
42  ExprHighlighter(QTextDocument* parent)
43  :QSyntaxHighlighter(parent),lightness(130)
44  {
45  init();
46  }
47 
48  ExprHighlighter(QTextEdit* edit)
49  :QSyntaxHighlighter(edit),lightness(130)
50  {
51  init();
52  }
53 
54  void fixStyle(const QPalette& palette)
55  {
56  lightness=palette.color(QPalette::Base).value()<127 ? 250: 130;
57  init();
58  }
59 
60  void init()
61  {
62  HighlightingRule rule;
63  highlightingRules.clear();
64 
65  // Operator highlighting, disabled for now
66  //operatorFormat.setForeground(QColor::fromHsv(50,128,lightness));
67  //QStringList operatorPatterns;
68  //operatorPatterns<<"(?:->)|(?:[()\\+-/\\*%\\^:\\?\\[\\]])";
69  //foreach (QString pattern,operatorPatterns){
70  // rule.pattern=QRegExp(pattern);
71  // rule.format=operatorFormat;
72  // highlightingRules.append(rule);
73  //}
74 
75  numberFormat.setForeground(QColor::fromHsv(180,204,lightness));
76  rule.pattern=QRegExp("\\b[0-9]*\\.[0-9]*)?|[0-9]+\\b"); // \\b?[^\\$][A-Za-z][A-Za-z0-9]*\\b");
77  rule.format=numberFormat;
78  //highlightingRules.append(rule);
79 
80  variableFormat.setForeground(QColor::fromHsv(200,153,lightness));
81  //variableFormat.setFontWeight(QFont::Bold);
82  rule.pattern=QRegExp("\\$[A-Za-z][A-Za-z0-9]*\\b");
84  highlightingRules.append(rule);
85 
86  singleLineCommentFormat.setForeground(QColor::fromHsv(210,128,lightness));
87  rule.pattern=QRegExp("#[^\n]*");
89  highlightingRules.append(rule);
90 
91  }
92 
93  void highlightBlock(const QString& text)
94  {
95  foreach (HighlightingRule rule,highlightingRules){
96  QRegExp expression(rule.pattern);
97  int index=text.indexOf(expression);
98  while(index>=0){
99  int length=expression.matchedLength();
100  setFormat(index,length,rule.format);
101  index=text.indexOf(expression,index+length);
102  }
103  }
104  setCurrentBlockState(0);
105  }
106 };
107 #endif
QTextCharFormat numberFormat
QTextCharFormat singleLineCommentFormat
QTextCharFormat variableFormat
double length(const Vec3d &v)
For a multi line expression
Definition: userdoc.txt:571
The result is computed int int< br >< divstyle="margin-left:40px;"> Picks values randomly between loRange and hiRange based on supplied index(which is automatically hashed).&nbsp
void highlightBlock(const QString &text)
QVector< HighlightingRule > highlightingRules
void fixStyle(const QPalette &palette)
QTextCharFormat operatorFormat
ExprHighlighter(QTextEdit *edit)
ExprHighlighter(QTextDocument *parent)