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!";
103 component = std::make_unique<QQmlComponent>(m_engine.get());
116 qWarning(LOG_PLASMAQUICK) <<
"No module or type specified";
120 component = std::make_unique<QQmlComponent>(m_engine.get());
130void SharedQmlEnginePrivate::endExecute()
135 executionEndTimer->
start(0);
137 scheduleExecutionEnd();
141void SharedQmlEnginePrivate::scheduleExecutionEnd()
144 q->completeInitialization();
147 q->completeInitialization();
154 , d(new SharedQmlEnginePrivate(this))
157 d->rootContext->setParent(
this);
160 d->rootContext->setContextObject(d->context);
165 , d(new SharedQmlEnginePrivate(this))
167 d->rootContext =
new AppletContext(
engine().get(), applet,
this);
170 d->rootContext->setContextObject(d->context);
173SharedQmlEngine::~SharedQmlEngine()
176 delete d->rootObject;
182 d->context->setTranslationDomain(translationDomain);
185QString SharedQmlEngine::translationDomain()
const
187 return d->context->translationDomain();
192 d->beginExecute(source);
197 d->beginExecute(module, type);
200QUrl SharedQmlEngine::source()
const
203 return d->component->url();
225 return d->rootObject;
230 return d->component.get();
235 return d->rootContext;
253 d->executionEndTimer->stop();
256 qWarning(LOG_PLASMAQUICK) <<
"No component for" << source();
261 d->errorPrint(d->component.get());
265 for (
auto it = initialProperties.constBegin(); it != initialProperties.constEnd(); ++it) {
266 d->rootObject->setProperty(it.key().toUtf8().data(), it.value());
269 d->component->completeCreate();
285 for (
auto it = initialProperties.constBegin(); it != initialProperties.constEnd(); ++it) {
286 object->setProperty(it.key().toUtf8().data(), it.value());
290 if (!component->
isError() &&
object) {
298 object->setParent(rootObject());
305 d->errorPrint(component);
312#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)