Skip to content

Commit 32f4549

Browse files
scottfurrydanmar
authored andcommitted
CodeEditor Widget Styling (cppcheck-opensource#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.
1 parent 1e53cf0 commit 32f4549

4 files changed

Lines changed: 120 additions & 20 deletions

File tree

gui/codeeditor.cpp

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
#include "codeeditor.h"
44

55

6-
Highlighter::Highlighter(QTextDocument *parent)
7-
: QSyntaxHighlighter(parent)
6+
Highlighter::Highlighter( QTextDocument *parent,
7+
CodeEditorStyle *widgetStyle ) :
8+
QSyntaxHighlighter( parent ),
9+
mWidgetStyle( widgetStyle )
810
{
911
HighlightingRule rule;
1012

11-
mKeywordFormat.setForeground(Qt::darkBlue);
12-
mKeywordFormat.setFontWeight(QFont::Bold);
13+
mKeywordFormat.setForeground( mWidgetStyle->keywordColor );
14+
mKeywordFormat.setFontWeight( mWidgetStyle->keywordWeight );
1315
QStringList keywordPatterns;
1416
keywordPatterns << "bool"
1517
<< "break"
@@ -57,28 +59,32 @@ Highlighter::Highlighter(QTextDocument *parent)
5759
mHighlightingRules.append(rule);
5860
}
5961

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

66-
mQuotationFormat.setForeground(Qt::darkGreen);
68+
mQuotationFormat.setForeground( mWidgetStyle->quoteColor );
69+
mQuotationFormat.setFontWeight( mWidgetStyle->quoteWeight );
6770
rule.pattern = QRegularExpression("\".*\"");
6871
rule.format = mQuotationFormat;
6972
mHighlightingRules.append(rule);
7073

71-
mSingleLineCommentFormat.setForeground(Qt::gray);
74+
mSingleLineCommentFormat.setForeground( mWidgetStyle->commentColor );
75+
mSingleLineCommentFormat.setFontWeight( mWidgetStyle->commentWeight );
7276
rule.pattern = QRegularExpression("//[^\n]*");
7377
rule.format = mSingleLineCommentFormat;
7478
mHighlightingRules.append(rule);
7579

7680
mHighlightingRulesWithSymbols = mHighlightingRules;
7781

78-
mMultiLineCommentFormat.setForeground(Qt::gray);
82+
mMultiLineCommentFormat.setForeground( mWidgetStyle->commentColor );
83+
mMultiLineCommentFormat.setFontWeight( mWidgetStyle->commentWeight );
7984

80-
mSymbolFormat.setForeground(Qt::red);
81-
mSymbolFormat.setBackground(QColor(220,220,255));
85+
mSymbolFormat.setForeground( mWidgetStyle->symbolFGColor );
86+
mSymbolFormat.setBackground( mWidgetStyle->symbolBGColor );
87+
mSymbolFormat.setFontWeight( mWidgetStyle->symbolWeight );
8288

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

130136

131-
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
137+
CodeEditor::CodeEditor( QWidget *parent,
138+
CodeEditorStyle *widgetStyle /*= nullptr*/ ) :
139+
QPlainTextEdit(parent)
132140
{
141+
if( widgetStyle ) mWidgetStyle = widgetStyle;
142+
else mWidgetStyle = new CodeEditorStyle( defaultStyle );
143+
133144
mLineNumberArea = new LineNumberArea(this);
134-
mHighlighter = new Highlighter(this->document());
145+
mHighlighter = new Highlighter(this->document(), mWidgetStyle);
135146
mErrorPosition = -1;
136147

148+
// set widget coloring by overriding widget style sheet
149+
QString bgcolor = QString("background:rgb(%1,%2,%3);")
150+
.arg(mWidgetStyle->widgetBGColor.red())
151+
.arg(mWidgetStyle->widgetBGColor.green())
152+
.arg(mWidgetStyle->widgetBGColor.blue());
153+
QString fgcolor = QString("color:rgb(%1,%2,%3);")
154+
.arg(mWidgetStyle->widgetFGColor.red())
155+
.arg(mWidgetStyle->widgetFGColor.green())
156+
.arg(mWidgetStyle->widgetFGColor.blue());
157+
QString style = QString("%1 %2")
158+
.arg(bgcolor)
159+
.arg(fgcolor);
160+
setObjectName("CodeEditor");
161+
setStyleSheet(style);
162+
137163
setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
138164

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

214240
QTextEdit::ExtraSelection selection;
215241

216-
QColor lineColor = QColor(255,220,220);
217-
218-
selection.format.setBackground(lineColor);
242+
selection.format.setBackground( mWidgetStyle->highlightBGColor );
219243
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
220244
selection.cursor = QTextCursor(document());
221245
selection.cursor.setPosition(mErrorPosition);
@@ -228,7 +252,7 @@ void CodeEditor::highlightErrorLine()
228252
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
229253
{
230254
QPainter painter(mLineNumberArea);
231-
painter.fillRect(event->rect(), QColor(240,240,240));
255+
painter.fillRect(event->rect(), mWidgetStyle->lineNumBGColor );
232256

233257
QTextBlock block = firstVisibleBlock();
234258
int blockNumber = block.blockNumber();
@@ -238,7 +262,7 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
238262
while (block.isValid() && top <= event->rect().bottom()) {
239263
if (block.isVisible() && bottom >= event->rect().top()) {
240264
QString number = QString::number(blockNumber + 1);
241-
painter.setPen(Qt::black);
265+
painter.setPen( mWidgetStyle->lineNumFGColor );
242266
painter.drawText(0, top, mLineNumberArea->width(), fontMetrics().height(),
243267
Qt::AlignRight, number);
244268
}

gui/codeeditor.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <QPlainTextEdit>
66
#include <QObject>
77
#include <QRegularExpression>
8+
#include "codeeditorstyle.h"
89

910
class QPaintEvent;
1011
class QResizeEvent;
@@ -18,7 +19,8 @@ class Highlighter : public QSyntaxHighlighter {
1819
Q_OBJECT
1920

2021
public:
21-
explicit Highlighter(QTextDocument *parent);
22+
explicit Highlighter( QTextDocument *parent,
23+
CodeEditorStyle *widgetStyle );
2224

2325
void setSymbols(const QStringList &symbols);
2426

@@ -42,13 +44,16 @@ class Highlighter : public QSyntaxHighlighter {
4244
QTextCharFormat mMultiLineCommentFormat;
4345
QTextCharFormat mQuotationFormat;
4446
QTextCharFormat mSymbolFormat;
47+
48+
CodeEditorStyle *mWidgetStyle;
4549
};
4650

4751
class CodeEditor : public QPlainTextEdit {
4852
Q_OBJECT
4953

5054
public:
51-
explicit CodeEditor(QWidget *parent);
55+
explicit CodeEditor( QWidget *parent,
56+
CodeEditorStyle *widgetStyle = nullptr );
5257
CodeEditor(const CodeEditor &) = delete;
5358
CodeEditor &operator=(const CodeEditor &) = delete;
5459

@@ -74,6 +79,7 @@ private slots:
7479
private:
7580
QWidget *mLineNumberArea;
7681
Highlighter *mHighlighter;
82+
CodeEditorStyle *mWidgetStyle;
7783
int mErrorPosition;
7884
};
7985

gui/codeeditorstyle.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#ifndef CODEEDITORSTYLE_H
2+
#define CODEEDITORSTYLE_H
3+
4+
#include <QString>
5+
#include <QColor>
6+
#include <QFont>
7+
8+
class CodeEditorStyle {
9+
private:
10+
CodeEditorStyle(){};
11+
public:
12+
CodeEditorStyle(
13+
const QColor& CtrlFGColor, const QColor& CtrlBGColor,
14+
const QColor& HiLiBGColor,
15+
const QColor& LnNumFGColor, const QColor& LnNumBGColor,
16+
const QColor& KeyWdFGColor, const QFont::Weight& KeyWdWeight,
17+
const QColor& ClsFGColor, const QFont::Weight& ClsWeight,
18+
const QColor& QteFGColor, const QFont::Weight& QteWeight,
19+
const QColor& CmtFGColor, const QFont::Weight& CmtWeight,
20+
const QColor& SymbFGColor, const QColor& SymbBGColor,
21+
const QFont::Weight& SymbWeight ) :
22+
widgetFGColor( CtrlFGColor ),
23+
widgetBGColor( CtrlBGColor ),
24+
highlightBGColor( HiLiBGColor ),
25+
lineNumFGColor( LnNumFGColor ),
26+
lineNumBGColor( LnNumBGColor ),
27+
keywordColor( KeyWdFGColor ),
28+
keywordWeight( KeyWdWeight ),
29+
classColor( ClsFGColor ),
30+
classWeight( ClsWeight ),
31+
quoteColor( QteFGColor ),
32+
quoteWeight( QteWeight ),
33+
commentColor( CmtFGColor ),
34+
commentWeight( CmtWeight ),
35+
symbolFGColor( SymbFGColor ),
36+
symbolBGColor( SymbBGColor ),
37+
symbolWeight( SymbWeight )
38+
{}
39+
40+
public:
41+
QColor widgetFGColor;
42+
QColor widgetBGColor;
43+
QColor highlightBGColor;
44+
QColor lineNumFGColor;
45+
QColor lineNumBGColor;
46+
QColor keywordColor;
47+
QFont::Weight keywordWeight;
48+
QColor classColor;
49+
QFont::Weight classWeight;
50+
QColor quoteColor;
51+
QFont::Weight quoteWeight;
52+
QColor commentColor;
53+
QFont::Weight commentWeight;
54+
QColor symbolFGColor;
55+
QColor symbolBGColor;
56+
QFont::Weight symbolWeight;
57+
};
58+
59+
static const CodeEditorStyle defaultStyle({
60+
/* editor FG/BG */ Qt::black, QColor( 240, 240, 240 ),
61+
/* highlight BG */ QColor( 255, 220, 220 ),
62+
/* line number FG/BG */ Qt::black, QColor( 240, 240, 240 ),
63+
/* keyword FG/Weight */ Qt::darkBlue, QFont::Bold,
64+
/* class FG/Weight */ Qt::darkMagenta, QFont::Bold,
65+
/* quote FG/Weight */ Qt::darkGreen, QFont::Normal,
66+
/* comment FG/Weight */ Qt::gray, QFont::Normal,
67+
/* Symbol FG/BG/Weight */ Qt::red, QColor( 220, 220, 255 ), QFont::Normal });
68+
69+
#endif /* CODEEDITORSTYLE_H */

gui/gui.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ HEADERS += aboutdialog.h \
9090
applicationlist.h \
9191
checkstatistics.h \
9292
checkthread.h \
93+
codeeditorstyle.h \
9394
codeeditor.h \
9495
common.h \
9596
csvreport.h \

0 commit comments

Comments
 (0)