1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQuick module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qquickparticlepainter_p.h"
43 #include <QQuickWindow>
47 \qmltype ParticlePainter
48 \instantiates QQuickParticlePainter
49 \inqmlmodule QtQuick.Particles 2
51 \brief For specifying how to paint particles
52 \ingroup qtquick-particles
54 The default implementation paints nothing. See the subclasses if you want to
55 paint something visible.
59 \qmlproperty ParticleSystem QtQuick.Particles2::ParticlePainter::system
60 This is the system whose particles can be painted by the element.
61 If the ParticlePainter is a direct child of a ParticleSystem, it will automatically be associated with it.
64 \qmlproperty list<string> QtQuick.Particles2::ParticlePainter::groups
65 Which logical particle groups will be painted.
67 If empty, it will paint the default particle group ("").
69 QQuickParticlePainter::QQuickParticlePainter(QQuickItem *parent) :
71 m_system(0), m_count(0), m_pleaseReset(true), m_window(0)
75 void QQuickParticlePainter::itemChange(ItemChange change, const ItemChangeData &data)
77 if (change == QQuickItem::ItemSceneChange) {
79 disconnect(m_window, SIGNAL(sceneGraphInvalidated()), this, SLOT(sceneGraphInvalidated()));
80 m_window = data.window;
82 connect(m_window, SIGNAL(sceneGraphInvalidated()), this, SLOT(sceneGraphInvalidated()), Qt::DirectConnection);
87 void QQuickParticlePainter::componentComplete()
89 if (!m_system && qobject_cast<QQuickParticleSystem*>(parentItem()))
90 setSystem(qobject_cast<QQuickParticleSystem*>(parentItem()));
91 QQuickItem::componentComplete();
95 void QQuickParticlePainter::setSystem(QQuickParticleSystem *arg)
97 if (m_system != arg) {
100 m_system->registerParticlePainter(this);
103 emit systemChanged(arg);
107 void QQuickParticlePainter::load(QQuickParticleData* d)
109 initialize(d->group, d->index);
112 m_pendingCommits << qMakePair<int, int>(d->group, d->index);
115 void QQuickParticlePainter::reload(QQuickParticleData* d)
119 m_pendingCommits << qMakePair<int, int>(d->group, d->index);
122 void QQuickParticlePainter::reset()
124 m_pendingCommits.clear();
125 m_pleaseReset = true;
128 void QQuickParticlePainter::setCount(int c)//### TODO: some resizeing so that particles can reallocate on size change instead of recreate
130 Q_ASSERT(c >= 0); //XXX
138 int QQuickParticlePainter::count()
143 void QQuickParticlePainter::calcSystemOffset(bool resetPending)
145 if (!m_system || !parentItem())
147 QPointF lastOffset = m_systemOffset;
148 m_systemOffset = -1 * this->mapFromItem(m_system, QPointF(0.0, 0.0));
149 if (lastOffset != m_systemOffset && !resetPending){
150 //Reload all particles//TODO: Necessary?
151 foreach (const QString &g, m_groups){
152 int gId = m_system->groupIds[g];
153 foreach (QQuickParticleData* d, m_system->groupData[gId]->data)
158 typedef QPair<int,int> intPair;
159 void QQuickParticlePainter::performPendingCommits()
162 foreach (intPair p, m_pendingCommits)
163 commit(p.first, p.second);
164 m_pendingCommits.clear();