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