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 beginExecute(
const QUrl &source);
49 void scheduleExecutionEnd();
50 void minimumWidthChanged();
51 void minimumHeightChanged();
52 void maximumWidthChanged();
53 void maximumHeightChanged();
54 void preferredWidthChanged();
55 void preferredHeightChanged();
60 std::unique_ptr<QQmlComponent> component;
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::beginExecute(
const QUrl &source)
99 qWarning(LOG_PLASMAQUICK) <<
"File name empty!";
102 component = std::make_unique<QQmlComponent>(m_engine.get());
115 qWarning(LOG_PLASMAQUICK) <<
"No module or type specified";
119 component = std::make_unique<QQmlComponent>(m_engine.get());
129void SharedQmlEnginePrivate::endExecute()
134 executionEndTimer->
start(0);
136 scheduleExecutionEnd();
140void SharedQmlEnginePrivate::scheduleExecutionEnd()
143 q->completeInitialization();
146 q->completeInitialization();
153 , d(new SharedQmlEnginePrivate(this))
156 d->rootContext->setParent(
this);
159 d->rootContext->setContextObject(d->context);
164 , d(new SharedQmlEnginePrivate(this))
166 d->rootContext =
new AppletContext(
engine().get(), applet,
this);
169 d->rootContext->setContextObject(d->context);
172SharedQmlEngine::~SharedQmlEngine()
175 delete d->rootObject;
181 d->context->setTranslationDomain(translationDomain);
184QString SharedQmlEngine::translationDomain()
const
186 return d->context->translationDomain();
191 d->beginExecute(source);
196 d->beginExecute(module, type);
199QUrl SharedQmlEngine::source()
const
202 return d->component->url();
224 return d->rootObject;
229 return d->component.get();
234 return d->rootContext;
252 d->executionEndTimer->stop();
255 qWarning(LOG_PLASMAQUICK) <<
"No component for" << source();
260 d->errorPrint(d->component.get());
264 for (
auto it = initialProperties.constBegin(); it != initialProperties.constEnd(); ++it) {
265 d->rootObject->setProperty(it.key().toUtf8().data(), it.value());
268 d->component->completeCreate();
284 for (
auto it = initialProperties.constBegin(); it != initialProperties.constEnd(); ++it) {
285 object->setProperty(it.key().toUtf8().data(), it.value());
289 if (!component->
isError() &&
object) {
297 object->setParent(rootObject());
304 d->errorPrint(component);
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...
bool isInitializationDelayed() const
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...
bool isEmpty() const const
ObjectOwnership objectOwnership(QObject *object)
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)
void setInterval(int msec)
void setSingleShot(bool singleShot)
bool isEmpty() const const
QVariant fromValue(T &&value)