1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qqmlaspectengine_p.h" |
41 | |
42 | #include <Qt3DCore/qentity.h> |
43 | #include <QtCore/QDebug> |
44 | #include <QtQml/QQmlComponent> |
45 | #include <QtQml/QQmlContext> |
46 | |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | namespace Qt3DCore { |
50 | namespace Quick { |
51 | |
52 | /*! |
53 | \namespace Qt3DCore::Quick |
54 | \inmodule Qt3DCore |
55 | \ingroup qt3d-namespaces |
56 | |
57 | \brief Contains classes used for implementing QML functionality into Qt3D |
58 | applications. |
59 | */ |
60 | |
61 | QQmlAspectEnginePrivate::QQmlAspectEnginePrivate() |
62 | : QObjectPrivate() |
63 | , m_qmlEngine(new QQmlEngine()) |
64 | , m_aspectEngine(new QAspectEngine()) |
65 | , m_component(nullptr) |
66 | { |
67 | } |
68 | |
69 | /*! |
70 | * \class Qt3DCore::Quick::QQmlAspectEngine |
71 | * \inheaderfile Qt3DQuick/QQmlAspectEngine |
72 | * \inmodule Qt3DCore |
73 | * |
74 | * \brief The QQmlAspectEngine provides an environment for the QAspectEngine and |
75 | * a method for instantiating QML components. |
76 | */ |
77 | |
78 | /*! |
79 | * \enum Qt3DCore::Quick::QQmlAspectEngine::Status |
80 | * |
81 | * The status of the engine. |
82 | * |
83 | * \value Null |
84 | * \value Ready |
85 | * \value Loading |
86 | * \value Error |
87 | */ |
88 | |
89 | /*! |
90 | * \fn void Qt3DCore::Quick::QQmlAspectEngine::statusChanged(Status status) |
91 | * |
92 | * This signal is emitted with \a status when the status of the engine changes. |
93 | */ |
94 | |
95 | /*! |
96 | * \fn void Qt3DCore::Quick::QQmlAspectEngine::sceneCreated(QObject* rootObject) |
97 | * |
98 | * This signal is emitted with \a rootObject when the scene has been instantiated. This provides a |
99 | * chance to manipulate the scene before passing it over to the aspect engine. Useful for |
100 | * convenience window classes to set up cameras and surfaces on the framegraph and event sources |
101 | * for the input aspect etc. |
102 | */ |
103 | |
104 | /*! |
105 | * Constructs a new QQmlAspectEngine with \a parent. |
106 | */ |
107 | QQmlAspectEngine::QQmlAspectEngine(QObject *parent) |
108 | : QObject(*new QQmlAspectEnginePrivate, parent) |
109 | { |
110 | } |
111 | |
112 | /*! \internal */ |
113 | QQmlAspectEngine::~QQmlAspectEngine() |
114 | { |
115 | } |
116 | |
117 | /*! |
118 | * \return the status. |
119 | */ |
120 | QQmlAspectEngine::Status QQmlAspectEngine::status() const |
121 | { |
122 | Q_D(const QQmlAspectEngine); |
123 | |
124 | if (!d->m_component) |
125 | return Null; |
126 | |
127 | return Status(d->m_component->status()); |
128 | } |
129 | |
130 | /*! |
131 | * Sets \a source as a source for the QML component to be created. |
132 | */ |
133 | void QQmlAspectEngine::setSource(const QUrl &source) |
134 | { |
135 | Q_D(QQmlAspectEngine); |
136 | |
137 | if (d->m_component) { |
138 | d->m_aspectEngine->setRootEntity(QEntityPtr()); |
139 | d->m_component = nullptr; |
140 | } |
141 | |
142 | if (!source.isEmpty()) { |
143 | d->m_component = new QQmlComponent(d->m_qmlEngine.data(), source); |
144 | if (!d->m_component->isLoading()) { |
145 | d->_q_continueExecute(); |
146 | } else { |
147 | QObject::connect(sender: d->m_component, SIGNAL(statusChanged(QQmlComponent::Status)), |
148 | receiver: this, SLOT(_q_continueExecute())); |
149 | } |
150 | } |
151 | } |
152 | |
153 | /*! |
154 | * \return the engine. |
155 | */ |
156 | QQmlEngine *QQmlAspectEngine::qmlEngine() const |
157 | { |
158 | Q_D(const QQmlAspectEngine); |
159 | return d->m_qmlEngine.data(); |
160 | } |
161 | |
162 | /*! |
163 | * \return the aspectEngine. |
164 | */ |
165 | QAspectEngine *QQmlAspectEngine::aspectEngine() const |
166 | { |
167 | Q_D(const QQmlAspectEngine); |
168 | return d->m_aspectEngine.data(); |
169 | } |
170 | |
171 | void QQmlAspectEnginePrivate::_q_continueExecute() |
172 | { |
173 | Q_Q(QQmlAspectEngine); |
174 | |
175 | QObject::disconnect(sender: m_component, SIGNAL(statusChanged(QQmlComponent::Status)), |
176 | receiver: q, SLOT(_q_continueExecute())); |
177 | |
178 | if (m_component->isError()) { |
179 | const auto errors = m_component->errors(); |
180 | for (const QQmlError &error : errors) { |
181 | QMessageLogger(error.url().toString().toLatin1().constData(), error.line(), 0).warning() |
182 | << error; |
183 | } |
184 | emit q->statusChanged(q->status()); |
185 | return; |
186 | } |
187 | |
188 | QObject* obj = m_component->create(); |
189 | |
190 | if (m_component->isError()) { |
191 | const auto errors = m_component->errors(); |
192 | for (const QQmlError &error : errors) { |
193 | QMessageLogger(error.url().toString().toLatin1().constData(), error.line(), 0).warning() |
194 | << error; |
195 | } |
196 | emit q->statusChanged(q->status()); |
197 | return; |
198 | } |
199 | |
200 | // Let users know we have loaded the QML file, and the scene has been instantiated. |
201 | // This gives a chance to manipulate the scene before passing it over to the |
202 | // aspect engine. Useful for convenience window classes to set up cameras and surfaces |
203 | // on the framegraph and event sources for the input aspect etc. |
204 | emit q->sceneCreated(rootObject: obj); |
205 | m_aspectEngine->setRootEntity(QEntityPtr(qobject_cast<QEntity *>(object: obj))); |
206 | emit q->statusChanged(q->status()); |
207 | } |
208 | |
209 | } // namespace Quick |
210 | } // namespace Qt3DCore |
211 | |
212 | QT_END_NAMESPACE |
213 | |
214 | #include "moc_qqmlaspectengine.cpp" |
215 | |