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 "qquickparticlepainter_p.h"
41#include <QQuickWindow>
42#include <QDebug>
43QT_BEGIN_NAMESPACE
44/*!
45 \qmltype ParticlePainter
46 \instantiates QQuickParticlePainter
47 \inqmlmodule QtQuick.Particles
48 \inherits Item
49 \brief For specifying how to paint particles.
50 \ingroup qtquick-particles
51
52 The default implementation paints nothing. See the subclasses if you want to
53 paint something visible.
54
55*/
56/*!
57 \qmlproperty ParticleSystem QtQuick.Particles::ParticlePainter::system
58 This is the system whose particles can be painted by the element.
59 If the ParticlePainter is a direct child of a ParticleSystem, it will automatically be associated with it.
60*/
61/*!
62 \qmlproperty list<string> QtQuick.Particles::ParticlePainter::groups
63 Which logical particle groups will be painted.
64
65 If empty, it will paint the default particle group ("").
66*/
67QQuickParticlePainter::QQuickParticlePainter(QQuickItem *parent)
68 : QQuickItem(parent)
69 , m_system(nullptr)
70 , m_count(0)
71 , m_pleaseReset(true)
72 , m_window(nullptr)
73 , m_windowChanged(false)
74 , m_groupIdsNeedRecalculation(false)
75{
76}
77
78void QQuickParticlePainter::itemChange(ItemChange change, const ItemChangeData &data)
79{
80 if (change == QQuickItem::ItemSceneChange) {
81 if (m_window)
82 disconnect(sender: m_window, SIGNAL(sceneGraphInvalidated()), receiver: this, SLOT(sceneGraphInvalidated()));
83 m_window = data.window;
84 m_windowChanged = true;
85 if (m_window)
86 connect(sender: m_window, SIGNAL(sceneGraphInvalidated()), receiver: this, SLOT(sceneGraphInvalidated()), Qt::DirectConnection);
87 }
88 QQuickItem::itemChange(change, data);
89}
90
91void QQuickParticlePainter::componentComplete()
92{
93 if (!m_system && qobject_cast<QQuickParticleSystem*>(object: parentItem()))
94 setSystem(qobject_cast<QQuickParticleSystem*>(object: parentItem()));
95 QQuickItem::componentComplete();
96}
97
98void QQuickParticlePainter::recalculateGroupIds() const
99{
100 if (!m_system) {
101 m_groupIds.clear();
102 return;
103 }
104
105 m_groupIdsNeedRecalculation = false;
106 m_groupIds.clear();
107
108 const auto groupList = groups();
109 for (const QString &str : groupList) {
110 QQuickParticleGroupData::ID groupId = m_system->groupIds.value(akey: str, adefaultValue: QQuickParticleGroupData::InvalidID);
111 if (groupId == QQuickParticleGroupData::InvalidID) {
112 // invalid data, not finished setting up, or whatever. Fallback: do not cache.
113 m_groupIdsNeedRecalculation = true;
114 } else {
115 m_groupIds.append(t: groupId);
116 }
117 }
118}
119
120void QQuickParticlePainter::setSystem(QQuickParticleSystem *arg)
121{
122 if (m_system != arg) {
123 m_system = arg;
124 m_groupIdsNeedRecalculation = true;
125 if (m_system){
126 m_system->registerParticlePainter(p: this);
127 reset();
128 }
129 emit systemChanged(arg);
130 }
131}
132
133void QQuickParticlePainter::setGroups(const QStringList &arg)
134{
135 if (m_groups != arg) {
136 m_groups = arg;
137 m_groupIdsNeedRecalculation = true;
138 //Note: The system watches this as it has to recalc things when groups change. It will request a reset if necessary
139 Q_EMIT groupsChanged(arg);
140 }
141}
142
143void QQuickParticlePainter::load(QQuickParticleData* d)
144{
145 initialize(gIdx: d->groupId, pIdx: d->index);
146 if (m_pleaseReset)
147 return;
148 m_pendingCommits << qMakePair<int, int>(x: d->groupId, y: d->index);
149}
150
151void QQuickParticlePainter::reload(QQuickParticleData* d)
152{
153 if (m_pleaseReset)
154 return;
155 m_pendingCommits << qMakePair<int, int>(x: d->groupId, y: d->index);
156}
157
158void QQuickParticlePainter::reset()
159{
160 m_pendingCommits.clear();
161 m_pleaseReset = true;
162}
163
164void QQuickParticlePainter::setCount(int c)//### TODO: some resizeing so that particles can reallocate on size change instead of recreate
165{
166 Q_ASSERT(c >= 0); //XXX
167 if (c == m_count)
168 return;
169 m_count = c;
170 emit countChanged();
171 reset();
172}
173
174void QQuickParticlePainter::calcSystemOffset(bool resetPending)
175{
176 if (!m_system || !parentItem())
177 return;
178 QPointF lastOffset = m_systemOffset;
179 m_systemOffset = -1 * this->mapFromItem(item: m_system, point: QPointF(0.0, 0.0));
180 if (lastOffset != m_systemOffset && !resetPending){
181 //Reload all particles//TODO: Necessary?
182 foreach (const QString &g, m_groups){
183 int gId = m_system->groupIds[g];
184 foreach (QQuickParticleData* d, m_system->groupData[gId]->data)
185 reload(d);
186 }
187 }
188}
189typedef QPair<int,int> intPair;
190void QQuickParticlePainter::performPendingCommits()
191{
192 calcSystemOffset();
193 foreach (intPair p, m_pendingCommits)
194 commit(gIdx: p.first, pIdx: p.second);
195 m_pendingCommits.clear();
196}
197
198QT_END_NAMESPACE
199
200#include "moc_qquickparticlepainter_p.cpp"
201

source code of qtdeclarative/src/particles/qquickparticlepainter.cpp