Add new Animation, RotationStackedAnimation.

It can do 3D rotation animations without the user does not care about layouts and hidden widgets

svn path=/trunk/KDE/kdelibs/; revision=1039141
This commit is contained in:
Igor Trindade Oliveira 2009-10-22 20:17:07 +00:00
parent 82177676af
commit a7db9faf50
5 changed files with 368 additions and 0 deletions

View File

@ -53,6 +53,8 @@ set(plasma_LIB_SRCS
animations/pause.cpp
animations/pulser.cpp
animations/rotation.cpp
animations/rotationstacked.cpp
animations/stackedlayout.cpp
applet.cpp
configloader.cpp
containment.cpp
@ -353,6 +355,7 @@ install(FILES
animations/abstractanimation.h
animations/animation.h
animations/animationgroup.h
animations/rotationstacked.h
DESTINATION ${INCLUDE_INSTALL_DIR}/plasma/animations COMPONENT Devel)

View File

@ -0,0 +1,152 @@
/***********************************************************************/
/* stackedlayout.h */
/* */
/* Copyright(C) 2009 Igor Trindade Oliveira <igor.oliveira@indt.org.br>*/
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */
/* */
/* This library 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 */
/* Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the Free Software */
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA */
/* 02110-1301 USA */
/***********************************************************************/
#include "rotationstacked.h"
#include "stackedlayout.h"
#include <QGraphicsRotation>
#include <QSequentialAnimationGroup>
namespace Plasma
{
class RotationStackedAnimationPrivate {
public:
QGraphicsRotation *backRotation;
QGraphicsRotation *frontRotation;
AbstractAnimation::Reference reference;
QPair<QGraphicsWidget *, QGraphicsWidget *> animObjects;
StackedLayout *sLayout;
};
RotationStackedAnimation::RotationStackedAnimation(QObject *parent)
: d(new RotationStackedAnimationPrivate)
{
d->backRotation = new QGraphicsRotation(this);
d->frontRotation = new QGraphicsRotation(this);
d->sLayout = new StackedLayout;
}
RotationStackedAnimation::~RotationStackedAnimation()
{
delete d->sLayout;
delete d;
}
void RotationStackedAnimation::setReference(AbstractAnimation::Reference reference)
{
d->reference = reference;
}
AbstractAnimation::Reference RotationStackedAnimation::reference()
{
return d->reference;
}
void RotationStackedAnimation::setWidgetsToAnimate(QGraphicsWidget *front, QGraphicsWidget *back)
{
d->animObjects = qMakePair(front, back);
d->sLayout->addWidget(front);
d->sLayout->addWidget(back);
}
QGraphicsLayoutItem *RotationStackedAnimation::layout()
{
return d->sLayout;
}
QPair<QGraphicsWidget *,QGraphicsWidget *> RotationStackedAnimation::widgetsToAnimate()
{
return d->animObjects;
}
QAbstractAnimation *RotationStackedAnimation::render(QObject *parent)
{
Q_UNUSED(parent);
QPair<QGraphicsWidget *,QGraphicsWidget *> widgets = widgetsToAnimate();
QSequentialAnimationGroup *groupAnim = new QSequentialAnimationGroup(parent);
QPropertyAnimation *frontAnim = new QPropertyAnimation(d->frontRotation, "angle", groupAnim);
QPropertyAnimation *backAnim = new QPropertyAnimation(d->backRotation, "angle", groupAnim);
const qreal widgetFrontWidth = widgets.first->size().width();
const qreal widgetFrontHeight = widgets.first->size().height();
const qreal widgetBackWidth = widgets.second->size().width();
const qreal widgetBackHeight = widgets.second->size().height();
QPair<QVector3D, QVector3D> vector;
if(reference() == Center) {
vector.first = QVector3D(widgetFrontWidth/2, widgetFrontHeight/2, 0);
vector.second = QVector3D(widgetBackWidth/2, widgetBackHeight/2, 0);
if(direction() == MoveLeft || direction() == MoveRight) {
d->frontRotation->setAxis(Qt::YAxis);
d->backRotation->setAxis(Qt::YAxis);
if(direction() == MoveLeft) {
d->backRotation->setAngle(265);
backAnim->setEndValue(360);
frontAnim->setEndValue(90);
} else {
d->backRotation->setAngle(-90);
}
}
}
d->frontRotation->setOrigin(vector.first);
d->backRotation->setOrigin(vector.second);
QList<QGraphicsTransform *> backTransformation;
QList<QGraphicsTransform *> frontTransformation;
frontTransformation.append(d->frontRotation);
backTransformation.append(d->backRotation);
widgets.first->setTransformations(frontTransformation);
widgets.second->setTransformations(backTransformation);
frontAnim->setDuration(duration()/2);
backAnim->setDuration(duration()/2);
connect(frontAnim, SIGNAL(finished()), this, SLOT(rotateBackWidget()));
groupAnim->addAnimation(frontAnim);
groupAnim->addAnimation(backAnim);
return groupAnim;
}
void RotationStackedAnimation::rotateBackWidget()
{
d->sLayout->setCurrentWidgetIndex(1);
}
}

View File

@ -0,0 +1,59 @@
#////////////////////////////////////////////////////////////////////////
// rotation.cpp //
// //
// Copyright(C) 2009 Igor Trindade Oliveira <igor.oliveira@indt.org.br>//
// //
// This library is free software; you can redistribute it and/or //
// modify it under the terms of the GNU Lesser General Public //
// License as published by the Free Software Foundation; either //
// version 2.1 of the License, or (at your option) any later version. //
// //
// This library 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 //
// Lesser General Public License for more details. //
// //
// You should have received a copy of the GNU Lesser General Public //
// License along with this library; if not, write to the Free Software //
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA //
// 02110-1301 USA //
/////////////////////////////////////////////////////////////////////////
#ifndef ROTATIONSTACKED_H
#define ROTATIONSTACKED_H
#include <plasma/animations/animation.h>
#include <plasma/plasma_export.h>
namespace Plasma {
class RotationStackedAnimationPrivate;
class RotationStackedAnimation : public Animation
{
Q_OBJECT
public:
RotationStackedAnimation(QObject *parent = 0);
~RotationStackedAnimation();
QAbstractAnimation *render(QObject *parent = 0);
void setReference(Reference reference);
Reference reference();
QGraphicsLayoutItem *layout();
void setWidgetsToAnimate(QGraphicsWidget *front, QGraphicsWidget *back);
QPair<QGraphicsWidget *, QGraphicsWidget *> widgetsToAnimate();
public Q_SLOTS:
void rotateBackWidget();
private:
RotationStackedAnimationPrivate *d;
};
} // Plasma
#endif

View File

@ -0,0 +1,103 @@
/***********************************************************************/
/* stackedlayout.h */
/* */
/* Copyright(C) 2009 Igor Trindade Oliveira <igor.oliveira@indt.org.br>*/
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */
/* */
/* This library 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 */
/* Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the Free Software */
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA */
/* 02110-1301 USA */
/***********************************************************************/
#include "stackedlayout.h"
#include <QGraphicsWidget>
#include <QDebug>
StackedLayout::StackedLayout(QGraphicsLayoutItem *parent)
: QGraphicsLayout(parent), m_currentWidgetIndex(-1)
{
}
StackedLayout::~StackedLayout()
{
}
void StackedLayout::setGeometry(const QRectF &rect)
{
QGraphicsLayout::setGeometry(rect);
for(int i = 0; i < items.size(); i++) {
itemAt(i)->setGeometry(rect);
}
}
QSizeF StackedLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
Q_UNUSED(which);
Q_UNUSED(constraint);
qreal left, top, right, bottom;
getContentsMargins(&left, &top, &right, &bottom);
QSizeF currentWidgetSize = itemAt(m_currentWidgetIndex)->effectiveSizeHint(which, constraint);
return QSizeF( left + right + currentWidgetSize.width(), right + bottom + currentWidgetSize.height());
}
int StackedLayout::count() const
{
return items.count();
}
QGraphicsLayoutItem *StackedLayout::itemAt(int i) const
{
return items.at(i);
}
void StackedLayout::insertWidget(QGraphicsLayoutItem *item, int pos)
{
if(!pos && (m_currentWidgetIndex == -1)) {
m_currentWidgetIndex = 0;
} else {
item->graphicsItem()->hide();
}
items.insert(pos, item);
}
void StackedLayout::addWidget(QGraphicsLayoutItem *item)
{
insertWidget(item, items.size());
}
void StackedLayout::removeAt(int index)
{
items.removeAt(index);
}
void StackedLayout::setCurrentWidgetIndex(qint32 index)
{
QGraphicsItem *currentWidget = itemAt(m_currentWidgetIndex)->graphicsItem();
QGraphicsItem *hidenWidget = itemAt(index)->graphicsItem();
currentWidget->hide();
hidenWidget->show();
m_currentWidgetIndex = index;
}
qint32 StackedLayout::currentWidgetIndex() const
{
return m_currentWidgetIndex;
}

View File

@ -0,0 +1,51 @@
////////////////////////////////////////////////////////////////////////
// rotation.cpp //
// //
// Copyright(C) 2009 Igor Trindade Oliveira <igor.oliveira@indt.org.br>//
// //
// This library is free software; you can redistribute it and/or //
// modify it under the terms of the GNU Lesser General Public //
// License as published by the Free Software Foundation; either //
// version 2.1 of the License, or (at your option) any later version. //
// //
// This library 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 //
// Lesser General Public License for more details. //
// //
// You should have received a copy of the GNU Lesser General Public //
// License along with this library; if not, write to the Free Software //
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA //
// 02110-1301 USA //
/////////////////////////////////////////////////////////////////////////
#ifndef STACKEDLAYOUT_H
#define STACKEDLAYOUT_H
#include <QGraphicsLayout>
#include <QList>
class StackedLayout : public QGraphicsLayout {
public:
StackedLayout(QGraphicsLayoutItem *parent = 0);
~StackedLayout();
void setGeometry(const QRectF &rect);
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const;
int count() const;
QGraphicsLayoutItem *itemAt(int i) const;
void insertWidget(QGraphicsLayoutItem *item, int pos);
void addWidget(QGraphicsLayoutItem *item);
void removeAt(int index);
qint32 currentWidgetIndex() const;
void setCurrentWidgetIndex(qint32 index);
private:
QList<QGraphicsLayoutItem *> items;
qint32 m_currentWidgetIndex;
};
#endif