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, &QQuickItem::childrenChanged, this, &SettingHighlighterPrivate::updateTarget);
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 updateTarget();
80}
81
82bool SettingHighlighterPrivate::highlight() const
83{
84 return m_highlight;
85}
86
87void SettingHighlighterPrivate::setHighlight(bool highlight)
88{
89 if (m_highlight == highlight) {
90 return;
91 }
92
93 m_highlight = highlight;
94 Q_EMIT highlightChanged();
95 updateTarget();
96}
97
98bool SettingHighlighterPrivate::defaultIndicatorVisible() const
99{
100 return m_enabled;
101}
102
103void SettingHighlighterPrivate::setDefaultIndicatorVisible(bool enabled)
104{
105 if (m_enabled == enabled) {
106 return;
107 }
108
109 m_enabled = enabled;
110 Q_EMIT defaultIndicatorVisibleChanged(m_enabled);
111 updateTarget();
112}
113
114void SettingHighlighterPrivate::updateTarget()
115{
116 if (!m_isComponentComplete) {
117 return;
118 }
119
120 if (!m_styleTarget && m_target) {
121 m_styleTarget = findStyleItem(m_target);
122 }
123
124 if (m_styleTarget) {
125 if (itemClassName(m_styleTarget).contains("GridViewInternal")) {
126 m_styleTarget->setProperty("neutralHighlight", m_highlight && m_enabled);
127 } else {
128 m_styleTarget->setProperty("_kde_highlight_neutral", m_highlight && m_enabled);
129 }
130 m_styleTarget->polish();
131 }
132}
133
134void SettingHighlighterPrivate::componentComplete()
135{
136 m_isComponentComplete = true;
137
138 updateTarget();
139}
140
141#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)
virtual const QMetaObject * metaObject() const const
QList< QQuickItem * > childItems() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.