summaryrefslogtreecommitdiffstats
path: root/src/quick3d/quick3d/qqmlaspectengine.cpp
blob: e8b164751e47a1689b4193fe38e6b8efdd01e2aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qqmlaspectengine_p.h"

#include <Qt3DCore/qentity.h>
#include <QtCore/QDebug>
#include <QtQml/QQmlComponent>
#include <QtQml/QQmlContext>

QT_BEGIN_NAMESPACE

namespace Qt3DCore {
namespace Quick {

/*!
    \namespace Qt3DCore::Quick
    \inmodule Qt3DCore
    \ingroup qt3d-namespaces

    \brief Contains classes used for implementing QML functionality into Qt3D
    applications.
*/

QQmlAspectEnginePrivate::QQmlAspectEnginePrivate()
    : QObjectPrivate()
    , m_qmlEngine(new QQmlEngine())
    , m_aspectEngine(new QAspectEngine())
    , m_component(nullptr)
{
}

/*!
 * \class Qt3DCore::Quick::QQmlAspectEngine
 * \inheaderfile Qt3DQuick/QQmlAspectEngine
 * \inmodule Qt3DCore
 *
 * \brief The QQmlAspectEngine provides an environment for the QAspectEngine and
 * a method for instantiating QML components.
 */

/*!
 * \enum Qt3DCore::Quick::QQmlAspectEngine::Status
 *
 * The status of the engine.
 *
 * \value Null
 * \value Ready
 * \value Loading
 * \value Error
 */

/*!
 * \fn void Qt3DCore::Quick::QQmlAspectEngine::statusChanged(Status status)
 *
 * This signal is emitted with \a status when the status of the engine changes.
 */

/*!
 * \fn void Qt3DCore::Quick::QQmlAspectEngine::sceneCreated(QObject* rootObject)
 *
 * This signal is emitted with \a rootObject when the scene has been instantiated. This provides a
 * chance to manipulate the scene before passing it over to the aspect engine. Useful for
 * convenience window classes to set up cameras and surfaces on the framegraph and event sources
 * for the input aspect etc.
 */

/*!
 * Constructs a new QQmlAspectEngine with \a parent.
 */
QQmlAspectEngine::QQmlAspectEngine(QObject *parent)
    : QObject(*new QQmlAspectEnginePrivate, parent)
{
}

/*! \internal */
QQmlAspectEngine::~QQmlAspectEngine()
{
}

/*!
 * \return the status.
 */
QQmlAspectEngine::Status QQmlAspectEngine::status() const
{
    Q_D(const QQmlAspectEngine);

    if (!d->m_component)
        return Null;

    return Status(d->m_component->status());
}

/*!
 * Sets \a source as a source for the QML component to be created.
 */
void QQmlAspectEngine::setSource(const QUrl &source)
{
    Q_D(QQmlAspectEngine);

    if (d->m_component) {
        d->m_aspectEngine->setRootEntity(QEntityPtr());
        d->m_component = nullptr;
    }

    if (!source.isEmpty()) {
        d->m_component = new QQmlComponent(d->m_qmlEngine.data(), source);
        if (!d->m_component->isLoading()) {
            d->_q_continueExecute();
        } else {
            QObject::connect(d->m_component, SIGNAL(statusChanged(QQmlComponent::Status)),
                             this, SLOT(_q_continueExecute()));
        }
    }
}

/*!
 * \return the engine.
 */
QQmlEngine *QQmlAspectEngine::qmlEngine() const
{
    Q_D(const QQmlAspectEngine);
    return d->m_qmlEngine.data();
}

/*!
 * \return the aspectEngine.
 */
QAspectEngine *QQmlAspectEngine::aspectEngine() const
{
    Q_D(const QQmlAspectEngine);
    return d->m_aspectEngine.data();
}

void QQmlAspectEnginePrivate::_q_continueExecute()
{
    Q_Q(QQmlAspectEngine);

    QObject::disconnect(m_component, SIGNAL(statusChanged(QQmlComponent::Status)),
                        q, SLOT(_q_continueExecute()));

    if (m_component->isError()) {
        const auto errors = m_component->errors();
        for (const QQmlError &error : errors) {
            QMessageLogger(error.url().toString().toLatin1().constData(), error.line(), 0).warning()
                << error;
        }
        emit q->statusChanged(q->status());
        return;
    }

    QObject* obj = m_component->create();

    if (m_component->isError()) {
        const auto errors = m_component->errors();
        for (const QQmlError &error : errors) {
            QMessageLogger(error.url().toString().toLatin1().constData(), error.line(), 0).warning()
                << error;
        }
        emit q->statusChanged(q->status());
        return;
    }

    // Let users know we have loaded the QML file, and the scene has been instantiated.
    // This gives a chance to manipulate the scene before passing it over to the
    // aspect engine. Useful for convenience window classes to set up cameras and surfaces
    // on the framegraph and event sources for the input aspect etc.
    emit q->sceneCreated(obj);
    m_aspectEngine->setRootEntity(QEntityPtr(qobject_cast<QEntity *>(obj)));
    emit q->statusChanged(q->status());
}

} // namespace Quick
} // namespace Qt3DCore

QT_END_NAMESPACE

#include "moc_qqmlaspectengine.cpp"