From 799642b3d0d7ec353696edfbce5840c30e822ad2 Mon Sep 17 00:00:00 2001 From: Alessandro Portale Date: Fri, 27 Sep 2024 12:30:26 +0200 Subject: [PATCH] Utils: Add a string based theme color getter to Utils::Theme Lua plugins need to access theme colors. Lua plugins are meant to be compatible with various subsequent Qt Creator versions. The enum-based interface would therefor require the enum to be frozen across minor Qt Creator versions. In order to avoid that (we still dream about cleaning up the themes), this new string based theme color getter should be good for the Lua use- case. The getter restricts the used colors to the "Token_" range. In addition, the flags (eventually) allow token based theming of "hybrid" themes. Change-Id: Ic3c9e0a9bff9d055c15fe161375bbef7b09fd0e1 Reviewed-by: Marcus Tillmanns --- src/libs/utils/theme/theme.cpp | 13 +++++++++++++ src/libs/utils/theme/theme.h | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/src/libs/utils/theme/theme.cpp b/src/libs/utils/theme/theme.cpp index ef20ddddb7c..1b7ccee0b60 100644 --- a/src/libs/utils/theme/theme.cpp +++ b/src/libs/utils/theme/theme.cpp @@ -374,6 +374,19 @@ void Theme::setHelpMenu(QMenu *menu) #endif } +expected_str Theme::colorToken(const QString &tokenName, + [[maybe_unused]] TokenFlags flags) +{ + const QString colorName = "Token_" + tokenName; + static const QMetaEnum colorEnum = QMetaEnum::fromType(); + bool ok = false; + const Color result = static_cast(colorEnum.keyToValue(colorName.toLatin1(), &ok)); + if (!ok) + return make_unexpected(QString::fromLatin1("%1 - Color token \"%2\" not found.") + .arg(Q_FUNC_INFO).arg(tokenName)); + return result; +} + QPalette Theme::initialPalette() { if (!m_initialPalette) { diff --git a/src/libs/utils/theme/theme.h b/src/libs/utils/theme/theme.h index dbf39780ac2..9508cfd7aeb 100644 --- a/src/libs/utils/theme/theme.h +++ b/src/libs/utils/theme/theme.h @@ -5,6 +5,8 @@ #include "../utils_global.h" +#include + #include // QGradientStops #include @@ -535,9 +537,14 @@ public: QDSTheme }; + enum TokenFlag { + UsedInToolbar = 1, + }; + Q_ENUM(Color) Q_ENUM(ImageFile) Q_ENUM(Flag) + Q_DECLARE_FLAGS(TokenFlags, TokenFlag) Q_INVOKABLE bool flag(Utils::Theme::Flag f) const; Q_INVOKABLE QColor color(Utils::Theme::Color role) const; @@ -560,6 +567,8 @@ public: static void setHelpMenu(QMenu *menu); + static expected_str colorToken(const QString &token, TokenFlags flags = {}); + protected: Theme(Theme *originTheme, QObject *parent = nullptr); ThemePrivate *d;