BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
PythonSyntaxHighlighter.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Info/PythonSyntaxHighlighter.h
6 //! @brief Defines class PythonSyntaxHighlighter
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 /*
16 This is a C++ port of the following PyQt example
17 http://diotavelli.net/PyQtWiki/Python%20syntax%20highlighting
18 C++ port by Frankie Simon (docklight.de, www.fuh-edv.de)
19 
20 The following free software license applies for this file ("X11 license"):
21 
22 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
23 and associated documentation files (the "Software"), to deal in the Software without restriction,
24 including without limitation the rights to use, copy, modify, merge, publish, distribute,
25 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
26 furnished to do so, subject to the following conditions:
27 
28 The above copyright notice and this permission notice shall be included in all copies or substantial
29 portions of the Software.
30 
31 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
32 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
35 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37 
38 #ifndef BORNAGAIN_GUI_VIEW_INFO_PYTHONSYNTAXHIGHLIGHTER_H
39 #define BORNAGAIN_GUI_VIEW_INFO_PYTHONSYNTAXHIGHLIGHTER_H
40 
41 #include <QSyntaxHighlighter>
42 
43 //! Container to describe a highlighting rule. Based on a regular expression, a relevant match # and
44 //! the format.
46 public:
47  HighlightingRule(const QString& patternStr, int n, const QTextCharFormat& matchingFormat)
48  {
49  originalRuleStr = patternStr;
50  pattern = QRegExp(patternStr);
51  nth = n;
52  format = matchingFormat;
53  }
54  QString originalRuleStr;
55  QRegExp pattern;
56  int nth;
57  QTextCharFormat format;
58 };
59 
60 //! Implementation of highlighting for Python code.
61 class PythonSyntaxHighlighter : public QSyntaxHighlighter {
62  Q_OBJECT
63 public:
64  PythonSyntaxHighlighter(QTextDocument* parent = nullptr);
65 
66 protected:
67  void highlightBlock(const QString& text) override;
68 
69 private:
70  QStringList keywords;
71  QStringList operators;
72  QStringList braces;
73 
74  QHash<QString, QTextCharFormat> basicStyles;
75 
77 
78  //! Highlighst multi-line strings, returns true if after processing we are still within the
79  // multi-line section.
80  bool matchMultiline(const QString& text, const QRegExp& delimiter, int inState,
81  const QTextCharFormat& style);
82  QTextCharFormat getTextCharFormat(const QString& colorName, const QString& style = "");
83 
84  QList<HighlightingRule> rules;
85  QRegExp triSingleQuote;
86  QRegExp triDoubleQuote;
87 };
88 
89 #endif // BORNAGAIN_GUI_VIEW_INFO_PYTHONSYNTAXHIGHLIGHTER_H
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)
Implementation of highlighting for Python code.
PythonSyntaxHighlighter(QTextDocument *parent=nullptr)
void highlightBlock(const QString &text) override
QHash< QString, QTextCharFormat > basicStyles
bool matchMultiline(const QString &text, const QRegExp &delimiter, int inState, const QTextCharFormat &style)
Highlighst multi-line strings, returns true if after processing we are still within the.
QTextCharFormat getTextCharFormat(const QString &colorName, const QString &style="")
QList< HighlightingRule > rules