diff options
Diffstat (limited to 'src/declarative/extra/qmlbehavior.cpp')
-rw-r--r-- | src/declarative/extra/qmlbehavior.cpp | 179 |
1 files changed, 36 insertions, 143 deletions
diff --git a/src/declarative/extra/qmlbehavior.cpp b/src/declarative/extra/qmlbehavior.cpp index 7784ef5..e30bd39 100644 --- a/src/declarative/extra/qmlbehavior.cpp +++ b/src/declarative/extra/qmlbehavior.cpp @@ -44,180 +44,77 @@ #include "qmltransition.h" #include "qmlbehavior.h" #include <QtDeclarative/qmlcontext.h> +#include <QtDeclarative/qmlinfo.h> #include <QtCore/qparallelanimationgroup.h> QT_BEGIN_NAMESPACE QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,Behavior,QmlBehavior) -class QmlBehaviorData : public QObject -{ -Q_OBJECT -public: - QmlBehaviorData(QObject *parent) - : QObject(parent) {} - - Q_PROPERTY(QVariant endValue READ endValue NOTIFY valuesChanged) - Q_PROPERTY(QVariant startValue READ startValue NOTIFY valuesChanged) - QVariant endValue() const { return e; } - QVariant startValue() const { return s; } - - QVariant e; - QVariant s; - -Q_SIGNALS: - void valuesChanged(); - -private: - friend class QmlBehavior; -}; - class QmlBehaviorPrivate : public QObjectPrivate { + Q_DECLARE_PUBLIC(QmlBehavior) public: - QmlBehaviorPrivate() : operations(this) {} + QmlBehaviorPrivate() : animation(0) {} + QmlMetaProperty property; QVariant currentValue; - - QVariant fromValue; - QVariant toValue; - class AnimationList : public QmlConcreteList<QmlAbstractAnimation *> - { - public: - AnimationList(QmlBehaviorPrivate *parent) : _parent(parent) {} - virtual void append(QmlAbstractAnimation *a) - { - QmlConcreteList<QmlAbstractAnimation *>::append(a); - _parent->group->addAnimation(a->qtAnimation()); - if (_parent->property.isValid()) { - a->setTarget(_parent->property); - } - } - virtual void clear() { QmlConcreteList<QmlAbstractAnimation *>::clear(); } //### - private: - QmlBehaviorPrivate *_parent; - }; - AnimationList operations; - QParallelAnimationGroup *group; + QmlAbstractAnimation *animation; }; -/*! - \qmlclass Behavior QmlBehavior - \brief The Behavior element allows you to specify a default animation for a property change. - - In example below, the rect will use a bounce easing curve over 200 millisecond for any changes to its y property: - \code - Rectangle { - width: 20; height: 20 - color: "#00ff00" - y: 200 //initial value - y: Behavior { - NumberAnimation { - easing: "easeOutBounce(amplitude:100)" - duration: 200 - } - } - } - \endcode -*/ - QmlBehavior::QmlBehavior(QObject *parent) -: QObject(*(new QmlBehaviorPrivate), parent) -{ - Q_D(QmlBehavior); - d->group = new QParallelAnimationGroup; - QFx_setParent_noEvent(d->group, this); -} - -/*! - \qmlproperty QVariant Behavior::from - This property holds a selector specifying a starting value for the behavior. - - If you only want the behavior to apply when the change starts at a - specific value you can specify fromValue. This selector is used in conjunction - with the to selector. -*/ - -QVariant QmlBehavior::fromValue() const + : QObject(*(new QmlBehaviorPrivate), parent) { - Q_D(const QmlBehavior); - return d->fromValue; } -void QmlBehavior::setFromValue(const QVariant &v) +QmlAbstractAnimation *QmlBehavior::animation() { Q_D(QmlBehavior); - d->fromValue = v; + return d->animation; } -/*! - \qmlproperty QVariant Behavior::to - This property holds a selector specifying a ending value for the behavior. - - If you only want the behavior to apply when the change ends at a - specific value you can specify toValue. This selector is used in conjunction - with the from selector. -*/ - -QVariant QmlBehavior::toValue() const -{ - Q_D(const QmlBehavior); - return d->toValue; -} - -void QmlBehavior::setToValue(const QVariant &v) +void QmlBehavior::setAnimation(QmlAbstractAnimation *animation) { Q_D(QmlBehavior); - d->toValue = v; -} + if (d->animation) { + qmlInfo(this) << "Can't change the animation assigned to a Behavior."; + return; + } -QmlList<QmlAbstractAnimation *>* QmlBehavior::operations() -{ - Q_D(QmlBehavior); - return &d->operations; + d->animation = animation; + if (d->animation) + d->animation->setTarget(d->property); } QmlBehavior::~QmlBehavior() { - //### do we need any other cleanup here? } -bool QmlBehavior::_ignore = false; -void QmlBehavior::propertyValueChanged() +void QmlBehavior::write(const QVariant &value) { Q_D(QmlBehavior); - if (_ignore) + if (!d->animation) { + d->property.write(value, QmlMetaProperty::BypassInterceptor | QmlMetaProperty::DontRemoveBinding); return; + } - QVariant newValue = d->property.read(); - - if ((!fromValue().isValid() || fromValue() == d->currentValue) && - (!toValue().isValid() || toValue() == newValue)) { - - //### does this clean up everything needed? - d->group->stop(); - - QmlStateOperation::ActionList actions; - Action action; - action.property = d->property; - action.fromValue = d->currentValue; - action.toValue = newValue; - actions << action; + d->currentValue = d->property.read(); - _ignore = true; - d->property.write(d->currentValue); + d->animation->qtAnimation()->stop(); - QList<QmlMetaProperty> after; - for (int ii = 0; ii < d->operations.count(); ++ii) { - d->operations.at(ii)->transition(actions, after, QmlAbstractAnimation::Forward); - } - d->group->start(); - if (!after.contains(d->property)) - d->property.write(newValue); - _ignore = false; - } + QmlStateOperation::ActionList actions; + Action action; + action.property = d->property; + action.fromValue = d->currentValue; + action.toValue = value; + actions << action; - d->currentValue = newValue; + QList<QmlMetaProperty> after; + if (d->animation) + d->animation->transition(actions, after, QmlAbstractAnimation::Forward); + d->animation->qtAnimation()->start(); + if (!after.contains(d->property)) + d->property.write(value, QmlMetaProperty::BypassInterceptor | QmlMetaProperty::DontRemoveBinding); } void QmlBehavior::setTarget(const QmlMetaProperty &property) @@ -225,12 +122,8 @@ void QmlBehavior::setTarget(const QmlMetaProperty &property) Q_D(QmlBehavior); d->property = property; d->currentValue = property.read(); - d->property.connectNotifier(this, SLOT(propertyValueChanged())); - for (int ii = 0; ii < d->operations.count(); ++ii) { - d->operations.at(ii)->setTarget(property); - } + if (d->animation) + d->animation->setTarget(property); } QT_END_NAMESPACE - -#include "qmlbehavior.moc" |