fd74860f1e181d186db31fe5a06fbee08c97a9fa
[profile/ivi/qtdeclarative.git] / src / quick / particles / qquickparticlepainter.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the Declarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qquickparticlepainter_p.h"
43 #include <QDebug>
44 QT_BEGIN_NAMESPACE
45 /*!
46     \qmlclass ParticlePainter QQuickParticlePainter
47     \inqmlmodule QtQuick.Particles 2
48     \inherits ParticlePainter
49     \brief ParticlePainter elements allow you to specify how to paint particles.
50
51     The default implementation paints nothing. See the subclasses if you want to
52     paint something visible.
53
54 */
55 /*!
56     \qmlproperty ParticleSystem QtQuick.Particles2::ParticlePainter::system
57     This is the system whose particles can be painted by the element.
58     If the ParticlePainter is a direct child of a ParticleSystem, it will automatically be associated with it.
59 */
60 /*!
61     \qmlproperty list<string> QtQuick.Particles2::ParticlePainter::groups
62     Which logical particle groups will be painted.
63
64     If empty, it will paint the default particle group ("").
65 */
66 QQuickParticlePainter::QQuickParticlePainter(QQuickItem *parent) :
67     QQuickItem(parent),
68     m_system(0), m_count(0), m_pleaseReset(true)
69 {
70 }
71
72 void QQuickParticlePainter::componentComplete()
73 {
74     if (!m_system && qobject_cast<QQuickParticleSystem*>(parentItem()))
75         setSystem(qobject_cast<QQuickParticleSystem*>(parentItem()));
76     QQuickItem::componentComplete();
77 }
78
79
80 void QQuickParticlePainter::setSystem(QQuickParticleSystem *arg)
81 {
82     if (m_system != arg) {
83         m_system = arg;
84         if (m_system){
85             m_system->registerParticlePainter(this);
86             reset();
87         }
88         emit systemChanged(arg);
89     }
90 }
91
92 void QQuickParticlePainter::load(QQuickParticleData* d)
93 {
94     initialize(d->group, d->index);
95     if (m_pleaseReset)
96         return;
97     m_pendingCommits << qMakePair<int, int>(d->group, d->index);
98 }
99
100 void QQuickParticlePainter::reload(QQuickParticleData* d)
101 {
102     if (m_pleaseReset)
103         return;
104     m_pendingCommits << qMakePair<int, int>(d->group, d->index);
105 }
106
107 void QQuickParticlePainter::reset()
108 {
109     m_pendingCommits.clear();
110     m_pleaseReset = true;
111 }
112
113 void QQuickParticlePainter::setCount(int c)//### TODO: some resizeing so that particles can reallocate on size change instead of recreate
114 {
115     Q_ASSERT(c >= 0); //XXX
116     if (c == m_count)
117         return;
118     m_count = c;
119     emit countChanged();
120     reset();
121 }
122
123 int QQuickParticlePainter::count()
124 {
125     return m_count;
126 }
127
128 void QQuickParticlePainter::calcSystemOffset(bool resetPending)
129 {
130     if (!m_system || !parentItem())
131         return;
132     QPointF lastOffset = m_systemOffset;
133     m_systemOffset = -1 * this->mapFromItem(m_system, QPointF(0.0, 0.0));
134     if (lastOffset != m_systemOffset && !resetPending){
135         //Reload all particles//TODO: Necessary?
136         foreach (const QString &g, m_groups){
137             int gId = m_system->groupIds[g];
138             foreach (QQuickParticleData* d, m_system->groupData[gId]->data)
139                 reload(d);
140         }
141     }
142 }
143 typedef QPair<int,int> intPair;
144 void QQuickParticlePainter::performPendingCommits()
145 {
146     calcSystemOffset();
147     foreach (intPair p, m_pendingCommits)
148         commit(p.first, p.second);
149     m_pendingCommits.clear();
150 }
151
152 QT_END_NAMESPACE