2018-02-17 22:24:41 +01:00
|
|
|
#ifndef CODEEDITOR_H
|
|
|
|
#define CODEEDITOR_H
|
|
|
|
|
|
|
|
#include <QSyntaxHighlighter>
|
|
|
|
#include <QPlainTextEdit>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QRegularExpression>
|
2019-06-08 07:23:48 +02:00
|
|
|
#include "codeeditorstyle.h"
|
2018-02-17 22:24:41 +01:00
|
|
|
|
|
|
|
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-17 22:24:41 +01:00
|
|
|
|
2018-02-18 12:06:54 +01:00
|
|
|
void setSymbols(const QStringList &symbols);
|
|
|
|
|
2019-06-25 15:29:15 +02:00
|
|
|
void setStyle(const CodeEditorStyle &newStyle);
|
2019-06-23 19:04:53 +02:00
|
|
|
|
2018-02-17 22:24:41 +01:00
|
|
|
protected:
|
|
|
|
void highlightBlock(const QString &text) override;
|
|
|
|
|
|
|
|
private:
|
2019-06-23 19:04:53 +02:00
|
|
|
enum RuleRole {
|
|
|
|
Keyword = 1,
|
|
|
|
Class = 2,
|
|
|
|
Comment = 3,
|
|
|
|
Quote = 4,
|
|
|
|
Symbol = 5
|
|
|
|
};
|
2018-02-17 22:24:41 +01:00
|
|
|
struct HighlightingRule {
|
|
|
|
QRegularExpression pattern;
|
|
|
|
QTextCharFormat format;
|
2019-06-23 19:04:53 +02:00
|
|
|
RuleRole ruleRole;
|
2018-02-17 22:24:41 +01:00
|
|
|
};
|
2019-06-23 19:04:53 +02:00
|
|
|
|
2019-06-25 15:29:15 +02:00
|
|
|
void applyFormat(HighlightingRule &rule);
|
2019-06-23 19:04:53 +02:00
|
|
|
|
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;
|
2019-06-08 07:23:48 +02:00
|
|
|
|
|
|
|
CodeEditorStyle *mWidgetStyle;
|
2018-02-17 22:24:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class CodeEditor : public QPlainTextEdit {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2019-06-23 19:04:53 +02:00
|
|
|
explicit CodeEditor(QWidget *parent);
|
2018-05-01 09:35:53 +02:00
|
|
|
CodeEditor(const CodeEditor &) = delete;
|
|
|
|
CodeEditor &operator=(const CodeEditor &) = delete;
|
2019-06-23 19:04:53 +02:00
|
|
|
~CodeEditor();
|
2018-02-17 22:24:41 +01:00
|
|
|
|
|
|
|
void lineNumberAreaPaintEvent(QPaintEvent *event);
|
|
|
|
int lineNumberAreaWidth();
|
2019-06-23 19:04:53 +02:00
|
|
|
void setStyle(const CodeEditorStyle& newStyle);
|
2018-02-17 22:24:41 +01:00
|
|
|
|
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);
|
2018-02-17 22:24:41 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void resizeEvent(QResizeEvent *event) override;
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void updateLineNumberAreaWidth(int newBlockCount);
|
2018-02-21 22:49:50 +01:00
|
|
|
void highlightErrorLine();
|
2018-02-17 22:24:41 +01:00
|
|
|
void updateLineNumberArea(const QRect &, int);
|
|
|
|
|
2019-06-23 19:04:53 +02:00
|
|
|
private:
|
|
|
|
QString generateStyleString();
|
|
|
|
|
2018-02-17 22:24:41 +01:00
|
|
|
private:
|
2018-06-18 10:10:11 +02:00
|
|
|
QWidget *mLineNumberArea;
|
|
|
|
Highlighter *mHighlighter;
|
2019-06-08 07:23:48 +02:00
|
|
|
CodeEditorStyle *mWidgetStyle;
|
2018-02-17 22:24:41 +01:00
|
|
|
int mErrorPosition;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class LineNumberArea : public QWidget {
|
|
|
|
public:
|
|
|
|
explicit LineNumberArea(CodeEditor *editor) : QWidget(editor) {
|
2018-06-18 10:10:11 +02:00
|
|
|
mCodeEditor = editor;
|
2018-02-17 22:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize sizeHint() const override {
|
2018-06-18 10:10:11 +02:00
|
|
|
return QSize(mCodeEditor->lineNumberAreaWidth(), 0);
|
2018-02-17 22:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *event) override {
|
2018-06-18 10:10:11 +02:00
|
|
|
mCodeEditor->lineNumberAreaPaintEvent(event);
|
2018-02-17 22:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-06-18 10:10:11 +02:00
|
|
|
CodeEditor *mCodeEditor;
|
2018-02-17 22:24:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CODEEDITOR_H
|