source: trunk/demos/qtdemo/demoitemanimation.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 6.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the demonstration applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "demoitemanimation.h"
43#include "demoitem.h"
44#include "colors.h"
45
46DemoItemAnimation::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
62DemoItemAnimation::~DemoItemAnimation()
63{
64 // Do not delete demoitem. It is not
65 // owned by an animation
66 delete this->timeline;
67}
68
69void DemoItemAnimation::prepare()
70{
71 this->demoItem()->prepare();
72}
73
74void DemoItemAnimation::setStartPos(const QPointF &pos){
75 this->startPos = pos;
76}
77
78void DemoItemAnimation::setDuration(int duration)
79{
80 duration = int(duration * Colors::animSpeed);
81 this->timeline->setDuration(duration);
82 this->moveOnPlay = true;
83}
84
85void DemoItemAnimation::setCurrentTime(int ms)
86{
87 this->timeline->setCurrentTime(ms);
88}
89
90bool DemoItemAnimation::notOwnerOfItem()
91{
92 return this != demoItem()->currentAnimation;
93}
94
95void 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
138void DemoItemAnimation::playWithoutDelay()
139{
140 if (this->moveOnPlay && !(Colors::noAnimations && !this->forcePlay))
141 this->timeline->start();
142 this->demoItem()->animationStarted(this->inOrOut);
143}
144
145void 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
155void DemoItemAnimation::setRepeat(int nr)
156{
157 this->timeline->setLoopCount(nr);
158}
159
160void DemoItemAnimation::playReverse()
161{
162}
163
164bool DemoItemAnimation::running()
165{
166 return (this->timeLine()->state() == QTimeLine::Running);
167}
168
169bool DemoItemAnimation::runningOrItemLocked()
170{
171 return (this->running() || this->demoItem()->locked);
172}
173
174void DemoItemAnimation::lockItem(bool state)
175{
176 this->demoItem()->locked = state;
177}
178
179DemoItem *DemoItemAnimation::demoItem()
180{
181 return (DemoItem *) this->item();
182}
183
184void DemoItemAnimation::setOpacityAt0(qreal opacity)
185{
186 this->opacityAt0 = opacity;
187}
188
189void DemoItemAnimation::setOpacityAt1(qreal opacity)
190{
191 this->opacityAt1 = opacity;
192}
193
194void DemoItemAnimation::setOpacity(qreal step)
195{
196 DemoItem *demoItem = (DemoItem *) item();
197 demoItem->opacity = this->opacityAt0 + step * step * step * (this->opacityAt1 - this->opacityAt0);
198}
199
200void 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
Note: See TracBrowser for help on using the repository browser.