From 32f45490e228eff874a1f273fe10b4ba914c652c Mon Sep 17 00:00:00 2001 From: Scott Furry Date: Fri, 7 Jun 2019 23:23:48 -0600 Subject: [PATCH] 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. --- gui/codeeditor.cpp | 60 ++++++++++++++++++++++++++----------- gui/codeeditor.h | 10 +++++-- gui/codeeditorstyle.h | 69 +++++++++++++++++++++++++++++++++++++++++++ gui/gui.pro | 1 + 4 files changed, 120 insertions(+), 20 deletions(-) create mode 100644 gui/codeeditorstyle.h diff --git a/gui/codeeditor.cpp b/gui/codeeditor.cpp index 1bef14935..645092df8 100644 --- a/gui/codeeditor.cpp +++ b/gui/codeeditor.cpp @@ -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" @@ -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("\\*/"); @@ -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))); @@ -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); @@ -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(); @@ -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); } diff --git a/gui/codeeditor.h b/gui/codeeditor.h index bdc52c56e..f88750e5d 100644 --- a/gui/codeeditor.h +++ b/gui/codeeditor.h @@ -5,6 +5,7 @@ #include #include #include +#include "codeeditorstyle.h" class QPaintEvent; class QResizeEvent; @@ -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); @@ -42,13 +44,16 @@ private: 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; @@ -74,6 +79,7 @@ private slots: private: QWidget *mLineNumberArea; Highlighter *mHighlighter; + CodeEditorStyle *mWidgetStyle; int mErrorPosition; }; diff --git a/gui/codeeditorstyle.h b/gui/codeeditorstyle.h new file mode 100644 index 000000000..50c84221e --- /dev/null +++ b/gui/codeeditorstyle.h @@ -0,0 +1,69 @@ +#ifndef CODEEDITORSTYLE_H +#define CODEEDITORSTYLE_H + +#include +#include +#include + +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 */ diff --git a/gui/gui.pro b/gui/gui.pro index 349633f61..86b15cf17 100644 --- a/gui/gui.pro +++ b/gui/gui.pro @@ -90,6 +90,7 @@ HEADERS += aboutdialog.h \ applicationlist.h \ checkstatistics.h \ checkthread.h \ + codeeditorstyle.h \ codeeditor.h \ common.h \ csvreport.h \