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 <marcus.tillmanns@qt.io>
This commit is contained in:
Alessandro Portale
2024-09-27 12:30:26 +02:00
parent 990a72cfcb
commit 799642b3d0
2 changed files with 22 additions and 0 deletions

View File

@@ -374,6 +374,19 @@ void Theme::setHelpMenu(QMenu *menu)
#endif
}
expected_str<Theme::Color> Theme::colorToken(const QString &tokenName,
[[maybe_unused]] TokenFlags flags)
{
const QString colorName = "Token_" + tokenName;
static const QMetaEnum colorEnum = QMetaEnum::fromType<Theme::Color>();
bool ok = false;
const Color result = static_cast<Color>(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) {

View File

@@ -5,6 +5,8 @@
#include "../utils_global.h"
#include <utils/expected.h>
#include <QBrush> // QGradientStops
#include <QObject>
@@ -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<Color> colorToken(const QString &token, TokenFlags flags = {});
protected:
Theme(Theme *originTheme, QObject *parent = nullptr);
ThemePrivate *d;