2009-10-15 21:36:38 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Mehmet Ali Akmanalp <makmanalp@wpi.edu>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "animationgroup.h"
|
|
|
|
#include <QMapIterator>
|
|
|
|
#include <QParallelAnimationGroup>
|
|
|
|
#include <QSequentialAnimationGroup>
|
2009-12-03 20:30:02 +01:00
|
|
|
#include <QDebug>
|
2009-10-15 21:36:38 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2009-10-21 06:15:52 +02:00
|
|
|
class AnimationGroupPrivate
|
|
|
|
{
|
|
|
|
public:
|
2009-12-03 20:30:02 +01:00
|
|
|
AnimationGroupPrivate(): forwards(QAbstractAnimation::Forward),
|
|
|
|
parallel(false),
|
|
|
|
dirtyFlag(false), duration(0),
|
|
|
|
anim(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
QAbstractAnimation::Direction forwards;
|
2009-10-21 06:15:52 +02:00
|
|
|
bool parallel;
|
2009-12-03 20:30:02 +01:00
|
|
|
bool dirtyFlag;
|
|
|
|
int duration;
|
|
|
|
QAnimationGroup *anim;
|
2009-10-21 06:15:52 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-10-15 21:36:38 +02:00
|
|
|
AnimationGroup::AnimationGroup(QObject* parent)
|
2009-12-03 20:30:02 +01:00
|
|
|
: QAbstractAnimation(parent),
|
2009-10-21 06:15:52 +02:00
|
|
|
d(new AnimationGroupPrivate)
|
2009-10-15 21:36:38 +02:00
|
|
|
{
|
2009-12-03 20:30:02 +01:00
|
|
|
d->anim = new QSequentialAnimationGroup(this);
|
2009-10-15 21:36:38 +02:00
|
|
|
}
|
|
|
|
|
2009-10-16 18:36:22 +02:00
|
|
|
AnimationGroup::~AnimationGroup()
|
|
|
|
{
|
2009-10-21 06:15:52 +02:00
|
|
|
delete d;
|
2009-10-15 21:36:38 +02:00
|
|
|
}
|
|
|
|
|
2009-12-03 20:30:02 +01:00
|
|
|
QAbstractAnimation::Direction AnimationGroup::direction() const
|
2009-10-16 18:36:22 +02:00
|
|
|
{
|
2009-12-03 20:30:02 +01:00
|
|
|
return d->forwards;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationGroup::setParallel(bool parallel)
|
|
|
|
{
|
|
|
|
if (isParallel() == parallel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->parallel = parallel;
|
|
|
|
|
|
|
|
QAnimationGroup *newGroup;
|
|
|
|
if (parallel)
|
|
|
|
newGroup = new QParallelAnimationGroup(this);
|
|
|
|
else
|
|
|
|
newGroup = new QSequentialAnimationGroup(this);
|
|
|
|
|
|
|
|
while (d->anim->animationCount()) {
|
2009-12-03 20:55:31 +01:00
|
|
|
newGroup->addAnimation(d->anim->takeAnimationAt(0));
|
2009-12-03 20:30:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
delete d->anim;
|
|
|
|
d->anim = newGroup;
|
2009-10-15 21:36:38 +02:00
|
|
|
}
|
|
|
|
|
2009-10-16 18:36:22 +02:00
|
|
|
bool AnimationGroup::isParallel() const
|
|
|
|
{
|
2009-10-21 06:15:52 +02:00
|
|
|
return d->parallel;
|
2009-10-15 21:36:38 +02:00
|
|
|
}
|
|
|
|
|
2009-12-03 20:30:02 +01:00
|
|
|
int AnimationGroup::add(QAbstractAnimation* elem)
|
2009-10-16 18:36:22 +02:00
|
|
|
{
|
2009-12-03 20:30:02 +01:00
|
|
|
d->anim->addAnimation(elem);
|
|
|
|
calculateGroupDuration();
|
|
|
|
return d->anim->animationCount();
|
2009-10-15 21:36:38 +02:00
|
|
|
}
|
|
|
|
|
2009-12-03 20:30:02 +01:00
|
|
|
void AnimationGroup::remove(QAbstractAnimation* elem)
|
2009-10-21 09:51:11 +02:00
|
|
|
{
|
2009-12-03 20:30:02 +01:00
|
|
|
d->anim->removeAnimation(elem);
|
|
|
|
calculateGroupDuration();
|
2009-10-21 09:51:11 +02:00
|
|
|
}
|
|
|
|
|
2009-12-03 20:30:02 +01:00
|
|
|
QAbstractAnimation* AnimationGroup::at(int id) const
|
2009-10-16 18:36:22 +02:00
|
|
|
{
|
2009-12-03 20:30:02 +01:00
|
|
|
return d->anim->animationAt(id);
|
2009-10-15 21:36:38 +02:00
|
|
|
}
|
|
|
|
|
2009-10-16 18:36:22 +02:00
|
|
|
void AnimationGroup::remove(int id)
|
|
|
|
{
|
2009-12-03 20:30:02 +01:00
|
|
|
//This used to save some code...
|
|
|
|
//d->anim->takeAnimationAt(id);
|
|
|
|
d->anim->removeAnimation(d->anim->animationAt(id));
|
|
|
|
calculateGroupDuration();
|
|
|
|
}
|
2009-10-21 06:15:52 +02:00
|
|
|
|
2009-12-03 20:30:02 +01:00
|
|
|
void AnimationGroup::start(QAbstractAnimation::DeletionPolicy policy)
|
|
|
|
{
|
|
|
|
if (d->anim) {
|
|
|
|
d->anim->setDirection(d->forwards);
|
|
|
|
d->anim->start(policy);
|
|
|
|
}
|
2009-10-15 21:36:38 +02:00
|
|
|
}
|
|
|
|
|
2009-12-03 20:30:02 +01:00
|
|
|
void AnimationGroup::updateCurrentTime(int currentTime)
|
2009-10-16 18:36:22 +02:00
|
|
|
{
|
2009-12-03 20:30:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* XXX: not sure if is a bug in my code or Qt, but 'start()' is not being
|
|
|
|
* called when the animation is inside of an animatin group.
|
|
|
|
* The solution for while is to explicitly call it in 'updateCurrentTime'
|
|
|
|
* and use this flag for control.
|
|
|
|
*/
|
|
|
|
if (!d->dirtyFlag) {
|
|
|
|
d->dirtyFlag = true;
|
|
|
|
start();
|
2009-10-15 21:36:38 +02:00
|
|
|
}
|
|
|
|
|
2009-12-03 20:30:02 +01:00
|
|
|
if (d->forwards == QAbstractAnimation::Forward) {
|
|
|
|
if (currentTime == duration()) {
|
|
|
|
d->dirtyFlag = false;
|
|
|
|
}
|
|
|
|
} else if (d->forwards == QAbstractAnimation::Backward) {
|
|
|
|
if (currentTime == 0) {
|
|
|
|
d->dirtyFlag = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationGroup::calculateGroupDuration()
|
|
|
|
{
|
|
|
|
QAbstractAnimation *tmp;
|
|
|
|
d->duration = 0;
|
2009-10-21 06:15:52 +02:00
|
|
|
if (d->parallel) {
|
2009-12-03 20:30:02 +01:00
|
|
|
for (int i = 0; i < d->anim->animationCount(); ++i) {
|
|
|
|
tmp = d->anim->animationAt(i);
|
|
|
|
if (d->duration < tmp->duration())
|
|
|
|
d->duration = tmp->duration();
|
|
|
|
}
|
|
|
|
|
2009-10-15 21:36:38 +02:00
|
|
|
} else {
|
2009-12-03 20:30:02 +01:00
|
|
|
for (int i = 0; i < d->anim->animationCount(); ++i) {
|
|
|
|
tmp = d->anim->animationAt(i);
|
|
|
|
d->duration += tmp->duration();
|
|
|
|
}
|
|
|
|
|
2009-10-15 21:36:38 +02:00
|
|
|
}
|
2009-12-03 20:30:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int AnimationGroup::duration() const
|
|
|
|
{
|
|
|
|
return d->duration;
|
|
|
|
}
|
2009-10-15 21:36:38 +02:00
|
|
|
|
2009-12-03 20:30:02 +01:00
|
|
|
void AnimationGroup::updateDirection(QAbstractAnimation::Direction direction)
|
|
|
|
{
|
|
|
|
d->forwards = direction;
|
|
|
|
QAbstractAnimation *tmp;
|
|
|
|
for (int i = 0; i < d->anim->animationCount(); ++i) {
|
|
|
|
tmp = d->anim->animationAt(i);
|
|
|
|
tmp->setDirection(d->forwards);
|
2009-10-15 21:36:38 +02:00
|
|
|
}
|
|
|
|
|
2009-12-03 20:30:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationGroup::updateState(QAbstractAnimation::State oldState, QAbstractAnimation::State newState)
|
|
|
|
{
|
|
|
|
/* TODO: watch animation state and eventually emit 'finished' signal */
|
2009-10-15 21:36:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} //namespace Plasma
|
|
|
|
|
|
|
|
#include <../animationgroup.moc>
|