Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
20 : QSyntaxHighlighter(parent)
21 {
22 }
23 
25 {
26 }
27 
29 {
30  QTextCharFormat commentFormat;
31  commentFormat.setFontWeight(QFont::Courier);
32  commentFormat.setForeground(Qt::darkGreen);
33 
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";
38 
39  QTextCharFormat qouteFormat;
40  qouteFormat.setForeground(Qt::gray);
41 
42  QTextCharFormat numberFormat;
43  numberFormat.setForeground(Qt::red);
44 
45  QRegExp numberExpression("\\d+");
46  int indexNumbers = text.indexOf(numberExpression);
47 
48  while (indexNumbers >= 0) {
49  int length = numberExpression.matchedLength();
50  setFormat(indexNumbers, length, numberFormat);
51  indexNumbers = text.indexOf(numberExpression, indexNumbers + length);
52  }
53 
54  QRegExp qouteExpression("\"");
55  int startQouteIndex = text.indexOf(qouteExpression);
56  int endQouteIndex = text.indexOf(qouteExpression, startQouteIndex);
57 
58  QRegExp keyword(pattern);
59  int indexKeyword = text.indexOf(keyword);
60 
61  QRegExp startCommentExpression("^#");
62  QRegExp endCommentExpression("\\n");
63  int startCommentIndex = text.indexOf(startCommentExpression);
64  int endCommentIndex = text.indexOf(endCommentExpression, startCommentIndex);
65 
66 
67  while (startQouteIndex >= 0) {
68  endQouteIndex = text.indexOf(qouteExpression, startQouteIndex+1);
69  int commentLength;
70  if (endQouteIndex == -1) {
71  setCurrentBlockState(1);
72  commentLength = text.length() - startQouteIndex;
73  } else {
74  commentLength = endQouteIndex - startQouteIndex
75  + qouteExpression.matchedLength();
76  }
77  setFormat(startQouteIndex, commentLength, qouteFormat);
78  startQouteIndex = text.indexOf(qouteExpression,
79  startQouteIndex + commentLength);
80  }
81 
82  if(startCommentIndex < 0)
83  {
84  while (indexKeyword >= 0) {
85  int length = keyword.matchedLength();
86  setFormat(indexKeyword, length, keywordFormat);
87  indexKeyword = text.indexOf(keyword, indexKeyword + length);
88  }
89  }
90 
91  while (startCommentIndex >= 0) {
92  endCommentIndex = text.indexOf(endCommentExpression, startCommentIndex);
93  int commentLength;
94  if (endCommentIndex == -1) {
95  setCurrentBlockState(1);
96  commentLength = text.length() - startCommentIndex;
97  } else {
98  commentLength = endCommentIndex - startCommentIndex
99  + endCommentExpression.matchedLength();
100  }
101  setFormat(startCommentIndex, commentLength, commentFormat);
102  startCommentIndex = text.indexOf(startCommentExpression,
103  startCommentIndex + commentLength);
104  }
105 }
106 
108 {
109 }
110