Libplasma

sharedqmlengine.h
1/*
2 SPDX-FileCopyrightText: 2013 Marco Martin <mart@kde.org>
3 SPDX-FileCopyrightText:
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef PLASMA_SHAREDQMLENGINE_H
9#define PLASMA_SHAREDQMLENGINE_H
10
11#include <plasmaquick/plasmaquick_export.h>
12
13#include <QObject>
14#include <QQmlComponent>
15#include <QQmlContext>
16
17#include <memory>
18
19class QQmlComponent;
20class QQmlEngine;
22
23namespace Plasma
24{
25class Applet;
26}
27
28namespace PlasmaQuick
29{
30class SharedQmlEnginePrivate;
31
32/**
33 * @short An object that instantiates an entire QML context, with its own declarative engine
34 *
35 * PlasmaQuick::SharedQmlEngine provides a class to conveniently use QML based
36 * declarative user interfaces.
37 * A SharedQmlEngine corresponds to one QML file (which can include others).
38 * It will a shared QQmlEngine with a single root object, described in the QML file.
39 *
40 * @since 6.0
41 */
42class PLASMAQUICK_EXPORT SharedQmlEngine : public QObject
43{
44 Q_OBJECT
45
46 Q_PROPERTY(QUrl source READ source WRITE setSource)
47 Q_PROPERTY(QString translationDomain READ translationDomain WRITE setTranslationDomain)
48 Q_PROPERTY(bool initializationDelayed READ isInitializationDelayed WRITE setInitializationDelayed)
49 Q_PROPERTY(QObject *rootObject READ rootObject)
50 Q_PROPERTY(QQmlComponent::Status status READ status NOTIFY statusChanged)
51
52public:
53 /**
54 * Construct a new PlasmaQuick::SharedQmlEngine
55 *
56 * @param parent The QObject parent for this object.
57 */
58 explicit SharedQmlEngine(QObject *parent = nullptr);
59 explicit SharedQmlEngine(Plasma::Applet *applet, QObject *parent = nullptr);
60
61 ~SharedQmlEngine() override;
62
63 /**
64 * Call this method before calling setupBindings to install a translation domain for all
65 * i18n global functions. If a translation domain is set all i18n calls delegate to the
66 * matching i18nd calls with the provided translation domain.
67 *
68 * The translationDomain affects all i18n calls including those from imports. Because of
69 * that modules intended to be used as imports should prefer the i18nd variants and set
70 * the translation domain explicitly in each call.
71 *
72 * This method is only required if your declarative usage is inside a library. If it's
73 * in an application there is no need to set the translation domain as the application's
74 * domain can be used.
75 *
76 * @param translationDomain The translation domain to be used for i18n calls.
77 */
78 void setTranslationDomain(const QString &translationDomain);
79
80 /**
81 * @return the translation domain for the i18n calls done in this QML engine
82 */
83 QString translationDomain() const;
84
85 /**
86 * Sets the path of the QML file to parse and execute
87 *
88 * @param path the absolute path of a QML file
89 */
90 void setSource(const QUrl &source);
91
92 /**
93 * Sets the QML source to execute from a type in a module.
94 *
95 * @param module The module to load the type from.
96 * @param type The type to load from the module.
97 */
98 void setSourceFromModule(QAnyStringView module, QAnyStringView type);
99
100 /**
101 * @return the absolute path of the current QML file
102 */
103 QUrl source() const;
104
105 /**
106 * Sets whether the execution of the QML file has to be delayed later in the event loop. It has to be called before setQmlPath().
107 * In this case it will be possible to assign new objects in the main engine context
108 * before the main component gets initialized.
109 * In that case it will be possible to access it immediately from the QML code.
110 * The initialization will either be completed automatically asynchronously
111 * or explicitly by calling completeInitialization()
112 *
113 * @param delay if true the initialization of the QML file will be delayed
114 * at the end of the event loop
115 */
116 void setInitializationDelayed(const bool delay);
117
118 /**
119 * @return true if the initialization of the QML file will be delayed
120 * at the end of the event loop
121 */
122 bool isInitializationDelayed() const;
123
124 /**
125 * @return the declarative engine that runs the qml file assigned to this widget.
126 */
127 std::shared_ptr<QQmlEngine> engine();
128
129 /**
130 * @return the root object of the declarative object tree
131 */
132 QObject *rootObject() const;
133
134 /**
135 * @return the main QQmlComponent of the engine
136 */
137 QQmlComponent *mainComponent() const;
138
139 /**
140 * The components's creation context.
141 */
142 QQmlContext *rootContext() const;
143
144 /**
145 * The component's current status.
146 */
148
149 /**
150 * Creates and returns an object based on the provided url to a Qml file
151 * with the same QQmlEngine and the same root context as the main object,
152 * that will be the parent of the newly created object
153 * @param source url where the QML file is located
154 * @param context The QQmlContext in which we will create the object,
155 * if 0 it will use the engine's root context
156 * @param initialProperties optional properties that will be set on
157 * the object when created (and before Component.onCompleted
158 * gets emitted
159 */
160 QObject *createObjectFromSource(const QUrl &source, QQmlContext *context = nullptr, const QVariantHash &initialProperties = QVariantHash());
161
162 /**
163 * Creates and returns an object based on the provided QQmlComponent
164 * with the same QQmlEngine and the same root context as the admin object,
165 * that will be the parent of the newly created object
166 * @param component the component we want to instantiate
167 * @param context The QQmlContext in which we will create the object,
168 * if 0 it will use the engine's root context
169 * @param initialProperties optional properties that will be set on
170 * the object when created (and before Component.onCompleted
171 * gets emitted
172 */
173 QObject *createObjectFromComponent(QQmlComponent *component, QQmlContext *context = nullptr, const QVariantHash &initialProperties = QVariantHash());
174
175public Q_SLOTS:
176 /**
177 * Finishes the process of initialization.
178 * If isInitializationDelayed() is false, calling this will have no effect.
179 * @param initialProperties optional properties that will be set on
180 * the object when created (and before Component.onCompleted
181 * gets emitted
182 */
183 void completeInitialization(const QVariantHash &initialProperties = QVariantHash());
184
185Q_SIGNALS:
186 /**
187 * Emitted when the parsing and execution of the QML file is terminated
188 */
189 void finished();
190
191 void statusChanged(QQmlComponent::Status);
192
193private:
194 const std::unique_ptr<SharedQmlEnginePrivate> d;
195
196 Q_PRIVATE_SLOT(d, void scheduleExecutionEnd())
197};
198
199}
200
201#endif // multiple inclusion guard
An object that instantiates an entire QML context, with its own declarative engine.
void finished()
Emitted when the parsing and execution of the QML file is terminated.
The base Applet class.
Definition applet.h:64
Q_SCRIPTABLE CaptureState status()
The EdgeEventForwarder class This class forwards edge events to be replayed within the given margin T...
Definition action.h:20
Namespace for everything in libplasma.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:34:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.