CustusX  2023.01.05-dev+develop.0da12
An IGT application
snwSyntaxHighlighter.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 
12 #include "snwSyntaxHighlighter.h"
13 #include <iostream>
14 
15 namespace snw
16 {
17 
18 SyntaxHighlighter::SyntaxHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent)
19 {
20 
21 }
22 
23 void SyntaxHighlighter::highlightBlock(const QString &text)
24 {
25  QTextCharFormat format;
26  QString pattern;
27 
28  //xml element: <elem>
29  format = QTextCharFormat();
30  format.setForeground(QColor("darkviolet"));
31  format.setFontWeight(QFont::Bold);
32  pattern = "<[^>]*>";
33  this->applyFormat(text, format, pattern);
34 
35  //xml comment: <!comment>
36  format = QTextCharFormat();
37  format.setForeground(QColor("green"));
38  format.setFontItalic(true);
39  pattern = "<![^>]*>";
40  applyFormat(text, format, pattern);
41 
42  //quotations: "text"
43  format = QTextCharFormat();
44  format.setForeground(QColor("blue"));
45  pattern = "\"[^\"]*\"";
46  applyFormat(text, format, pattern);
47 }
48 
49 void SyntaxHighlighter::highlightTimestamp(const QString &text)
50 {
51  QTextCharFormat format;
52  QString pattern;
53 
54  // timestamp format: [12:30:00 :000] with some parts optional
55  QString stampPattern = "^\\[?[0-9]{2}:[0-9]{2}:[0-9]{2}( :[0-9]{3})?\\]?";
56  // heading part: timestamp + function name from SW_LOG.
57  QString headingPattern = stampPattern+"\\s\\w*\\s--\\s";
58  headingPattern = headingPattern+"|------->.*"; // add the SW_LOG control lines.
59 
60  // set entire header to bold
61  format.setFontWeight(QFont::Bold);
62  format.setForeground(Qt::black);
63  applyFormat(text, format, headingPattern);
64 
65  // set the timestamp part to magenta
66  format.setFontWeight(QFont::Bold);
67  format.setForeground(Qt::darkMagenta);
68  applyFormat(text, format, stampPattern);
69 }
70 
71 void SyntaxHighlighter::applyFormat(const QString &text, const QTextCharFormat& format, const QString pattern)
72 {
73  QRegExp expression(pattern);
74  int index = text.indexOf(expression);
75  while (index >= 0)
76  {
77  int length = expression.matchedLength();
78 // std::cout << "---[hit] " << index << " " << length << " - " << text.mid(index,length).toStdString() << std::endl;
79  setFormat(index, length, format);
80  index = text.indexOf(expression, index + length);
81  }
82 }
83 
84 
85 } // namespace snw
SyntaxHighlighter(QTextDocument *parent)
RealScalar length() const
virtual void highlightBlock(const QString &text)