KDeclarative

settingstateproxy.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 "settingstateproxy.h"
9 
10 #include <QDebug>
11 #include <QMetaMethod>
12 
14 {
15  return m_configObject;
16 }
17 
18 void SettingStateProxy::setConfigObject(KCoreConfigSkeleton *configObject)
19 {
20  if (m_configObject == configObject) {
21  return;
22  }
23 
24  if (m_configObject) {
25  m_configObject->disconnect(this);
26  }
27 
28  m_configObject = configObject;
29  Q_EMIT configObjectChanged();
30  updateState();
31  connectSetting();
32 }
33 
35 {
36  return m_settingName;
37 }
38 
39 void SettingStateProxy::setSettingName(const QString &settingName)
40 {
41  if (m_settingName == settingName) {
42  return;
43  }
44 
45  if (m_configObject) {
46  m_configObject->disconnect(this);
47  }
48 
49  m_settingName = settingName;
50  Q_EMIT settingNameChanged();
51  updateState();
52  connectSetting();
53 }
54 
55 bool SettingStateProxy::isImmutable() const
56 {
57  return m_immutable;
58 }
59 
60 bool SettingStateProxy::isDefaulted() const
61 {
62  return m_defaulted;
63 }
64 
65 void SettingStateProxy::updateState()
66 {
67  const auto item = m_configObject ? m_configObject->findItem(m_settingName) : nullptr;
68  const auto immutable = item ? item->isImmutable() : false;
69  const auto defaulted = item ? item->isDefault() : true;
70 
71  if (m_immutable != immutable) {
72  m_immutable = immutable;
73  Q_EMIT immutableChanged();
74  }
75 
76  if (m_defaulted != defaulted) {
77  m_defaulted = defaulted;
78  Q_EMIT defaultedChanged();
79  }
80 }
81 
82 void SettingStateProxy::connectSetting()
83 {
84  const auto item = m_configObject ? m_configObject->findItem(m_settingName) : nullptr;
85  if (!item) {
86  return;
87  }
88 
89  const auto updateStateSlotIndex = metaObject()->indexOfMethod("updateState()");
90  Q_ASSERT(updateStateSlotIndex >= 0);
91  const auto updateStateSlot = metaObject()->method(updateStateSlotIndex);
92  Q_ASSERT(updateStateSlot.isValid());
93 
94  const auto itemHasSignals = dynamic_cast<KConfigCompilerSignallingItem*>(item) || dynamic_cast<KPropertySkeletonItem*>(item);
95  if (!itemHasSignals) {
96  qWarning() << "Attempting to use SettingStateProxy with a non signalling item:" << m_settingName;
97  return;
98  }
99 
100  const auto propertyName = [this] {
101  auto name = m_settingName;
102  if (name.at(0).isUpper()) {
103  name[0] = name[0].toLower();
104  }
105  return name.toUtf8();
106  }();
107 
108  const auto metaObject = m_configObject->metaObject();
109  const auto propertyIndex = metaObject->indexOfProperty(propertyName.constData());
110  Q_ASSERT(propertyIndex >= 0);
111  const auto property = metaObject->property(propertyIndex);
112  Q_ASSERT(property.isValid());
113  if (!property.hasNotifySignal()) {
114  qWarning() << "Attempting to use SettingStateProxy with a non notifying property:" << propertyName;
115  return;
116  }
117 
118  const auto changedSignal = property.notifySignal();
119  Q_ASSERT(changedSignal.isValid());
120  connect(m_configObject, changedSignal, this, updateStateSlot);
122  this, &SettingStateProxy::updateState);
123 }
bool immutable
Indicates if the setting is marked as immutable.
QString name(const QVariant &location)
QMetaMethod notifySignal() const const
int indexOfMethod(const char *method) const const
virtual const QMetaObject * metaObject() const const
QVariant property(const char *name) const const
QString settingName
The name of the setting in the config object.
bool isUpper() const const
KCoreConfigSkeleton configObject
The config object which will be monitored for setting state changes.
QString toLower() const const
int indexOfProperty(const char *name) const const
const QChar at(int position) const const
bool isValid() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool defaulted
Indicates if the setting differs from its default value.
QMetaMethod method(int index) const const
Q_EMITQ_EMIT
QMetaProperty property(int index) const const
QByteArray toUtf8() 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.