Cppcheck
codeeditor.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef CODEEDITOR_H
20 #define CODEEDITOR_H
21 
22 #include <QObject>
23 #include <QPlainTextEdit>
24 #include <QRegularExpression>
25 #include <QSize>
26 #include <QString>
27 #include <QStringList>
28 #include <QSyntaxHighlighter>
29 #include <QTextCharFormat>
30 #include <QVector>
31 #include <QWidget>
32 
33 class CodeEditorStyle;
34 class QPaintEvent;
35 class QRect;
36 class QResizeEvent;
37 class QTextDocument;
38 
39 class Highlighter : public QSyntaxHighlighter {
40  Q_OBJECT
41 
42 public:
43  explicit Highlighter(QTextDocument *parent,
44  CodeEditorStyle *widgetStyle);
45 
46  void setSymbols(const QStringList &symbols);
47 
48  void setStyle(const CodeEditorStyle &newStyle);
49 
50 protected:
51  void highlightBlock(const QString &text) override;
52 
53 private:
54  enum RuleRole {
55  Keyword = 1,
56  Class = 2,
57  Comment = 3,
58  Quote = 4,
59  Symbol = 5
60  };
62  QRegularExpression pattern;
63  QTextCharFormat format;
65  };
66 
67  void applyFormat(HighlightingRule &rule);
68 
69  QVector<HighlightingRule> mHighlightingRules;
70  QVector<HighlightingRule> mHighlightingRulesWithSymbols;
71 
72  QRegularExpression mCommentStartExpression;
73  QRegularExpression mCommentEndExpression;
74 
75  QTextCharFormat mKeywordFormat;
76  QTextCharFormat mClassFormat;
77  QTextCharFormat mSingleLineCommentFormat;
78  QTextCharFormat mMultiLineCommentFormat;
79  QTextCharFormat mQuotationFormat;
80  QTextCharFormat mSymbolFormat;
81 
83 };
84 
85 class CodeEditor : public QPlainTextEdit {
86  Q_OBJECT
87 
88 public:
89  explicit CodeEditor(QWidget *parent);
90  CodeEditor(const CodeEditor &) = delete;
91  CodeEditor &operator=(const CodeEditor &) = delete;
92  ~CodeEditor() override;
93 
94  void lineNumberAreaPaintEvent(const QPaintEvent *event);
95  int lineNumberAreaWidth();
96  void setStyle(const CodeEditorStyle& newStyle);
97 
98  /**
99  * Set source code to show, goto error line and highlight that line.
100  * \param code The source code.
101  * \param errorLine line number
102  * \param symbols the related symbols, these are marked
103  */
104  void setError(const QString &code, int errorLine, const QStringList &symbols);
105 
106  /**
107  * Goto another error in existing source file
108  * \param errorLine line number
109  * \param symbols the related symbols, these are marked
110  */
111  void setError(int errorLine, const QStringList &symbols);
112 
113  void setFileName(const QString &fileName) {
114  mFileName = fileName;
115  }
116 
117  const QString& getFileName() const {
118  return mFileName;
119  }
120 
121  void clear() {
122  mFileName.clear();
123  setPlainText(QString());
124  }
125 
126 protected:
127  void resizeEvent(QResizeEvent *event) override;
128 
129 private slots:
130  void updateLineNumberAreaWidth(int newBlockCount);
131  void highlightErrorLine();
132  void updateLineNumberArea(const QRect & /*rect*/, int /*dy*/);
133 
134 private:
135  QString generateStyleString();
136 
137 private:
138  QWidget *mLineNumberArea;
142  QString mFileName;
143 };
144 
145 
146 class LineNumberArea : public QWidget {
147 public:
148  explicit LineNumberArea(CodeEditor *editor) : QWidget(editor) {
149  mCodeEditor = editor;
150  }
151 
152  QSize sizeHint() const override {
153  return QSize(mCodeEditor->lineNumberAreaWidth(), 0);
154  }
155 
156 protected:
157  void paintEvent(QPaintEvent *event) override {
159  }
160 
161 private:
163 };
164 
165 #endif // CODEEDITOR_H
~CodeEditor() override
Definition: codeeditor.cpp:305
const QString & getFileName() const
Definition: codeeditor.h:117
void updateLineNumberAreaWidth(int newBlockCount)
Definition: codeeditor.cpp:380
CodeEditor & operator=(const CodeEditor &)=delete
void clear()
Definition: codeeditor.h:121
Highlighter * mHighlighter
Definition: codeeditor.h:139
CodeEditor(const CodeEditor &)=delete
void lineNumberAreaPaintEvent(const QPaintEvent *event)
Definition: codeeditor.cpp:423
void updateLineNumberArea(const QRect &, int)
Definition: codeeditor.cpp:385
QWidget * mLineNumberArea
Definition: codeeditor.h:138
int mErrorPosition
Definition: codeeditor.h:141
QString mFileName
Definition: codeeditor.h:142
void setStyle(const CodeEditorStyle &newStyle)
Definition: codeeditor.cpp:325
void setFileName(const QString &fileName)
Definition: codeeditor.h:113
void resizeEvent(QResizeEvent *event) override
Definition: codeeditor.cpp:396
void highlightErrorLine()
Definition: codeeditor.cpp:403
CodeEditorStyle * mWidgetStyle
Definition: codeeditor.h:140
QString generateStyleString()
Definition: codeeditor.cpp:448
void setError(const QString &code, int errorLine, const QStringList &symbols)
Set source code to show, goto error line and highlight that line.
Definition: codeeditor.cpp:335
CodeEditor(QWidget *parent)
Definition: codeeditor.cpp:272
int lineNumberAreaWidth()
Definition: codeeditor.cpp:363
QRegularExpression mCommentStartExpression
Definition: codeeditor.h:72
QTextCharFormat mKeywordFormat
Definition: codeeditor.h:75
QTextCharFormat mSingleLineCommentFormat
Definition: codeeditor.h:77
QTextCharFormat mClassFormat
Definition: codeeditor.h:76
void applyFormat(HighlightingRule &rule)
Definition: codeeditor.cpp:251
Highlighter(QTextDocument *parent, CodeEditorStyle *widgetStyle)
Definition: codeeditor.cpp:45
QVector< HighlightingRule > mHighlightingRules
Definition: codeeditor.h:69
QRegularExpression mCommentEndExpression
Definition: codeeditor.h:73
void highlightBlock(const QString &text) override
Definition: codeeditor.cpp:219
QTextCharFormat mQuotationFormat
Definition: codeeditor.h:79
CodeEditorStyle * mWidgetStyle
Definition: codeeditor.h:82
void setStyle(const CodeEditorStyle &newStyle)
Definition: codeeditor.cpp:195
void setSymbols(const QStringList &symbols)
Definition: codeeditor.cpp:183
QTextCharFormat mMultiLineCommentFormat
Definition: codeeditor.h:78
QTextCharFormat mSymbolFormat
Definition: codeeditor.h:80
QVector< HighlightingRule > mHighlightingRulesWithSymbols
Definition: codeeditor.h:70
void paintEvent(QPaintEvent *event) override
Definition: codeeditor.h:157
CodeEditor * mCodeEditor
Definition: codeeditor.h:162
QSize sizeHint() const override
Definition: codeeditor.h:152
LineNumberArea(CodeEditor *editor)
Definition: codeeditor.h:148
QRegularExpression pattern
Definition: codeeditor.h:62