Developer Documentation
PythonSyntaxHighlighter.hh
1 
2 /*
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 (docklight.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 
28 
29 
30 #include <QSyntaxHighlighter>
31 
34 {
35 public:
36  HighlightingRule(const QString &patternStr, int n, const QTextCharFormat &matchingFormat)
37 {
38  originalRuleStr = patternStr;
39  pattern = QRegExp(patternStr);
40  nth = n;
41  format = matchingFormat;
42 }
43  QString originalRuleStr;
44  QRegExp pattern;
45  int nth;
46  QTextCharFormat format;
47 };
48 
50 class PythonSyntaxHighlighter : public QSyntaxHighlighter
51 {
52  Q_OBJECT
53 public:
54  explicit PythonSyntaxHighlighter(QTextDocument *parent = 0);
55 protected:
56  void highlightBlock(const QString &text);
57 private:
58  QStringList keywords;
59  QStringList operators;
60  QStringList braces;
61 
62  QHash<QString, QTextCharFormat> basicStyles;
63 
64  void initializeRules();
65 
67  bool matchMultiline(const QString &text, const QRegExp &delimiter, const int inState, const QTextCharFormat &style);
68  const QTextCharFormat getTextCharFormat(const QString &colorName, const QString &style = QString());
69 
70  QList<HighlightingRule> rules;
71  QRegExp triSingleQuote;
72  QRegExp triDoubleQuote;
73 };
Container to describe a highlighting rule. Based on a regular expression, a relevant match # and the ...
Implementation of highlighting for Python code.