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