| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the demonstration applications of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 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 |  | 
|---|