CodeEditor Widget Styling (#1874)
* CodeEditor Widget Styling With profileration of Qt5 styling methods, problems with presentation can occur when using cppcheck-gui and user choosen themes. With a dark theme, a highlighted line in the CodeEditor appears with white text on a light background or dark colors on a dark background. Commit makes changes to enforce a default style on the Code Editor widget. Mechanism is provided, if desired, where a user defined styling can be provided to CodeEditor widget. * CodeEditor Widget Styling With profileration of Qt5 styling methods, problems with presentation can occur when using cppcheck-gui and user choosen themes. With a dark theme, a highlighted line in the CodeEditor appears with white text on a light background or dark colors on a dark background. Commit makes changes to enforce a default style on the Code Editor widget. Mechanism is provided, if desired, where a user defined styling can be provided to CodeEditor widget. 2nd commit - remove declarations in gui/codeeditorstyle.h to possibly resolve appveyor window builds.
This commit is contained in:
parent
1e53cf0397
commit
32f45490e2
|
@ -3,13 +3,15 @@
|
||||||
#include "codeeditor.h"
|
#include "codeeditor.h"
|
||||||
|
|
||||||
|
|
||||||
Highlighter::Highlighter(QTextDocument *parent)
|
Highlighter::Highlighter( QTextDocument *parent,
|
||||||
: QSyntaxHighlighter(parent)
|
CodeEditorStyle *widgetStyle ) :
|
||||||
|
QSyntaxHighlighter( parent ),
|
||||||
|
mWidgetStyle( widgetStyle )
|
||||||
{
|
{
|
||||||
HighlightingRule rule;
|
HighlightingRule rule;
|
||||||
|
|
||||||
mKeywordFormat.setForeground(Qt::darkBlue);
|
mKeywordFormat.setForeground( mWidgetStyle->keywordColor );
|
||||||
mKeywordFormat.setFontWeight(QFont::Bold);
|
mKeywordFormat.setFontWeight( mWidgetStyle->keywordWeight );
|
||||||
QStringList keywordPatterns;
|
QStringList keywordPatterns;
|
||||||
keywordPatterns << "bool"
|
keywordPatterns << "bool"
|
||||||
<< "break"
|
<< "break"
|
||||||
|
@ -57,28 +59,32 @@ Highlighter::Highlighter(QTextDocument *parent)
|
||||||
mHighlightingRules.append(rule);
|
mHighlightingRules.append(rule);
|
||||||
}
|
}
|
||||||
|
|
||||||
mClassFormat.setFontWeight(QFont::Bold);
|
mClassFormat.setForeground( mWidgetStyle->classColor );
|
||||||
mClassFormat.setForeground(Qt::darkMagenta);
|
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(Qt::darkGreen);
|
mQuotationFormat.setForeground( mWidgetStyle->quoteColor );
|
||||||
|
mQuotationFormat.setFontWeight( mWidgetStyle->quoteWeight );
|
||||||
rule.pattern = QRegularExpression("\".*\"");
|
rule.pattern = QRegularExpression("\".*\"");
|
||||||
rule.format = mQuotationFormat;
|
rule.format = mQuotationFormat;
|
||||||
mHighlightingRules.append(rule);
|
mHighlightingRules.append(rule);
|
||||||
|
|
||||||
mSingleLineCommentFormat.setForeground(Qt::gray);
|
mSingleLineCommentFormat.setForeground( mWidgetStyle->commentColor );
|
||||||
|
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(Qt::gray);
|
mMultiLineCommentFormat.setForeground( mWidgetStyle->commentColor );
|
||||||
|
mMultiLineCommentFormat.setFontWeight( mWidgetStyle->commentWeight );
|
||||||
|
|
||||||
mSymbolFormat.setForeground(Qt::red);
|
mSymbolFormat.setForeground( mWidgetStyle->symbolFGColor );
|
||||||
mSymbolFormat.setBackground(QColor(220,220,255));
|
mSymbolFormat.setBackground( mWidgetStyle->symbolBGColor );
|
||||||
|
mSymbolFormat.setFontWeight( mWidgetStyle->symbolWeight );
|
||||||
|
|
||||||
mCommentStartExpression = QRegularExpression("/\\*");
|
mCommentStartExpression = QRegularExpression("/\\*");
|
||||||
mCommentEndExpression = QRegularExpression("\\*/");
|
mCommentEndExpression = QRegularExpression("\\*/");
|
||||||
|
@ -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);
|
mLineNumberArea = new LineNumberArea(this);
|
||||||
mHighlighter = new Highlighter(this->document());
|
mHighlighter = new Highlighter(this->document(), mWidgetStyle);
|
||||||
mErrorPosition = -1;
|
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));
|
setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
|
||||||
|
|
||||||
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
|
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
|
||||||
|
@ -213,9 +239,7 @@ void CodeEditor::highlightErrorLine()
|
||||||
|
|
||||||
QTextEdit::ExtraSelection selection;
|
QTextEdit::ExtraSelection selection;
|
||||||
|
|
||||||
QColor lineColor = QColor(255,220,220);
|
selection.format.setBackground( mWidgetStyle->highlightBGColor );
|
||||||
|
|
||||||
selection.format.setBackground(lineColor);
|
|
||||||
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);
|
||||||
|
@ -228,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(), QColor(240,240,240));
|
painter.fillRect(event->rect(), mWidgetStyle->lineNumBGColor );
|
||||||
|
|
||||||
QTextBlock block = firstVisibleBlock();
|
QTextBlock block = firstVisibleBlock();
|
||||||
int blockNumber = block.blockNumber();
|
int blockNumber = block.blockNumber();
|
||||||
|
@ -238,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(Qt::black);
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <QPlainTextEdit>
|
#include <QPlainTextEdit>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
#include "codeeditorstyle.h"
|
||||||
|
|
||||||
class QPaintEvent;
|
class QPaintEvent;
|
||||||
class QResizeEvent;
|
class QResizeEvent;
|
||||||
|
@ -18,7 +19,8 @@ class Highlighter : public QSyntaxHighlighter {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Highlighter(QTextDocument *parent);
|
explicit Highlighter( QTextDocument *parent,
|
||||||
|
CodeEditorStyle *widgetStyle );
|
||||||
|
|
||||||
void setSymbols(const QStringList &symbols);
|
void setSymbols(const QStringList &symbols);
|
||||||
|
|
||||||
|
@ -42,13 +44,16 @@ private:
|
||||||
QTextCharFormat mMultiLineCommentFormat;
|
QTextCharFormat mMultiLineCommentFormat;
|
||||||
QTextCharFormat mQuotationFormat;
|
QTextCharFormat mQuotationFormat;
|
||||||
QTextCharFormat mSymbolFormat;
|
QTextCharFormat mSymbolFormat;
|
||||||
|
|
||||||
|
CodeEditorStyle *mWidgetStyle;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CodeEditor : public QPlainTextEdit {
|
class CodeEditor : public QPlainTextEdit {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit CodeEditor(QWidget *parent);
|
explicit CodeEditor( QWidget *parent,
|
||||||
|
CodeEditorStyle *widgetStyle = nullptr );
|
||||||
CodeEditor(const CodeEditor &) = delete;
|
CodeEditor(const CodeEditor &) = delete;
|
||||||
CodeEditor &operator=(const CodeEditor &) = delete;
|
CodeEditor &operator=(const CodeEditor &) = delete;
|
||||||
|
|
||||||
|
@ -74,6 +79,7 @@ private slots:
|
||||||
private:
|
private:
|
||||||
QWidget *mLineNumberArea;
|
QWidget *mLineNumberArea;
|
||||||
Highlighter *mHighlighter;
|
Highlighter *mHighlighter;
|
||||||
|
CodeEditorStyle *mWidgetStyle;
|
||||||
int mErrorPosition;
|
int mErrorPosition;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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 */
|
|
@ -90,6 +90,7 @@ HEADERS += aboutdialog.h \
|
||||||
applicationlist.h \
|
applicationlist.h \
|
||||||
checkstatistics.h \
|
checkstatistics.h \
|
||||||
checkthread.h \
|
checkthread.h \
|
||||||
|
codeeditorstyle.h \
|
||||||
codeeditor.h \
|
codeeditor.h \
|
||||||
common.h \
|
common.h \
|
||||||
csvreport.h \
|
csvreport.h \
|
||||||
|
|
Loading…
Reference in New Issue