1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef ITEMPARTICLE_H
5#define ITEMPARTICLE_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17#include "qquickparticlepainter_p.h"
18#include <QPointer>
19#include <QSet>
20#include <private/qquickanimation_p_p.h>
21QT_BEGIN_NAMESPACE
22
23class QQuickItemParticleAttached;
24
25class Q_QUICKPARTICLES_PRIVATE_EXPORT QQuickItemParticle : public QQuickParticlePainter
26{
27 Q_OBJECT
28 Q_PROPERTY(bool fade READ fade WRITE setFade NOTIFY fadeChanged FINAL)
29 Q_PROPERTY(QQmlComponent* delegate READ delegate WRITE setDelegate NOTIFY delegateChanged FINAL)
30 QML_NAMED_ELEMENT(ItemParticle)
31 QML_ADDED_IN_VERSION(2, 0)
32 QML_ATTACHED(QQuickItemParticleAttached)
33public:
34 explicit QQuickItemParticle(QQuickItem *parent = nullptr);
35 ~QQuickItemParticle();
36
37 bool fade() const { return m_fade; }
38
39 QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;
40
41 static QQuickItemParticleAttached *qmlAttachedProperties(QObject *object);
42 QQmlComponent* delegate() const
43 {
44 return m_delegate;
45 }
46
47Q_SIGNALS:
48 void fadeChanged();
49
50 void delegateChanged(QQmlComponent* arg);
51
52public Q_SLOTS:
53 //TODO: Add a follow mode, where moving the delegate causes the logical particle to go with it?
54 void freeze(QQuickItem* item);
55 void unfreeze(QQuickItem* item);
56 void take(QQuickItem* item,bool prioritize=false);//take by modelparticle
57 void give(QQuickItem* item);//give from modelparticle
58
59 void setFade(bool arg){if (arg == m_fade) return; m_fade = arg; Q_EMIT fadeChanged();}
60 void setDelegate(QQmlComponent* arg)
61 {
62 if (m_delegate != arg) {
63 m_delegate = arg;
64 Q_EMIT delegateChanged(arg);
65 }
66 }
67
68protected:
69 void reset() override;
70 void commit(int gIdx, int pIdx) override;
71 void initialize(int gIdx, int pIdx) override;
72 void prepareNextFrame();
73private:
74 void processDeletables();
75 void tick(int time = 0);
76 QSet<QQuickItem* > m_deletables;
77 QList<QQuickItem* > m_managed;
78 bool m_fade;
79
80 QList<QQuickItem*> m_pendingItems;
81 QSet<QQuickItem*> m_stasis;
82 qreal m_lastT;
83 int m_activeCount;
84 QQmlComponent* m_delegate;
85
86 typedef QTickAnimationProxy<QQuickItemParticle, &QQuickItemParticle::tick> Clock;
87 Clock *clock;
88};
89
90class QQuickItemParticleAttached : public QObject
91{
92 Q_OBJECT
93 Q_PROPERTY(QQuickItemParticle* particle READ particle CONSTANT FINAL);
94public:
95 QQuickItemParticleAttached(QObject* parent)
96 : QObject(parent), m_mp(0), m_parentItem(nullptr)
97 {;}
98 QQuickItemParticle* particle() const { return m_mp; }
99 void detach(){Q_EMIT detached();}
100 void attach(){Q_EMIT attached();}
101private:
102 QQuickItemParticle* m_mp;
103 QPointer<QQuickItem> m_parentItem;
104 friend class QQuickItemParticle;
105Q_SIGNALS:
106 void detached();
107 void attached();
108};
109
110QT_END_NAMESPACE
111
112#endif // ITEMPARTICLE_H
113

source code of qtdeclarative/src/particles/qquickitemparticle_p.h