File indexing completed on 2024-05-19 15:09:22

0001 /*
0002     SPDX-FileCopyrightText: 2020 Kevin Ottens <kevin.ottens@enioka.com>
0003     SPDX-FileCopyrightText: 2020 Cyril Rossi <cyril.rossi@enioka.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "settinghighlighterprivate.h"
0009 
0010 #include <QGuiApplication>
0011 #include <QQmlContext>
0012 
0013 namespace
0014 {
0015 QByteArray itemClassName(QQuickItem *item)
0016 {
0017     // Split since some exported types will be of the form: Foo_QMLTYPE_XX
0018     const auto className = QByteArray(item->metaObject()->className()).split('_').first();
0019     return className;
0020 }
0021 
0022 QList<QQuickItem *> findDescendantItems(QQuickItem *item)
0023 {
0024     const auto children = item->childItems();
0025     auto result = children;
0026     for (auto child : children) {
0027         result += findDescendantItems(child);
0028     }
0029     return result;
0030 }
0031 
0032 QQuickItem *findStyleItem(QQuickItem *item)
0033 {
0034     const auto className = itemClassName(item);
0035 
0036     auto descendant = findDescendantItems(item);
0037     for (auto child : std::as_const(descendant)) {
0038         if (className.contains("FontWidget") && itemClassName(child).contains("TextField")) {
0039             return child->property("background").value<QQuickItem *>();
0040         }
0041         if (itemClassName(child).contains("GridViewInternal")) {
0042             return child;
0043         }
0044         if (itemClassName(child).contains("GridView")) {
0045             return child->property("view").value<QQuickItem *>();
0046         }
0047         if (itemClassName(child).contains("CheckIndicator") //
0048             || itemClassName(child).contains("KQuickStyleItem")) {
0049             return child;
0050         }
0051     }
0052     return nullptr;
0053 }
0054 
0055 } // namespace
0056 
0057 QQuickItem *SettingHighlighterPrivate::target() const
0058 {
0059     return m_target;
0060 }
0061 
0062 void SettingHighlighterPrivate::setTarget(QQuickItem *target)
0063 {
0064     if (m_target == target) {
0065         return;
0066     }
0067 
0068     m_target = target;
0069     Q_EMIT targetChanged();
0070 }
0071 
0072 bool SettingHighlighterPrivate::highlight() const
0073 {
0074     return m_highlight;
0075 }
0076 
0077 void SettingHighlighterPrivate::setHighlight(bool highlight)
0078 {
0079     if (m_highlight == highlight) {
0080         return;
0081     }
0082 
0083     m_highlight = highlight;
0084     updateTarget();
0085     Q_EMIT highlightChanged();
0086 }
0087 
0088 bool SettingHighlighterPrivate::defaultIndicatorVisible() const
0089 {
0090     return m_enabled;
0091 }
0092 
0093 void SettingHighlighterPrivate::setDefaultIndicatorVisible(bool enabled)
0094 {
0095     if (m_enabled == enabled) {
0096         return;
0097     }
0098 
0099     m_enabled = enabled;
0100     updateTarget();
0101 
0102     Q_EMIT defaultIndicatorVisibleChanged(m_enabled);
0103 }
0104 
0105 void SettingHighlighterPrivate::updateTarget()
0106 {
0107     if (!m_styleTarget) {
0108         if (!m_target) {
0109             // parent is SettingStateBinding/SettingHighlighter, use its visual parent as target item.
0110             const auto *parentItem = qobject_cast<QQuickItem *>(parent());
0111             if (parentItem) {
0112                 setTarget(parentItem->parentItem());
0113             }
0114         }
0115         if (m_target) {
0116             m_styleTarget = findStyleItem(m_target);
0117         }
0118     }
0119 
0120     if (m_styleTarget) {
0121         if (itemClassName(m_styleTarget).contains("GridViewInternal")) {
0122             m_styleTarget->setProperty("neutralHighlight", m_highlight && m_enabled);
0123         } else {
0124             m_styleTarget->setProperty("_kde_highlight_neutral", m_highlight && m_enabled);
0125         }
0126         m_styleTarget->polish();
0127     }
0128 }
0129 
0130 #include "moc_settinghighlighterprivate.cpp"