Plasma-framework

sharedqmlengine.cpp
1/*
2 SPDX-FileCopyrightText: 2013 Marco Martin <mart@kde.org>
3 SPDX-FileCopyrightText: 2023 Alexander Lohnau <alexander.lohnau@gmx.de>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "sharedqmlengine.h"
9#include "appletcontext_p.h"
10
11#include <KLocalizedContext>
12#include <QDebug>
13#include <QQmlContext>
14#include <QQmlEngine>
15#include <QQmlNetworkAccessManagerFactory>
16#include <QQuickItem>
17#include <QTimer>
18
19#include <Plasma/Applet>
20
21#include "debug_p.h"
22
23namespace PlasmaQuick
24{
25
26class SharedQmlEnginePrivate
27{
28public:
29 SharedQmlEnginePrivate(SharedQmlEngine *parent)
30 : q(parent)
31 , component(nullptr)
32 , delay(false)
33 , m_engine(engine())
34 {
35 executionEndTimer = new QTimer(q);
36 executionEndTimer->setInterval(0);
37 executionEndTimer->setSingleShot(true);
38 QObject::connect(executionEndTimer, &QTimer::timeout, q, [this]() {
39 scheduleExecutionEnd();
40 });
41 }
42
43 ~SharedQmlEnginePrivate() = default;
44
45 void errorPrint(QQmlComponent *component);
46 void execute(const QUrl &source);
47 void scheduleExecutionEnd();
48 void minimumWidthChanged();
49 void minimumHeightChanged();
50 void maximumWidthChanged();
51 void maximumHeightChanged();
52 void preferredWidthChanged();
53 void preferredHeightChanged();
54
55 SharedQmlEngine *q;
56
57 QUrl source;
58
59 QPointer<QObject> rootObject;
60 QQmlComponent *component;
61 QTimer *executionEndTimer;
62 KLocalizedContext *context{nullptr};
63 QQmlContext *rootContext;
64 bool delay;
65 std::shared_ptr<QQmlEngine> m_engine;
66
67private:
68 static std::shared_ptr<QQmlEngine> engine()
69 {
70 if (auto locked = s_engine.lock()) {
71 return locked;
72 }
73 auto createdEngine = std::make_shared<QQmlEngine>();
74 s_engine = createdEngine;
75 return createdEngine;
76 }
77
78 static std::weak_ptr<QQmlEngine> s_engine;
79};
80
81std::weak_ptr<QQmlEngine> SharedQmlEnginePrivate::s_engine = {};
82
83void SharedQmlEnginePrivate::errorPrint(QQmlComponent *component)
84{
85 QString errorStr = QStringLiteral("Error loading QML file.\n");
86 if (component->isError()) {
87 const QList<QQmlError> errors = component->errors();
88 for (const QQmlError &error : errors) {
89 errorStr +=
90 (error.line() > 0 ? QString(QString::number(error.line()) + QLatin1String(": ")) : QLatin1String("")) + error.description() + QLatin1Char('\n');
91 }
92 }
93 qWarning(LOG_PLASMAQUICK) << component->url().toString() << '\n' << errorStr;
94}
95
96void SharedQmlEnginePrivate::execute(const QUrl &source)
97{
98 if (source.isEmpty()) {
99 qWarning(LOG_PLASMAQUICK) << "File name empty!";
100 return;
101 }
102
103 delete component;
104 component = new QQmlComponent(m_engine.get(), q);
105 QObject::connect(component, &QQmlComponent::statusChanged, q, &SharedQmlEngine::statusChanged, Qt::QueuedConnection);
106
107 component->loadUrl(source);
108 rootObject = component->beginCreate(rootContext);
109
110 if (delay) {
111 executionEndTimer->start(0);
112 } else {
113 scheduleExecutionEnd();
114 }
115}
116
117void SharedQmlEnginePrivate::scheduleExecutionEnd()
118{
119 if (component->isReady() || component->isError()) {
120 q->completeInitialization();
121 } else {
122 QObject::connect(component, &QQmlComponent::statusChanged, q, [this]() {
123 q->completeInitialization();
124 });
125 }
126}
127
129 : QObject(parent)
130 , d(new SharedQmlEnginePrivate(this))
131{
132 d->rootContext = new QQmlContext(engine().get());
133 d->rootContext->setParent(this); // Delete the context when deleting the shared engine
134
135 d->context = new KLocalizedContext(d->rootContext);
136 d->rootContext->setContextObject(d->context);
137}
138
140 : QObject(parent)
141 , d(new SharedQmlEnginePrivate(this))
142{
143 d->rootContext = new AppletContext(engine().get(), applet, this);
144
145 d->context = new KLocalizedContext(d->rootContext);
146 d->rootContext->setContextObject(d->context);
147}
148
149SharedQmlEngine::~SharedQmlEngine()
150{
151 delete d->component;
153 delete d->rootObject;
154 }
155}
156
157void SharedQmlEngine::setTranslationDomain(const QString &translationDomain)
158{
159 d->context->setTranslationDomain(translationDomain);
160}
161
162QString SharedQmlEngine::translationDomain() const
163{
164 return d->context->translationDomain();
165}
166
168{
169 d->source = source;
170 d->execute(source);
171}
172
173QUrl SharedQmlEngine::source() const
174{
175 return d->source;
176}
177
179{
180 d->delay = delay;
181}
182
184{
185 return d->delay;
186}
187
188std::shared_ptr<QQmlEngine> SharedQmlEngine::engine()
189{
190 return d->m_engine;
191}
192
193QObject *SharedQmlEngine::rootObject() const
194{
195 return d->rootObject;
196}
197
199{
200 return d->component;
201}
202
204{
205 return d->rootContext;
206}
207
208QQmlComponent::Status SharedQmlEngine::status() const
209{
210 if (!d->m_engine) {
212 }
213
214 if (!d->component) {
215 return QQmlComponent::Null;
216 }
217
218 return QQmlComponent::Status(d->component->status());
219}
220
221void SharedQmlEngine::completeInitialization(const QVariantHash &initialProperties)
222{
223 d->executionEndTimer->stop();
224
225 if (!d->component) {
226 qWarning(LOG_PLASMAQUICK) << "No component for" << source();
227 return;
228 }
229
230 if (d->component->status() != QQmlComponent::Ready || d->component->isError()) {
231 d->errorPrint(d->component);
232 return;
233 }
234
235 for (auto it = initialProperties.constBegin(); it != initialProperties.constEnd(); ++it) {
236 d->rootObject->setProperty(it.key().toUtf8().data(), it.value());
237 }
238
239 d->component->completeCreate();
241}
242
243QObject *SharedQmlEngine::createObjectFromSource(const QUrl &source, QQmlContext *context, const QVariantHash &initialProperties)
244{
245 QQmlComponent *component = new QQmlComponent(d->m_engine.get(), this);
246 component->loadUrl(source);
247
248 return createObjectFromComponent(component, context, initialProperties);
249}
250
251QObject *SharedQmlEngine::createObjectFromComponent(QQmlComponent *component, QQmlContext *context, const QVariantHash &initialProperties)
252{
253 QObject *object = component->beginCreate(context ? context : d->rootContext);
254
255 for (auto it = initialProperties.constBegin(); it != initialProperties.constEnd(); ++it) {
256 object->setProperty(it.key().toUtf8().data(), it.value());
257 }
258 component->completeCreate();
259
260 if (!component->isError() && object) {
261 // memory management
262 component->setParent(object);
263 // reparent to root object if wasn't specified otherwise by initialProperties
264 if (!initialProperties.contains(QLatin1String("parent"))) {
265 if (qobject_cast<QQuickItem *>(rootObject())) {
266 object->setProperty("parent", QVariant::fromValue(rootObject()));
267 } else {
268 object->setParent(rootObject());
269 }
270 }
271
272 return object;
273
274 } else {
275 d->errorPrint(component);
276 delete object;
277 return nullptr;
278 }
279}
280}
281
282#include "moc_sharedqmlengine.cpp"
SharedQmlEngine(QObject *parent=nullptr)
Construct a new PlasmaQuick::SharedQmlEngine.
QQmlContext * rootContext() const
The components's creation context.
void setInitializationDelayed(const bool delay)
Sets whether the execution of the QML file has to be delayed later in the event loop.
void setSource(const QUrl &source)
Sets the path of the QML file to parse and execute.
void finished()
Emitted when the parsing and execution of the QML file is terminated.
QQmlComponent * mainComponent() const
void setTranslationDomain(const QString &translationDomain)
Call this method before calling setupBindings to install a translation domain for all i18n global fun...
QObject * createObjectFromSource(const QUrl &source, QQmlContext *context=nullptr, const QVariantHash &initialProperties=QVariantHash())
Creates and returns an object based on the provided url to a Qml file with the same QQmlEngine and th...
void completeInitialization(const QVariantHash &initialProperties=QVariantHash())
Finishes the process of initialization.
std::shared_ptr< QQmlEngine > engine()
QObject * createObjectFromComponent(QQmlComponent *component, QQmlContext *context=nullptr, const QVariantHash &initialProperties=QVariantHash())
Creates and returns an object based on the provided QQmlComponent with the same QQmlEngine and the sa...
The base Applet class.
Definition applet.h:64
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
The EdgeEventForwarder class This class forwards edge events to be replayed within the given margin T...
Definition action.h:20
ObjectOwnership objectOwnership(QObject *object)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setParent(QObject *parent)
bool setProperty(const char *name, QVariant &&value)
virtual QObject * beginCreate(QQmlContext *context)
virtual void completeCreate()
QList< QQmlError > errors() const const
bool isError() const const
bool isReady() const const
void loadUrl(const QUrl &url)
void statusChanged(QQmlComponent::Status status)
QString number(double n, char format, int precision)
QueuedConnection
void setInterval(int msec)
void setSingleShot(bool singleShot)
void start()
void timeout()
bool isEmpty() const const
QVariant fromValue(T &&value)
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.