Add QQuickItemParticle give() method body

The current version of QQuickItemParticle give() method was not
implemented and a simple TODO comment was present instead.
I added a working body and add also a reparent feature when an
added item is released.

Fixes: QTBUG-76827
Change-Id: Ib7d289cad2ff0cd166e766eb7f07e92437e7681b
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Fabio Falsini 2019-11-20 20:04:30 +01:00
parent 0c2eaad357
commit 9d9c5a786c
4 changed files with 108 additions and 6 deletions

View File

@ -159,8 +159,15 @@ void QQuickItemParticle::take(QQuickItem *item, bool prioritize)
void QQuickItemParticle::give(QQuickItem *item)
{
//TODO: This
Q_UNUSED(item);
for (auto groupId : groupIds()) {
for (QQuickParticleData* data : qAsConst(m_system->groupData[groupId]->data)) {
if (data->delegate == item){
m_deletables << item;
data->delegate = nullptr;
return;
}
}
}
}
void QQuickItemParticle::initialize(int gIdx, int pIdx)
@ -179,8 +186,11 @@ void QQuickItemParticle::processDeletables()
item->setOpacity(0.);
item->setVisible(false);
QQuickItemParticleAttached* mpa;
if ((mpa = qobject_cast<QQuickItemParticleAttached*>(qmlAttachedPropertiesObject<QQuickItemParticle>(item))))
mpa->detach();//reparent as well?
if ((mpa = qobject_cast<QQuickItemParticleAttached*>(qmlAttachedPropertiesObject<QQuickItemParticle>(item)))) {
if (mpa->m_parentItem != nullptr)
item->setParentItem(mpa->m_parentItem);
mpa->detach();
}
int idx = -1;
if ((idx = m_managed.indexOf(item)) != -1) {
m_managed.takeAt(idx);
@ -205,9 +215,12 @@ void QQuickItemParticle::tick(int time)
m_deletables << d->delegate;
d->delegate = nullptr;
}
QQuickItem* parentItem = nullptr;
if (!m_pendingItems.isEmpty()){
d->delegate = m_pendingItems.front();
QQuickItem *item = m_pendingItems.front();
m_pendingItems.pop_front();
parentItem = item->parentItem();
d->delegate = item;
}else if (m_delegate){
d->delegate = qobject_cast<QQuickItem*>(m_delegate->create(qmlContext(this)));
if (d->delegate)
@ -218,6 +231,7 @@ void QQuickItemParticle::tick(int time)
d->delegate->setY(d->curY(m_system) - d->delegate->height() / 2);
QQuickItemParticleAttached* mpa = qobject_cast<QQuickItemParticleAttached*>(qmlAttachedPropertiesObject<QQuickItemParticle>(d->delegate));
if (mpa){
mpa->m_parentItem = parentItem;
mpa->m_mp = this;
mpa->attach();
}

View File

@ -130,13 +130,14 @@ class QQuickItemParticleAttached : public QObject
Q_PROPERTY(QQuickItemParticle* particle READ particle CONSTANT);
public:
QQuickItemParticleAttached(QObject* parent)
: QObject(parent), m_mp(0)
: QObject(parent), m_mp(0), m_parentItem(nullptr)
{;}
QQuickItemParticle* particle() const { return m_mp; }
void detach(){Q_EMIT detached();}
void attach(){Q_EMIT attached();}
private:
QQuickItemParticle* m_mp;
QPointer<QQuickItem> m_parentItem;
friend class QQuickItemParticle;
Q_SIGNALS:
void detached();

View File

@ -0,0 +1,74 @@
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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 General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** 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-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick 2.0
import QtQuick.Particles 2.0
Rectangle {
id: testTakeGive
color: "black"
width: 320
height: 320
function takeItems()
{
for(var i = 0; i < imgList.count; i++) ip.take(imgList.itemAt(i));
}
function giveItems()
{
for(var i = 0; i < imgList.count; i++) ip.give(imgList.itemAt(i));
}
Repeater {
id: imgList
model: 100
delegate: Image {
ItemParticle.onAttached: sys.acc = sys.acc + 1
ItemParticle.onDetached: sys.acc = sys.acc - 1;
source: "../../shared/star.png"
}
}
ParticleSystem {
id: sys
objectName: "system"
anchors.fill: parent
property int acc: 0
ItemParticle {
id: ip
}
Emitter{
//0,0 position
size: 32
emitRate: 1000
lifeSpan: Emitter.InfiniteLife
}
}
}

View File

@ -45,6 +45,7 @@ private slots:
void test_basic();
void test_deletion();
void test_noDeletion();
void test_takeGive();
};
void tst_qquickitemparticle::initTestCase()
@ -107,6 +108,18 @@ void tst_qquickitemparticle::test_noDeletion()
delete view;
}
void tst_qquickitemparticle::test_takeGive()
{
QQuickView* view = createView(testFileUrl("takeGive.qml"), 500);
QQuickParticleSystem* system = view->rootObject()->findChild<QQuickParticleSystem*>("system");
QMetaObject::invokeMethod(view->rootObject(), "takeItems");
ensureAnimTime(1000, system->m_animation);
QVERIFY(system->property("acc").toInt() == 100);
QMetaObject::invokeMethod(view->rootObject(), "giveItems");
QTRY_VERIFY(system->property("acc").toInt() == 0);
delete view;
}
QTEST_MAIN(tst_qquickitemparticle);
#include "tst_qquickitemparticle.moc"