DynExp
Highly flexible laboratory automation for dynamically changing experiments.
PythonSyntaxHighlighter.h
Go to the documentation of this file.
1 /*
2 $Id: PythonSyntaxHighlighter.h 167 2013 - 11 - 03 17 : 01 : 22Z oliver $
3 This is a C++ port of the following PyQt example
4 http://diotavelli.net/PyQtWiki/Python%20syntax%20highlighting
5 C++ port by Frankie Simon (www.kickdrive.de, www.fuh-edv.de)
6 
7 The following free software license applies for this file ("X11 license"):
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10 and associated documentation files (the "Software"), to deal in the Software without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13 subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in all copies or substantial
16 portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
19 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24 
25 #pragma once
26 
27 #include <QSyntaxHighlighter>
28 
31 {
32 public:
33  HighlightingRule(const QString& patternStr, int n, const QTextCharFormat& matchingFormat)
34  {
35  originalRuleStr = patternStr;
36  pattern = QRegularExpression(patternStr);
37  nth = n;
38  format = matchingFormat;
39  }
40  QString originalRuleStr;
41  QRegularExpression pattern;
42  int nth;
43  QTextCharFormat format;
44 };
45 
47 class PythonSyntaxHighlighter : public QSyntaxHighlighter
48 {
49  Q_OBJECT
50 public:
51  PythonSyntaxHighlighter(QObject* parent) : QSyntaxHighlighter(parent) { Init(); }
52  PythonSyntaxHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent) { Init(); }
53 
54 protected:
55  void highlightBlock(const QString& text);
56 
57 private:
58  QStringList keywords;
59  QStringList operators;
60  QStringList braces;
61 
62  QHash<QString, QTextCharFormat> basicStyles;
63 
64  void Init();
65 
66  void initializeRules();
67 
69  bool matchMultiline(const QString& text, const QRegularExpression& delimiter, const int inState, const QTextCharFormat& style);
70  const QTextCharFormat getTextCharFormat(const QString& colorName, const QString& style = QString());
71 
72  QList<HighlightingRule> rules;
73  QRegularExpression triSingleQuote;
74  QRegularExpression triDoubleQuote;
75 };
Container to describe a highlighting rule. Based on a regular expression, a relevant match # and the ...
HighlightingRule(const QString &patternStr, int n, const QTextCharFormat &matchingFormat)
QRegularExpression pattern
Implementation of highlighting for Python code.
void highlightBlock(const QString &text)
QRegularExpression triDoubleQuote
const QTextCharFormat getTextCharFormat(const QString &colorName, const QString &style=QString())
PythonSyntaxHighlighter(QObject *parent)
QRegularExpression triSingleQuote
QHash< QString, QTextCharFormat > basicStyles
bool matchMultiline(const QString &text, const QRegularExpression &delimiter, const int inState, const QTextCharFormat &style)
Highlighst multi-line strings, returns true if after processing we are still within the multi-line se...
QList< HighlightingRule > rules
PythonSyntaxHighlighter(QTextDocument *parent)