QCssParser: Add missing palette roles

Qt CSS allows to read palette colors by using "palette(role)" but
the supported roles haven't been updated since at least Qt 5.12,
therefore placeholder-text and accent are not recognized.

An "accent-color" property has been added to *set* the accent
color on an object when it was introduced but it wasn't possible
to *read* the accent color, even though it was documented to work.

For sake of completeness, add tooltip-base and tooltip-text as well.

Also amend the comment in QPalette to mention QCss and add a
static_assert so this doesn't happen again.

Pick-to: 6.9 6.9.0 6.8
Change-Id: I59ed09dc91fa18ca0356857c8ddb43946b309035
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
This commit is contained in:
Kai Uwe Broulik 2025-01-23 09:03:45 +01:00
parent 93694e99c2
commit 9194366923
5 changed files with 23 additions and 6 deletions

View File

@ -45,7 +45,8 @@ public:
operator QVariant() const; operator QVariant() const;
// Do not change the order, the serialization format depends on it // Do not change the order, the serialization format depends on it
// Ensure these values are kept in sync with QQuickColorGroup's properties. // Ensure these values are kept in sync with QQuickColorGroup's properties
// and QCss::KnownValue!
enum ColorGroup { Active, Disabled, Inactive, NColorGroups, Current, All, Normal = Active }; enum ColorGroup { Active, Disabled, Inactive, NColorGroups, Current, All, Normal = Active };
Q_ENUM(ColorGroup) Q_ENUM(ColorGroup)
enum ColorRole { WindowText, Button, Light, Midlight, Dark, Mid, enum ColorRole { WindowText, Button, Light, Midlight, Dark, Mid,

View File

@ -161,6 +161,7 @@ static const QCssKnownValue properties[NumProperties - 1] = {
}; };
static const QCssKnownValue values[NumKnownValues - 1] = { static const QCssKnownValue values[NumKnownValues - 1] = {
{ "accent", Value_Accent },
{ "active", Value_Active }, { "active", Value_Active },
{ "alternate-base", Value_AlternateBase }, { "alternate-base", Value_AlternateBase },
{ "always", Value_Always }, { "always", Value_Always },
@ -204,6 +205,7 @@ static const QCssKnownValue values[NumKnownValues - 1] = {
{ "midlight", Value_Midlight }, { "midlight", Value_Midlight },
{ "miterjoin", Value_MiterJoin}, { "miterjoin", Value_MiterJoin},
{ "native", Value_Native }, { "native", Value_Native },
{ "no-role", Value_NoRole },
{ "none", Value_None }, { "none", Value_None },
{ "normal", Value_Normal }, { "normal", Value_Normal },
{ "nowrap", Value_NoWrap }, { "nowrap", Value_NoWrap },
@ -212,6 +214,7 @@ static const QCssKnownValue values[NumKnownValues - 1] = {
{ "on", Value_On }, { "on", Value_On },
{ "outset", Value_Outset }, { "outset", Value_Outset },
{ "overline", Value_Overline }, { "overline", Value_Overline },
{ "placeholder-text", Value_PlaceholderText },
{ "pre", Value_Pre }, { "pre", Value_Pre },
{ "pre-line", Value_PreLine }, { "pre-line", Value_PreLine },
{ "pre-wrap", Value_PreWrap }, { "pre-wrap", Value_PreWrap },
@ -230,6 +233,8 @@ static const QCssKnownValue values[NumKnownValues - 1] = {
{ "super", Value_Super }, { "super", Value_Super },
{ "svgmiterjoin", Value_SvgMiterJoin}, { "svgmiterjoin", Value_SvgMiterJoin},
{ "text", Value_Text }, { "text", Value_Text },
{ "tooltip-base", Value_ToolTipBase },
{ "tooltip-text", Value_ToolTipText },
{ "top", Value_Top }, { "top", Value_Top },
{ "transparent", Value_Transparent }, { "transparent", Value_Transparent },
{ "underline", Value_Underline }, { "underline", Value_Underline },
@ -244,10 +249,10 @@ static const QCssKnownValue values[NumKnownValues - 1] = {
}; };
//Map id to strings as they appears in the 'values' array above //Map id to strings as they appears in the 'values' array above
static const short indexOfId[NumKnownValues] = { 0, 44, 51, 45, 52, 53, 60, 37, 28, 78, 79, 27, 46, 6, 71, 50, static const short indexOfId[NumKnownValues] = { 0, 46, 54, 47, 55, 56, 63, 38, 29, 83, 84, 28, 48, 7, 76, 52,
31, 65, 66, 29, 55, 69, 7, 11, 42, 62, 20, 14, 18, 19, 21, 23, 54, 26, 49, 75, 39, 3, 2, 43, 70, 17, 12, 32, 68, 69, 30, 58, 74, 8, 12, 43, 65, 21, 15, 19, 20, 22, 24, 57, 27, 51, 80, 40, 4, 3, 45, 75, 18, 13,
63, 15, 34, 72, 35, 73, 61, 74, 36, 64, 22, 56, 41, 5, 57, 67, 77, 9, 30, 40, 13, 38, 68, 8, 10, 4, 76, 66, 16, 35, 77, 36, 78, 64, 79, 37, 67, 23, 59, 42, 6, 60, 70, 82, 10, 31, 41, 14, 39, 71, 9, 11, 5, 81,
59, 24, 25, 32, 33, 1, 16, 0, 58, 48, 47 }; 62, 25, 26, 33, 34, 2, 44, 72, 73, 53, 0, 17, 1, 61, 50, 49 };
QString Value::toString() const QString Value::toString() const
{ {
@ -731,6 +736,8 @@ static ColorData parseColorValue(QCss::Value v)
const QString &identifier = lst.at(0); const QString &identifier = lst.at(0);
if ((identifier.compare("palette"_L1, Qt::CaseInsensitive)) == 0) { if ((identifier.compare("palette"_L1, Qt::CaseInsensitive)) == 0) {
static_assert((Value_LastColorRole - Value_FirstColorRole + 1) == QPalette::ColorRole::NColorRoles);
int role = findKnownValue(lst.at(1).trimmed(), values, NumKnownValues); int role = findKnownValue(lst.at(1).trimmed(), values, NumKnownValues);
if (role >= Value_FirstColorRole && role <= Value_LastColorRole) if (role >= Value_FirstColorRole && role <= Value_LastColorRole)
return (QPalette::ColorRole)(role-Value_FirstColorRole); return (QPalette::ColorRole)(role-Value_FirstColorRole);

View File

@ -258,7 +258,12 @@ enum KnownValue {
Value_Link, Value_Link,
Value_LinkVisited, Value_LinkVisited,
Value_AlternateBase, Value_AlternateBase,
Value_LastColorRole = Value_AlternateBase, Value_NoRole,
Value_ToolTipBase,
Value_ToolTipText,
Value_PlaceholderText,
Value_Accent,
Value_LastColorRole = Value_Accent,
Value_Disabled, Value_Disabled,
Value_Active, Value_Active,

View File

@ -3451,8 +3451,11 @@
| \c link-visited \br | \c link-visited \br
| \c mid \br | \c mid \br
| \c midlight \br | \c midlight \br
| \c placeholder-text \br
| \c shadow \br | \c shadow \br
| \c text \br | \c text \br
| \c tooltip-base \br
| \c tooltip-text \br
| \c window \br | \c window \br
| \c window-text \br | \c window-text \br
\li These values correspond the \l{QPalette::ColorRole}{Color roles} \li These values correspond the \l{QPalette::ColorRole}{Color roles}

View File

@ -899,6 +899,7 @@ void tst_QCssParser::colorValue_data()
QTest::newRow("invalid7") << "color: hsla(1, a, 1, 21)" << QColor(); QTest::newRow("invalid7") << "color: hsla(1, a, 1, 21)" << QColor();
QTest::newRow("role") << "color: palette(base)" << qApp->palette().color(QPalette::Base); QTest::newRow("role") << "color: palette(base)" << qApp->palette().color(QPalette::Base);
QTest::newRow("role2") << "color: palette( window-text ) " << qApp->palette().color(QPalette::WindowText); QTest::newRow("role2") << "color: palette( window-text ) " << qApp->palette().color(QPalette::WindowText);
QTest::newRow("role3") << "color: palette(accent)" << qApp->palette().color(QPalette::Accent);
QTest::newRow("transparent") << "color: transparent" << QColor(Qt::transparent); QTest::newRow("transparent") << "color: transparent" << QColor(Qt::transparent);
QTest::newRow("rgb-invalid") << "color: rgb(10, 20, 30, 40)" << QColor(); QTest::newRow("rgb-invalid") << "color: rgb(10, 20, 30, 40)" << QColor();