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 | |
13 | namespace |
14 | { |
15 | QByteArray 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(sep: '_').first(); |
19 | return className; |
20 | } |
21 | |
22 | QList<QQuickItem *> findDescendantItems(QQuickItem *item) |
23 | { |
24 | const auto children = item->childItems(); |
25 | auto result = children; |
26 | for (auto child : children) { |
27 | result += findDescendantItems(item: child); |
28 | } |
29 | return result; |
30 | } |
31 | |
32 | QQuickItem *findStyleItem(QQuickItem *item) |
33 | { |
34 | const auto className = itemClassName(item); |
35 | |
36 | auto descendant = findDescendantItems(item); |
37 | for (auto child : std::as_const(t&: descendant)) { |
38 | if (className.contains(bv: "FontWidget" ) && itemClassName(item: child).contains(bv: "TextField" )) { |
39 | return child->property(name: "background" ).value<QQuickItem *>(); |
40 | } |
41 | if (itemClassName(item: child).contains(bv: "GridViewInternal" )) { |
42 | return child; |
43 | } |
44 | if (itemClassName(item: child).contains(bv: "GridView" )) { |
45 | return child->property(name: "view" ).value<QQuickItem *>(); |
46 | } |
47 | if (itemClassName(item: child).contains(bv: "CheckIndicator" ) // |
48 | || itemClassName(item: child).contains(bv: "KQuickStyleItem" )) { |
49 | return child; |
50 | } |
51 | } |
52 | return nullptr; |
53 | } |
54 | |
55 | } // namespace |
56 | |
57 | QQuickItem *SettingHighlighterPrivate::target() const |
58 | { |
59 | return m_target; |
60 | } |
61 | |
62 | void SettingHighlighterPrivate::setTarget(QQuickItem *target) |
63 | { |
64 | if (m_target == target) { |
65 | return; |
66 | } |
67 | |
68 | if (m_target) { |
69 | disconnect(sender: m_target, signal: nullptr, receiver: this, member: nullptr); |
70 | } |
71 | |
72 | m_target = target; |
73 | |
74 | if (m_target) { |
75 | connect(sender: m_target, signal: &QQuickItem::childrenChanged, context: this, slot: &SettingHighlighterPrivate::updateTarget); |
76 | } |
77 | |
78 | Q_EMIT targetChanged(); |
79 | } |
80 | |
81 | bool SettingHighlighterPrivate::highlight() const |
82 | { |
83 | return m_highlight; |
84 | } |
85 | |
86 | void SettingHighlighterPrivate::setHighlight(bool highlight) |
87 | { |
88 | if (m_highlight == highlight) { |
89 | return; |
90 | } |
91 | |
92 | m_highlight = highlight; |
93 | Q_EMIT highlightChanged(); |
94 | } |
95 | |
96 | bool SettingHighlighterPrivate::defaultIndicatorVisible() const |
97 | { |
98 | return m_enabled; |
99 | } |
100 | |
101 | void SettingHighlighterPrivate::setDefaultIndicatorVisible(bool enabled) |
102 | { |
103 | if (m_enabled == enabled) { |
104 | return; |
105 | } |
106 | |
107 | m_enabled = enabled; |
108 | Q_EMIT defaultIndicatorVisibleChanged(enabled: m_enabled); |
109 | } |
110 | |
111 | void SettingHighlighterPrivate::updateTarget() |
112 | { |
113 | if (!m_isComponentComplete) { |
114 | return; |
115 | } |
116 | |
117 | if (!m_styleTarget && m_target) { |
118 | m_styleTarget = findStyleItem(item: m_target); |
119 | } |
120 | |
121 | if (m_styleTarget) { |
122 | if (itemClassName(item: m_styleTarget).contains(bv: "GridViewInternal" )) { |
123 | m_styleTarget->setProperty(name: "neutralHighlight" , value: m_highlight && m_enabled); |
124 | } else { |
125 | m_styleTarget->setProperty(name: "_kde_highlight_neutral" , value: m_highlight && m_enabled); |
126 | } |
127 | m_styleTarget->polish(); |
128 | } |
129 | } |
130 | |
131 | void SettingHighlighterPrivate::componentComplete() |
132 | { |
133 | m_isComponentComplete = true; |
134 | |
135 | connect(sender: this, signal: &SettingHighlighterPrivate::targetChanged, context: this, slot: &SettingHighlighterPrivate::updateTarget); |
136 | connect(sender: this, signal: &SettingHighlighterPrivate::highlightChanged, context: this, slot: &SettingHighlighterPrivate::updateTarget); |
137 | connect(sender: this, signal: &SettingHighlighterPrivate::defaultIndicatorVisibleChanged, context: this, slot: &SettingHighlighterPrivate::updateTarget); |
138 | |
139 | updateTarget(); |
140 | } |
141 | |
142 | #include "moc_settinghighlighterprivate.cpp" |
143 | |