src/declarative/util/qdeclarativetransition.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/qdeclarativestate_p.h"
       
    43 #include "private/qdeclarativestategroup_p.h"
       
    44 #include "private/qdeclarativestate_p_p.h"
       
    45 #include "private/qdeclarativestateoperations_p.h"
       
    46 #include "private/qdeclarativeanimation_p.h"
       
    47 #include "private/qdeclarativeanimation_p_p.h"
       
    48 #include "private/qdeclarativetransitionmanager_p_p.h"
       
    49 
       
    50 #include <QParallelAnimationGroup>
       
    51 
       
    52 QT_BEGIN_NAMESPACE
       
    53 
       
    54 /*!
       
    55     \qmlclass Transition QDeclarativeTransition
       
    56     \since 4.7
       
    57     \brief The Transition element defines animated transitions that occur on state changes.
       
    58 
       
    59     \sa {qmlstates}{States}, {state-transitions}{Transitions}, {QtDeclarative}
       
    60 */
       
    61 
       
    62 /*!
       
    63     \internal
       
    64     \class QDeclarativeTransition
       
    65     \brief The QDeclarativeTransition class allows you to define animated transitions that occur on state changes.
       
    66 */
       
    67 
       
    68 //ParallelAnimationWrapper allows us to do a "callback" when the animation finishes, rather than connecting
       
    69 //and disconnecting signals and slots frequently
       
    70 class ParallelAnimationWrapper : public QParallelAnimationGroup
       
    71 {
       
    72     Q_OBJECT
       
    73 public:
       
    74     ParallelAnimationWrapper(QObject *parent = 0) : QParallelAnimationGroup(parent) {}
       
    75     QDeclarativeTransitionPrivate *trans;
       
    76 protected:
       
    77     virtual void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState);
       
    78 };
       
    79 
       
    80 class QDeclarativeTransitionPrivate : public QObjectPrivate
       
    81 {
       
    82     Q_DECLARE_PUBLIC(QDeclarativeTransition)
       
    83 public:
       
    84     QDeclarativeTransitionPrivate() 
       
    85     : fromState(QLatin1String("*")), toState(QLatin1String("*")), 
       
    86       reversed(false), reversible(false), endState(0)
       
    87     {
       
    88         group.trans = this;
       
    89     }
       
    90 
       
    91     QString fromState;
       
    92     QString toState;
       
    93     bool reversed;
       
    94     bool reversible;
       
    95     ParallelAnimationWrapper group;
       
    96     QDeclarativeTransitionManager *endState;
       
    97 
       
    98     void complete()
       
    99     {
       
   100         endState->complete();
       
   101     }
       
   102     static void append_animation(QDeclarativeListProperty<QDeclarativeAbstractAnimation> *list, QDeclarativeAbstractAnimation *a);
       
   103     QList<QDeclarativeAbstractAnimation *> animations;
       
   104 };
       
   105 
       
   106 void QDeclarativeTransitionPrivate::append_animation(QDeclarativeListProperty<QDeclarativeAbstractAnimation> *list, QDeclarativeAbstractAnimation *a)
       
   107 {
       
   108     QDeclarativeTransition *q = static_cast<QDeclarativeTransition *>(list->object);
       
   109     q->d_func()->animations.append(a);
       
   110     q->d_func()->group.addAnimation(a->qtAnimation());
       
   111     a->setDisableUserControl();
       
   112 }
       
   113 
       
   114 void ParallelAnimationWrapper::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
       
   115 {
       
   116     QParallelAnimationGroup::updateState(newState, oldState);
       
   117     if (newState == Stopped && (duration() == -1
       
   118         || (direction() == QAbstractAnimation::Forward && currentLoopTime() == duration())
       
   119         || (direction() == QAbstractAnimation::Backward && currentLoopTime() == 0)))
       
   120     {
       
   121         trans->complete();
       
   122     }
       
   123 }
       
   124 
       
   125 
       
   126 
       
   127 QDeclarativeTransition::QDeclarativeTransition(QObject *parent)
       
   128     : QObject(*(new QDeclarativeTransitionPrivate), parent)
       
   129 {
       
   130 }
       
   131 
       
   132 QDeclarativeTransition::~QDeclarativeTransition()
       
   133 {
       
   134 }
       
   135 
       
   136 void QDeclarativeTransition::stop()
       
   137 {
       
   138     Q_D(QDeclarativeTransition);
       
   139     d->group.stop();
       
   140 }
       
   141 
       
   142 void QDeclarativeTransition::setReversed(bool r)
       
   143 {
       
   144     Q_D(QDeclarativeTransition);
       
   145     d->reversed = r;
       
   146 }
       
   147 
       
   148 void QDeclarativeTransition::prepare(QDeclarativeStateOperation::ActionList &actions,
       
   149                             QList<QDeclarativeProperty> &after,
       
   150                             QDeclarativeTransitionManager *endState)
       
   151 {
       
   152     Q_D(QDeclarativeTransition);
       
   153 
       
   154     qmlExecuteDeferred(this);
       
   155 
       
   156     if (d->reversed) {
       
   157         for (int ii = d->animations.count() - 1; ii >= 0; --ii) {
       
   158             d->animations.at(ii)->transition(actions, after, QDeclarativeAbstractAnimation::Backward);
       
   159         }
       
   160     } else {
       
   161         for (int ii = 0; ii < d->animations.count(); ++ii) {
       
   162             d->animations.at(ii)->transition(actions, after, QDeclarativeAbstractAnimation::Forward);
       
   163         }
       
   164     }
       
   165 
       
   166     d->endState = endState;
       
   167     d->group.setDirection(d->reversed ? QAbstractAnimation::Backward : QAbstractAnimation::Forward);
       
   168     d->group.start();
       
   169 }
       
   170 
       
   171 /*!
       
   172     \qmlproperty string Transition::from
       
   173     \qmlproperty string Transition::to
       
   174     These properties are selectors indicating which state changes should trigger the transition.
       
   175 
       
   176     from is used in conjunction with to to determine when a transition should
       
   177     be applied. By default from and to are both "*" (any state). In the following example,
       
   178     the transition is applied when changing from state1 to state2.
       
   179     \code
       
   180     Transition {
       
   181         from: "state1"
       
   182         to: "state2"
       
   183         ...
       
   184     }
       
   185     \endcode
       
   186 */
       
   187 QString QDeclarativeTransition::fromState() const
       
   188 {
       
   189     Q_D(const QDeclarativeTransition);
       
   190     return d->fromState;
       
   191 }
       
   192 
       
   193 void QDeclarativeTransition::setFromState(const QString &f)
       
   194 {
       
   195     Q_D(QDeclarativeTransition);
       
   196     if (f == d->fromState)
       
   197         return;
       
   198 
       
   199     d->fromState = f;
       
   200     emit fromChanged();
       
   201 }
       
   202 
       
   203 /*!
       
   204     \qmlproperty bool Transition::reversible
       
   205     This property holds whether the transition should be automatically reversed when the conditions that triggered this transition are reversed.
       
   206 
       
   207     The default value is false.
       
   208 */
       
   209 bool QDeclarativeTransition::reversible() const
       
   210 {
       
   211     Q_D(const QDeclarativeTransition);
       
   212     return d->reversible;
       
   213 }
       
   214 
       
   215 void QDeclarativeTransition::setReversible(bool r)
       
   216 {
       
   217     Q_D(QDeclarativeTransition);
       
   218     if (r == d->reversible)
       
   219         return;
       
   220 
       
   221     d->reversible = r;
       
   222     emit reversibleChanged();
       
   223 }
       
   224 
       
   225 QString QDeclarativeTransition::toState() const
       
   226 {
       
   227     Q_D(const QDeclarativeTransition);
       
   228     return d->toState;
       
   229 }
       
   230 
       
   231 void QDeclarativeTransition::setToState(const QString &t)
       
   232 {
       
   233     Q_D(QDeclarativeTransition);
       
   234     if (t == d->toState)
       
   235         return;
       
   236 
       
   237     d->toState = t;
       
   238     emit toChanged();
       
   239 }
       
   240 
       
   241 /*!
       
   242     \qmlproperty list<Animation> Transition::animations
       
   243     \default
       
   244     This property holds a list of the animations to be run for this transition.
       
   245 
       
   246     The top-level animations are run in parallel. To run them sequentially,
       
   247     you can create a single SequentialAnimation which contains all the animations,
       
   248     and assign that to animations the animations property.
       
   249     \default
       
   250 */
       
   251 QDeclarativeListProperty<QDeclarativeAbstractAnimation> QDeclarativeTransition::animations()
       
   252 {
       
   253     Q_D(QDeclarativeTransition);
       
   254     return QDeclarativeListProperty<QDeclarativeAbstractAnimation>(this, &d->animations, QDeclarativeTransitionPrivate::append_animation);
       
   255 }
       
   256 
       
   257 QT_END_NAMESPACE
       
   258 
       
   259 #include <qdeclarativetransition.moc>