Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 42 additions & 18 deletions gui/codeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
#include "codeeditor.h"


Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
Highlighter::Highlighter( QTextDocument *parent,
CodeEditorStyle *widgetStyle ) :
QSyntaxHighlighter( parent ),
mWidgetStyle( widgetStyle )
{
HighlightingRule rule;

mKeywordFormat.setForeground(Qt::darkBlue);
mKeywordFormat.setFontWeight(QFont::Bold);
mKeywordFormat.setForeground( mWidgetStyle->keywordColor );
mKeywordFormat.setFontWeight( mWidgetStyle->keywordWeight );
QStringList keywordPatterns;
keywordPatterns << "bool"
<< "break"
Expand Down Expand Up @@ -57,28 +59,32 @@ Highlighter::Highlighter(QTextDocument *parent)
mHighlightingRules.append(rule);
}

mClassFormat.setFontWeight(QFont::Bold);
mClassFormat.setForeground(Qt::darkMagenta);
mClassFormat.setForeground( mWidgetStyle->classColor );
mClassFormat.setFontWeight( mWidgetStyle->classWeight );
rule.pattern = QRegularExpression("\\bQ[A-Za-z]+\\b");
rule.format = mClassFormat;
mHighlightingRules.append(rule);

mQuotationFormat.setForeground(Qt::darkGreen);
mQuotationFormat.setForeground( mWidgetStyle->quoteColor );
mQuotationFormat.setFontWeight( mWidgetStyle->quoteWeight );
rule.pattern = QRegularExpression("\".*\"");
rule.format = mQuotationFormat;
mHighlightingRules.append(rule);

mSingleLineCommentFormat.setForeground(Qt::gray);
mSingleLineCommentFormat.setForeground( mWidgetStyle->commentColor );
mSingleLineCommentFormat.setFontWeight( mWidgetStyle->commentWeight );
rule.pattern = QRegularExpression("//[^\n]*");
rule.format = mSingleLineCommentFormat;
mHighlightingRules.append(rule);

mHighlightingRulesWithSymbols = mHighlightingRules;

mMultiLineCommentFormat.setForeground(Qt::gray);
mMultiLineCommentFormat.setForeground( mWidgetStyle->commentColor );
mMultiLineCommentFormat.setFontWeight( mWidgetStyle->commentWeight );

mSymbolFormat.setForeground(Qt::red);
mSymbolFormat.setBackground(QColor(220,220,255));
mSymbolFormat.setForeground( mWidgetStyle->symbolFGColor );
mSymbolFormat.setBackground( mWidgetStyle->symbolBGColor );
mSymbolFormat.setFontWeight( mWidgetStyle->symbolWeight );

mCommentStartExpression = QRegularExpression("/\\*");
mCommentEndExpression = QRegularExpression("\\*/");
Expand Down Expand Up @@ -128,12 +134,32 @@ void Highlighter::highlightBlock(const QString &text)
}


CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
CodeEditor::CodeEditor( QWidget *parent,
CodeEditorStyle *widgetStyle /*= nullptr*/ ) :
QPlainTextEdit(parent)
{
if( widgetStyle ) mWidgetStyle = widgetStyle;
else mWidgetStyle = new CodeEditorStyle( defaultStyle );

mLineNumberArea = new LineNumberArea(this);
mHighlighter = new Highlighter(this->document());
mHighlighter = new Highlighter(this->document(), mWidgetStyle);
mErrorPosition = -1;

// set widget coloring by overriding widget style sheet
QString bgcolor = QString("background:rgb(%1,%2,%3);")
.arg(mWidgetStyle->widgetBGColor.red())
.arg(mWidgetStyle->widgetBGColor.green())
.arg(mWidgetStyle->widgetBGColor.blue());
QString fgcolor = QString("color:rgb(%1,%2,%3);")
.arg(mWidgetStyle->widgetFGColor.red())
.arg(mWidgetStyle->widgetFGColor.green())
.arg(mWidgetStyle->widgetFGColor.blue());
QString style = QString("%1 %2")
.arg(bgcolor)
.arg(fgcolor);
setObjectName("CodeEditor");
setStyleSheet(style);

setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));

connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
Expand Down Expand Up @@ -213,9 +239,7 @@ void CodeEditor::highlightErrorLine()

QTextEdit::ExtraSelection selection;

QColor lineColor = QColor(255,220,220);

selection.format.setBackground(lineColor);
selection.format.setBackground( mWidgetStyle->highlightBGColor );
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = QTextCursor(document());
selection.cursor.setPosition(mErrorPosition);
Expand All @@ -228,7 +252,7 @@ void CodeEditor::highlightErrorLine()
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
QPainter painter(mLineNumberArea);
painter.fillRect(event->rect(), QColor(240,240,240));
painter.fillRect(event->rect(), mWidgetStyle->lineNumBGColor );

QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
Expand All @@ -238,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(Qt::black);
painter.setPen( mWidgetStyle->lineNumFGColor );
painter.drawText(0, top, mLineNumberArea->width(), fontMetrics().height(),
Qt::AlignRight, number);
}
Expand Down
10 changes: 8 additions & 2 deletions gui/codeeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QPlainTextEdit>
#include <QObject>
#include <QRegularExpression>
#include "codeeditorstyle.h"

class QPaintEvent;
class QResizeEvent;
Expand All @@ -18,7 +19,8 @@ class Highlighter : public QSyntaxHighlighter {
Q_OBJECT

public:
explicit Highlighter(QTextDocument *parent);
explicit Highlighter( QTextDocument *parent,
CodeEditorStyle *widgetStyle );

void setSymbols(const QStringList &symbols);

Expand All @@ -42,13 +44,16 @@ class Highlighter : public QSyntaxHighlighter {
QTextCharFormat mMultiLineCommentFormat;
QTextCharFormat mQuotationFormat;
QTextCharFormat mSymbolFormat;

CodeEditorStyle *mWidgetStyle;
};

class CodeEditor : public QPlainTextEdit {
Q_OBJECT

public:
explicit CodeEditor(QWidget *parent);
explicit CodeEditor( QWidget *parent,
CodeEditorStyle *widgetStyle = nullptr );
CodeEditor(const CodeEditor &) = delete;
CodeEditor &operator=(const CodeEditor &) = delete;

Expand All @@ -74,6 +79,7 @@ private slots:
private:
QWidget *mLineNumberArea;
Highlighter *mHighlighter;
CodeEditorStyle *mWidgetStyle;
int mErrorPosition;
};

Expand Down
69 changes: 69 additions & 0 deletions gui/codeeditorstyle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#ifndef CODEEDITORSTYLE_H
#define CODEEDITORSTYLE_H

#include <QString>
#include <QColor>
#include <QFont>

class CodeEditorStyle {
private:
CodeEditorStyle(){};
public:
CodeEditorStyle(
const QColor& CtrlFGColor, const QColor& CtrlBGColor,
const QColor& HiLiBGColor,
const QColor& LnNumFGColor, const QColor& LnNumBGColor,
const QColor& KeyWdFGColor, const QFont::Weight& KeyWdWeight,
const QColor& ClsFGColor, const QFont::Weight& ClsWeight,
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 )
{}

public:
QColor widgetFGColor;
QColor widgetBGColor;
QColor highlightBGColor;
QColor lineNumFGColor;
QColor lineNumBGColor;
QColor keywordColor;
QFont::Weight keywordWeight;
QColor classColor;
QFont::Weight classWeight;
QColor quoteColor;
QFont::Weight quoteWeight;
QColor commentColor;
QFont::Weight commentWeight;
QColor symbolFGColor;
QColor symbolBGColor;
QFont::Weight symbolWeight;
};

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 });

#endif /* CODEEDITORSTYLE_H */
1 change: 1 addition & 0 deletions gui/gui.pro
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ HEADERS += aboutdialog.h \
applicationlist.h \
checkstatistics.h \
checkthread.h \
codeeditorstyle.h \
codeeditor.h \
common.h \
csvreport.h \
Expand Down