KDeclarative

settinghighlighterprivate.cpp
1 /*
2  SPDX-FileCopyrightText: 2020 Kevin Ottens <[email protected]>
3  SPDX-FileCopyrightText: 2020 Cyril Rossi <[email protected]>
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('_').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(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 : qAsConst(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 
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  m_target = target;
69  Q_EMIT targetChanged();
70 }
71 
72 bool SettingHighlighterPrivate::highlight() const
73 {
74  return m_highlight;
75 }
76 
77 void SettingHighlighterPrivate::setHighlight(bool highlight)
78 {
79  if (m_highlight == highlight) {
80  return;
81  }
82 
83  m_highlight = highlight;
84  updateTarget();
85  Q_EMIT highlightChanged();
86 }
87 
88 bool SettingHighlighterPrivate::defaultIndicatorVisible() const
89 {
90  return m_enabled;
91 }
92 
93 void SettingHighlighterPrivate::setDefaultIndicatorVisible(bool enabled)
94 {
95  if (m_enabled == enabled) {
96  return;
97  }
98 
99  m_enabled = enabled;
100  updateTarget();
101 
102  Q_EMIT defaultIndicatorVisibleChanged(m_enabled);
103 }
104 
105 void SettingHighlighterPrivate::updateTarget()
106 {
107  if (!m_styleTarget) {
108  if (!m_target) {
109  // parent is SettingStateBinding/SettingHighlighter, use its visual parent as target item.
110  const auto *parentItem = qobject_cast<QQuickItem*>(parent());
111  if (parentItem) {
112  setTarget(parentItem->parentItem());
113  }
114  }
115  if (m_target) {
116  m_styleTarget = findStyleItem(m_target);
117  }
118  }
119 
120  if (m_styleTarget) {
121  if (itemClassName(m_styleTarget).contains("GridViewInternal")) {
122  m_styleTarget->setProperty("neutralHighlight", m_highlight && m_enabled);
123  } else {
124  m_styleTarget->setProperty("_kde_highlight_neutral", m_highlight && m_enabled);
125  }
126  m_styleTarget->polish();
127  }
128 }
QList< QQuickItem * > childItems() const const
virtual const QMetaObject * metaObject() const const
T value() const const
QVariant property(const char *name) const const
const char * className() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:44:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.