1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the demonstration applications of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "demoitemanimation.h"
46 DemoItemAnimation::DemoItemAnimation(DemoItem
*item
, INOROUT inOrOut
)
48 this->opacityAt0
= 1.0;
49 this->opacityAt1
= 1.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
);
62 DemoItemAnimation::~DemoItemAnimation()
64 // Do not delete demoitem. It is not
65 // owned by an animation
66 delete this->timeline
;
69 void DemoItemAnimation::prepare()
71 this->demoItem()->prepare();
74 void DemoItemAnimation::setStartPos(const QPointF
&pos
){
78 void DemoItemAnimation::setDuration(int duration
)
80 duration
= int(duration
* Colors::animSpeed
);
81 this->timeline
->setDuration(duration
);
82 this->moveOnPlay
= true;
85 void DemoItemAnimation::setCurrentTime(int ms
)
87 this->timeline
->setCurrentTime(ms
);
90 bool DemoItemAnimation::notOwnerOfItem()
92 return this != demoItem()->currentAnimation
;
95 void DemoItemAnimation::play(bool fromStart
, bool force
)
97 this->fromStart
= fromStart
;
98 this->forcePlay
= force
;
100 QPointF currPos
= this->demoItem()->pos();
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();
109 if (Colors::noAnimations
&& !this->forcePlay
){
110 this->timeline
->setCurrentTime(1);
111 this->demoItem()->setPos(this->posAt(1));
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
);
119 this->setPosAt(0.0, this->startPos
);
121 if (this->fromStart
){
122 this->timeline
->setCurrentTime(0);
123 this->demoItem()->setPos(this->posAt(0));
127 if (this->inOrOut
== ANIM_IN
)
128 this->demoItem()->setRecursiveVisible(true);
130 if (this->startDelay
){
131 QTimer::singleShot(this->startDelay
, this, SLOT(playWithoutDelay()));
135 this->playWithoutDelay();
138 void DemoItemAnimation::playWithoutDelay()
140 if (this->moveOnPlay
&& !(Colors::noAnimations
&& !this->forcePlay
))
141 this->timeline
->start();
142 this->demoItem()->animationStarted(this->inOrOut
);
145 void DemoItemAnimation::stop(bool reset
)
147 this->timeline
->stop();
149 this->demoItem()->setPos(this->posAt(0));
150 if (this->hideOnFinished
&& !this->moveOnPlay
)
151 this->demoItem()->setRecursiveVisible(false);
152 this->demoItem()->animationStopped(this->inOrOut
);
155 void DemoItemAnimation::setRepeat(int nr
)
157 this->timeline
->setLoopCount(nr
);
160 void DemoItemAnimation::playReverse()
164 bool DemoItemAnimation::running()
166 return (this->timeLine()->state() == QTimeLine::Running
);
169 bool DemoItemAnimation::runningOrItemLocked()
171 return (this->running() || this->demoItem()->locked
);
174 void DemoItemAnimation::lockItem(bool state
)
176 this->demoItem()->locked
= state
;
179 DemoItem
*DemoItemAnimation::demoItem()
181 return (DemoItem
*) this->item();
184 void DemoItemAnimation::setOpacityAt0(qreal opacity
)
186 this->opacityAt0
= opacity
;
189 void DemoItemAnimation::setOpacityAt1(qreal opacity
)
191 this->opacityAt1
= opacity
;
194 void DemoItemAnimation::setOpacity(qreal step
)
196 DemoItem
*demoItem
= (DemoItem
*) item();
197 demoItem
->opacity
= this->opacityAt0
+ step
* step
* step
* (this->opacityAt1
- this->opacityAt0
);
200 void DemoItemAnimation::afterAnimationStep(qreal step
)
203 if (this->timeline
->loopCount() > 0){
204 // animation finished.
205 if (this->hideOnFinished
)
206 this->demoItem()->setRecursiveVisible(false);
207 this->demoItem()->animationStopped(this->inOrOut
);
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.