parent
32f45490e2
commit
0947a36c4f
|
@ -3,15 +3,15 @@
|
|||
#include "codeeditor.h"
|
||||
|
||||
|
||||
Highlighter::Highlighter( QTextDocument *parent,
|
||||
CodeEditorStyle *widgetStyle ) :
|
||||
QSyntaxHighlighter( parent ),
|
||||
mWidgetStyle( widgetStyle )
|
||||
Highlighter::Highlighter(QTextDocument *parent,
|
||||
CodeEditorStyle *widgetStyle) :
|
||||
QSyntaxHighlighter(parent),
|
||||
mWidgetStyle(widgetStyle)
|
||||
{
|
||||
HighlightingRule rule;
|
||||
|
||||
mKeywordFormat.setForeground( mWidgetStyle->keywordColor );
|
||||
mKeywordFormat.setFontWeight( mWidgetStyle->keywordWeight );
|
||||
mKeywordFormat.setForeground(mWidgetStyle->keywordColor);
|
||||
mKeywordFormat.setFontWeight(mWidgetStyle->keywordWeight);
|
||||
QStringList keywordPatterns;
|
||||
keywordPatterns << "bool"
|
||||
<< "break"
|
||||
|
@ -59,32 +59,32 @@ Highlighter::Highlighter( QTextDocument *parent,
|
|||
mHighlightingRules.append(rule);
|
||||
}
|
||||
|
||||
mClassFormat.setForeground( mWidgetStyle->classColor );
|
||||
mClassFormat.setFontWeight( mWidgetStyle->classWeight );
|
||||
mClassFormat.setForeground(mWidgetStyle->classColor);
|
||||
mClassFormat.setFontWeight(mWidgetStyle->classWeight);
|
||||
rule.pattern = QRegularExpression("\\bQ[A-Za-z]+\\b");
|
||||
rule.format = mClassFormat;
|
||||
mHighlightingRules.append(rule);
|
||||
|
||||
mQuotationFormat.setForeground( mWidgetStyle->quoteColor );
|
||||
mQuotationFormat.setFontWeight( mWidgetStyle->quoteWeight );
|
||||
mQuotationFormat.setForeground(mWidgetStyle->quoteColor);
|
||||
mQuotationFormat.setFontWeight(mWidgetStyle->quoteWeight);
|
||||
rule.pattern = QRegularExpression("\".*\"");
|
||||
rule.format = mQuotationFormat;
|
||||
mHighlightingRules.append(rule);
|
||||
|
||||
mSingleLineCommentFormat.setForeground( mWidgetStyle->commentColor );
|
||||
mSingleLineCommentFormat.setFontWeight( mWidgetStyle->commentWeight );
|
||||
mSingleLineCommentFormat.setForeground(mWidgetStyle->commentColor);
|
||||
mSingleLineCommentFormat.setFontWeight(mWidgetStyle->commentWeight);
|
||||
rule.pattern = QRegularExpression("//[^\n]*");
|
||||
rule.format = mSingleLineCommentFormat;
|
||||
mHighlightingRules.append(rule);
|
||||
|
||||
mHighlightingRulesWithSymbols = mHighlightingRules;
|
||||
|
||||
mMultiLineCommentFormat.setForeground( mWidgetStyle->commentColor );
|
||||
mMultiLineCommentFormat.setFontWeight( mWidgetStyle->commentWeight );
|
||||
mMultiLineCommentFormat.setForeground(mWidgetStyle->commentColor);
|
||||
mMultiLineCommentFormat.setFontWeight(mWidgetStyle->commentWeight);
|
||||
|
||||
mSymbolFormat.setForeground( mWidgetStyle->symbolFGColor );
|
||||
mSymbolFormat.setBackground( mWidgetStyle->symbolBGColor );
|
||||
mSymbolFormat.setFontWeight( mWidgetStyle->symbolWeight );
|
||||
mSymbolFormat.setForeground(mWidgetStyle->symbolFGColor);
|
||||
mSymbolFormat.setBackground(mWidgetStyle->symbolBGColor);
|
||||
mSymbolFormat.setFontWeight(mWidgetStyle->symbolWeight);
|
||||
|
||||
mCommentStartExpression = QRegularExpression("/\\*");
|
||||
mCommentEndExpression = QRegularExpression("\\*/");
|
||||
|
@ -134,12 +134,12 @@ void Highlighter::highlightBlock(const QString &text)
|
|||
}
|
||||
|
||||
|
||||
CodeEditor::CodeEditor( QWidget *parent,
|
||||
CodeEditorStyle *widgetStyle /*= nullptr*/ ) :
|
||||
CodeEditor::CodeEditor(QWidget *parent,
|
||||
CodeEditorStyle *widgetStyle /*= nullptr*/) :
|
||||
QPlainTextEdit(parent)
|
||||
{
|
||||
if( widgetStyle ) mWidgetStyle = widgetStyle;
|
||||
else mWidgetStyle = new CodeEditorStyle( defaultStyle );
|
||||
if (widgetStyle) mWidgetStyle = widgetStyle;
|
||||
else mWidgetStyle = new CodeEditorStyle(defaultStyle);
|
||||
|
||||
mLineNumberArea = new LineNumberArea(this);
|
||||
mHighlighter = new Highlighter(this->document(), mWidgetStyle);
|
||||
|
@ -239,7 +239,7 @@ void CodeEditor::highlightErrorLine()
|
|||
|
||||
QTextEdit::ExtraSelection selection;
|
||||
|
||||
selection.format.setBackground( mWidgetStyle->highlightBGColor );
|
||||
selection.format.setBackground(mWidgetStyle->highlightBGColor);
|
||||
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
|
||||
selection.cursor = QTextCursor(document());
|
||||
selection.cursor.setPosition(mErrorPosition);
|
||||
|
@ -252,7 +252,7 @@ void CodeEditor::highlightErrorLine()
|
|||
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
{
|
||||
QPainter painter(mLineNumberArea);
|
||||
painter.fillRect(event->rect(), mWidgetStyle->lineNumBGColor );
|
||||
painter.fillRect(event->rect(), mWidgetStyle->lineNumBGColor);
|
||||
|
||||
QTextBlock block = firstVisibleBlock();
|
||||
int blockNumber = block.blockNumber();
|
||||
|
@ -262,7 +262,7 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
|
|||
while (block.isValid() && top <= event->rect().bottom()) {
|
||||
if (block.isVisible() && bottom >= event->rect().top()) {
|
||||
QString number = QString::number(blockNumber + 1);
|
||||
painter.setPen( mWidgetStyle->lineNumFGColor );
|
||||
painter.setPen(mWidgetStyle->lineNumFGColor);
|
||||
painter.drawText(0, top, mLineNumberArea->width(), fontMetrics().height(),
|
||||
Qt::AlignRight, number);
|
||||
}
|
||||
|
|
|
@ -19,8 +19,8 @@ class Highlighter : public QSyntaxHighlighter {
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Highlighter( QTextDocument *parent,
|
||||
CodeEditorStyle *widgetStyle );
|
||||
explicit Highlighter(QTextDocument *parent,
|
||||
CodeEditorStyle *widgetStyle);
|
||||
|
||||
void setSymbols(const QStringList &symbols);
|
||||
|
||||
|
@ -52,8 +52,8 @@ class CodeEditor : public QPlainTextEdit {
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit CodeEditor( QWidget *parent,
|
||||
CodeEditorStyle *widgetStyle = nullptr );
|
||||
explicit CodeEditor(QWidget *parent,
|
||||
CodeEditorStyle *widgetStyle = nullptr);
|
||||
CodeEditor(const CodeEditor &) = delete;
|
||||
CodeEditor &operator=(const CodeEditor &) = delete;
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
class CodeEditorStyle {
|
||||
private:
|
||||
CodeEditorStyle(){};
|
||||
CodeEditorStyle() {};
|
||||
public:
|
||||
CodeEditorStyle(
|
||||
const QColor& CtrlFGColor, const QColor& CtrlBGColor,
|
||||
|
@ -18,23 +18,23 @@ public:
|
|||
const QColor& QteFGColor, const QFont::Weight& QteWeight,
|
||||
const QColor& CmtFGColor, const QFont::Weight& CmtWeight,
|
||||
const QColor& SymbFGColor, const QColor& SymbBGColor,
|
||||
const QFont::Weight& SymbWeight ) :
|
||||
widgetFGColor( CtrlFGColor ),
|
||||
widgetBGColor( CtrlBGColor ),
|
||||
highlightBGColor( HiLiBGColor ),
|
||||
lineNumFGColor( LnNumFGColor ),
|
||||
lineNumBGColor( LnNumBGColor ),
|
||||
keywordColor( KeyWdFGColor ),
|
||||
keywordWeight( KeyWdWeight ),
|
||||
classColor( ClsFGColor ),
|
||||
classWeight( ClsWeight ),
|
||||
quoteColor( QteFGColor ),
|
||||
quoteWeight( QteWeight ),
|
||||
commentColor( CmtFGColor ),
|
||||
commentWeight( CmtWeight ),
|
||||
symbolFGColor( SymbFGColor ),
|
||||
symbolBGColor( SymbBGColor ),
|
||||
symbolWeight( SymbWeight )
|
||||
const QFont::Weight& SymbWeight) :
|
||||
widgetFGColor(CtrlFGColor),
|
||||
widgetBGColor(CtrlBGColor),
|
||||
highlightBGColor(HiLiBGColor),
|
||||
lineNumFGColor(LnNumFGColor),
|
||||
lineNumBGColor(LnNumBGColor),
|
||||
keywordColor(KeyWdFGColor),
|
||||
keywordWeight(KeyWdWeight),
|
||||
classColor(ClsFGColor),
|
||||
classWeight(ClsWeight),
|
||||
quoteColor(QteFGColor),
|
||||
quoteWeight(QteWeight),
|
||||
commentColor(CmtFGColor),
|
||||
commentWeight(CmtWeight),
|
||||
symbolFGColor(SymbFGColor),
|
||||
symbolBGColor(SymbBGColor),
|
||||
symbolWeight(SymbWeight)
|
||||
{}
|
||||
|
||||
public:
|
||||
|
@ -57,13 +57,14 @@ public:
|
|||
};
|
||||
|
||||
static const CodeEditorStyle defaultStyle({
|
||||
/* editor FG/BG */ Qt::black, QColor( 240, 240, 240 ),
|
||||
/* highlight BG */ QColor( 255, 220, 220 ),
|
||||
/* line number FG/BG */ Qt::black, QColor( 240, 240, 240 ),
|
||||
/* keyword FG/Weight */ Qt::darkBlue, QFont::Bold,
|
||||
/* class FG/Weight */ Qt::darkMagenta, QFont::Bold,
|
||||
/* quote FG/Weight */ Qt::darkGreen, QFont::Normal,
|
||||
/* comment FG/Weight */ Qt::gray, QFont::Normal,
|
||||
/* Symbol FG/BG/Weight */ Qt::red, QColor( 220, 220, 255 ), QFont::Normal });
|
||||
/* editor FG/BG */ Qt::black, QColor(240, 240, 240),
|
||||
/* highlight BG */ QColor(255, 220, 220),
|
||||
/* line number FG/BG */ Qt::black, QColor(240, 240, 240),
|
||||
/* keyword FG/Weight */ Qt::darkBlue, QFont::Bold,
|
||||
/* class FG/Weight */ Qt::darkMagenta, QFont::Bold,
|
||||
/* quote FG/Weight */ Qt::darkGreen, QFont::Normal,
|
||||
/* comment FG/Weight */ Qt::gray, QFont::Normal,
|
||||
/* Symbol FG/BG/Weight */ Qt::red, QColor(220, 220, 255), QFont::Normal
|
||||
});
|
||||
|
||||
#endif /* CODEEDITORSTYLE_H */
|
||||
|
|
Loading…
Reference in New Issue