Qt 4.8
qdeclarativebehavior.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "private/qdeclarativebehavior_p.h"
43 
44 #include "private/qdeclarativeanimation_p.h"
45 #include "private/qdeclarativetransition_p.h"
46 
47 #include <qdeclarativecontext.h>
48 #include <qdeclarativeinfo.h>
49 #include <qdeclarativeproperty_p.h>
50 #include <qdeclarativeguard_p.h>
51 #include <qdeclarativeengine_p.h>
52 
53 #include <private/qobject_p.h>
54 
56 
58 {
60 public:
62  , blockRunningChanged(false) {}
63 
68  bool enabled;
69  bool finalized;
71 };
72 
105  : QObject(*(new QDeclarativeBehaviorPrivate), parent)
106 {
107 }
108 
110 {
111 }
112 
124 {
126  return d->animation;
127 }
128 
130 {
132  if (d->animation) {
133  qmlInfo(this) << tr("Cannot change the animation assigned to a Behavior.");
134  return;
135  }
136 
137  d->animation = animation;
138  if (d->animation) {
139  d->animation->setDefaultTarget(d->property);
140  d->animation->setDisableUserControl();
141  connect(d->animation->qtAnimation(),
143  this,
145  }
146 }
147 
148 
150 {
152  if (!d->blockRunningChanged)
153  d->animation->notifyRunningChanged(newState == QAbstractAnimation::Running);
154 }
155 
156 
170 {
171  Q_D(const QDeclarativeBehavior);
172  return d->enabled;
173 }
174 
176 {
178  if (d->enabled == enabled)
179  return;
180  d->enabled = enabled;
182 }
183 
185 {
187  qmlExecuteDeferred(this);
188  if (!d->animation || !d->enabled || !d->finalized) {
190  d->targetValue = value;
191  return;
192  }
193 
194  if (d->animation->isRunning() && value == d->targetValue)
195  return;
196 
197  d->currentValue = d->property.read();
198  d->targetValue = value;
199 
200  if (d->animation->qtAnimation()->duration() != -1
201  && d->animation->qtAnimation()->state() != QAbstractAnimation::Stopped) {
202  d->blockRunningChanged = true;
203  d->animation->qtAnimation()->stop();
204  }
205 
207  QDeclarativeAction action;
208  action.property = d->property;
209  action.fromValue = d->currentValue;
210  action.toValue = value;
211  actions << action;
212 
214  d->animation->transition(actions, after, QDeclarativeAbstractAnimation::Forward);
215  d->animation->qtAnimation()->start();
216  d->blockRunningChanged = false;
217  if (!after.contains(d->property))
219 }
220 
222 {
224  d->property = property;
225  d->currentValue = property.read();
226  if (d->animation)
227  d->animation->setDefaultTarget(property);
228 
230  engPriv->registerFinalizedParserStatusObject(this, this->metaObject()->indexOfSlot("componentFinalized()"));
231 }
232 
234 {
236  d->finalized = true;
237 }
238 
void setEnabled(bool enabled)
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
void setAnimation(QDeclarativeAbstractAnimation *)
double d
Definition: qnumeric_p.h:62
QDeclarativeProperty property
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
State
This enum describes the state of the animation.
virtual void setTarget(const QDeclarativeProperty &)
Set the target property for the value interceptor.
#define SLOT(a)
Definition: qobjectdefs.h:226
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
static QDeclarativeEnginePrivate * get(QDeclarativeEngine *e)
virtual void write(const QVariant &value)
This method will be called when a new value is assigned to the property being intercepted.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
Q_DECLARATIVE_EXPORT void qmlExecuteDeferred(QObject *)
#define emit
Definition: qobjectdefs.h:76
void registerFinalizedParserStatusObject(QObject *obj, int index)
bool enabled() const
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
Q_DECLARATIVE_EXPORT QDeclarativeEngine * qmlEngine(const QObject *)
QDeclarativeGuard< QDeclarativeAbstractAnimation > animation
void qtAnimationStateChanged(QAbstractAnimation::State, QAbstractAnimation::State)
QVariant property(const char *name) const
Returns the value of the object&#39;s name property.
Definition: qobject.cpp:3807
QObject * parent
Definition: qobject.h:92
The QDeclarativeProperty class abstracts accessing properties on objects created from QML...
QDeclarativeInfo qmlInfo(const QObject *me)
QDeclarativeBehavior(QObject *parent=0)
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
QDeclarativeAbstractAnimation * animation()
static bool write(QObject *, const QDeclarativePropertyCache::Data &, const QVariant &, QDeclarativeContextData *, WriteFlags flags=0)