aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlmodels/qqmlinstantiator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/qmlmodels/qqmlinstantiator.cpp')
-rw-r--r--src/qmlmodels/qqmlinstantiator.cpp101
1 files changed, 34 insertions, 67 deletions
diff --git a/src/qmlmodels/qqmlinstantiator.cpp b/src/qmlmodels/qqmlinstantiator.cpp
index 3a0d746eb6..552d1a6b73 100644
--- a/src/qmlmodels/qqmlinstantiator.cpp
+++ b/src/qmlmodels/qqmlinstantiator.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 Research In Motion.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtQml 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 The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/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 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$
-**
-****************************************************************************/
+// Copyright (C) 2016 Research In Motion.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qqmlinstantiator_p.h"
#include "qqmlinstantiator_p_p.h"
@@ -65,25 +29,25 @@ QQmlInstantiatorPrivate::QQmlInstantiatorPrivate()
{
}
-QQmlInstantiatorPrivate::~QQmlInstantiatorPrivate()
-{
- qDeleteAll(objects);
-}
-
void QQmlInstantiatorPrivate::clear()
{
Q_Q(QQmlInstantiator);
if (!instanceModel)
return;
- if (!objects.count())
+
+ if (objects.isEmpty())
return;
- for (int i=0; i < objects.count(); i++) {
- q->objectRemoved(i, objects[i]);
- instanceModel->release(objects[i]);
+ for (int i=0; i < objects.size(); i++) {
+ QObject *object = objects[i];
+ emit q->objectRemoved(i, object);
+ instanceModel->release(object);
+ if (object && object->parent() == q)
+ object->setParent(nullptr);
}
+
objects.clear();
- q->objectChanged();
+ emit q->objectChanged();
}
QObject *QQmlInstantiatorPrivate::modelObject(int index, bool async)
@@ -128,7 +92,8 @@ void QQmlInstantiatorPrivate::_q_createdItem(int idx, QObject* item)
return;
if (requestedIndex != idx) // Asynchronous creation, reference the object
(void)instanceModel->object(idx);
- item->setParent(q);
+ if (!item->parent())
+ item->setParent(q);
if (objects.size() < idx + 1) {
int modelCount = instanceModel->count();
if (objects.capacity() < modelCount)
@@ -138,7 +103,7 @@ void QQmlInstantiatorPrivate::_q_createdItem(int idx, QObject* item)
if (QObject *o = objects.at(idx))
instanceModel->release(o);
objects.replace(idx, item);
- if (objects.count() == 1)
+ if (objects.size() == 1)
q->objectChanged();
q->objectAdded(idx, item);
}
@@ -147,7 +112,7 @@ void QQmlInstantiatorPrivate::_q_modelUpdated(const QQmlChangeSet &changeSet, bo
{
Q_Q(QQmlInstantiator);
- if (!componentComplete || effectiveReset)
+ if (!componentComplete || effectiveReset || !active)
return;
if (reset) {
@@ -161,8 +126,8 @@ void QQmlInstantiatorPrivate::_q_modelUpdated(const QQmlChangeSet &changeSet, bo
QHash<int, QVector<QPointer<QObject> > > moved;
const QVector<QQmlChangeSet::Change> &removes = changeSet.removes();
for (const QQmlChangeSet::Change &remove : removes) {
- int index = qMin(remove.index, objects.count());
- int count = qMin(remove.index + remove.count, objects.count()) - index;
+ int index = qMin(remove.index, objects.size());
+ int count = qMin(remove.index + remove.count, objects.size()) - index;
if (remove.isMove()) {
moved.insert(remove.moveId, objects.mid(index, count));
objects.erase(
@@ -181,7 +146,7 @@ void QQmlInstantiatorPrivate::_q_modelUpdated(const QQmlChangeSet &changeSet, bo
const QVector<QQmlChangeSet::Change> &inserts = changeSet.inserts();
for (const QQmlChangeSet::Change &insert : inserts) {
- int index = qMin(insert.index, objects.count());
+ int index = qMin(insert.index, objects.size());
if (insert.isMove()) {
QVector<QPointer<QObject> > movedObjects = moved.value(insert.moveId);
objects = objects.mid(0, index) + movedObjects + objects.mid(index);
@@ -242,10 +207,12 @@ QQmlInstantiator::QQmlInstantiator(QObject *parent)
QQmlInstantiator::~QQmlInstantiator()
{
+ Q_D(QQmlInstantiator);
+ d->clear();
}
/*!
- \qmlsignal QtQml::Instantiator::objectAdded(int index, QtObject object)
+ \qmlsignal QtQml.Models::Instantiator::objectAdded(int index, QtObject object)
This signal is emitted when an object is added to the Instantiator. The \a index
parameter holds the index which the object has been given, and the \a object
@@ -253,7 +220,7 @@ QQmlInstantiator::~QQmlInstantiator()
*/
/*!
- \qmlsignal QtQml::Instantiator::objectRemoved(int index, QtObject object)
+ \qmlsignal QtQml.Models::Instantiator::objectRemoved(int index, QtObject object)
This signal is emitted when an object is removed from the Instantiator. The \a index
parameter holds the index which the object had been given, and the \a object
@@ -263,7 +230,7 @@ QQmlInstantiator::~QQmlInstantiator()
in these cases it will be deleted shortly after the signal is handled.
*/
/*!
- \qmlproperty bool QtQml::Instantiator::active
+ \qmlproperty bool QtQml.Models::Instantiator::active
When active is true, and the delegate component is ready, the Instantiator will
create objects according to the model. When active is false, no objects
@@ -288,7 +255,7 @@ void QQmlInstantiator::setActive(bool newVal)
}
/*!
- \qmlproperty bool QtQml::Instantiator::asynchronous
+ \qmlproperty bool QtQml.Models::Instantiator::asynchronous
When asynchronous is true the Instantiator will attempt to create objects
asynchronously. This means that objects may not be available immediately,
@@ -315,7 +282,7 @@ void QQmlInstantiator::setAsync(bool newVal)
/*!
- \qmlproperty int QtQml::Instantiator::count
+ \qmlproperty int QtQml.Models::Instantiator::count
The number of objects the Instantiator is currently managing.
*/
@@ -323,12 +290,12 @@ void QQmlInstantiator::setAsync(bool newVal)
int QQmlInstantiator::count() const
{
Q_D(const QQmlInstantiator);
- return d->objects.count();
+ return d->objects.size();
}
/*!
- \qmlproperty QtQml::Component QtQml::Instantiator::delegate
- \default
+ \qmlproperty QtQml::Component QtQml.Models::Instantiator::delegate
+ \qmldefault
The component used to create all objects.
@@ -366,7 +333,7 @@ void QQmlInstantiator::setDelegate(QQmlComponent* c)
}
/*!
- \qmlproperty variant QtQml::Instantiator::model
+ \qmlproperty variant QtQml.Models::Instantiator::model
This property can be set to any of the supported \l {qml-data-models}{data models}:
@@ -447,7 +414,7 @@ void QQmlInstantiator::setModel(const QVariant &v)
}
/*!
- \qmlproperty QtObject QtQml::Instantiator::object
+ \qmlproperty QtObject QtQml.Models::Instantiator::object
This is a reference to the first created object, intended as a convenience
for the case where only one object has been created.
@@ -455,20 +422,20 @@ void QQmlInstantiator::setModel(const QVariant &v)
QObject *QQmlInstantiator::object() const
{
Q_D(const QQmlInstantiator);
- if (d->objects.count())
+ if (d->objects.size())
return d->objects[0];
return nullptr;
}
/*!
- \qmlmethod QtObject QtQml::Instantiator::objectAt(int index)
+ \qmlmethod QtObject QtQml.Models::Instantiator::objectAt(int index)
Returns a reference to the object with the given \a index.
*/
QObject *QQmlInstantiator::objectAt(int index) const
{
Q_D(const QQmlInstantiator);
- if (index >= 0 && index < d->objects.count())
+ if (index >= 0 && index < d->objects.size())
return d->objects[index];
return nullptr;
}