2009-10-15 21:53:49 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// rotation.cpp //
|
|
|
|
// //
|
|
|
|
// Copyright(C) 2009 Igor Trindade Oliveira <igor.oliveira@indt.org.br>//
|
|
|
|
// Copyright(C) 2009 Adenilson Cavalcanti <adenilson.silva@idnt.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 //
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2009-10-22 21:46:16 +02:00
|
|
|
#include "rotation_p.h"
|
2009-10-15 21:53:49 +02:00
|
|
|
|
|
|
|
#include <QGraphicsRotation>
|
|
|
|
|
2009-10-22 06:00:02 +02:00
|
|
|
#include <kdebug.h>
|
|
|
|
|
2009-10-15 21:53:49 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2009-10-16 21:52:43 +02:00
|
|
|
class RotationAnimationPrivate {
|
2009-10-21 07:35:56 +02:00
|
|
|
public:
|
2009-10-16 21:52:43 +02:00
|
|
|
|
2009-10-21 07:35:56 +02:00
|
|
|
/* TODO: check if the rotation object will be deleted
|
|
|
|
* when the animation runs
|
|
|
|
*/
|
|
|
|
QGraphicsRotation *rotation;
|
2009-10-22 17:35:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Animation rotation angle (e.g. 45, 180, etc)
|
|
|
|
*/
|
|
|
|
qreal angle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotation axis (e.g. X, Y, Z)
|
|
|
|
*/
|
|
|
|
Qt::Axis axis;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotation reference (e.g. Center, Up, Down, Left, Right) can
|
|
|
|
* be combined (i.e. Center|Up)
|
|
|
|
*/
|
|
|
|
qint8 reference;
|
2009-10-16 21:52:43 +02:00
|
|
|
};
|
|
|
|
|
2009-12-03 20:30:02 +01:00
|
|
|
void RotationAnimation::setWidgetToAnimate(QGraphicsWidget *widget)
|
|
|
|
{
|
2009-12-04 21:49:09 +01:00
|
|
|
if(widget == widgetToAnimate()) {
|
|
|
|
return;
|
2009-12-03 20:30:02 +01:00
|
|
|
}
|
2009-12-04 21:49:09 +01:00
|
|
|
|
|
|
|
Animation::setWidgetToAnimate(widget);
|
2009-12-03 20:30:02 +01:00
|
|
|
}
|
2009-10-16 21:52:43 +02:00
|
|
|
|
2009-12-03 20:30:02 +01:00
|
|
|
RotationAnimation::RotationAnimation(QObject *parent,
|
|
|
|
const qint8 &reference,
|
|
|
|
const Qt::Axis &axis,
|
|
|
|
const qreal &angle)
|
|
|
|
: Animation(parent), d(new RotationAnimationPrivate)
|
2009-10-16 21:52:43 +02:00
|
|
|
{
|
2009-10-21 07:32:04 +02:00
|
|
|
setAngle(angle);
|
|
|
|
setAxis(axis);
|
|
|
|
setReference(reference);
|
2009-10-16 21:52:43 +02:00
|
|
|
|
|
|
|
d->rotation = new QGraphicsRotation(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
RotationAnimation::~RotationAnimation()
|
2009-10-15 21:53:49 +02:00
|
|
|
{
|
2009-10-16 21:52:43 +02:00
|
|
|
delete d;
|
2009-10-15 21:53:49 +02:00
|
|
|
}
|
|
|
|
|
2009-12-04 21:49:09 +01:00
|
|
|
Qt::Axis RotationAnimation::axis() const
|
|
|
|
{
|
|
|
|
return d->axis;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RotationAnimation::setAxis(const Qt::Axis &axis)
|
|
|
|
{
|
|
|
|
d->axis = axis;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint8 RotationAnimation::reference() const
|
|
|
|
{
|
|
|
|
return d->reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RotationAnimation::setReference(const qint8 &reference)
|
|
|
|
{
|
|
|
|
d->reference = reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal RotationAnimation::angle() const
|
|
|
|
{
|
|
|
|
return d->angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RotationAnimation::setAngle(const qreal &angle)
|
|
|
|
{
|
|
|
|
d->angle = angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RotationAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
|
2009-10-15 21:53:49 +02:00
|
|
|
{
|
2009-10-20 23:21:40 +02:00
|
|
|
QGraphicsWidget *m_object = widgetToAnimate();
|
2009-10-15 21:53:49 +02:00
|
|
|
|
2009-12-04 21:49:09 +01:00
|
|
|
if (!m_object) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-15 21:53:49 +02:00
|
|
|
QVector3D vector(0, 0, 0);
|
|
|
|
|
|
|
|
const qreal widgetWidth = m_object->size().width();
|
|
|
|
const qreal widgetHeight = m_object->size().height();
|
|
|
|
|
2009-10-21 07:32:04 +02:00
|
|
|
if (axis() == Qt::XAxis) {
|
|
|
|
switch (reference()) {
|
2009-10-22 06:00:02 +02:00
|
|
|
case Center:
|
|
|
|
vector.setY(widgetHeight/2);
|
|
|
|
break;
|
|
|
|
case Up:
|
|
|
|
vector.setY(0);
|
|
|
|
break;
|
|
|
|
case Down:
|
|
|
|
vector.setY(widgetHeight);
|
|
|
|
break;
|
2009-10-15 21:53:49 +02:00
|
|
|
}
|
2009-10-21 07:35:56 +02:00
|
|
|
|
2009-10-21 07:32:04 +02:00
|
|
|
} else if(axis() == Qt::YAxis) {
|
|
|
|
switch (reference()) {
|
2009-10-22 06:00:02 +02:00
|
|
|
case Center:
|
|
|
|
vector.setX(widgetWidth/2);
|
|
|
|
break;
|
|
|
|
case Left:
|
|
|
|
vector.setX(0);
|
|
|
|
break;
|
|
|
|
case Right:
|
|
|
|
vector.setX(widgetWidth);
|
|
|
|
break;
|
2009-10-15 21:53:49 +02:00
|
|
|
}
|
2009-10-21 07:35:56 +02:00
|
|
|
|
2009-10-21 07:32:04 +02:00
|
|
|
} else if (axis() == Qt::ZAxis) {
|
|
|
|
switch (reference()) {
|
2009-10-22 06:00:02 +02:00
|
|
|
case Center:
|
|
|
|
vector.setX(widgetWidth/2);
|
|
|
|
vector.setY(widgetHeight/2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Center|Up:
|
|
|
|
vector.setX(widgetWidth/2);
|
|
|
|
vector.setY(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Center|Down:
|
|
|
|
vector.setX(widgetWidth/2);
|
|
|
|
vector.setY(widgetHeight);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Center|Left:
|
|
|
|
vector.setX(0);
|
|
|
|
vector.setY(widgetHeight/2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Center|Right:
|
|
|
|
vector.setX(widgetWidth);
|
|
|
|
vector.setY(widgetHeight/2);
|
|
|
|
break;
|
2009-10-15 21:53:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-16 21:52:43 +02:00
|
|
|
d->rotation->setOrigin(vector);
|
2009-10-21 07:32:04 +02:00
|
|
|
d->rotation->setAxis(axis());
|
2009-10-15 21:53:49 +02:00
|
|
|
|
|
|
|
QList<QGraphicsTransform *> transformation;
|
2009-10-16 21:52:43 +02:00
|
|
|
transformation.append(d->rotation);
|
2009-10-15 21:53:49 +02:00
|
|
|
m_object->setTransformations(transformation);
|
|
|
|
|
2009-12-04 21:49:09 +01:00
|
|
|
if ((oldState == Stopped) && (newState == Running)) {
|
|
|
|
d->rotation->setAngle(direction() == Forward ? 0 : angle());
|
|
|
|
} else if (newState == Stopped) {
|
|
|
|
d->rotation->setAngle(direction() == Forward ? angle() : 0);
|
2009-10-23 20:39:57 +02:00
|
|
|
}
|
2009-10-22 17:35:08 +02:00
|
|
|
}
|
|
|
|
|
2009-12-04 21:49:09 +01:00
|
|
|
void RotationAnimation::updateCurrentTime(int currentTime)
|
2009-10-22 17:35:08 +02:00
|
|
|
{
|
2009-12-04 21:49:09 +01:00
|
|
|
QGraphicsWidget *w = widgetToAnimate();
|
|
|
|
if (w) {
|
|
|
|
qreal delta = currentTime / qreal(duration());
|
|
|
|
delta = angle() * delta;
|
|
|
|
d->rotation->setAngle(delta);
|
|
|
|
}
|
2009-10-22 17:35:08 +02:00
|
|
|
}
|
|
|
|
|
2009-10-15 21:53:49 +02:00
|
|
|
}
|