KCMUtils

kquickmanagedconfigmodule.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Kevin Ottens <kevin.ottens@enioka.com>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "kquickmanagedconfigmodule.h"
7
8#include <KCoreConfigSkeleton>
9#include <QPointer>
10#include <QTimer>
11
12class KQuickManagedConfigModulePrivate
13{
14public:
15 KQuickManagedConfigModulePrivate(KQuickManagedConfigModule *mod)
16 {
17 QTimer::singleShot(0, mod, [mod]() {
18 const auto skeletons = mod->findChildren<KCoreConfigSkeleton *>();
19 for (auto *skeleton : skeletons) {
20 mod->registerSettings(skeleton);
21 }
22 });
23 }
24
26};
27
29 : KQuickConfigModule(parent, metaData)
30 , d(new KQuickManagedConfigModulePrivate(this))
31{
32}
33
35
37{
38 for (const auto &skeleton : std::as_const(d->_skeletons)) {
39 if (skeleton) {
40 skeleton->load();
41 }
42 }
43}
44
46{
47 for (const auto &skeleton : std::as_const(d->_skeletons)) {
48 if (skeleton) {
49 skeleton->save();
50 }
51 }
52}
53
55{
56 for (const auto &skeleton : std::as_const(d->_skeletons)) {
57 if (skeleton) {
58 skeleton->setDefaults();
59 }
60 }
61}
62
63bool KQuickManagedConfigModule::isSaveNeeded() const
64{
65 return false;
66}
67
68bool KQuickManagedConfigModule::isDefaults() const
69{
70 return true;
71}
72
74{
75 bool needsSave = false;
76 bool representsDefaults = true;
77 for (const auto &skeleton : std::as_const(d->_skeletons)) {
78 if (skeleton) {
79 needsSave |= skeleton->isSaveNeeded();
80 representsDefaults &= skeleton->isDefaults();
81 }
82 }
83
84 if (!needsSave) {
85 needsSave = isSaveNeeded();
86 }
87
88 if (representsDefaults) {
89 representsDefaults = isDefaults();
90 }
91
92 setRepresentsDefaults(representsDefaults);
93 setNeedsSave(needsSave);
94}
95
97{
98 if (!skeleton || d->_skeletons.contains(skeleton)) {
99 return;
100 }
101
102 d->_skeletons.append(skeleton);
103
104 auto settingsChangedSlotIndex = metaObject()->indexOfMethod("settingsChanged()");
105 auto settingsChangedSlot = metaObject()->method(settingsChangedSlotIndex);
106
107 QObject::connect(skeleton, &KCoreConfigSkeleton::configChanged, this, &KQuickManagedConfigModule::settingsChanged);
108
109 const auto items = skeleton->items();
110 for (auto item : items) {
111 const auto itemHasSignals = dynamic_cast<KConfigCompilerSignallingItem *>(item) || dynamic_cast<KPropertySkeletonItem *>(item);
112 if (!itemHasSignals) {
113 continue;
114 }
115
116 auto name = item->name();
117 if (name.at(0).isUpper()) {
118 name[0] = name[0].toLower();
119 }
120
121 const auto metaObject = skeleton->metaObject();
122 const auto propertyIndex = metaObject->indexOfProperty(name.toUtf8().constData());
123 const auto property = metaObject->property(propertyIndex);
124 if (!property.hasNotifySignal()) {
125 continue;
126 }
127
128 const auto changedSignal = property.notifySignal();
129 QObject::connect(skeleton, changedSignal, this, settingsChangedSlot);
130 }
131
132 auto toRemove = std::remove_if(d->_skeletons.begin(), d->_skeletons.end(), [](const QPointer<KCoreConfigSkeleton> &value) {
133 return value.isNull();
134 });
135 d->_skeletons.erase(toRemove, d->_skeletons.end());
136
137 QMetaObject::invokeMethod(this, "settingsChanged", Qt::QueuedConnection);
138}
139
140#include "moc_kquickmanagedconfigmodule.cpp"
void setNeedsSave(bool needs)
Set this property to true when the user changes something in the module, signaling that a save (such ...
virtual void load()
Load the configuration data into the module.
void setRepresentsDefaults(bool defaults)
Set this property to true when the user sets the state of the module to the default settings (e....
KConfigSkeletonItem::List items() const
The base class for QtQuick configuration modules.
The base class for configuration modules using KConfigXT settings.
~KQuickManagedConfigModule() override
Destroys the module.
void settingsChanged()
Forces the module to reevaluate the saveNeeded and representsDefault state.
void registerSettings(KCoreConfigSkeleton *skeleton)
Allow to register manually settings class generated from a kcfg file.
void save() override
Save the configuration data.
void load() override
Load the configuration data into the module.
KQuickManagedConfigModule(QObject *parent, const KPluginMetaData &metaData)
Base class for all KControlModules.
void defaults() override
Sets the configuration to sensible default values.
const char * constData() const const
bool isUpper(char32_t ucs4)
int indexOfMethod(const char *method) const const
int indexOfProperty(const char *name) const const
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
QMetaMethod method(int index) const const
QMetaProperty property(int index) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QList< T > findChildren(Qt::FindChildOptions options) const const
virtual const QMetaObject * metaObject() const const
QVariant property(const char *name) const const
const QChar at(qsizetype position) const const
QString toLower() const const
QByteArray toUtf8() const const
QueuedConnection
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:56 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.