00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef _SeExprEdHighlighter_h_
00015 #define _SeExprEdHighlighter_h_
00016 #include <QtGui/QSyntaxHighlighter>
00017 #include <QtGui/QPalette>
00018 #include <iostream>
00019
00020 class SeExprEdHighlighter : public QSyntaxHighlighter
00021 {
00022 struct HighlightingRule{
00023 QRegExp pattern;
00024 QTextCharFormat format;
00025 };
00026 QVector<HighlightingRule> highlightingRules;
00027 QTextCharFormat singleLineCommentFormat;
00028 QTextCharFormat variableFormat;
00029 QTextCharFormat numberFormat;
00030 QTextCharFormat operatorFormat;
00031
00032 int lightness;
00033
00034 public:
00035 SeExprEdHighlighter(QTextDocument* parent)
00036 :QSyntaxHighlighter(parent),lightness(130)
00037 {
00038 init();
00039 }
00040
00041 SeExprEdHighlighter(QTextEdit* edit)
00042 :QSyntaxHighlighter(edit),lightness(130)
00043 {
00044 init();
00045 }
00046
00047 void fixStyle(const QPalette& palette)
00048 {
00049 lightness=palette.color(QPalette::Base).value()<127 ? 250: 130;
00050 init();
00051 }
00052
00053 void init()
00054 {
00055 HighlightingRule rule;
00056 highlightingRules.clear();
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 numberFormat.setForeground(QColor::fromHsv(180,204,lightness));
00069 rule.pattern=QRegExp("\\b[0-9]*\\.[0-9]*)?|[0-9]+\\b");
00070 rule.format=numberFormat;
00071
00072
00073 variableFormat.setForeground(QColor::fromHsv(200,153,lightness));
00074
00075 rule.pattern=QRegExp("\\$[A-Za-z][A-Za-z0-9]*\\b");
00076 rule.format=variableFormat;
00077 highlightingRules.append(rule);
00078
00079 singleLineCommentFormat.setForeground(QColor::fromHsv(210,128,lightness));
00080 rule.pattern=QRegExp("#[^\n]*");
00081 rule.format=singleLineCommentFormat;
00082 highlightingRules.append(rule);
00083
00084 }
00085
00086 void highlightBlock(const QString& text)
00087 {
00088 foreach (HighlightingRule rule,highlightingRules){
00089 QRegExp expression(rule.pattern);
00090 int index=text.indexOf(expression);
00091 while(index>=0){
00092 int length=expression.matchedLength();
00093 setFormat(index,length,rule.format);
00094 index=text.indexOf(expression,index+length);
00095 }
00096 }
00097 setCurrentBlockState(0);
00098 }
00099 };
00100 #endif