diff options
author | Kevin Ottens <kevin.ottens@kdab.com> | 2014-12-17 12:35:40 +0100 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2014-12-17 17:44:23 +0100 |
commit | 8c8fe2cb0d1b0d26090b7c7689b2f28ffce6a642 (patch) | |
tree | cae9bb74e670c1aff971d97461e91849687bb81b /src/quick3d | |
parent | 88ac3a98e6969ee70f5e3aa62835c69ff9d1d547 (diff) |
Add a new EntityLoader element
It also come with an example where we swap between two entities loaded
from QML files.
Change-Id: I09d5c5852ad5ea1e85d2acfffd51413de437ce22
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/quick3d')
-rw-r--r-- | src/quick3d/imports/core/qt3dquick3dcoreplugin.cpp | 2 | ||||
-rw-r--r-- | src/quick3d/quick3d/items/items.pri | 3 | ||||
-rw-r--r-- | src/quick3d/quick3d/items/quick3dentityloader.cpp | 214 | ||||
-rw-r--r-- | src/quick3d/quick3d/items/quick3dentityloader.h | 95 | ||||
-rw-r--r-- | src/quick3d/quick3d/items/quick3dentityloader_p.h | 90 | ||||
-rw-r--r-- | src/quick3d/quick3d/quick3d.pro | 2 |
6 files changed, 405 insertions, 1 deletions
diff --git a/src/quick3d/imports/core/qt3dquick3dcoreplugin.cpp b/src/quick3d/imports/core/qt3dquick3dcoreplugin.cpp index 2da408812..8db3d09f3 100644 --- a/src/quick3d/imports/core/qt3dquick3dcoreplugin.cpp +++ b/src/quick3d/imports/core/qt3dquick3dcoreplugin.cpp @@ -49,6 +49,7 @@ #include <Qt3DCore/qtranslatetransform.h> #include <Qt3DCore/qrotatetransform.h> #include <Qt3DQuick/quick3dentity.h> +#include <Qt3DQuick/quick3dentityloader.h> #include <Qt3DQuick/quick3dtransform.h> #include <Qt3DQuick/quick3dconfiguration.h> #include <private/qt3dquick_global_p.h> @@ -64,6 +65,7 @@ void Qt3DQuick3DCorePlugin::registerTypes(const char *uri) qmlRegisterType<Qt3D::Quick::Quick3DConfiguration>(uri, 2, 0, "Configuration"); qmlRegisterExtendedType<Qt3D::QEntity, Qt3D::Quick::Quick3DEntity>(uri, 2, 0, "Entity"); + qmlRegisterType<Qt3D::Quick::Quick3DEntityLoader>(uri, 2, 0, "EntityLoader"); qmlRegisterExtendedType<Qt3D::QTransform, Qt3D::Quick::Quick3DTransform>(uri, 2, 0, "Transform"); // Ideally we want to make Node an uncreatable type // We would need qmlRegisterUncreatableExtendedType for that diff --git a/src/quick3d/quick3d/items/items.pri b/src/quick3d/quick3d/items/items.pri index 199532964..edf00bd5e 100644 --- a/src/quick3d/quick3d/items/items.pri +++ b/src/quick3d/quick3d/items/items.pri @@ -1,12 +1,15 @@ HEADERS += \ $$PWD/quick3dnode.h \ $$PWD/quick3dentity.h \ + $$PWD/quick3dentityloader.h \ + $$PWD/quick3dentityloader_p.h \ $$PWD/quick3dtransform.h \ $$PWD/quick3dconfiguration.h SOURCES += \ $$PWD/quick3dnode.cpp \ $$PWD/quick3dentity.cpp \ + $$PWD/quick3dentityloader.cpp \ $$PWD/quick3dtransform.cpp \ $$PWD/quick3dconfiguration.cpp diff --git a/src/quick3d/quick3d/items/quick3dentityloader.cpp b/src/quick3d/quick3d/items/quick3dentityloader.cpp new file mode 100644 index 000000000..aa6ab05e3 --- /dev/null +++ b/src/quick3d/quick3d/items/quick3dentityloader.cpp @@ -0,0 +1,214 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "quick3dentityloader.h" +#include "quick3dentityloader_p.h" + +#include <QQmlContext> +#include <QQmlEngine> +#include <QQmlIncubator> + +#include <QtQml/private/qqmlengine_p.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +namespace Quick { + +class Quick3DEntityLoaderIncubator : public QQmlIncubator +{ +public: + Quick3DEntityLoaderIncubator(Quick3DEntityLoaderPrivate *loader) + : QQmlIncubator(Asynchronous), + m_loader(loader) + { + } + +protected: + void setInitialState(QObject *object) Q_DECL_OVERRIDE + { + Q_ASSERT(m_loader->m_entity == Q_NULLPTR); + m_loader->m_entity = qobject_cast<QEntity*>(object); + Q_ASSERT(m_loader->m_entity != Q_NULLPTR); + m_loader->m_entity->setParent(m_loader->q_ptr); + + Quick3DEntityLoader *loader = static_cast<Quick3DEntityLoader*>(m_loader->q_ptr); + emit loader->entityChanged(); + } + +private: + Quick3DEntityLoaderPrivate *m_loader; +}; + +Quick3DEntityLoader::Quick3DEntityLoader(QNode *parent) + : QEntity(*new Quick3DEntityLoaderPrivate(this), parent) +{ +} + +QObject *Quick3DEntityLoader::entity() const +{ + Q_D(const Quick3DEntityLoader); + return d->m_entity; +} + +QUrl Quick3DEntityLoader::source() const +{ + Q_D(const Quick3DEntityLoader); + return d->m_source; +} + +void Quick3DEntityLoader::setSource(const QUrl &url) +{ + Q_D(Quick3DEntityLoader); + + if (url == d->m_source) + return; + + d->clear(); + d->m_source = url; + emit sourceChanged(); + d->loadFromSource(); +} + +void Quick3DEntityLoader::copy(const QNode *ref) +{ + QNode::copy(ref); + const Quick3DEntityLoader *loader = static_cast<const Quick3DEntityLoader*>(ref); + d_func()->m_source = loader->d_func()->m_source; + d_func()->m_entity = static_cast<QEntity*>(QNode::clone(loader->d_func()->m_entity)); + d_func()->m_entity->setParent(this); +} + +Quick3DEntityLoaderPrivate::Quick3DEntityLoaderPrivate(Quick3DEntityLoader *qq) + : QEntityPrivate(qq), + m_incubator(Q_NULLPTR), + m_context(Q_NULLPTR), + m_component(Q_NULLPTR), + m_entity(Q_NULLPTR) +{ +} + +void Quick3DEntityLoaderPrivate::clear() +{ + if (m_incubator) { + m_incubator->clear(); + delete m_incubator; + m_incubator = Q_NULLPTR; + } + + if (m_entity) { + m_entity->setParent(Q_NULLPTR); + delete m_entity; + m_entity = Q_NULLPTR; + } + + if (m_component) { + delete m_component; + m_component = Q_NULLPTR; + } + + if (m_context) { + delete m_context; + m_context = Q_NULLPTR; + } +} + +void Quick3DEntityLoaderPrivate::loadFromSource() +{ + Q_Q(Quick3DEntityLoader); + + if (m_source.isEmpty()) { + emit q->entityChanged(); + return; + } + + loadComponent(m_source); +} + +void Quick3DEntityLoaderPrivate::loadComponent(const QUrl &source) +{ + Q_Q(Quick3DEntityLoader); + + Q_ASSERT(m_entity == Q_NULLPTR); + Q_ASSERT(m_component == Q_NULLPTR); + Q_ASSERT(m_context == Q_NULLPTR); + + m_component = new QQmlComponent(qmlEngine(q), q); + QObject::connect(m_component, SIGNAL(statusChanged(QQmlComponent::Status)), + q, SLOT(_q_componentStatusChanged(QQmlComponent::Status))); + m_component->loadUrl(source, QQmlComponent::Asynchronous); +} + +void Quick3DEntityLoaderPrivate::_q_componentStatusChanged(QQmlComponent::Status status) +{ + Q_Q(Quick3DEntityLoader); + + Q_ASSERT(m_entity == Q_NULLPTR); + Q_ASSERT(m_component != Q_NULLPTR); + Q_ASSERT(m_context == Q_NULLPTR); + Q_ASSERT(m_incubator == Q_NULLPTR); + + if (!m_component->errors().isEmpty()) { + QQmlEnginePrivate::warning(qmlEngine(q), m_component->errors()); + clear(); + emit q->entityChanged(); + return; + } + + // Still loading + if (status != QQmlComponent::Ready) + return; + + m_context = new QQmlContext(qmlContext(q)); + m_context->setContextObject(q); + + m_incubator = new Quick3DEntityLoaderIncubator(this); + m_component->create(*m_incubator, m_context); +} + +} // Quick + +} // Qt3D + +QT_END_NAMESPACE + +#include "moc_quick3dentityloader.cpp" diff --git a/src/quick3d/quick3d/items/quick3dentityloader.h b/src/quick3d/quick3d/items/quick3dentityloader.h new file mode 100644 index 000000000..12da21efd --- /dev/null +++ b/src/quick3d/quick3d/items/quick3dentityloader.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_QUICK_QUICK3DENTITYLOADER_H +#define QT3D_QUICK_QUICK3DENTITYLOADER_H + +#include <QObject> +#include <QUrl> + +#include <Qt3DCore/QEntity> + +#include <Qt3DQuick/qt3dquick_global.h> + + +QT_BEGIN_NAMESPACE + +namespace Qt3D { + +class QEntity; + +namespace Quick { + +class Quick3DEntityLoaderPrivate; + +class QT3DQUICKSHARED_EXPORT Quick3DEntityLoader : public QEntity +{ + Q_OBJECT + Q_PROPERTY(QObject *entity READ entity NOTIFY entityChanged) + Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) +public: + explicit Quick3DEntityLoader(QNode *parent = 0); + + QObject *entity() const; + + QUrl source() const; + void setSource(const QUrl &url); + +Q_SIGNALS: + void entityChanged(); + void sourceChanged(); + +protected: + void copy(const QNode *ref) Q_DECL_OVERRIDE; + +private: + Q_DECLARE_PRIVATE(Quick3DEntityLoader) + Q_PRIVATE_SLOT(d_func(), void _q_componentStatusChanged(QQmlComponent::Status)) + QT3D_CLONEABLE(Quick3DEntityLoader) +}; + +} // Quick; + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QUICK3DENTITYLOADER_H diff --git a/src/quick3d/quick3d/items/quick3dentityloader_p.h b/src/quick3d/quick3d/items/quick3dentityloader_p.h new file mode 100644 index 000000000..0ed970dc4 --- /dev/null +++ b/src/quick3d/quick3d/items/quick3dentityloader_p.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3D_QUICK_QUICK3DENTITYLOADER_P_H +#define QT3D_QUICK_QUICK3DENTITYLOADER_P_H + +#include "quick3dentityloader.h" + +#include <QQmlComponent> + +#include <Qt3DCore/private/qentity_p.h> + +QT_BEGIN_NAMESPACE + +class QQmlIncubator; +class QQmlContext; + +namespace Qt3D { + +class QEntity; + +namespace Quick { + +class Quick3DEntityLoaderIncubator; + +class Quick3DEntityLoaderPrivate : public QEntityPrivate +{ +public: + Quick3DEntityLoaderPrivate(Quick3DEntityLoader *qq); + + Q_DECLARE_PUBLIC(Quick3DEntityLoader) + + void clear(); + void loadFromSource(); + void loadComponent(const QUrl &source); + + void _q_componentStatusChanged(QQmlComponent::Status status); + + QUrl m_source; + Quick3DEntityLoaderIncubator *m_incubator; + QQmlContext *m_context; + QQmlComponent *m_component; + QEntity *m_entity; +}; + +} // Quick + +} // Qt3D + +QT_END_NAMESPACE + +#endif // QT3D_QUICK_QUICK3DENTITYLOADER_P_H diff --git a/src/quick3d/quick3d/quick3d.pro b/src/quick3d/quick3d/quick3d.pro index 78442693a..14647858b 100644 --- a/src/quick3d/quick3d/quick3d.pro +++ b/src/quick3d/quick3d/quick3d.pro @@ -1,6 +1,6 @@ TARGET = Qt3DQuick -QT += core-private gui-private qml qml-private quick quick-private 3dcore +QT += core-private gui-private qml qml-private quick quick-private 3dcore 3dcore-private DEFINES += QT3DQUICK_LIBRARY |