KCMUtils

kquickconfigmodule.h
1/*
2 SPDX-FileCopyrightText: 1999 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
3 SPDX-FileCopyrightText: 2001 Michael Goffioul <kdeprint@swing.be>
4 SPDX-FileCopyrightText: 2004 Frans Englich <frans.englich@telia.com>
5 SPDX-FileCopyrightText: 2009 Dario Freddi <drf@kde.org>
6 SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
7 SPDX-FileCopyrightText: 2023 Alexander Lohnau <alexander.lohnau@gmx.de>
8
9 SPDX-License-Identifier: LGPL-2.0-or-later
10*/
11
12#ifndef KQUICKCONFIGMODULE_H
13#define KQUICKCONFIGMODULE_H
14
15#include "kcmutilsquick_export.h"
16
17#include <QObject>
18#include <QQmlComponent>
19#include <QStringList>
20#include <QVariant>
21
22#include <KPluginFactory>
23#include <KPluginMetaData>
24
25#include <memory>
26#include <qqmlintegration.h>
27
28#include "kabstractconfigmodule.h"
29#include "kquickconfigmoduleloader.h"
30
31class QQuickItem;
32class QQmlEngine;
33class KQuickConfigModulePrivate;
34
35/**
36 * @class KQuickConfigModule kquickconfigmodule.h KQuickConfigModule
37 *
38 * The base class for QtQuick configuration modules.
39 * Configuration modules are realized as plugins that are dynamically loaded.
40 *
41 * All the necessary glue logic and the GUI bells and whistles
42 * are provided by the control center and must not concern
43 * the module author.
44 *
45 * To write a config module, you have to create a C++ plugin
46 * and an accompanying QML user interface.
47 *
48 * To allow KCMUtils to load your ConfigModule subclass, you must create a KPluginFactory implementation.
49 *
50 * \code
51 * #include <KPluginFactory>
52 *
53 * K_PLUGIN_CLASS_WITH_JSON(MyConfigModule, "yourmetadata.json")
54 * \endcode
55 *
56 * The constructor of the ConfigModule then looks like this:
57 * \code
58 * YourConfigModule::YourConfigModule(QObject *parent, const KPluginMetaData &metaData)
59 * : KQuickConfigModule(parent, metaData)
60 * {
61 * }
62 * \endcode
63 *
64 * The QML part must be in the KPackage format, installed under share/kpackage/kcms.
65 * @see KPackage::Package
66 *
67 * The package must have the same name as the plugin filename, to be installed
68 * by CMake with the command:
69 * \code
70 * kpackage_install_package(packagedir kcm_yourconfigmodule kcms)
71 * \endcode
72 * The "packagedir" is the subdirectory in the source tree where the package sources are
73 * located, and "kcm_yourconfigmodule" is id of the plugin.
74 * Finally "kcms" is the literal string "kcms", so that the package is
75 * installed as a configuration module (and not some other kind of package).
76 *
77 * The QML part can access all the properties of ConfigModule (together with the properties
78 * defined in its subclass) by accessing to the global object "kcm", or with the
79 * import of "org.kde.kcmutils" the ConfigModule attached property.
80 *
81 * \code
82 * import QtQuick
83 * import QtQuick.Controls as QQC2
84 * import org.kde.kcmutils as KCMUtils
85 * import org.kde.kirigami as Kirigami
86 *
87 * Item {
88 * // implicit size will be used as initial size when loaded in kcmshell6
89 * implicitWidth: Kirigami.Units.gridUnit * 30
90 * implicitHeight: Kirigami.Units.gridUnit * 30
91 *
92 * KCMUtils.ConfigModule.buttons: KCMUtils.ConfigModule.Help | KCMUtils.ConfigModule.Apply
93 *
94 * QQC2.Label {
95 * // The following two bindings are equivalent:
96 * text: kcm.needsSave
97 * enabled: KCMUtils.ConfigModule.needsSave
98 * }
99 * }
100 * \endcode
101 *
102 * See https://develop.kde.org/docs/features/configuration/kcm/ for more detailed documentation.
103 * @since 6.0
104 */
105class KCMUTILSQUICK_EXPORT KQuickConfigModule : public KAbstractConfigModule
106{
108
109 Q_PROPERTY(QQuickItem *mainUi READ mainUi CONSTANT)
110 Q_PROPERTY(int columnWidth READ columnWidth WRITE setColumnWidth NOTIFY columnWidthChanged)
111 Q_PROPERTY(int depth READ depth NOTIFY depthChanged)
112 Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
113
114 QML_NAMED_ELEMENT(ConfigModule)
115 QML_ATTACHED(KQuickConfigModule)
116
117public:
118 /**
119 * Destroys the module.
120 */
121 ~KQuickConfigModule() override;
122
123 /**
124 * @return the qml engine that built the main config UI
125 */
126 std::shared_ptr<QQmlEngine> engine() const;
127
128 /**
129 * The error string in case the mainUi failed to load.
130 */
131 QString errorString() const;
132
133 // QML property accessors
134
135 /**
136 * Load the main UI for this configuration module. It's a QQuickItem coming from
137 * the QML package named the same as the KAboutData's component name for
138 * this config module.
139 *
140 * Normally, ui/main.qml will be loaded from the qrc baked into the plugin. However,
141 * if the PLASMA_PLATFORM environmental variable is set, the module will try to load
142 * a platform-specific QML file as its mainUi starting point.
143 *
144 * For example:
145 *
146 * environment has set
147 * `export PLASMA_PLATFORM=phone:handset`
148 *
149 * The module will try to find main_phone.qml, then main_handset.qml, then main.qml.
150 * The first file that is found will be used as mainUi. If none is found and main.qml
151 * doesn't exist, the module is broken and an error will be displayed.
152 *
153 * @return The main UI for this configuration module. It's a QQuickItem coming from
154 */
156
157 /*
158 * @return a subpage at a given depth
159 * @note This does not include the mainUi. i.e a depth of 2 is a mainUi and one subPage
160 * at index 0
161 */
162 QQuickItem *subPage(int index) const;
163
164 /**
165 * returns the width the kcm wants in column mode.
166 * If a columnWidth is valid ( > 0 ) and less than the systemsettings' view width,
167 * more than one will be visible at once, and the first page will be a sidebar to the last page pushed.
168 * As default, this is -1 which will make the shell always show only one page at a time.
169 */
170 int columnWidth() const;
171
172 /**
173 * Sets the column width we want.
174 */
175 void setColumnWidth(int width);
176
177 /**
178 * @returns how many pages this kcm has.
179 * It is guaranteed to be at least 1 (the main ui) plus how many times a new page has been pushed without pop
180 */
181 int depth() const;
182
183 /**
184 * Sets the current page index this kcm should display
185 */
186 void setCurrentIndex(int index);
187
188 /**
189 * @returns the index of the page this kcm should display
190 */
191 int currentIndex() const;
192
193 static KQuickConfigModule *qmlAttachedProperties(QObject *object);
194
195public Q_SLOTS:
196 /**
197 * Push a new sub page in the KCM hierarchy: pages will be seen as a Kirigami PageRow
198 */
199 void push(const QString &fileName, const QVariantMap &initialProperties = QVariantMap());
200
201 /**
202 *
203 */
204 void push(QQuickItem *item);
205
206 /**
207 * pop the last page of the KCM hierarchy, the page is destroyed
208 */
209 void pop();
210
211 /**
212 * remove and return the last page of the KCM hierarchy:
213 * the popped page won't be deleted, it's the caller's responsibility to manage the lifetime of the returned item
214 * @returns the last page if any, nullptr otherwise
215 */
217
219
220 // QML NOTIFY signaling
221
222 /**
223 * Emitted when a new sub page is pushed
224 */
226
227 /**
228 * Emitted when a sub page is popped
229 */
230 // RFC: page argument?
232
233 /**
234 * Emitted when the wanted column width of the kcm changes
235 */
236 void columnWidthChanged(int width);
237
238 /**
239 * Emitted when the current page changed
240 */
241 void currentIndexChanged(int index);
242
243 /**
244 * Emitted when the number of pages changed
245 */
246 void depthChanged(int index);
247
248 /**
249 * Emitted when the main Ui has loaded successfully and `mainUi()` is available
250 */
252
253protected:
254 /**
255 * Base class for all QtQuick config modules.
256 * Use KQuickConfigModuleLoader to instantiate this class
257 *
258 * @note do not emit changed signals here, since they are not yet connected to any slot.
259 */
261
262private:
263 void setInternalEngine(const std::shared_ptr<QQmlEngine> &engine);
265 KQuickConfigModuleLoader::loadModule(const KPluginMetaData &metaData, QObject *parent, const QVariantList &args, const std::shared_ptr<QQmlEngine> &engine);
266 const std::unique_ptr<KQuickConfigModulePrivate> d;
267};
268
269#endif // KQUICKCONFIGMODULE_H
KPluginMetaData metaData() const
Returns the metaData that was used when instantiating the plugin.
void mainUiReady()
Emitted when the main Ui has loaded successfully and mainUi() is available.
void setColumnWidth(int width)
Sets the column width we want.
void columnWidthChanged(int width)
Emitted when the wanted column width of the kcm changes.
KQuickConfigModule(QObject *parent, const KPluginMetaData &metaData)
Base class for all QtQuick config modules.
void depthChanged(int index)
Emitted when the number of pages changed.
void pop()
pop the last page of the KCM hierarchy, the page is destroyed
void setCurrentIndex(int index)
Sets the current page index this kcm should display.
void currentIndexChanged(int index)
Emitted when the current page changed.
void pagePushed(QQuickItem *page)
Emitted when a new sub page is pushed.
QString errorString() const
The error string in case the mainUi failed to load.
std::shared_ptr< QQmlEngine > engine() const
void pageRemoved()
Emitted when a sub page is popped.
void push(const QString &fileName, const QVariantMap &initialProperties=QVariantMap())
Push a new sub page in the KCM hierarchy: pages will be seen as a Kirigami PageRow.
QQuickItem * mainUi()
Load the main UI for this configuration module.
QQuickItem * takeLast()
remove and return the last page of the KCM hierarchy: the popped page won't be deleted,...
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:51:31 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.