8#include "sharedqmlengine_p.h"
10#if KI18N_VERSION >= QT_VERSION_CHECK(6, 8, 0)
11#include <KLocalizedQmlContext>
13#include <KLocalizedContext>
18#include <QQmlIncubator>
19#include <QQmlNetworkAccessManagerFactory>
24#include "kcmutils_debug.h"
26using namespace Qt::StringLiterals;
28class SharedQmlEnginePrivate
31 SharedQmlEnginePrivate(
const std::shared_ptr<QQmlEngine> &engine, SharedQmlEngine *parent)
40 scheduleExecutionEnd();
44 ~SharedQmlEnginePrivate()
50 void execute(
const QUrl &source);
51 void scheduleExecutionEnd();
52 void minimumWidthChanged();
53 void minimumHeightChanged();
54 void maximumWidthChanged();
55 void maximumHeightChanged();
56 void preferredWidthChanged();
57 void preferredHeightChanged();
58 void checkInitializationCompleted();
67#if KI18N_VERSION >= QT_VERSION_CHECK(6, 8, 0)
74 std::shared_ptr<QQmlEngine> m_engine;
80 if (component && component->
isError()) {
81 errors = component->
errors();
82 }
else if (incubator && incubator->
isError()) {
83 errors = incubator->
errors();
88 qCWarning(KCMUTILS_LOG).noquote() <<
"Error loading QML file" << component->
url().toString();
89 for (
const auto &error : errors) {
91 qCWarning(KCMUTILS_LOG).noquote().nospace() << indent <<
error;
95void SharedQmlEnginePrivate::execute(
const QUrl &source)
100 delete incubator.
object();
102 m_engine->addImportPath(QStringLiteral(
"qrc:/"));
106 executionEndTimer.
start(0);
108 scheduleExecutionEnd();
112void SharedQmlEnginePrivate::scheduleExecutionEnd()
115 q->completeInitialization();
118 q->completeInitialization();
123SharedQmlEngine::SharedQmlEngine(
const std::shared_ptr<QQmlEngine> &engine,
QObject *parent)
125 , d(new SharedQmlEnginePrivate(engine, this))
128 d->rootContext->setParent(
this);
130#if KI18N_VERSION < QT_VERSION_CHECK(6, 8, 0)
135 d->rootContext->setContextObject(d->context);
138SharedQmlEngine::~SharedQmlEngine() =
default;
140void SharedQmlEngine::setTranslationDomain(
const QString &translationDomain)
142 d->context->setTranslationDomain(translationDomain);
145QString SharedQmlEngine::translationDomain()
const
147 return d->context->translationDomain();
150void SharedQmlEngine::setSource(
const QUrl &source)
156QUrl SharedQmlEngine::source()
const
161void SharedQmlEngine::setInitializationDelayed(
const bool delay)
166bool SharedQmlEngine::isInitializationDelayed()
const
171std::shared_ptr<QQmlEngine> SharedQmlEngine::engine()
176QObject *SharedQmlEngine::rootObject()
const
178 if (d->incubator.isLoading()) {
179 qCWarning(KCMUTILS_LOG) <<
"Trying to use rootObject before initialization is completed, whilst using setInitializationDelayed. Forcing completion";
180 d->incubator.forceCompletion();
182 return d->incubator.object();
192 return d->rootContext;
195bool SharedQmlEngine::isError()
const
197 return !d->m_engine || !d->component || d->component->isError() || d->incubator.isError();
203 for (
const auto &e : errors) {
209QString SharedQmlEngine::errorString()
const
211 if (d->component && d->component->isError()) {
212 return d->component->errorString();
213 }
else if (d->incubator.isError()) {
214 return qmlErrorsToString(d->incubator.errors());
220void SharedQmlEnginePrivate::checkInitializationCompleted()
224 checkInitializationCompleted();
229 if (!incubator.
object()) {
230 errorPrint(component, &incubator);
233 Q_EMIT q->finished();
236void SharedQmlEngine::completeInitialization(
const QVariantMap &initialProperties)
238 d->executionEndTimer.stop();
239 if (d->incubator.object()) {
244 qCWarning(KCMUTILS_LOG) <<
"No component for" << source();
248 if (!d->component->isReady()) {
249 d->errorPrint(d->component);
253 d->incubator.setInitialProperties(initialProperties);
254 d->component->create(d->incubator, d->rootContext);
257 d->checkInitializationCompleted();
259 d->incubator.forceCompletion();
261 if (!d->incubator.object()) {
262 d->errorPrint(d->component, &d->incubator);
268QObject *SharedQmlEngine::createObjectFromSource(
const QUrl &source,
QQmlContext *context,
const QVariantMap &initialProperties)
273 return createObjectFromComponent(component, context, initialProperties);
280 if (!component->
isError() &&
object) {
282 const auto root = rootObject();
283 object->setParent(root);
287 if (!initialProperties.contains(
QLatin1String(
"parent")) && root && root->isQuickItemType()) {
292 d->errorPrint(component);
298#include "moc_sharedqmlengine_p.cpp"
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setParent(QObject *parent)
QObject * createWithInitialProperties(const QVariantMap &initialProperties, QQmlContext *context)
QList< QQmlError > errors() const const
bool isError() const const
bool isReady() const const
void loadUrl(const QUrl &url)
void statusChanged(QQmlComponent::Status status)
QList< QQmlError > errors() const const
bool isError() const const
bool isReady() const const
QObject * object() const const
QString number(double n, char format, int precision)
void setInterval(int msec)
void setSingleShot(bool singleShot)
bool isEmpty() const const
QVariant fromValue(T &&value)