OpenCoverage

qquickitemparticle.cpp

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/particles/qquickitemparticle.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtQuick 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 "qquickitemparticle_p.h"-
41#include <QtQuick/qsgnode.h>-
42#include <QTimer>-
43#include <QQmlComponent>-
44#include <QDebug>-
45-
46QT_BEGIN_NAMESPACE-
47-
48/*!-
49 \qmltype ItemParticle-
50 \instantiates QQuickItemParticle-
51 \inqmlmodule QtQuick.Particles-
52 \inherits ParticlePainter-
53 \brief For specifying a delegate to paint particles.-
54 \ingroup qtquick-particles-
55-
56*/-
57-
58-
59/*!-
60 \qmlmethod QtQuick.Particles::ItemParticle::freeze(Item item)-
61-
62 Suspends the flow of time for the logical particle which item represents, allowing you to control its movement.-
63*/-
64-
65/*!-
66 \qmlmethod QtQuick.Particles::ItemParticle::unfreeze(Item item)-
67-
68 Restarts the flow of time for the logical particle which item represents, allowing it to be moved by the particle system again.-
69*/-
70-
71/*!-
72 \qmlmethod QtQuick.Particles::ItemParticle::take(Item item, bool prioritize)-
73-
74 Asks the ItemParticle to take over control of item positioning temporarily.-
75 It will follow the movement of a logical particle when one is available.-
76-
77 By default items form a queue when waiting for a logical particle, but if prioritize is true then it will go immediately to the-
78 head of the queue.-
79-
80 ItemParticle does not take ownership of the item, and will relinquish-
81 control when the logical particle expires. Commonly at this point you will-
82 want to put it back in the queue, you can do this with the below line in-
83 the delegate definition:-
84 \code-
85 ItemParticle.onDetached: itemParticleInstance.take(delegateRootItem);-
86 \endcode-
87 or delete it, such as with the below line in the delegate definition:-
88 \code-
89 ItemParticle.onDetached: delegateRootItem.destroy();-
90 \endcode-
91*/-
92-
93/*!-
94 \qmlmethod QtQuick.Particles::ItemParticle::give(Item item)-
95-
96 Orders the ItemParticle to give you control of the item. It will cease controlling it and the item will lose its association to the logical particle.-
97*/-
98-
99/*!-
100 \qmlproperty bool QtQuick.Particles::ItemParticle::fade-
101-
102 If true, the item will automatically be faded in and out-
103 at the ends of its lifetime. If false, you will have to-
104 implement any entry effect yourself.-
105-
106 Default is true.-
107*/-
108/*!-
109 \qmlproperty Component QtQuick.Particles::ItemParticle::delegate-
110-
111 An instance of the delegate will be created for every logical particle, and-
112 moved along with it. As an alternative to using delegate, you can create-
113 Item instances yourself and hand them to the ItemParticle to move using the-
114 \l take method.-
115-
116 Any delegate instances created by ItemParticle will be destroyed when-
117 the logical particle expires.-
118*/-
119-
120QQuickItemParticle::QQuickItemParticle(QQuickItem *parent) :-
121 QQuickParticlePainter(parent), m_fade(true), m_lastT(0), m_activeCount(0), m_delegate(nullptr)-
122{-
123 setFlag(QQuickItem::ItemHasContents);-
124 clock = new Clock(this);-
125 clock->start();-
126}
executed 8 times by 2 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemparticle
8
127-
128QQuickItemParticle::~QQuickItemParticle()-
129{-
130 delete clock;-
131}
executed 8 times by 2 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemparticle
8
132-
133void QQuickItemParticle::freeze(QQuickItem* item)-
134{-
135 m_stasis << item;-
136}
never executed: end of block
0
137-
138-
139void QQuickItemParticle::unfreeze(QQuickItem* item)-
140{-
141 m_stasis.remove(item);-
142}
never executed: end of block
0
143-
144void QQuickItemParticle::take(QQuickItem *item, bool prioritize)-
145{-
146 if (prioritize)
prioritizeDescription
TRUEnever evaluated
FALSEevaluated 904 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
0-904
147 m_pendingItems.push_front(item);
never executed: m_pendingItems.push_front(item);
0
148 else-
149 m_pendingItems.push_back(item);
executed 904 times by 1 test: m_pendingItems.push_back(item);
Executed by:
  • tst_qquickitemparticle
904
150}-
151-
152void QQuickItemParticle::give(QQuickItem *item)-
153{-
154 //TODO: This-
155 Q_UNUSED(item);-
156}
never executed: end of block
0
157-
158void QQuickItemParticle::initialize(int gIdx, int pIdx)-
159{-
160 m_loadables << m_system->groupData[gIdx]->data[pIdx];//defer to other thread-
161}
executed 3392 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
3392
162-
163void QQuickItemParticle::commit(int, int)-
164{-
165}-
166-
167void QQuickItemParticle::processDeletables()-
168{-
169 foreach (QQuickItem* item, m_deletables){
_container_.controlDescription
TRUEevaluated 1716 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEevaluated 1716 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
_container_.controlDescription
TRUEevaluated 1966 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemparticle
FALSEnever evaluated
_container_.i != _container_.eDescription
TRUEevaluated 1716 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEevaluated 250 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemparticle
0-1966
170 if (m_fade)
m_fadeDescription
TRUEevaluated 1716 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEnever evaluated
0-1716
171 item->setOpacity(0.);
executed 1716 times by 1 test: item->setOpacity(0.);
Executed by:
  • tst_qquickitemparticle
1716
172 item->setVisible(false);-
173 QQuickItemParticleAttached* mpa;-
174 if ((mpa = qobject_cast<QQuickItemParticleAttached*>(qmlAttachedPropertiesObject<QQuickItemParticle>(item))))
(mpa = qobject...ticle>(item)))Description
TRUEevaluated 1716 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEnever evaluated
0-1716
175 mpa->detach();//reparent as well?
executed 1716 times by 1 test: mpa->detach();
Executed by:
  • tst_qquickitemparticle
1716
176 int idx = -1;-
177 if ((idx = m_managed.indexOf(item)) != -1) {
(idx = m_manag...f(item)) != -1Description
TRUEevaluated 1012 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEevaluated 704 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
704-1012
178 m_managed.takeAt(idx);-
179 delete item;-
180 }
executed 1012 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
1012
181 m_activeCount--;-
182 }
executed 1716 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
1716
183 m_deletables.clear();-
184}
executed 250 times by 2 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemparticle
250
185-
186void QQuickItemParticle::tick(int time)-
187{-
188 Q_UNUSED(time);//only needed because QTickAnimationProxy expects one-
189 processDeletables();-
190-
191 foreach (QQuickParticleData* d, m_loadables){
_container_.controlDescription
TRUEevaluated 3296 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEevaluated 3296 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
_container_.controlDescription
TRUEevaluated 3522 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemparticle
FALSEnever evaluated
_container_.i != _container_.eDescription
TRUEevaluated 3296 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEevaluated 226 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemparticle
0-3522
192 Q_ASSERT(d);-
193 if (m_stasis.contains(d->delegate))
m_stasis.contains(d->delegate)Description
TRUEnever evaluated
FALSEevaluated 3296 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
0-3296
194 qWarning() << "Current model particles prefers overwrite:false";
never executed: QMessageLogger(__FILE__, 194, __PRETTY_FUNCTION__).warning() << "Current model particles prefers overwrite:false";
0
195 //remove old item from the particle that is dying to make room for this one-
196 if (d->delegate) {
d->delegateDescription
TRUEevaluated 1812 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEevaluated 1484 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
1484-1812
197 m_deletables << d->delegate;-
198 d->delegate = nullptr;-
199 }
executed 1812 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
1812
200 if (!m_pendingItems.isEmpty()){
!m_pendingItems.isEmpty()Description
TRUEevaluated 904 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEevaluated 2392 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
904-2392
201 d->delegate = m_pendingItems.front();-
202 m_pendingItems.pop_front();-
203 }else if (m_delegate){
executed 904 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
m_delegateDescription
TRUEevaluated 2272 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEevaluated 120 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
120-2272
204 d->delegate = qobject_cast<QQuickItem*>(m_delegate->create(qmlContext(this)));-
205 if (d->delegate)
d->delegateDescription
TRUEevaluated 2272 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEnever evaluated
0-2272
206 m_managed << d->delegate;
executed 2272 times by 1 test: m_managed << d->delegate;
Executed by:
  • tst_qquickitemparticle
2272
207 }
executed 2272 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
2272
208 if (d && d->delegate){//###Data can be zero if creating an item leads to a reset - this screws things up.
dDescription
TRUEevaluated 3296 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEnever evaluated
d->delegateDescription
TRUEevaluated 3176 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEevaluated 120 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
0-3296
209 d->delegate->setX(d->curX(m_system) - d->delegate->width() / 2); //TODO: adjust for system?-
210 d->delegate->setY(d->curY(m_system) - d->delegate->height() / 2);-
211 QQuickItemParticleAttached* mpa = qobject_cast<QQuickItemParticleAttached*>(qmlAttachedPropertiesObject<QQuickItemParticle>(d->delegate));-
212 if (mpa){
mpaDescription
TRUEevaluated 3176 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEnever evaluated
0-3176
213 mpa->m_mp = this;-
214 mpa->attach();-
215 }
executed 3176 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
3176
216 d->delegate->setParentItem(this);-
217 if (m_fade)
m_fadeDescription
TRUEevaluated 3176 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEnever evaluated
0-3176
218 d->delegate->setOpacity(0.);
executed 3176 times by 1 test: d->delegate->setOpacity(0.);
Executed by:
  • tst_qquickitemparticle
3176
219 d->delegate->setVisible(false);//Will be set to true when we prepare the next frame-
220 m_activeCount++;-
221 }
executed 3176 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
3176
222 }
executed 3296 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
3296
223 m_loadables.clear();-
224}
executed 226 times by 2 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemparticle
226
225-
226void QQuickItemParticle::reset()-
227{-
228 QQuickParticlePainter::reset();-
229 m_loadables.clear();-
230-
231 // delete all managed items which had their logical particles cleared-
232 // but leave it alone if the logical particle is maintained-
233 QSet<QQuickItem*> lost = QSet<QQuickItem*>::fromList(m_managed);-
234 for (auto groupId : groupIds()) {-
235 for (QQuickParticleData* d : qAsConst(m_system->groupData[groupId]->data)) {-
236 lost.remove(d->delegate);-
237 }
executed 2824 times by 2 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemparticle
2824
238 }
executed 16 times by 2 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemparticle
16
239 m_deletables.append(lost.toList());-
240 //TODO: This doesn't yet handle calling detach on taken particles in the system reset case-
241 processDeletables();-
242}
executed 24 times by 2 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemparticle
24
243-
244-
245QSGNode* QQuickItemParticle::updatePaintNode(QSGNode* n, UpdatePaintNodeData* d)-
246{-
247 //Dummy update just to get painting tick-
248 if (m_pleaseReset){
m_pleaseResetDescription
TRUEevaluated 8 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemparticle
FALSEevaluated 346 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
8-346
249 m_pleaseReset = false;-
250 //Refill loadables, delayed here so as to only happen once per frame max-
251 //### Constant resetting might lead to m_loadables never being populated when tick() occurs-
252 for (auto groupId : groupIds()) {-
253 for (QQuickParticleData* d : qAsConst(m_system->groupData[groupId]->data)) {-
254 if (!d->delegate && d->t != -1 && d->stillAlive(m_system)) {
!d->delegateDescription
TRUEevaluated 1412 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemparticle
FALSEnever evaluated
d->t != -1Description
TRUEnever evaluated
FALSEevaluated 1412 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemparticle
d->stillAlive(m_system)Description
TRUEnever evaluated
FALSEnever evaluated
0-1412
255 m_loadables << d;-
256 }
never executed: end of block
0
257 }
executed 1412 times by 2 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemparticle
1412
258 }
executed 8 times by 2 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemparticle
8
259 }
executed 8 times by 2 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemparticle
8
260 prepareNextFrame();-
261-
262 update();//Get called again-
263 if (n)
nDescription
TRUEnever evaluated
FALSEevaluated 354 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemparticle
0-354
264 n->markDirty(QSGNode::DirtyMaterial);
never executed: n->markDirty(QSGNode::DirtyMaterial);
0
265 return QQuickItem::updatePaintNode(n,d);
executed 354 times by 2 tests: return QQuickItem::updatePaintNode(n,d);
Executed by:
  • tst_examples
  • tst_qquickitemparticle
354
266}-
267-
268void QQuickItemParticle::prepareNextFrame()-
269{-
270 if (!m_system)
!m_systemDescription
TRUEnever evaluated
FALSEevaluated 354 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemparticle
0-354
271 return;
never executed: return;
0
272 qint64 timeStamp = m_system->systemSync(this);-
273 qreal curT = timeStamp/1000.0;-
274 qreal dt = curT - m_lastT;-
275 m_lastT = curT;-
276 if (!m_activeCount)
!m_activeCountDescription
TRUEevaluated 33 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemparticle
FALSEevaluated 321 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
33-321
277 return;
executed 33 times by 2 tests: return;
Executed by:
  • tst_examples
  • tst_qquickitemparticle
33
278-
279 //TODO: Size, better fade?-
280 for (auto groupId : groupIds()) {-
281 for (QQuickParticleData* data : qAsConst(m_system->groupData[groupId]->data)) {-
282 QQuickItem* item = data->delegate;-
283 if (!item)
!itemDescription
TRUEevaluated 29113 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEevaluated 52987 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
29113-52987
284 continue;
executed 29113 times by 1 test: continue;
Executed by:
  • tst_qquickitemparticle
29113
285 float t = ((timeStamp / 1000.0f) - data->t) / data->lifeSpan;-
286 if (m_stasis.contains(item)) {
m_stasis.contains(item)Description
TRUEnever evaluated
FALSEevaluated 52987 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
0-52987
287 data->t += dt;//Stasis effect-
288 continue;
never executed: continue;
0
289 }-
290 if (t >= 1.0f){//Usually happens from load
t >= 1.0fDescription
TRUEnever evaluated
FALSEevaluated 52987 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
0-52987
291 m_deletables << item;-
292 data->delegate = nullptr;-
293 }else{//Fade
never executed: end of block
0
294 data->delegate->setVisible(true);-
295 if (m_fade){
m_fadeDescription
TRUEevaluated 52987 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEnever evaluated
0-52987
296 float o = 1.f;-
297 if (t <0.2f)
t <0.2fDescription
TRUEevaluated 13262 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEevaluated 39725 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
13262-39725
298 o = t * 5;
executed 13262 times by 1 test: o = t * 5;
Executed by:
  • tst_qquickitemparticle
13262
299 if (t > 0.8f)
t > 0.8fDescription
TRUEevaluated 5907 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
FALSEevaluated 47080 times by 1 test
Evaluated by:
  • tst_qquickitemparticle
5907-47080
300 o = (1-t)*5;
executed 5907 times by 1 test: o = (1-t)*5;
Executed by:
  • tst_qquickitemparticle
5907
301 item->setOpacity(o);-
302 }
executed 52987 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
52987
303 }
executed 52987 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
52987
304 item->setX(data->curX(m_system) - item->width() / 2 - m_systemOffset.x());-
305 item->setY(data->curY(m_system) - item->height() / 2 - m_systemOffset.y());-
306 }
executed 52987 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
52987
307 }
executed 321 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
321
308}
executed 321 times by 1 test: end of block
Executed by:
  • tst_qquickitemparticle
321
309-
310QQuickItemParticleAttached *QQuickItemParticle::qmlAttachedProperties(QObject *object)-
311{-
312 return new QQuickItemParticleAttached(object);
executed 2472 times by 1 test: return new QQuickItemParticleAttached(object);
Executed by:
  • tst_qquickitemparticle
2472
313}-
314-
315QT_END_NAMESPACE-
316-
317#include "moc_qquickitemparticle_p.cpp"-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.0