Implement color role AccentColor in QStyleSheetStyle and QCssParser

The color role AccentColor has been added to QPalette.

This patch implements the new color role in QCssParser and subsequently
in QStyleSheetStyle.
The QBrush variable names used to populate brushes, have been changed
into speaking names for better code readability.

tst_QCssParser has been adapted accordingly.
The test function accentColor() has been added in tst_QStyleSheetStyle.

Documentation has been updated.

Change-Id: Ib09ddc1b61868f2bb8f70f654e83ea1c35276d30
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Axel Spoerl 2023-05-18 11:15:43 +02:00 committed by Volker Hilsheimer
parent 227b639499
commit 980a499ad0
6 changed files with 76 additions and 23 deletions

View File

@ -47,6 +47,7 @@ static const QCssKnownValue properties[NumProperties - 1] = {
{ "-qt-style-features", QtStyleFeatures }, { "-qt-style-features", QtStyleFeatures },
{ "-qt-table-type", QtTableType }, { "-qt-table-type", QtTableType },
{ "-qt-user-state", QtUserState }, { "-qt-user-state", QtUserState },
{ "accent-color", QtAccentColor },
{ "alternate-background-color", QtAlternateBackground }, { "alternate-background-color", QtAlternateBackground },
{ "background", Background }, { "background", Background },
{ "background-attachment", BackgroundAttachment }, { "background-attachment", BackgroundAttachment },
@ -1341,17 +1342,23 @@ bool ValueExtractor::extractFont(QFont *font, int *fontSizeAdjustment)
return hit; return hit;
} }
bool ValueExtractor::extractPalette(QBrush *fg, QBrush *sfg, QBrush *sbg, QBrush *abg, QBrush *pfg) bool ValueExtractor::extractPalette(QBrush *foreground,
QBrush *selectedForeground,
QBrush *selectedBackground,
QBrush *alternateBackground,
QBrush *placeHolderTextForeground,
QBrush *accentColor)
{ {
bool hit = false; bool hit = false;
for (int i = 0; i < declarations.size(); ++i) { for (int i = 0; i < declarations.size(); ++i) {
const Declaration &decl = declarations.at(i); const Declaration &decl = declarations.at(i);
switch (decl.d->propertyId) { switch (decl.d->propertyId) {
case Color: *fg = decl.brushValue(pal); break; case Color: *foreground = decl.brushValue(pal); break;
case QtSelectionForeground: *sfg = decl.brushValue(pal); break; case QtSelectionForeground: *selectedForeground = decl.brushValue(pal); break;
case QtSelectionBackground: *sbg = decl.brushValue(pal); break; case QtSelectionBackground: *selectedBackground = decl.brushValue(pal); break;
case QtAlternateBackground: *abg = decl.brushValue(pal); break; case QtAlternateBackground: *alternateBackground = decl.brushValue(pal); break;
case QtPlaceHolderTextColor: *pfg = decl.brushValue(pal); break; case QtPlaceHolderTextColor: *placeHolderTextForeground = decl.brushValue(pal); break;
case QtAccentColor: *accentColor = decl.brushValue(pal); break;
default: continue; default: continue;
} }
hit = true; hit = true;

View File

@ -166,6 +166,7 @@ enum Property {
WordSpacing, WordSpacing,
TextDecorationColor, TextDecorationColor,
QtPlaceHolderTextColor, QtPlaceHolderTextColor,
QtAccentColor,
NumProperties NumProperties
}; };
@ -823,7 +824,9 @@ struct Q_GUI_EXPORT ValueExtractor
bool extractBox(int *margins, int *paddings, int *spacing = nullptr); bool extractBox(int *margins, int *paddings, int *spacing = nullptr);
bool extractBorder(int *borders, QBrush *colors, BorderStyle *Styles, QSize *radii); bool extractBorder(int *borders, QBrush *colors, BorderStyle *Styles, QSize *radii);
bool extractOutline(int *borders, QBrush *colors, BorderStyle *Styles, QSize *radii, int *offsets); bool extractOutline(int *borders, QBrush *colors, BorderStyle *Styles, QSize *radii, int *offsets);
bool extractPalette(QBrush *fg, QBrush *sfg, QBrush *sbg, QBrush *abg, QBrush *pfg); bool extractPalette(QBrush *foreground, QBrush *selectedForeground, QBrush *selectedBackground,
QBrush *alternateBackground, QBrush *placeHolderTextForeground,
QBrush *accentColor);
int extractStyleFeatures(); int extractStyleFeatures();
bool extractImage(QIcon *icon, Qt::Alignment *a, QSize *size); bool extractImage(QIcon *icon, Qt::Alignment *a, QSize *size);
bool extractIcon(QIcon *icon, QSize *size); bool extractIcon(QIcon *icon, QSize *size);

View File

@ -1357,6 +1357,13 @@
\li Type \li Type
\li Description \li Description
\row
\li \b{\c accent-color}
\li \l{#Brush}{Brush} \br
\li The property sets the \c AccentColor, which is used to emphasize
interactive UI elements.
If this property is not set, it defaults to the \c highlight color.
\row \row
\li \b{\c alternate-background-color} \target alternate-background-color-prop \li \b{\c alternate-background-color} \target alternate-background-color-prop
\li \l{#Brush}{Brush} \br \li \l{#Brush}{Brush} \br
@ -3064,6 +3071,7 @@
\row \row
\li \b{PaletteRole} \target PaletteRole \li \b{PaletteRole} \target PaletteRole
\li \c alternate-base \br \li \c alternate-base \br
| \c accentColor \br
| \c base \br | \c base \br
| \c bright-text \br | \c bright-text \br
| \c button \br | \c button \br

View File

@ -434,16 +434,26 @@ struct QStyleSheetBoxData : public QSharedData
struct QStyleSheetPaletteData : public QSharedData struct QStyleSheetPaletteData : public QSharedData
{ {
QStyleSheetPaletteData(const QBrush &fg, const QBrush &sfg, const QBrush &sbg, QStyleSheetPaletteData(const QBrush &foreground,
const QBrush &abg, const QBrush &pfg) const QBrush &selectedForeground,
: foreground(fg), selectionForeground(sfg), selectionBackground(sbg), const QBrush &selectedBackground,
alternateBackground(abg), placeholderForeground(pfg) { } const QBrush &alternateBackground,
const QBrush &placeHolderTextForeground,
const QBrush &accentColor)
: foreground(foreground)
, selectionForeground(selectedForeground)
, selectionBackground(selectedBackground)
, alternateBackground(alternateBackground)
, placeholderForeground(placeHolderTextForeground)
, accentColor(accentColor)
{ }
QBrush foreground; QBrush foreground;
QBrush selectionForeground; QBrush selectionForeground;
QBrush selectionBackground; QBrush selectionBackground;
QBrush alternateBackground; QBrush alternateBackground;
QBrush placeholderForeground; QBrush placeholderForeground;
QBrush accentColor;
}; };
struct QStyleSheetGeometryData : public QSharedData struct QStyleSheetGeometryData : public QSharedData
@ -955,10 +965,17 @@ QRenderRule::QRenderRule(const QList<Declaration> &declarations, const QObject *
bg = new QStyleSheetBackgroundData(brush, pixmap, repeat, alignment, origin, attachment, clip); bg = new QStyleSheetBackgroundData(brush, pixmap, repeat, alignment, origin, attachment, clip);
} }
QBrush sfg, fg, pfg; QBrush foreground;
QBrush sbg, abg; QBrush selectedForeground;
if (v.extractPalette(&fg, &sfg, &sbg, &abg, &pfg)) QBrush selectedBackground;
pal = new QStyleSheetPaletteData(fg, sfg, sbg, abg, pfg); QBrush alternateBackground;
QBrush placeHolderTextForeground;
QBrush accentColor;
if (v.extractPalette(&foreground, &selectedForeground, &selectedBackground,
&alternateBackground, &placeHolderTextForeground, &accentColor)) {
pal = new QStyleSheetPaletteData(foreground, selectedForeground, selectedBackground,
alternateBackground, placeHolderTextForeground, accentColor);
}
QIcon imgIcon; QIcon imgIcon;
alignment = Qt::AlignCenter; alignment = Qt::AlignCenter;
@ -1487,6 +1504,8 @@ void QRenderRule::configurePalette(QPalette *p, QPalette::ColorGroup cg, const Q
p->setBrush(cg, QPalette::AlternateBase, pal->alternateBackground); p->setBrush(cg, QPalette::AlternateBase, pal->alternateBackground);
if (pal->placeholderForeground.style() != Qt::NoBrush) if (pal->placeholderForeground.style() != Qt::NoBrush)
p->setBrush(cg, QPalette::PlaceholderText, pal->placeholderForeground); p->setBrush(cg, QPalette::PlaceholderText, pal->placeholderForeground);
if (pal->accentColor.style() != Qt::NoBrush)
p->setBrush(cg, QPalette::AccentColor, pal->accentColor);
} }
bool QRenderRule::hasModification() const bool QRenderRule::hasModification() const

View File

@ -1543,20 +1543,26 @@ void tst_QCssParser::gradient()
QList<QCss::StyleRule> rules = testSelector.styleRulesForNode(n); QList<QCss::StyleRule> rules = testSelector.styleRulesForNode(n);
QList<QCss::Declaration> decls = rules.at(0).declarations; QList<QCss::Declaration> decls = rules.at(0).declarations;
QCss::ValueExtractor ve(decls); QCss::ValueExtractor ve(decls);
QBrush fg, sfg, pfg; QBrush foreground;
QBrush sbg, abg; QBrush selectedForeground;
QVERIFY(ve.extractPalette(&fg, &sfg, &sbg, &abg, &pfg)); QBrush selectedBackground;
QBrush alternateBackground;
QBrush placeHolderTextForeground;
QBrush accentColor;
QVERIFY(ve.extractPalette(&foreground, &selectedForeground, &selectedBackground,
&alternateBackground, &placeHolderTextForeground, &accentColor));
if (type == "linear") { if (type == "linear") {
QCOMPARE(sbg.style(), Qt::LinearGradientPattern); QCOMPARE(selectedBackground.style(), Qt::LinearGradientPattern);
const QLinearGradient *lg = static_cast<const QLinearGradient *>(sbg.gradient()); const auto *lg = static_cast<const QLinearGradient *>(selectedBackground.gradient());
QCOMPARE(lg->start(), start); QCOMPARE(lg->start(), start);
QCOMPARE(lg->finalStop(), finalStop); QCOMPARE(lg->finalStop(), finalStop);
} else if (type == "conical") { } else if (type == "conical") {
QCOMPARE(sbg.style(), Qt::ConicalGradientPattern); QCOMPARE(selectedBackground.style(), Qt::ConicalGradientPattern);
const QConicalGradient *cg = static_cast<const QConicalGradient *>(sbg.gradient()); const auto *cg = static_cast<const QConicalGradient *>(selectedBackground.gradient());
QCOMPARE(cg->center(), start); QCOMPARE(cg->center(), start);
} }
const QGradient *g = sbg.gradient(); const QGradient *g = selectedBackground.gradient();
QCOMPARE(g->spread(), QGradient::Spread(spread)); QCOMPARE(g->spread(), QGradient::Spread(spread));
QCOMPARE(g->stops().at(0).first, stop0); QCOMPARE(g->stops().at(0).first, stop0);
QCOMPARE(g->stops().at(0).second, color0); QCOMPARE(g->stops().at(0).second, color0);

View File

@ -106,6 +106,7 @@ private slots:
void QTBUG36933_brokenPseudoClassLookup(); void QTBUG36933_brokenPseudoClassLookup();
void styleSheetChangeBeforePolish(); void styleSheetChangeBeforePolish();
void placeholderColor(); void placeholderColor();
void accentColor();
void enumPropertySelector_data(); void enumPropertySelector_data();
void enumPropertySelector(); void enumPropertySelector();
//at the end because it mess with the style. //at the end because it mess with the style.
@ -2360,6 +2361,15 @@ void tst_QStyleSheetStyle::placeholderColor()
QCOMPARE(le1.palette().placeholderText().color(), QColor(phSpec)); QCOMPARE(le1.palette().placeholderText().color(), QColor(phSpec));
} }
void tst_QStyleSheetStyle::accentColor()
{
QLineEdit lineEdit;
const QColor universe(42, 42, 42);
lineEdit.setStyleSheet(QString("QLineEdit { accent-color: %1; }").arg(universe.name()));
lineEdit.ensurePolished();
QCOMPARE(lineEdit.palette().accentColor().color(), universe);
}
void tst_QStyleSheetStyle::enumPropertySelector_data() void tst_QStyleSheetStyle::enumPropertySelector_data()
{ {
QTest::addColumn<QString>("styleSheet"); QTest::addColumn<QString>("styleSheet");