demos/qtdemo/demoitemanimation.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 demonstration applications 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 "demoitemanimation.h"
       
    43 #include "demoitem.h"
       
    44 #include "colors.h"
       
    45 
       
    46 DemoItemAnimation::DemoItemAnimation(DemoItem *item, INOROUT inOrOut)
       
    47 {
       
    48     this->opacityAt0 = 1.0;
       
    49     this->opacityAt1 = 1.0;
       
    50     this->startDelay = 0;
       
    51     this->inOrOut = inOrOut;
       
    52     this->hideOnFinished = false;
       
    53     this->forcePlay = false;
       
    54     this->timeline = new QTimeLine(5000);
       
    55     this->timeline->setFrameRange(0, 2000);
       
    56     this->timeline->setUpdateInterval(int(1000.0/Colors::fps));
       
    57     this->moveOnPlay = false;
       
    58     setTimeLine(this->timeline);
       
    59     setItem(item);
       
    60 }
       
    61 
       
    62 DemoItemAnimation::~DemoItemAnimation()
       
    63 {
       
    64     // Do not delete demoitem. It is not
       
    65     // owned by an animation
       
    66     delete this->timeline;
       
    67 }
       
    68 
       
    69 void DemoItemAnimation::prepare()
       
    70 {
       
    71     this->demoItem()->prepare();
       
    72 }
       
    73 
       
    74 void DemoItemAnimation::setStartPos(const QPointF &pos){
       
    75     this->startPos = pos;
       
    76 }
       
    77 
       
    78 void DemoItemAnimation::setDuration(int duration)
       
    79 {
       
    80     duration = int(duration * Colors::animSpeed);
       
    81     this->timeline->setDuration(duration);
       
    82     this->moveOnPlay = true;
       
    83 }
       
    84 
       
    85 void DemoItemAnimation::setCurrentTime(int ms)
       
    86 {
       
    87     this->timeline->setCurrentTime(ms);
       
    88 }
       
    89 
       
    90 bool DemoItemAnimation::notOwnerOfItem()
       
    91 {
       
    92     return this != demoItem()->currentAnimation;
       
    93 }
       
    94 
       
    95 void DemoItemAnimation::play(bool fromStart, bool force)
       
    96 {
       
    97     this->fromStart = fromStart;
       
    98     this->forcePlay = force;
       
    99 
       
   100     QPointF currPos = this->demoItem()->pos();
       
   101 
       
   102     // If the item that this animation controls in currently under the
       
   103     // control of another animation, stop that animation first
       
   104     if (this->demoItem()->currentAnimation)
       
   105         this->demoItem()->currentAnimation->timeline->stop();
       
   106     this->demoItem()->currentAnimation = this;
       
   107     this->timeline->stop();
       
   108 
       
   109     if (Colors::noAnimations && !this->forcePlay){
       
   110         this->timeline->setCurrentTime(1);
       
   111         this->demoItem()->setPos(this->posAt(1));
       
   112     }
       
   113     else{
       
   114         if (this->demoItem()->isVisible())
       
   115             // If the item is already visible, start the animation from
       
   116             // the items current position rather than from start.
       
   117             this->setPosAt(0.0, currPos);
       
   118         else
       
   119             this->setPosAt(0.0, this->startPos);
       
   120 
       
   121         if (this->fromStart){
       
   122             this->timeline->setCurrentTime(0);
       
   123             this->demoItem()->setPos(this->posAt(0));
       
   124         }
       
   125     }
       
   126 
       
   127     if (this->inOrOut == ANIM_IN)
       
   128         this->demoItem()->setRecursiveVisible(true);
       
   129 
       
   130     if (this->startDelay){
       
   131         QTimer::singleShot(this->startDelay, this, SLOT(playWithoutDelay()));
       
   132         return;
       
   133     }
       
   134     else
       
   135         this->playWithoutDelay();
       
   136 }
       
   137 
       
   138 void DemoItemAnimation::playWithoutDelay()
       
   139 {
       
   140     if (this->moveOnPlay && !(Colors::noAnimations && !this->forcePlay))
       
   141         this->timeline->start();
       
   142     this->demoItem()->animationStarted(this->inOrOut);
       
   143 }
       
   144 
       
   145 void DemoItemAnimation::stop(bool reset)
       
   146 {
       
   147     this->timeline->stop();
       
   148     if (reset)
       
   149         this->demoItem()->setPos(this->posAt(0));
       
   150     if (this->hideOnFinished && !this->moveOnPlay)
       
   151         this->demoItem()->setRecursiveVisible(false);
       
   152     this->demoItem()->animationStopped(this->inOrOut);
       
   153 }
       
   154 
       
   155 void DemoItemAnimation::setRepeat(int nr)
       
   156 {
       
   157     this->timeline->setLoopCount(nr);
       
   158 }
       
   159 
       
   160 void DemoItemAnimation::playReverse()
       
   161 {
       
   162 }
       
   163 
       
   164 bool DemoItemAnimation::running()
       
   165 {
       
   166     return (this->timeLine()->state() == QTimeLine::Running);
       
   167 }
       
   168 
       
   169 bool DemoItemAnimation::runningOrItemLocked()
       
   170 {
       
   171     return (this->running() || this->demoItem()->locked);
       
   172 }
       
   173 
       
   174 void DemoItemAnimation::lockItem(bool state)
       
   175 {
       
   176     this->demoItem()->locked = state;
       
   177 }
       
   178 
       
   179 DemoItem *DemoItemAnimation::demoItem()
       
   180 {
       
   181     return (DemoItem *) this->item();
       
   182 }
       
   183 
       
   184 void DemoItemAnimation::setOpacityAt0(qreal opacity)
       
   185 {
       
   186     this->opacityAt0 = opacity;
       
   187 }
       
   188 
       
   189 void DemoItemAnimation::setOpacityAt1(qreal opacity)
       
   190 {
       
   191     this->opacityAt1 = opacity;
       
   192 }
       
   193 
       
   194 void DemoItemAnimation::setOpacity(qreal step)
       
   195 {
       
   196     DemoItem *demoItem = (DemoItem *) item();
       
   197     demoItem->opacity = this->opacityAt0 + step * step * step * (this->opacityAt1 - this->opacityAt0);
       
   198 }
       
   199 
       
   200 void DemoItemAnimation::afterAnimationStep(qreal step)
       
   201 {
       
   202     if (step == 1.0f){
       
   203         if (this->timeline->loopCount() > 0){
       
   204             // animation finished.
       
   205             if (this->hideOnFinished)
       
   206                 this->demoItem()->setRecursiveVisible(false);
       
   207             this->demoItem()->animationStopped(this->inOrOut);
       
   208         }
       
   209     } else if (Colors::noAnimations && !this->forcePlay){
       
   210         // The animation is not at end, but
       
   211         // the animations should not play, so go to end.
       
   212         this->setStep(1.0f); // will make this method being called recursive.
       
   213     }
       
   214 }
       
   215 
       
   216 
       
   217 
       
   218 
       
   219