Fraxinus  22.04
An IGT application
cxElastixSyntaxHighlighter.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
13 #include <iostream>
14 
15 namespace cx
16 {
17 
19  QSyntaxHighlighter(parent)
20 {
21 
22 }
23 
25 {
26  QTextCharFormat format;
27  QString pattern;
28 
29  // ElastiX parameters: (name value)
30  format = QTextCharFormat();
31  format.setForeground(QColor("black"));
32  format.setFontWeight(75);
33  pattern = "\\([^\\)]*\\)";
34  applyFormat(text, format, pattern);
35 
36  //quotations: "text"
37  format = QTextCharFormat();
38  format.setForeground(QColor("blue"));
39  pattern = "\"[^\"]*\"";
40  applyFormat(text, format, pattern);
41 
42  //numbers: float or integer
43  format = QTextCharFormat();
44  format.setForeground(QColor("red"));
45  pattern = "\\d[\\d|\\.]*";
46  applyFormat(text, format, pattern);
47 
48  // ElastiX comment: // comment line
49  format = QTextCharFormat();
50  format.setForeground(QColor(63, 127, 95));
51  pattern = "//.*";
52 // pattern = "//[^\\n]*\\n";
53  applyFormat(text, format, pattern);
54 
55  // XML block: <name ...>, </name>
56  format = QTextCharFormat();
57  format.setForeground(QColor("green"));
58  pattern = "\\<[^!]\\S*[\\>]?";
59  applyFormat(text, format, pattern);
60 
61  // XML block: <!-- ... -->
62  format = QTextCharFormat();
63  format.setForeground(QColor("gray"));
64  pattern = "\\<!--.*--\\>";
65  applyFormat(text, format, pattern);
66 
67 }
68 
69 void ElastixSyntaxHighlighter::highlightTimestamp(const QString &text)
70 {
71  QTextCharFormat format;
72  QString pattern;
73 
74  // timestamp format: [12:30:00 :000] with some parts optional
75  QString stampPattern = "^\\[?[0-9]{2}:[0-9]{2}:[0-9]{2}( :[0-9]{3})?\\]?";
76  // heading part: timestamp + function name from SW_LOG.
77  QString headingPattern = stampPattern + "\\s\\w*\\s--\\s";
78  headingPattern = headingPattern + "|------->.*"; // add the SW_LOG control lines.
79 
80  // set entire header to bold
81  format.setFontWeight(QFont::Bold);
82  format.setForeground(Qt::black);
83  applyFormat(text, format, headingPattern);
84 
85  // set the timestamp part to magenta
86  format.setFontWeight(QFont::Bold);
87  format.setForeground(Qt::darkMagenta);
88  applyFormat(text, format, stampPattern);
89 }
90 
91 void ElastixSyntaxHighlighter::applyFormat(const QString &text, const QTextCharFormat& format, const QString pattern)
92 {
93  QRegExp expression(pattern);
94  int index = text.indexOf(expression);
95  while (index >= 0)
96  {
97  int length = expression.matchedLength();
98 // std::cout << "---[hit] " << index << " " << length << " - " << text.mid(index,length).toStdString() << std::endl;
99  setFormat(index, length, format);
100  index = text.indexOf(expression, index + length);
101  }
102 }
103 
104 } /* namespace cx */
ElastixSyntaxHighlighter(QTextDocument *parent)
virtual void highlightBlock(const QString &text)
RealScalar length() const
Namespace for all CustusX production code.