16 : QSyntaxHighlighter(parent)
26 QTextCharFormat commentFormat;
27 commentFormat.setFontWeight(QFont::Courier);
28 commentFormat.setForeground(Qt::darkGreen);
30 QTextCharFormat keywordFormat;
31 keywordFormat.setFontWeight(QFont::Bold);
32 keywordFormat.setForeground(Qt::blue);
33 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";
35 QTextCharFormat qouteFormat;
36 qouteFormat.setForeground(Qt::gray);
38 QTextCharFormat numberFormat;
39 numberFormat.setForeground(Qt::red);
41 QRegExp numberExpression(
"\\d+");
42 int indexNumbers = text.indexOf(numberExpression);
44 while (indexNumbers >= 0) {
45 int length = numberExpression.matchedLength();
46 setFormat(indexNumbers, length, numberFormat);
47 indexNumbers = text.indexOf(numberExpression, indexNumbers + length);
50 QRegExp qouteExpression(
"\"");
51 int startQouteIndex = text.indexOf(qouteExpression);
52 int endQouteIndex = text.indexOf(qouteExpression, startQouteIndex);
54 QRegExp keyword(pattern);
55 int indexKeyword = text.indexOf(keyword);
57 QRegExp startCommentExpression(
"^#");
58 QRegExp endCommentExpression(
"\\n");
59 int startCommentIndex = text.indexOf(startCommentExpression);
60 int endCommentIndex = text.indexOf(endCommentExpression, startCommentIndex);
63 while (startQouteIndex >= 0) {
64 endQouteIndex = text.indexOf(qouteExpression, startQouteIndex+1);
66 if (endQouteIndex == -1) {
67 setCurrentBlockState(1);
68 commentLength = text.length() - startQouteIndex;
70 commentLength = endQouteIndex - startQouteIndex
71 + qouteExpression.matchedLength();
73 setFormat(startQouteIndex, commentLength, qouteFormat);
74 startQouteIndex = text.indexOf(qouteExpression,
75 startQouteIndex + commentLength);
78 if(startCommentIndex < 0)
80 while (indexKeyword >= 0) {
81 int length = keyword.matchedLength();
82 setFormat(indexKeyword, length, keywordFormat);
83 indexKeyword = text.indexOf(keyword, indexKeyword + length);
87 while (startCommentIndex >= 0) {
88 endCommentIndex = text.indexOf(endCommentExpression, startCommentIndex);
90 if (endCommentIndex == -1) {
91 setCurrentBlockState(1);
92 commentLength = text.length() - startCommentIndex;
94 commentLength = endCommentIndex - startCommentIndex
95 + endCommentExpression.matchedLength();
97 setFormat(startCommentIndex, commentLength, commentFormat);
98 startCommentIndex = text.indexOf(startCommentExpression,
99 startCommentIndex + commentLength);
void highlightBlock(const QString &text) override
QmitkPythonScriptEditorHighlighter(QTextDocument *parent)
~QmitkPythonScriptEditorHighlighter() override
void highlightComments(const QString &text)