DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
PythonSyntaxHighlighter.h
Go to the documentation of this file.
1/*
2$Id: PythonSyntaxHighlighter.h 167 2013 - 11 - 03 17 : 01 : 22Z oliver $
3This is a C++ port of the following PyQt example
4http://diotavelli.net/PyQtWiki/Python%20syntax%20highlighting
5C++ port by Frankie Simon (www.kickdrive.de, www.fuh-edv.de)
6
7The following free software license applies for this file ("X11 license"):
8
9Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10and associated documentation files (the "Software"), to deal in the Software without restriction,
11including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all copies or substantial
16portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
19LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22USE OR OTHER DEALINGS IN THE SOFTWARE.
23*/
24
25#pragma once
26
27#include <QSyntaxHighlighter>
28
31{
32public:
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 }
41 QRegularExpression pattern;
42 int nth;
43 QTextCharFormat format;
44};
45
47class PythonSyntaxHighlighter : public QSyntaxHighlighter
48{
49 Q_OBJECT
50public:
51 PythonSyntaxHighlighter(QObject* parent) : QSyntaxHighlighter(parent) { Init(); }
52 PythonSyntaxHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent) { Init(); }
53
54protected:
55 void highlightBlock(const QString& text);
56
57private:
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)
const QTextCharFormat getTextCharFormat(const QString &colorName, const QString &style=QString())
PythonSyntaxHighlighter(QObject *parent)
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)