src/declarative/util/qdeclarativebehavior.cpp
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    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 
       
    55 QT_BEGIN_NAMESPACE
       
    56 
       
    57 class QDeclarativeBehaviorPrivate : public QObjectPrivate
       
    58 {
       
    59     Q_DECLARE_PUBLIC(QDeclarativeBehavior)
       
    60 public:
       
    61     QDeclarativeBehaviorPrivate() : animation(0), enabled(true), finalized(false) {}
       
    62 
       
    63     QDeclarativeProperty property;
       
    64     QVariant currentValue;
       
    65     QVariant targetValue;
       
    66     QDeclarativeGuard<QDeclarativeAbstractAnimation> animation;
       
    67     bool enabled;
       
    68     bool finalized;
       
    69 };
       
    70 
       
    71 /*!
       
    72     \qmlclass Behavior QDeclarativeBehavior
       
    73     \since 4.7
       
    74     \brief The Behavior element allows you to specify a default animation for a property change.
       
    75 
       
    76     Behaviors provide one way to specify \l{qdeclarativeanimation.html}{animations} in QML.
       
    77 
       
    78     In the example below, the rectangle will use a bounce easing curve over 200 millisecond for any changes to its y property:
       
    79     \code
       
    80     Rectangle {
       
    81         width: 20; height: 20
       
    82         color: "#00ff00"
       
    83         y: 200  // initial value
       
    84         Behavior on y {
       
    85             NumberAnimation {
       
    86                 easing.type: Easing.OutBounce
       
    87                 easing.amplitude: 100
       
    88                 duration: 200
       
    89             }
       
    90         }
       
    91     }
       
    92     \endcode
       
    93 
       
    94     Currently only a single Behavior may be specified for a property;
       
    95     this Behavior can be enabled and disabled via the \l{enabled} property.
       
    96 
       
    97     \sa QtDeclarative
       
    98 */
       
    99 
       
   100 
       
   101 QDeclarativeBehavior::QDeclarativeBehavior(QObject *parent)
       
   102     : QObject(*(new QDeclarativeBehaviorPrivate), parent)
       
   103 {
       
   104 }
       
   105 
       
   106 QDeclarativeBehavior::~QDeclarativeBehavior()
       
   107 {
       
   108 }
       
   109 
       
   110 /*!
       
   111     \qmlproperty Animation Behavior::animation
       
   112     \default
       
   113 
       
   114     The animation to use when the behavior is triggered.
       
   115 */
       
   116 
       
   117 QDeclarativeAbstractAnimation *QDeclarativeBehavior::animation()
       
   118 {
       
   119     Q_D(QDeclarativeBehavior);
       
   120     return d->animation;
       
   121 }
       
   122 
       
   123 void QDeclarativeBehavior::setAnimation(QDeclarativeAbstractAnimation *animation)
       
   124 {
       
   125     Q_D(QDeclarativeBehavior);
       
   126     if (d->animation) {
       
   127         qmlInfo(this) << tr("Cannot change the animation assigned to a Behavior.");
       
   128         return;
       
   129     }
       
   130 
       
   131     d->animation = animation;
       
   132     if (d->animation) {
       
   133         d->animation->setDefaultTarget(d->property);
       
   134         d->animation->setDisableUserControl();
       
   135     }
       
   136 }
       
   137 
       
   138 /*!
       
   139     \qmlproperty bool Behavior::enabled
       
   140     Whether the Behavior will be triggered when the property it is tracking changes.
       
   141 
       
   142     By default a Behavior is enabled.
       
   143 */
       
   144 
       
   145 bool QDeclarativeBehavior::enabled() const
       
   146 {
       
   147     Q_D(const QDeclarativeBehavior);
       
   148     return d->enabled;
       
   149 }
       
   150 
       
   151 void QDeclarativeBehavior::setEnabled(bool enabled)
       
   152 {
       
   153     Q_D(QDeclarativeBehavior);
       
   154     if (d->enabled == enabled)
       
   155         return;
       
   156     d->enabled = enabled;
       
   157     emit enabledChanged();
       
   158 }
       
   159 
       
   160 void QDeclarativeBehavior::write(const QVariant &value)
       
   161 {
       
   162     Q_D(QDeclarativeBehavior);
       
   163     qmlExecuteDeferred(this);
       
   164     if (!d->animation || !d->enabled || !d->finalized) {
       
   165         QDeclarativePropertyPrivate::write(d->property, value, QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding);
       
   166         d->targetValue = value;
       
   167         return;
       
   168     }
       
   169 
       
   170     if (value == d->targetValue)
       
   171         return;
       
   172 
       
   173     d->currentValue = d->property.read();
       
   174     d->targetValue = value;
       
   175 
       
   176     if (d->animation->qtAnimation()->duration() != -1)
       
   177         d->animation->qtAnimation()->stop();
       
   178 
       
   179     QDeclarativeStateOperation::ActionList actions;
       
   180     QDeclarativeAction action;
       
   181     action.property = d->property;
       
   182     action.fromValue = d->currentValue;
       
   183     action.toValue = value;
       
   184     actions << action;
       
   185 
       
   186     QList<QDeclarativeProperty> after;
       
   187     d->animation->transition(actions, after, QDeclarativeAbstractAnimation::Forward);
       
   188     d->animation->qtAnimation()->start();
       
   189     if (!after.contains(d->property))
       
   190         QDeclarativePropertyPrivate::write(d->property, value, QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding);    
       
   191 }
       
   192 
       
   193 void QDeclarativeBehavior::setTarget(const QDeclarativeProperty &property)
       
   194 {
       
   195     Q_D(QDeclarativeBehavior);
       
   196     d->property = property;
       
   197     d->currentValue = property.read();
       
   198     if (d->animation)
       
   199         d->animation->setDefaultTarget(property);
       
   200 
       
   201     QDeclarativeEnginePrivate *engPriv = QDeclarativeEnginePrivate::get(qmlEngine(this));
       
   202     engPriv->registerFinalizedParserStatusObject(this, this->metaObject()->indexOfSlot("componentFinalized()"));
       
   203 }
       
   204 
       
   205 void QDeclarativeBehavior::componentFinalized()
       
   206 {
       
   207     Q_D(QDeclarativeBehavior);
       
   208     d->finalized = true;
       
   209 }
       
   210 
       
   211 QT_END_NAMESPACE