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: &QQuickItem::childrenChanged, receiver: this, slot: &SettingHighlighterPrivate::updateTarget); |
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 | updateTarget(); |
80 | } |
81 | |
82 | bool SettingHighlighterPrivate::highlight() const |
83 | { |
84 | return m_highlight; |
85 | } |
86 | |
87 | void 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 | |
98 | bool SettingHighlighterPrivate::defaultIndicatorVisible() const |
99 | { |
100 | return m_enabled; |
101 | } |
102 | |
103 | void SettingHighlighterPrivate::setDefaultIndicatorVisible(bool enabled) |
104 | { |
105 | if (m_enabled == enabled) { |
106 | return; |
107 | } |
108 | |
109 | m_enabled = enabled; |
110 | Q_EMIT defaultIndicatorVisibleChanged(enabled: m_enabled); |
111 | updateTarget(); |
112 | } |
113 | |
114 | void SettingHighlighterPrivate::updateTarget() |
115 | { |
116 | if (!m_isComponentComplete) { |
117 | return; |
118 | } |
119 | |
120 | if (!m_styleTarget && m_target) { |
121 | m_styleTarget = findStyleItem(item: m_target); |
122 | } |
123 | |
124 | if (m_styleTarget) { |
125 | if (itemClassName(item: m_styleTarget).contains(bv: "GridViewInternal")) { |
126 | m_styleTarget->setProperty(name: "neutralHighlight", value: m_highlight && m_enabled); |
127 | } else { |
128 | m_styleTarget->setProperty(name: "_kde_highlight_neutral", value: m_highlight && m_enabled); |
129 | } |
130 | m_styleTarget->polish(); |
131 | } |
132 | } |
133 | |
134 | void SettingHighlighterPrivate::componentComplete() |
135 | { |
136 | m_isComponentComplete = true; |
137 | |
138 | updateTarget(); |
139 | } |
140 | |
141 | #include "moc_settinghighlighterprivate.cpp" |
142 |