d9666bf2644d0642a688c61650b540fd579d3fec
[profile/ivi/qtdeclarative.git] / src / quick / particles / qquickparticlegroup.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 "qquickparticlegroup_p.h"
43
44 /*!
45     \qmlclass ParticleGroup QQuickParticleGroup
46     \inqmlmodule QtQuick.Particles 2
47     \brief ParticleGroup elements allow you to set attributes on a logical particle group.
48
49     This element allows you to set timed transitions on particle groups.
50
51     You can also use this element to group particle system elements related to the logical
52     particle group. Emitters, Affectors and Painters set as direct children of a ParticleGroup
53     will automatically apply to that logical particle group. TrailEmitters will automatically follow
54     the group.
55
56     If a ParticleGroup element is not defined for a group, the group will function normally as if
57     none of the transition properties were set.
58 */
59 /*!
60     \qmlproperty ParticleSystem QtQuick.Particles2::ParticleGroup::system
61     This is the system which will contain the group.
62
63     If the ParticleGroup is a direct child of a ParticleSystem, it will automatically be associated with it.
64 */
65 /*!
66     \qmlproperty string QtQuick.Particles2::ParticleGroup::name
67     This is the name of the particle group, and how it is generally referred to by other elements.
68
69     If elements refer to a name which does not have an explicit ParticleGroup created, it will
70     work normally (with no transitions specified for the group). If you do not need to assign
71     duration based transitions to a group, you do not need to create a ParticleGroup with that name (although you may).
72 */
73 /*!
74     \qmlproperty int QtQuick.Particles2::ParticleGroup::duration
75     The time in milliseconds before the group will attempt to transition.
76
77 */
78 /*!
79     \qmlproperty ParticleSystem QtQuick.Particles2::ParticleGroup::durationVariation
80     The maximum number of milliseconds that the duration of the transition cycle varies per particle in the group.
81
82     Default value is zero.
83 */
84 /*!
85     \qmlproperty ParticleSystem QtQuick.Particles2::ParticleGroup::to
86     The weighted list of transitions valid for this group.
87
88     If the chosen transition stays in this group, another duration (+/- up to durationVariation)
89     milliseconds will occur before another transition is attempted.
90 */
91
92 QQuickParticleGroup::QQuickParticleGroup(QObject* parent)
93     : QQuickStochasticState(parent)
94     , m_system(0)
95 {
96
97 }
98
99 void delayedRedirect(QDeclarativeListProperty<QObject> *prop, QObject *value)
100 {
101     QQuickParticleGroup* pg = qobject_cast<QQuickParticleGroup*>(prop->object);
102     if (pg)
103         pg->delayRedirect(value);
104 }
105
106 QDeclarativeListProperty<QObject> QQuickParticleGroup::particleChildren()
107 {
108     QQuickParticleSystem* system = qobject_cast<QQuickParticleSystem*>(parent());
109     if (system)
110         return QDeclarativeListProperty<QObject>(this, 0, &QQuickParticleSystem::statePropertyRedirect);
111     else
112         return QDeclarativeListProperty<QObject>(this, 0, &delayedRedirect);
113 }
114
115 void QQuickParticleGroup::setSystem(QQuickParticleSystem* arg)
116 {
117     if (m_system != arg) {
118         m_system = arg;
119         m_system->registerParticleGroup(this);
120         performDelayedRedirects();
121         emit systemChanged(arg);
122     }
123 }
124
125 void QQuickParticleGroup::delayRedirect(QObject *obj)
126 {
127     m_delayedRedirects << obj;
128 }
129
130 void QQuickParticleGroup::performDelayedRedirects()
131 {
132     if (!m_system)
133         return;
134     foreach (QObject* obj, m_delayedRedirects)
135         m_system->stateRedirect(this, m_system, obj);
136
137     m_delayedRedirects.clear();
138 }
139
140 void QQuickParticleGroup::componentComplete(){
141     if (!m_system && qobject_cast<QQuickParticleSystem*>(parent()))
142         setSystem(qobject_cast<QQuickParticleSystem*>(parent()));
143 }