summaryrefslogtreecommitdiffstats
path: root/src/quick3d/quick3d/items/quick3dnodeinstantiator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/quick3d/quick3d/items/quick3dnodeinstantiator.cpp')
-rw-r--r--src/quick3d/quick3d/items/quick3dnodeinstantiator.cpp78
1 files changed, 51 insertions, 27 deletions
diff --git a/src/quick3d/quick3d/items/quick3dnodeinstantiator.cpp b/src/quick3d/quick3d/items/quick3dnodeinstantiator.cpp
index cc50af786..44c75a4df 100644
--- a/src/quick3d/quick3d/items/quick3dnodeinstantiator.cpp
+++ b/src/quick3d/quick3d/items/quick3dnodeinstantiator.cpp
@@ -6,8 +6,7 @@
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
-** $QT_BEGIN_LICENSE:COMM$
-**
+** $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
@@ -16,25 +15,26 @@
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
-** $QT_END_LICENSE$
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
**
+** $QT_END_LICENSE$
**
****************************************************************************/
@@ -73,6 +73,7 @@ public:
#endif
void _q_createdItem(int, QObject *);
void _q_modelUpdated(const QQmlChangeSet &, bool);
+ QObject *modelObject(int index, bool async);
bool m_componentComplete:1;
bool m_effectiveReset:1;
@@ -81,6 +82,7 @@ public:
#if QT_CONFIG(qml_delegate_model)
bool m_ownModel:1;
#endif
+ int m_requestedIndex;
QVariant m_model;
QQmlInstanceModel *m_instanceModel;
QQmlComponent *m_delegate;
@@ -99,6 +101,7 @@ Quick3DNodeInstantiatorPrivate::Quick3DNodeInstantiatorPrivate()
#if QT_CONFIG(qml_delegate_model)
, m_ownModel(false)
#endif
+ , m_requestedIndex(-1)
, m_model(QVariant(1))
, m_instanceModel(0)
, m_delegate(0)
@@ -129,6 +132,14 @@ void Quick3DNodeInstantiatorPrivate::clear()
emit q->objectChanged();
}
+QObject *Quick3DNodeInstantiatorPrivate::modelObject(int index, bool async)
+{
+ m_requestedIndex = index;
+ QObject *o = m_instanceModel->object(index, async ? QQmlIncubator::Asynchronous : QQmlIncubator::AsynchronousIfNested);
+ m_requestedIndex = -1;
+ return o;
+}
+
void Quick3DNodeInstantiatorPrivate::regenerate()
{
Q_Q(Quick3DNodeInstantiator);
@@ -146,8 +157,7 @@ void Quick3DNodeInstantiatorPrivate::regenerate()
}
for (int i = 0; i < m_instanceModel->count(); i++) {
- QObject *object = m_instanceModel->object(i, m_async ?
- QQmlIncubator::Asynchronous : QQmlIncubator::AsynchronousIfNested);
+ QObject *object = modelObject(i, m_async);
// If the item was already created we won't get a createdItem
if (object)
_q_createdItem(i, object);
@@ -161,8 +171,19 @@ void Quick3DNodeInstantiatorPrivate::_q_createdItem(int idx, QObject *item)
Q_Q(Quick3DNodeInstantiator);
if (m_objects.contains(item)) //Case when it was created synchronously in regenerate
return;
+ if (m_requestedIndex != idx) // Asynchronous creation, reference the object |
+ (void)m_instanceModel->object(idx);
static_cast<QNode *>(item)->setParent(q->parentNode());
- m_objects.insert(idx, item);
+ if (m_objects.size() < idx + 1) {
+ int modelCount = m_instanceModel->count();
+ if (m_objects.capacity() < modelCount)
+ m_objects.reserve(modelCount);
+ m_objects.resize(idx + 1);
+ }
+ if (QObject *o = m_objects.at(idx))
+ m_instanceModel->release(o);
+ m_objects.replace(idx, item);
+
if (m_objects.count() == 1)
emit q->objectChanged();
emit q->objectAdded(idx, item);
@@ -213,11 +234,14 @@ void Quick3DNodeInstantiatorPrivate::_q_modelUpdated(const QQmlChangeSet &change
QVector<QPointer<QObject> > movedObjects = moved.value(insert.moveId);
m_objects = m_objects.mid(0, index) + movedObjects + m_objects.mid(index);
} else for (int i = 0; i < insert.count; ++i) {
- int modelIndex = index + i;
- QObject *obj = m_instanceModel->object(modelIndex, m_async ?
- QQmlIncubator::Asynchronous : QQmlIncubator::AsynchronousIfNested);
- if (obj)
- _q_createdItem(modelIndex, obj);
+ if (insert.index <= m_objects.count())
+ m_objects.insert(insert.index, insert.count, nullptr);
+ for (int i = 0; i < insert.count; ++i) {
+ int modelIndex = index + i;
+ QObject *obj = modelObject(modelIndex, m_async);
+ if (obj)
+ _q_createdItem(modelIndex, obj);
+ }
}
difference += insert.count;
}