File indexing completed on 2024-11-24 04:41:43

0001 /*
0002  * SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #include "palette.h"
0008 #include <KTextTemplate/MetaType>
0009 #include <QObject>
0010 
0011 #include <QPalette>
0012 
0013 #define ROLE(R)                                                                                                                                                \
0014     {                                                                                                                                                          \
0015 #R, QPalette::R                                                                                                                                        \
0016     }
0017 
0018 static const struct {
0019     const char *name;
0020     QPalette::ColorRole role;
0021 } color_roles[] = {
0022     ROLE(AlternateBase),   ROLE(Base),  ROLE(BrightText),  ROLE(Button),      ROLE(ButtonText), ROLE(Dark),       ROLE(Highlight),
0023     ROLE(HighlightedText), ROLE(Light), ROLE(Link),        ROLE(LinkVisited), ROLE(Mid),        ROLE(Midlight),   ROLE(PlaceholderText),
0024     ROLE(Shadow),          ROLE(Text),  ROLE(ToolTipBase), ROLE(ToolTipText), ROLE(Window),     ROLE(WindowText),
0025 };
0026 KTEXTTEMPLATE_BEGIN_LOOKUP(QPalette)
0027 auto group = QPalette::Inactive;
0028 auto roleName = property;
0029 
0030 if (property.startsWith(QLatin1StringView("active"))) {
0031     roleName = property.mid(6);
0032     group = QPalette::Active;
0033 } else if (property.startsWith(QLatin1StringView("disabled"))) {
0034     roleName = property.mid(8);
0035     group = QPalette::Disabled;
0036 } else if (property.startsWith(QLatin1StringView("inactive"))) {
0037     roleName = property.mid(8);
0038     group = QPalette::Inactive;
0039 }
0040 
0041 for (const auto &role : color_roles) {
0042     if (roleName.compare(QLatin1StringView(role.name), Qt::CaseInsensitive) == 0) {
0043         return object.color(group, role.role);
0044     }
0045 }
0046 return {};
0047 KTEXTTEMPLATE_END_LOOKUP
0048 
0049 void Palette::registerMetaType()
0050 {
0051     KTextTemplate::registerMetaType<QPalette>();
0052 }