1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQuick module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qquickparticlepainter_p.h"
43 #include <QQuickWindow>
47 \qmlclass ParticlePainter QQuickParticlePainter
48 \inqmlmodule QtQuick.Particles 2
49 \inherits ParticlePainter
50 \brief For specifying how to paint particles
51 \ingroup qtquick-particles
53 The default implementation paints nothing. See the subclasses if you want to
54 paint something visible.
58 \qmlproperty ParticleSystem QtQuick.Particles2::ParticlePainter::system
59 This is the system whose particles can be painted by the element.
60 If the ParticlePainter is a direct child of a ParticleSystem, it will automatically be associated with it.
63 \qmlproperty list<string> QtQuick.Particles2::ParticlePainter::groups
64 Which logical particle groups will be painted.
66 If empty, it will paint the default particle group ("").
68 QQuickParticlePainter::QQuickParticlePainter(QQuickItem *parent) :
70 m_system(0), m_count(0), m_pleaseReset(true), m_window(0)
74 void QQuickParticlePainter::itemChange(ItemChange change, const ItemChangeData &data)
76 if (change == QQuickItem::ItemSceneChange) {
78 disconnect(m_window, SIGNAL(sceneGraphInvalidated()), this, SLOT(sceneGraphInvalidated()));
79 m_window = data.window;
81 connect(m_window, SIGNAL(sceneGraphInvalidated()), this, SLOT(sceneGraphInvalidated()), Qt::DirectConnection);
86 void QQuickParticlePainter::componentComplete()
88 if (!m_system && qobject_cast<QQuickParticleSystem*>(parentItem()))
89 setSystem(qobject_cast<QQuickParticleSystem*>(parentItem()));
90 QQuickItem::componentComplete();
94 void QQuickParticlePainter::setSystem(QQuickParticleSystem *arg)
96 if (m_system != arg) {
99 m_system->registerParticlePainter(this);
102 emit systemChanged(arg);
106 void QQuickParticlePainter::load(QQuickParticleData* d)
108 initialize(d->group, d->index);
111 m_pendingCommits << qMakePair<int, int>(d->group, d->index);
114 void QQuickParticlePainter::reload(QQuickParticleData* d)
118 m_pendingCommits << qMakePair<int, int>(d->group, d->index);
121 void QQuickParticlePainter::reset()
123 m_pendingCommits.clear();
124 m_pleaseReset = true;
127 void QQuickParticlePainter::setCount(int c)//### TODO: some resizeing so that particles can reallocate on size change instead of recreate
129 Q_ASSERT(c >= 0); //XXX
137 int QQuickParticlePainter::count()
142 void QQuickParticlePainter::calcSystemOffset(bool resetPending)
144 if (!m_system || !parentItem())
146 QPointF lastOffset = m_systemOffset;
147 m_systemOffset = -1 * this->mapFromItem(m_system, QPointF(0.0, 0.0));
148 if (lastOffset != m_systemOffset && !resetPending){
149 //Reload all particles//TODO: Necessary?
150 foreach (const QString &g, m_groups){
151 int gId = m_system->groupIds[g];
152 foreach (QQuickParticleData* d, m_system->groupData[gId]->data)
157 typedef QPair<int,int> intPair;
158 void QQuickParticlePainter::performPendingCommits()
161 foreach (intPair p, m_pendingCommits)
162 commit(p.first, p.second);
163 m_pendingCommits.clear();