KSeExpr  4.0.4.0
ExprHighlighter.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2019 Disney Enterprises, Inc.
2 // SPDX-License-Identifier: LicenseRef-Apache-2.0
3 // SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 /*
6  * @file ExprHighlighter.cpp
7  * @brief A Qt syntax highlighter for the SeExpr language
8  * @author aselle, amyspark
9  */
10 
11 #include "ExprHighlighter.h"
12 
13 ExprHighlighter::ExprHighlighter(QTextDocument *parent)
14  : QSyntaxHighlighter(parent)
15  , lightness(130)
16 {
17  init();
18 }
19 
21  : QSyntaxHighlighter(edit)
22  , lightness(130)
23 {
24  init();
25 }
26 
27 void ExprHighlighter::fixStyle(const QPalette &palette)
28 {
29  lightness = palette.color(QPalette::Base).value() < 127 ? 250 : 130;
30  init();
31 }
32 
34 {
35  HighlightingRule rule;
36  highlightingRules.clear();
37 
38  // Operator highlighting, disabled for now
39  // operatorFormat.setForeground(QColor::fromHsv(50,128,lightness));
40  // QStringList operatorPatterns;
41  // operatorPatterns<<"(?:->)|(?:[()\\+-/\\*%\\^:\\?\\[\\]])";
42  // foreach (QString pattern,operatorPatterns){
43  // rule.pattern=QRegExp(pattern);
44  // rule.format=operatorFormat;
45  // highlightingRules.append(rule);
46  //}
47 
48  numberFormat.setForeground(QColor::fromHsv(37, 200, lightness));
49  rule.pattern = QRegExp(QString::fromLatin1("\\b[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)\\b")); // \\b?[^\\$][A-Za-z][A-Za-z0-9]*\\b");
50  rule.format = numberFormat;
51  highlightingRules.append(rule);
52 
53  variableFormat.setForeground(QColor::fromHsv(200, 153, lightness));
54  // variableFormat.setFontWeight(QFont::Bold);
55  rule.pattern = QRegExp(QString::fromLatin1("\\$[A-Za-z][A-Za-z0-9]*\\b"));
56  rule.format = variableFormat;
57  highlightingRules.append(rule);
58 
59  singleLineCommentFormat.setForeground(QColor::fromHsv(54, 49, lightness));
60  rule.pattern = QRegExp(QString::fromLatin1("#[^\n]*"));
62  highlightingRules.append(rule);
63 }
64 
65 void ExprHighlighter::highlightBlock(const QString &text)
66 {
67  foreach (HighlightingRule rule, highlightingRules) {
68  QRegExp expression(rule.pattern);
69  int index = text.indexOf(expression);
70  while (index >= 0) {
71  int length = expression.matchedLength();
72  setFormat(index, length, rule.format);
73  index = text.indexOf(expression, index + length);
74  }
75  }
76  setCurrentBlockState(0);
77 }
void fixStyle(const QPalette &palette)
QTextCharFormat numberFormat
ExprHighlighter(QTextDocument *parent)
QTextCharFormat variableFormat
void highlightBlock(const QString &text) override
QVector< HighlightingRule > highlightingRules
QTextCharFormat singleLineCommentFormat
double length(const Vec3d &v)