File indexing completed on 2024-05-12 03:54:10

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     updateTarget();
0070     Q_EMIT targetChanged();
0071 }
0072 
0073 bool SettingHighlighterPrivate::highlight() const
0074 {
0075     return m_highlight;
0076 }
0077 
0078 void SettingHighlighterPrivate::setHighlight(bool highlight)
0079 {
0080     if (m_highlight == highlight) {
0081         return;
0082     }
0083 
0084     m_highlight = highlight;
0085     updateTarget();
0086     Q_EMIT highlightChanged();
0087 }
0088 
0089 bool SettingHighlighterPrivate::defaultIndicatorVisible() const
0090 {
0091     return m_enabled;
0092 }
0093 
0094 void SettingHighlighterPrivate::setDefaultIndicatorVisible(bool enabled)
0095 {
0096     if (m_enabled == enabled) {
0097         return;
0098     }
0099 
0100     m_enabled = enabled;
0101     updateTarget();
0102 
0103     Q_EMIT defaultIndicatorVisibleChanged(m_enabled);
0104 }
0105 
0106 void SettingHighlighterPrivate::updateTarget()
0107 {
0108     if (!m_isComponentComplete) {
0109         return;
0110     }
0111 
0112     if (!m_styleTarget && m_target) {
0113         m_styleTarget = findStyleItem(m_target);
0114     }
0115 
0116     if (m_styleTarget) {
0117         if (itemClassName(m_styleTarget).contains("GridViewInternal")) {
0118             m_styleTarget->setProperty("neutralHighlight", m_highlight && m_enabled);
0119         } else {
0120             m_styleTarget->setProperty("_kde_highlight_neutral", m_highlight && m_enabled);
0121         }
0122         m_styleTarget->polish();
0123     }
0124 }
0125 
0126 void SettingHighlighterPrivate::componentComplete()
0127 {
0128     m_isComponentComplete = true;
0129     updateTarget();
0130 }
0131 
0132 #include "moc_settinghighlighterprivate.cpp"