cppcheck/gui/codeeditor.h

157 lines
4.0 KiB
C
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
2022-02-05 11:45:17 +01:00
* Copyright (C) 2007-2022 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CODEEDITOR_H
#define CODEEDITOR_H
#include <QPlainTextEdit>
#include <QRegularExpression>
2022-02-02 16:17:28 +01:00
#include <QSyntaxHighlighter>
class CodeEditorStyle;
class QPaintEvent;
class QResizeEvent;
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);
2019-06-25 15:29:15 +02:00
void setStyle(const CodeEditorStyle &newStyle);
protected:
void highlightBlock(const QString &text) override;
private:
enum RuleRole {
Keyword = 1,
Class = 2,
Comment = 3,
Quote = 4,
Symbol = 5
};
struct HighlightingRule {
QRegularExpression pattern;
QTextCharFormat format;
RuleRole ruleRole;
};
2019-06-25 15:29:15 +02:00
void applyFormat(HighlightingRule &rule);
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:
explicit CodeEditor(QWidget *parent);
CodeEditor(const CodeEditor &) = delete;
CodeEditor &operator=(const CodeEditor &) = delete;
~CodeEditor() override;
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();
void setStyle(const CodeEditorStyle& newStyle);
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);
/**
* Goto another error in existing source file
* \param errorLine line number
* \param symbols the related symbols, these are marked
*/
void setError(int errorLine, const QStringList &symbols);
2020-07-19 11:10:53 +02:00
void setFileName(const QString &fileName) {
mFileName = fileName;
}
2020-07-19 11:10:53 +02:00
QString getFileName() const {
return mFileName;
}
2020-07-19 11:10:53 +02:00
void clear() {
mFileName.clear();
setPlainText(QString());
}
protected:
void resizeEvent(QResizeEvent *event) override;
private slots:
void updateLineNumberAreaWidth(int newBlockCount);
void highlightErrorLine();
void updateLineNumberArea(const QRect &, int);
private:
QString generateStyleString();
private:
2018-06-18 10:10:11 +02:00
QWidget *mLineNumberArea;
Highlighter *mHighlighter;
CodeEditorStyle *mWidgetStyle;
int mErrorPosition;
QString mFileName;
};
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