cppcheck/gui/codeeditor.h

107 lines
2.6 KiB
C
Raw Normal View History

#ifndef CODEEDITOR_H
#define CODEEDITOR_H
#include <QSyntaxHighlighter>
#include <QPlainTextEdit>
#include <QObject>
#include <QRegularExpression>
#include "codeeditorstyle.h"
class QPaintEvent;
class QResizeEvent;
class QSize;
class QWidget;
class LineNumberArea;
class Highlighter : public QSyntaxHighlighter {
Q_OBJECT
public:
2019-06-08 07:24:38 +02:00
explicit Highlighter(QTextDocument *parent,
CodeEditorStyle *widgetStyle);
2018-02-18 12:06:54 +01:00
void setSymbols(const QStringList &symbols);
protected:
void highlightBlock(const QString &text) override;
private:
struct HighlightingRule {
QRegularExpression pattern;
QTextCharFormat format;
};
2018-06-18 10:10:11 +02:00
QVector<HighlightingRule> mHighlightingRules;
QVector<HighlightingRule> mHighlightingRulesWithSymbols;
QRegularExpression mCommentStartExpression;
QRegularExpression mCommentEndExpression;
QTextCharFormat mKeywordFormat;
QTextCharFormat mClassFormat;
QTextCharFormat mSingleLineCommentFormat;
QTextCharFormat mMultiLineCommentFormat;
QTextCharFormat mQuotationFormat;
QTextCharFormat mSymbolFormat;
CodeEditorStyle *mWidgetStyle;
};
class CodeEditor : public QPlainTextEdit {
Q_OBJECT
public:
2019-06-08 07:24:38 +02:00
explicit CodeEditor(QWidget *parent,
CodeEditorStyle *widgetStyle = nullptr);
CodeEditor(const CodeEditor &) = delete;
CodeEditor &operator=(const CodeEditor &) = delete;
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();
2018-09-01 06:39:22 +02:00
/**
* Set source code to show, goto error line and highlight that line.
* \param code The source code.
* \param errorLine line number
* \param symbols the related symbols, these are marked
*/
2018-02-18 12:06:54 +01:00
void setError(const QString &code, int errorLine, const QStringList &symbols);
protected:
void resizeEvent(QResizeEvent *event) override;
private slots:
void updateLineNumberAreaWidth(int newBlockCount);
void highlightErrorLine();
void updateLineNumberArea(const QRect &, int);
private:
2018-06-18 10:10:11 +02:00
QWidget *mLineNumberArea;
Highlighter *mHighlighter;
CodeEditorStyle *mWidgetStyle;
int mErrorPosition;
};
class LineNumberArea : public QWidget {
public:
explicit LineNumberArea(CodeEditor *editor) : QWidget(editor) {
2018-06-18 10:10:11 +02:00
mCodeEditor = editor;
}
QSize sizeHint() const override {
2018-06-18 10:10:11 +02:00
return QSize(mCodeEditor->lineNumberAreaWidth(), 0);
}
protected:
void paintEvent(QPaintEvent *event) override {
2018-06-18 10:10:11 +02:00
mCodeEditor->lineNumberAreaPaintEvent(event);
}
private:
2018-06-18 10:10:11 +02:00
CodeEditor *mCodeEditor;
};
#endif // CODEEDITOR_H