Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkPythonScriptEditorHighlighter.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
16 : QSyntaxHighlighter(parent)
17 {
18 }
19 
21 {
22 }
23 
25 {
26  QTextCharFormat commentFormat;
27  commentFormat.setFontWeight(QFont::Courier);
28  commentFormat.setForeground(Qt::darkGreen);
29 
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";
34 
35  QTextCharFormat qouteFormat;
36  qouteFormat.setForeground(Qt::gray);
37 
38  QTextCharFormat numberFormat;
39  numberFormat.setForeground(Qt::red);
40 
41  QRegExp numberExpression("\\d+");
42  int indexNumbers = text.indexOf(numberExpression);
43 
44  while (indexNumbers >= 0) {
45  int length = numberExpression.matchedLength();
46  setFormat(indexNumbers, length, numberFormat);
47  indexNumbers = text.indexOf(numberExpression, indexNumbers + length);
48  }
49 
50  QRegExp qouteExpression("\"");
51  int startQouteIndex = text.indexOf(qouteExpression);
52  int endQouteIndex = text.indexOf(qouteExpression, startQouteIndex);
53 
54  QRegExp keyword(pattern);
55  int indexKeyword = text.indexOf(keyword);
56 
57  QRegExp startCommentExpression("^#");
58  QRegExp endCommentExpression("\\n");
59  int startCommentIndex = text.indexOf(startCommentExpression);
60  int endCommentIndex = text.indexOf(endCommentExpression, startCommentIndex);
61 
62 
63  while (startQouteIndex >= 0) {
64  endQouteIndex = text.indexOf(qouteExpression, startQouteIndex+1);
65  int commentLength;
66  if (endQouteIndex == -1) {
67  setCurrentBlockState(1);
68  commentLength = text.length() - startQouteIndex;
69  } else {
70  commentLength = endQouteIndex - startQouteIndex
71  + qouteExpression.matchedLength();
72  }
73  setFormat(startQouteIndex, commentLength, qouteFormat);
74  startQouteIndex = text.indexOf(qouteExpression,
75  startQouteIndex + commentLength);
76  }
77 
78  if(startCommentIndex < 0)
79  {
80  while (indexKeyword >= 0) {
81  int length = keyword.matchedLength();
82  setFormat(indexKeyword, length, keywordFormat);
83  indexKeyword = text.indexOf(keyword, indexKeyword + length);
84  }
85  }
86 
87  while (startCommentIndex >= 0) {
88  endCommentIndex = text.indexOf(endCommentExpression, startCommentIndex);
89  int commentLength;
90  if (endCommentIndex == -1) {
91  setCurrentBlockState(1);
92  commentLength = text.length() - startCommentIndex;
93  } else {
94  commentLength = endCommentIndex - startCommentIndex
95  + endCommentExpression.matchedLength();
96  }
97  setFormat(startCommentIndex, commentLength, commentFormat);
98  startCommentIndex = text.indexOf(startCommentExpression,
99  startCommentIndex + commentLength);
100  }
101 }
102 
104 {
105 }
106 
void highlightBlock(const QString &text) override