summaryrefslogtreecommitdiffstats
path: root/src/quick3d/quick3d/items/quick3dentityloader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/quick3d/quick3d/items/quick3dentityloader.cpp')
-rw-r--r--src/quick3d/quick3d/items/quick3dentityloader.cpp97
1 files changed, 87 insertions, 10 deletions
diff --git a/src/quick3d/quick3d/items/quick3dentityloader.cpp b/src/quick3d/quick3d/items/quick3dentityloader.cpp
index 9c82db7ad..8a9b19024 100644
--- a/src/quick3d/quick3d/items/quick3dentityloader.cpp
+++ b/src/quick3d/quick3d/items/quick3dentityloader.cpp
@@ -39,9 +39,9 @@
#include "quick3dentityloader_p_p.h"
-#include <QQmlContext>
-#include <QQmlEngine>
-#include <QQmlIncubator>
+#include <QtQml/QQmlContext>
+#include <QtQml/QQmlEngine>
+#include <QtQml/QQmlIncubator>
#include <QtQml/private/qqmlengine_p.h>
@@ -50,6 +50,32 @@ QT_BEGIN_NAMESPACE
namespace Qt3DCore {
namespace Quick {
+namespace {
+struct Quick3DQmlOwner
+{
+ Quick3DQmlOwner(QQmlEngine *e, QObject *o)
+ : engine(e)
+ , object(o)
+ {}
+
+ QQmlEngine *engine;
+ QObject *object;
+
+ QQmlContext *context() const
+ {
+ return engine->contextForObject(object);
+ }
+};
+
+Quick3DQmlOwner _q_findQmlOwner(QObject *object)
+{
+ auto o = object;
+ while (!qmlEngine(o) && o->parent())
+ o = o->parent();
+ return Quick3DQmlOwner(qmlEngine(o), o);
+}
+}
+
class Quick3DEntityLoaderIncubator : public QQmlIncubator
{
public:
@@ -71,13 +97,20 @@ protected:
Q_ASSERT(priv->m_entity != nullptr);
priv->m_entity->setParent(m_loader);
emit m_loader->entityChanged();
+ priv->setStatus(Quick3DEntityLoader::Ready);
+ break;
+ }
+
+ case Loading: {
+ priv->setStatus(Quick3DEntityLoader::Loading);
break;
}
case Error: {
- QQmlEnginePrivate::warning(qmlEngine(m_loader), errors());
+ QQmlEnginePrivate::warning(_q_findQmlOwner(m_loader).engine, errors());
priv->clear();
emit m_loader->entityChanged();
+ priv->setStatus(Quick3DEntityLoader::Error);
break;
}
@@ -95,13 +128,21 @@ private:
\inqmlmodule Qt3D.Core
\inherits Entity
\since 5.5
- \brief Provides the facility to load entities from qml source
+ \brief Provides a way to dynamically load an Entity subtree
An EntityLoader provides the facitily to load predefined set of entities
from qml source file. EntityLoader itself is an entity and the loaded entity
tree is set as a child of the loader. The loaded entity tree root can be
accessed with EntityLoader::entity property.
+
+ \badcode
+ EntityLoader {
+ id: loader
+ source: "./SphereEntity.qml"
+ }
+ \endcode
*/
+
Quick3DEntityLoader::Quick3DEntityLoader(QNode *parent)
: QEntity(*new Quick3DEntityLoaderPrivate, parent)
{
@@ -117,6 +158,10 @@ Quick3DEntityLoader::~Quick3DEntityLoader()
\qmlproperty QtQml::QtObject EntityLoader::entity
Holds the loaded entity tree root.
\readonly
+
+ This property allows access to the content of the loader. It references
+ either a valid Entity object if the status property equals
+ EntityLoader.Ready, it is equal to null otherwise.
*/
QObject *Quick3DEntityLoader::entity() const
{
@@ -147,12 +192,30 @@ void Quick3DEntityLoader::setSource(const QUrl &url)
d->loadFromSource();
}
+/*!
+ \qmlproperty Status Qt3DCore::EntityLoader::status
+
+ Holds the status of the entity loader.
+ \list
+ \li EntityLoader.Null
+ \li EntityLoader.Loading
+ \li EntityLoader.Ready
+ \li EntityLoader.Error
+ \endlist
+ */
+Quick3DEntityLoader::Status Quick3DEntityLoader::status() const
+{
+ Q_D(const Quick3DEntityLoader);
+ return d->m_status;
+}
+
Quick3DEntityLoaderPrivate::Quick3DEntityLoaderPrivate()
: QEntityPrivate(),
m_incubator(nullptr),
m_context(nullptr),
m_component(nullptr),
- m_entity(nullptr)
+ m_entity(nullptr),
+ m_status(Quick3DEntityLoader::Null)
{
}
@@ -201,7 +264,8 @@ void Quick3DEntityLoaderPrivate::loadComponent(const QUrl &source)
Q_ASSERT(m_component == nullptr);
Q_ASSERT(m_context == nullptr);
- m_component = new QQmlComponent(qmlEngine(q), q);
+ auto owner = _q_findQmlOwner(q);
+ m_component = new QQmlComponent(owner.engine, owner.object);
QObject::connect(m_component, SIGNAL(statusChanged(QQmlComponent::Status)),
q, SLOT(_q_componentStatusChanged(QQmlComponent::Status)));
m_component->loadUrl(source, QQmlComponent::Asynchronous);
@@ -216,8 +280,10 @@ void Quick3DEntityLoaderPrivate::_q_componentStatusChanged(QQmlComponent::Status
Q_ASSERT(m_context == nullptr);
Q_ASSERT(m_incubator == nullptr);
+ auto owner = _q_findQmlOwner(q);
+
if (!m_component->errors().isEmpty()) {
- QQmlEnginePrivate::warning(qmlEngine(q), m_component->errors());
+ QQmlEnginePrivate::warning(owner.engine, m_component->errors());
clear();
emit q->entityChanged();
return;
@@ -227,13 +293,24 @@ void Quick3DEntityLoaderPrivate::_q_componentStatusChanged(QQmlComponent::Status
if (status != QQmlComponent::Ready)
return;
- m_context = new QQmlContext(qmlContext(q));
- m_context->setContextObject(q);
+ m_context = new QQmlContext(owner.context());
+ m_context->setContextObject(owner.object);
m_incubator = new Quick3DEntityLoaderIncubator(q);
m_component->create(*m_incubator, m_context);
}
+void Quick3DEntityLoaderPrivate::setStatus(Quick3DEntityLoader::Status status)
+{
+ Q_Q(Quick3DEntityLoader);
+ if (status != m_status) {
+ m_status = status;
+ const bool blocked = q->blockNotifications(true);
+ emit q->statusChanged(m_status);
+ q->blockNotifications(blocked);
+ }
+}
+
} // namespace Quick
} // namespace Qt3DCore