20 : QSyntaxHighlighter(parent)
30 QTextCharFormat commentFormat;
31 commentFormat.setFontWeight(QFont::Courier);
32 commentFormat.setForeground(Qt::darkGreen);
34 QTextCharFormat keywordFormat;
35 keywordFormat.setFontWeight(QFont::Bold);
36 keywordFormat.setForeground(Qt::blue);
37 QString pattern =
"\\b(and|assert|break|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|not|or|pass|print|raise|return|try|while|yield|None|True|False)\\b";
39 QTextCharFormat qouteFormat;
40 qouteFormat.setForeground(Qt::gray);
42 QTextCharFormat numberFormat;
43 numberFormat.setForeground(Qt::red);
45 QRegExp numberExpression(
"\\d+");
46 int indexNumbers = text.indexOf(numberExpression);
48 while (indexNumbers >= 0) {
49 int length = numberExpression.matchedLength();
50 setFormat(indexNumbers, length, numberFormat);
51 indexNumbers = text.indexOf(numberExpression, indexNumbers + length);
54 QRegExp qouteExpression(
"\"");
55 int startQouteIndex = text.indexOf(qouteExpression);
56 int endQouteIndex = text.indexOf(qouteExpression, startQouteIndex);
58 QRegExp keyword(pattern);
59 int indexKeyword = text.indexOf(keyword);
61 QRegExp startCommentExpression(
"^#");
62 QRegExp endCommentExpression(
"\\n");
63 int startCommentIndex = text.indexOf(startCommentExpression);
64 int endCommentIndex = text.indexOf(endCommentExpression, startCommentIndex);
67 while (startQouteIndex >= 0) {
68 endQouteIndex = text.indexOf(qouteExpression, startQouteIndex+1);
70 if (endQouteIndex == -1) {
71 setCurrentBlockState(1);
72 commentLength = text.length() - startQouteIndex;
74 commentLength = endQouteIndex - startQouteIndex
75 + qouteExpression.matchedLength();
77 setFormat(startQouteIndex, commentLength, qouteFormat);
78 startQouteIndex = text.indexOf(qouteExpression,
79 startQouteIndex + commentLength);
82 if(startCommentIndex < 0)
84 while (indexKeyword >= 0) {
85 int length = keyword.matchedLength();
86 setFormat(indexKeyword, length, keywordFormat);
87 indexKeyword = text.indexOf(keyword, indexKeyword + length);
91 while (startCommentIndex >= 0) {
92 endCommentIndex = text.indexOf(endCommentExpression, startCommentIndex);
94 if (endCommentIndex == -1) {
95 setCurrentBlockState(1);
96 commentLength = text.length() - startCommentIndex;
98 commentLength = endCommentIndex - startCommentIndex
99 + endCommentExpression.matchedLength();
101 setFormat(startCommentIndex, commentLength, commentFormat);
102 startCommentIndex = text.indexOf(startCommentExpression,
103 startCommentIndex + commentLength);
virtual ~QmitkPythonScriptEditorHighlighter()
QmitkPythonScriptEditorHighlighter(QTextDocument *parent)
void highlightComments(const QString &text)
void highlightBlock(const QString &text)