KDeclarative

managedconfigmodule.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2019 Kevin Ottens <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "managedconfigmodule.h"
9 
10 #include <QPointer>
11 
12 #include <KConfigCore/KCoreConfigSkeleton>
13 
14 namespace KQuickAddons {
15 
16 class ManagedConfigModulePrivate
17 {
18 public:
19  ManagedConfigModulePrivate(ManagedConfigModule *module):
20  _q(module)
21  {
22  QMetaObject::invokeMethod(_q, "_k_registerSettings", Qt::QueuedConnection);
23  }
24 
25  void _k_registerSettings();
26 
27  ManagedConfigModule *_q;
29 };
30 
31 ManagedConfigModule::ManagedConfigModule(const KAboutData *aboutData, QObject *parent, const QVariantList &args)
32  : ConfigModule(aboutData, parent, args),
33  d(new ManagedConfigModulePrivate(this))
34 {
35 }
36 
37 ManagedConfigModule::ManagedConfigModule(const KPluginMetaData &metaData, QObject *parent, const QVariantList &args)
38  : ConfigModule(metaData, parent, args),
39  d(new ManagedConfigModulePrivate(this))
40 {
41 }
42 
44  : ConfigModule(parent, args),
45  d(new ManagedConfigModulePrivate(this))
46 {
47 }
48 
50 {
51  delete d;
52 }
53 
55 {
56  for (const auto &skeleton : qAsConst(d->_skeletons)) {
57  if (skeleton) {
58  skeleton->load();
59  }
60  }
61 }
62 
64 {
65  for (const auto &skeleton : qAsConst(d->_skeletons)) {
66  if (skeleton) {
67  skeleton->save();
68  }
69  }
70 }
71 
73 {
74  for (const auto &skeleton : qAsConst(d->_skeletons)) {
75  if (skeleton) {
76  skeleton->setDefaults();
77  }
78  }
79 }
80 
81 bool ManagedConfigModule::isSaveNeeded() const
82 {
83  return false;
84 }
85 
86 bool ManagedConfigModule::isDefaults() const
87 {
88  return true;
89 }
90 
91 void ManagedConfigModulePrivate::_k_registerSettings()
92 {
93  const auto skeletons = _q->findChildren<KCoreConfigSkeleton*>();
94  for (auto *skeleton : skeletons) {
95  _q->registerSettings(skeleton);
96  }
97 }
98 
100 {
101  bool needsSave = false;
102  bool representsDefaults = true;
103  for (const auto &skeleton : qAsConst(d->_skeletons)) {
104  if (skeleton) {
105  needsSave |= skeleton->isSaveNeeded();
106  representsDefaults &= skeleton->isDefaults();
107  }
108  }
109 
110  if (!needsSave) {
111  needsSave = isSaveNeeded();
112  }
113 
114  if (representsDefaults) {
115  representsDefaults = isDefaults();
116  }
117 
118  setRepresentsDefaults(representsDefaults);
119  setNeedsSave(needsSave);
120 }
121 
123 {
124  if (!skeleton || d->_skeletons.contains(skeleton)) {
125  return;
126  }
127 
128  d->_skeletons.append(skeleton);
129 
130  auto settingsChangedSlotIndex = metaObject()->indexOfMethod("settingsChanged()");
131  auto settingsChangedSlot = metaObject()->method(settingsChangedSlotIndex);
132 
134 
135  const auto items = skeleton->items();
136  for (auto item : items) {
137  const auto itemHasSignals = dynamic_cast<KConfigCompilerSignallingItem*>(item) || dynamic_cast<KPropertySkeletonItem*>(item);
138  if (!itemHasSignals) {
139  continue;
140  }
141 
142  auto name = item->name();
143  if (name.at(0).isUpper()) {
144  name[0] = name[0].toLower();
145  }
146 
147  const auto metaObject = skeleton->metaObject();
148  const auto propertyIndex = metaObject->indexOfProperty(name.toUtf8().constData());
149  const auto property = metaObject->property(propertyIndex);
150  if (!property.hasNotifySignal()) {
151  continue;
152  }
153 
154  const auto changedSignal = property.notifySignal();
155  QObject::connect(skeleton, changedSignal, this, settingsChangedSlot);
156  }
157 
158  auto toRemove = std::remove_if(d->_skeletons.begin(),
159  d->_skeletons.end(),
160  [](const QPointer<KCoreConfigSkeleton> &value) { return value.isNull(); } );
161  d->_skeletons.erase(toRemove, d->_skeletons.end());
162 
163  QMetaObject::invokeMethod(this, "settingsChanged", Qt::QueuedConnection);
164 }
165 
166 }
167 
168 #include "moc_managedconfigmodule.cpp"
169 
void setNeedsSave(bool needs)
Set this property to true when the user changes something in the module, signaling that a save (such ...
QMetaMethod notifySignal() const const
int indexOfMethod(const char *method) const const
void load() override
Load the configuration data into the module.
virtual const QMetaObject * metaObject() const const
void setRepresentsDefaults(bool defaults)
Set this property to true when the user sets the state of the module to the default settings (e...
ManagedConfigModule(const KAboutData *aboutData, QObject *parent=nullptr, const QVariantList &args=QVariantList())
Base class for all modules which manage automatically some of their state.
~ManagedConfigModule()
Destroys the module.
QVariant property(const char *name) const const
void registerSettings(KCoreConfigSkeleton *skeleton)
Allow to register manually settings class generated from a kcfg file.
The base class for configuration modules.
Definition: configmodule.h:114
void defaults() override
Sets the configuration to sensible default values.
bool isUpper() const const
const char * constData() const const
void save() override
Save the configuration data.
bool needsSave()
True when the module has something changed and needs save.
QString toLower() const const
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
int indexOfProperty(const char *name) const const
const QChar at(int position) const const
bool representsDefaults()
True when the module state represents the default settings.
QueuedConnection
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
QMetaMethod method(int index) const const
KConfigSkeletonItem::List items() const
void settingsChanged()
Forces the module to reevaluate the saveNeeded and representsDefault state.
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 Wed Jan 27 2021 22:38:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.