Plasma-framework

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 * @return the absolute path of the current QML file
94 */
95 QUrl source() const;
96
97 /**
98 * Sets whether the execution of the QML file has to be delayed later in the event loop. It has to be called before setQmlPath().
99 * In this case it will be possible to assign new objects in the main engine context
100 * before the main component gets initialized.
101 * In that case it will be possible to access it immediately from the QML code.
102 * The initialization will either be completed automatically asynchronously
103 * or explicitly by calling completeInitialization()
104 *
105 * @param delay if true the initialization of the QML file will be delayed
106 * at the end of the event loop
107 */
108 void setInitializationDelayed(const bool delay);
109
110 /**
111 * @return true if the initialization of the QML file will be delayed
112 * at the end of the event loop
113 */
114 bool isInitializationDelayed() const;
115
116 /**
117 * @return the declarative engine that runs the qml file assigned to this widget.
118 */
119 std::shared_ptr<QQmlEngine> engine();
120
121 /**
122 * @return the root object of the declarative object tree
123 */
124 QObject *rootObject() const;
125
126 /**
127 * @return the main QQmlComponent of the engine
128 */
129 QQmlComponent *mainComponent() const;
130
131 /**
132 * The components's creation context.
133 */
134 QQmlContext *rootContext() const;
135
136 /**
137 * The component's current status.
138 */
140
141 /**
142 * Creates and returns an object based on the provided url to a Qml file
143 * with the same QQmlEngine and the same root context as the main object,
144 * that will be the parent of the newly created object
145 * @param source url where the QML file is located
146 * @param context The QQmlContext in which we will create the object,
147 * if 0 it will use the engine's root context
148 * @param initialProperties optional properties that will be set on
149 * the object when created (and before Component.onCompleted
150 * gets emitted
151 */
152 QObject *createObjectFromSource(const QUrl &source, QQmlContext *context = nullptr, const QVariantHash &initialProperties = QVariantHash());
153
154 /**
155 * Creates and returns an object based on the provided QQmlComponent
156 * with the same QQmlEngine and the same root context as the admin object,
157 * that will be the parent of the newly created object
158 * @param component the component we want to instantiate
159 * @param context The QQmlContext in which we will create the object,
160 * if 0 it will use the engine's root context
161 * @param initialProperties optional properties that will be set on
162 * the object when created (and before Component.onCompleted
163 * gets emitted
164 */
165 QObject *createObjectFromComponent(QQmlComponent *component, QQmlContext *context = nullptr, const QVariantHash &initialProperties = QVariantHash());
166
167public Q_SLOTS:
168 /**
169 * Finishes the process of initialization.
170 * If isInitializationDelayed() is false, calling this will have no effect.
171 * @param initialProperties optional properties that will be set on
172 * the object when created (and before Component.onCompleted
173 * gets emitted
174 */
175 void completeInitialization(const QVariantHash &initialProperties = QVariantHash());
176
177Q_SIGNALS:
178 /**
179 * Emitted when the parsing and execution of the QML file is terminated
180 */
181 void finished();
182
183 void statusChanged(QQmlComponent::Status);
184
185private:
186 const std::unique_ptr<SharedQmlEnginePrivate> d;
187
188 Q_PRIVATE_SLOT(d, void scheduleExecutionEnd())
189};
190
191}
192
193#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 Fri May 17 2024 11:54:11 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.