Debian Codegraph @

avogadro/qtgui/generichighlighter.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_QTPLUGINS_GENERICHIGHLIGHTER_H
7 #define AVOGADRO_QTPLUGINS_GENERICHIGHLIGHTER_H
8
9 #include "avogadroqtguiexport.h"
10
11 #include <avogadro/core/avogadrocore.h>
12
13 #include <QtGui/QSyntaxHighlighter>
14 #include <QtGui/QTextCharFormat>
15
16 #include <QRegularExpression>
17
18 namespace Avogadro {
19 namespace QtGui {
20
21 /**
22 * @brief The GenericHighlighter class provides a regexp-based programmable
23 * syntax highlighter.
24 */
25 class AVOGADROQTGUI_EXPORT GenericHighlighter : public QSyntaxHighlighter
26 {
27 Q_OBJECT
28 public:
29 /**
30 * @brief The Rule class stores a syntax highlighting rule as a set of
31 * QRegularExpression patterns and a text format.
32 */
33 class AVOGADROQTGUI_EXPORT Rule
34 {
35 public:
36 Rule() {}
37 ~Rule() {}
38
39 /** Add the pattern @a regexp to this Rule. */
40 void addPattern(const QRegularExpression& regexp);
41
42 /** Set this Rule's text format. */
43 void setFormat(const QTextCharFormat& format);
44
45 /** Apply this rule to the string of text, updating the highlighter if any
46 * matches are found. */
47 void apply(const QString& text, GenericHighlighter& highlighter);
48
49 private:
50 QList<QRegularExpression> m_patterns;
51 QTextCharFormat m_format;
52 };
53
54 /** Construct a highlighter with an empty rule set. */
55 explicit GenericHighlighter(QObject* parent_ = nullptr);
56
57 ~GenericHighlighter() override;
58
59 /** Construct a new highlighter using the rule set of @a other. */
60 GenericHighlighter(const GenericHighlighter& other);
61
62 /** Replace this highlighter's rule set with that of @a other. */
63 GenericHighlighter& operator=(GenericHighlighter other);
64
65 /** Concatenate @a other's rule set with this highlighter's rule set. */
66 GenericHighlighter& operator+=(const GenericHighlighter& other);
67
68 /** Add a new rule to this highlighter, returning a reference to the new
69 * rule. */
70 Rule& addRule();
71
72 /** @return The number of rules in this highlighter. */
73 int ruleCount() const;
74
75 /** @return A reference to the rule at the specified zero-based index. */
76 Rule& rule(int idx);
77
78 /** @return A reference to the rule at the specified zero-based index. */
79 const Rule& rule(int idx) const;
80
81 /** @return An ordered list of this highlighter's rules. */
82 QList<Rule> rules() const;
83
84 friend void swap(GenericHighlighter& first, GenericHighlighter& second)
85 {
86 using std::swap;
87 swap(first.m_rules, second.m_rules);
88 }
89
90 protected:
91 void highlightBlock(const QString& text) override;
92
93 private:
94 QList<Rule> m_rules;
95 };
96
97 } // namespace QtGui
98 } // namespace Avogadro
99
100 #endif // AVOGADRO_QTPLUGINS_GENERICHIGHLIGHTER_H