KCMUtils

settinghighlighterprivate.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Kevin Ottens <kevin.ottens@enioka.com>
3 SPDX-FileCopyrightText: 2020 Cyril Rossi <cyril.rossi@enioka.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "settinghighlighterprivate.h"
9
10#include <QGuiApplication>
11#include <QQmlContext>
12
13namespace
14{
15QByteArray itemClassName(QQuickItem *item)
16{
17 // Split since some exported types will be of the form: Foo_QMLTYPE_XX
18 const auto className = QByteArray(item->metaObject()->className()).split('_').first();
19 return className;
20}
21
22QList<QQuickItem *> findDescendantItems(QQuickItem *item)
23{
24 const auto children = item->childItems();
25 auto result = children;
26 for (auto child : children) {
27 result += findDescendantItems(child);
28 }
29 return result;
30}
31
32QQuickItem *findStyleItem(QQuickItem *item)
33{
34 const auto className = itemClassName(item);
35
36 auto descendant = findDescendantItems(item);
37 for (auto child : std::as_const(descendant)) {
38 if (className.contains("FontWidget") && itemClassName(child).contains("TextField")) {
39 return child->property("background").value<QQuickItem *>();
40 }
41 if (itemClassName(child).contains("GridViewInternal")) {
42 return child;
43 }
44 if (itemClassName(child).contains("GridView")) {
45 return child->property("view").value<QQuickItem *>();
46 }
47 if (itemClassName(child).contains("CheckIndicator") //
48 || itemClassName(child).contains("KQuickStyleItem")) {
49 return child;
50 }
51 }
52 return nullptr;
53}
54
55} // namespace
56
57QQuickItem *SettingHighlighterPrivate::target() const
58{
59 return m_target;
60}
61
62void SettingHighlighterPrivate::setTarget(QQuickItem *target)
63{
64 if (m_target == target) {
65 return;
66 }
67
68 if (m_target) {
69 disconnect(m_target, nullptr, this, nullptr);
70 }
71
72 m_target = target;
73
74 if (m_target) {
75 connect(m_target, &QQuickItem::childrenChanged, this, &SettingHighlighterPrivate::updateTarget);
76 }
77
78 Q_EMIT targetChanged();
79}
80
81bool SettingHighlighterPrivate::highlight() const
82{
83 return m_highlight;
84}
85
86void SettingHighlighterPrivate::setHighlight(bool highlight)
87{
88 if (m_highlight == highlight) {
89 return;
90 }
91
92 m_highlight = highlight;
93 Q_EMIT highlightChanged();
94}
95
96bool SettingHighlighterPrivate::defaultIndicatorVisible() const
97{
98 return m_enabled;
99}
100
101void SettingHighlighterPrivate::setDefaultIndicatorVisible(bool enabled)
102{
103 if (m_enabled == enabled) {
104 return;
105 }
106
107 m_enabled = enabled;
108 Q_EMIT defaultIndicatorVisibleChanged(m_enabled);
109}
110
111void SettingHighlighterPrivate::updateTarget()
112{
113 if (!m_isComponentComplete) {
114 return;
115 }
116
117 if (!m_styleTarget && m_target) {
118 m_styleTarget = findStyleItem(m_target);
119 }
120
121 if (m_styleTarget) {
122 if (itemClassName(m_styleTarget).contains("GridViewInternal")) {
123 m_styleTarget->setProperty("neutralHighlight", m_highlight && m_enabled);
124 } else {
125 m_styleTarget->setProperty("_kde_highlight_neutral", m_highlight && m_enabled);
126 }
127 m_styleTarget->polish();
128 }
129}
130
131void SettingHighlighterPrivate::componentComplete()
132{
133 m_isComponentComplete = true;
134
135 connect(this, &SettingHighlighterPrivate::targetChanged, this, &SettingHighlighterPrivate::updateTarget);
136 connect(this, &SettingHighlighterPrivate::highlightChanged, this, &SettingHighlighterPrivate::updateTarget);
137 connect(this, &SettingHighlighterPrivate::defaultIndicatorVisibleChanged, this, &SettingHighlighterPrivate::updateTarget);
138
139 updateTarget();
140}
141
142#include "moc_settinghighlighterprivate.cpp"
QList< QByteArray > split(char sep) const const
T & first()
const char * className() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QList< T > findChildren(Qt::FindChildOptions options) const const
virtual const QMetaObject * metaObject() const const
QVariant property(const char *name) const const
QList< QQuickItem * > childItems() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:56 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.