Libplasma

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 beginExecute(const QUrl &source);
47 void beginExecute(QAnyStringView module, QAnyStringView type);
48 void endExecute();
49 void scheduleExecutionEnd();
50 void minimumWidthChanged();
51 void minimumHeightChanged();
52 void maximumWidthChanged();
53 void maximumHeightChanged();
54 void preferredWidthChanged();
55 void preferredHeightChanged();
56
57 SharedQmlEngine *q;
58
59 QPointer<QObject> rootObject;
60 std::unique_ptr<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::beginExecute(const QUrl &source)
97{
98 if (source.isEmpty()) {
99 qWarning(LOG_PLASMAQUICK) << "File name empty!";
100 }
101
102 component = std::make_unique<QQmlComponent>(m_engine.get());
103 // Important! Some parts of Plasma are extremely sensitive to status changed
104 // signal being emit in exactly the same way QQmlComponent does it. So this
105 // connection needs to happen before any loading of the component happens.
106 QObject::connect(component.get(), &QQmlComponent::statusChanged, q, &SharedQmlEngine::statusChanged, Qt::QueuedConnection);
107 component->loadUrl(source);
108
109 endExecute();
110}
111
112void SharedQmlEnginePrivate::beginExecute(QAnyStringView module, QAnyStringView type)
113{
114 if (module.isEmpty() || type.isEmpty()) {
115 qWarning(LOG_PLASMAQUICK) << "No module or type specified";
116 return;
117 }
118
119 component = std::make_unique<QQmlComponent>(m_engine.get());
120 // Important! Some parts of Plasma are extremely sensitive to status changed
121 // signal being emit in exactly the same way QQmlComponent does it. So this
122 // connection needs to happen before any loading of the component happens.
123 QObject::connect(component.get(), &QQmlComponent::statusChanged, q, &SharedQmlEngine::statusChanged, Qt::QueuedConnection);
124 component->loadFromModule(module, type);
125
126 endExecute();
127}
128
129void SharedQmlEnginePrivate::endExecute()
130{
131 rootObject = component->beginCreate(rootContext);
132
133 if (delay) {
134 executionEndTimer->start(0);
135 } else {
136 scheduleExecutionEnd();
137 }
138}
139
140void SharedQmlEnginePrivate::scheduleExecutionEnd()
141{
142 if (component->isReady() || component->isError()) {
143 q->completeInitialization();
144 } else {
145 QObject::connect(component.get(), &QQmlComponent::statusChanged, q, [this]() {
146 q->completeInitialization();
147 });
148 }
149}
150
152 : QObject(parent)
153 , d(new SharedQmlEnginePrivate(this))
154{
155 d->rootContext = new QQmlContext(engine().get());
156 d->rootContext->setParent(this); // Delete the context when deleting the shared engine
157
158 d->context = new KLocalizedContext(d->rootContext);
159 d->rootContext->setContextObject(d->context);
160}
161
163 : QObject(parent)
164 , d(new SharedQmlEnginePrivate(this))
165{
166 d->rootContext = new AppletContext(engine().get(), applet, this);
167
168 d->context = new KLocalizedContext(d->rootContext);
169 d->rootContext->setContextObject(d->context);
170}
171
172SharedQmlEngine::~SharedQmlEngine()
173{
175 delete d->rootObject;
176 }
177}
178
179void SharedQmlEngine::setTranslationDomain(const QString &translationDomain)
180{
181 d->context->setTranslationDomain(translationDomain);
182}
183
184QString SharedQmlEngine::translationDomain() const
185{
186 return d->context->translationDomain();
187}
188
190{
191 d->beginExecute(source);
192}
193
195{
196 d->beginExecute(module, type);
197}
198
199QUrl SharedQmlEngine::source() const
200{
201 if (d->component) {
202 return d->component->url();
203 }
204 return QUrl{};
205}
206
208{
209 d->delay = delay;
210}
211
213{
214 return d->delay;
215}
216
217std::shared_ptr<QQmlEngine> SharedQmlEngine::engine()
218{
219 return d->m_engine;
220}
221
222QObject *SharedQmlEngine::rootObject() const
223{
224 return d->rootObject;
225}
226
228{
229 return d->component.get();
230}
231
233{
234 return d->rootContext;
235}
236
237QQmlComponent::Status SharedQmlEngine::status() const
238{
239 if (!d->m_engine) {
241 }
242
243 if (!d->component) {
244 return QQmlComponent::Null;
245 }
246
247 return QQmlComponent::Status(d->component->status());
248}
249
250void SharedQmlEngine::completeInitialization(const QVariantHash &initialProperties)
251{
252 d->executionEndTimer->stop();
253
254 if (!d->component) {
255 qWarning(LOG_PLASMAQUICK) << "No component for" << source();
256 return;
257 }
258
259 if (d->component->status() != QQmlComponent::Ready || d->component->isError()) {
260 d->errorPrint(d->component.get());
261 return;
262 }
263
264 for (auto it = initialProperties.constBegin(); it != initialProperties.constEnd(); ++it) {
265 d->rootObject->setProperty(it.key().toUtf8().data(), it.value());
266 }
267
268 d->component->completeCreate();
270}
271
272QObject *SharedQmlEngine::createObjectFromSource(const QUrl &source, QQmlContext *context, const QVariantHash &initialProperties)
273{
274 QQmlComponent *component = new QQmlComponent(d->m_engine.get(), this);
275 component->loadUrl(source);
276
277 return createObjectFromComponent(component, context, initialProperties);
278}
279
280QObject *SharedQmlEngine::createObjectFromComponent(QQmlComponent *component, QQmlContext *context, const QVariantHash &initialProperties)
281{
282 QObject *object = component->beginCreate(context ? context : d->rootContext);
283
284 for (auto it = initialProperties.constBegin(); it != initialProperties.constEnd(); ++it) {
285 object->setProperty(it.key().toUtf8().data(), it.value());
286 }
287 component->completeCreate();
288
289 if (!component->isError() && object) {
290 // memory management
291 component->setParent(object);
292 // reparent to root object if wasn't specified otherwise by initialProperties
293 if (!initialProperties.contains(QLatin1String("parent"))) {
294 if (qobject_cast<QQuickItem *>(rootObject())) {
295 object->setProperty("parent", QVariant::fromValue(rootObject()));
296 } else {
297 object->setParent(rootObject());
298 }
299 }
300
301 return object;
302
303 } else {
304 d->errorPrint(component);
305 delete object;
306 return nullptr;
307 }
308}
309}
310
311#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.
void setSourceFromModule(QAnyStringView module, QAnyStringView type)
Sets the QML source to execute from a type in a module.
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
Type type(const QSqlDatabase &db)
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
bool isEmpty() const const
ObjectOwnership objectOwnership(QObject *object)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
void setParent(QObject *parent)
virtual QObject * beginCreate(QQmlContext *context)
virtual void completeCreate()
QList< QQmlError > errors() const const
bool isError() const const
bool isReady() const const
void loadFromModule(QAnyStringView uri, QAnyStringView typeName, QQmlComponent::CompilationMode mode)
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 Dec 13 2024 11:54:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.