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