GrantleeTheme

palette.cpp
1 /*
2  * SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  */
6 
7 #include "palette.h"
8 #include <QObject>
9 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
10 #include <grantlee/metatype.h>
11 #else
12 #include <KTextTemplate/metatype.h>
13 #endif
14 
15 #include <QPalette>
16 
17 #define ROLE(R) \
18  { \
19 #R, QPalette::R \
20  }
21 
22 static const struct {
23  const char *name;
25 } color_roles[] = {
26  ROLE(AlternateBase), ROLE(Base), ROLE(BrightText), ROLE(Button), ROLE(ButtonText), ROLE(Dark), ROLE(Highlight),
27  ROLE(HighlightedText), ROLE(Light), ROLE(Link), ROLE(LinkVisited), ROLE(Mid), ROLE(Midlight), ROLE(PlaceholderText),
28  ROLE(Shadow), ROLE(Text), ROLE(ToolTipBase), ROLE(ToolTipText), ROLE(Window), ROLE(WindowText),
29 };
30 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
31 GRANTLEE_BEGIN_LOOKUP(QPalette)
32 #else
33 KTEXTTEMPLATE_BEGIN_LOOKUP(QPalette)
34 #endif
35 auto group = QPalette::Inactive;
36 auto roleName = property;
37 
38 if (property.startsWith(QLatin1String("active"))) {
39  roleName = property.mid(6);
40  group = QPalette::Active;
41 } else if (property.startsWith(QLatin1String("disabled"))) {
42  roleName = property.mid(8);
43  group = QPalette::Disabled;
44 } else if (property.startsWith(QLatin1String("inactive"))) {
45  roleName = property.mid(8);
46  group = QPalette::Inactive;
47 }
48 
49 for (const auto &role : color_roles) {
50  if (roleName.compare(QLatin1String(role.name), Qt::CaseInsensitive) == 0) {
51  return object.color(group, role.role);
52  }
53 }
54 return {};
55 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
56 GRANTLEE_END_LOOKUP
57 #else
58 KTEXTTEMPLATE_END_LOOKUP
59 #endif
60 
61 void Palette::registerMetaType()
62 {
63 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
64  Grantlee::registerMetaType<QPalette>();
65 #else
66  KTextTemplate::registerMetaType<QPalette>();
67 #endif
68 }
CaseInsensitive
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Wed Sep 28 2022 04:06:59 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.