8#include "sharedqmlengine.h"
9#include "appletcontext_p.h"
11#include <KLocalizedContext>
15#include <QQmlNetworkAccessManagerFactory>
19#include <Plasma/Applet>
26class SharedQmlEnginePrivate
29 SharedQmlEnginePrivate(SharedQmlEngine *parent)
35 executionEndTimer =
new QTimer(q);
39 scheduleExecutionEnd();
43 ~SharedQmlEnginePrivate() =
default;
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();
65 std::shared_ptr<QQmlEngine> m_engine;
68 static std::shared_ptr<QQmlEngine> engine()
70 if (
auto locked = s_engine.lock()) {
73 auto createdEngine = std::make_shared<QQmlEngine>();
74 s_engine = createdEngine;
78 static std::weak_ptr<QQmlEngine> s_engine;
81std::weak_ptr<QQmlEngine> SharedQmlEnginePrivate::s_engine = {};
83void SharedQmlEnginePrivate::errorPrint(
QQmlComponent *component)
85 QString errorStr = QStringLiteral(
"Error loading QML file.\n");
93 qWarning(LOG_PLASMAQUICK) << component->
url().toString() <<
'\n' << errorStr;
96void SharedQmlEnginePrivate::execute(
const QUrl &source)
99 qWarning(LOG_PLASMAQUICK) <<
"File name empty!";
111 executionEndTimer->
start(0);
113 scheduleExecutionEnd();
117void SharedQmlEnginePrivate::scheduleExecutionEnd()
120 q->completeInitialization();
123 q->completeInitialization();
130 , d(new SharedQmlEnginePrivate(this))
133 d->rootContext->setParent(
this);
136 d->rootContext->setContextObject(d->context);
141 , d(new SharedQmlEnginePrivate(this))
143 d->rootContext =
new AppletContext(
engine().get(), applet,
this);
146 d->rootContext->setContextObject(d->context);
149SharedQmlEngine::~SharedQmlEngine()
153 delete d->rootObject;
159 d->context->setTranslationDomain(translationDomain);
162QString SharedQmlEngine::translationDomain()
const
164 return d->context->translationDomain();
173QUrl SharedQmlEngine::source()
const
195 return d->rootObject;
205 return d->rootContext;
223 d->executionEndTimer->stop();
226 qWarning(LOG_PLASMAQUICK) <<
"No component for" << source();
231 d->errorPrint(d->component);
235 for (
auto it = initialProperties.constBegin(); it != initialProperties.constEnd(); ++it) {
236 d->rootObject->setProperty(it.key().toUtf8().data(), it.value());
239 d->component->completeCreate();
255 for (
auto it = initialProperties.constBegin(); it != initialProperties.constEnd(); ++it) {
256 object->
setProperty(it.key().toUtf8().data(), it.value());
260 if (!component->
isError() && object) {
265 if (qobject_cast<QQuickItem *>(rootObject())) {
268 object->setParent(rootObject());
275 d->errorPrint(component);
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...
bool isInitializationDelayed() const
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...
ObjectOwnership objectOwnership(QObject *object)
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)
void setInterval(int msec)
void setSingleShot(bool singleShot)
bool isEmpty() const const
QVariant fromValue(T &&value)