From 586df4c6ac30d396f842d4f23a1a4a36e77cee72 Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Thu, 21 Jan 2010 07:23:18 +0000 Subject: [PATCH] a class to handle transitions betwen pixmap. puhbutton port in the works too svn path=/trunk/KDE/kdelibs/; revision=1077924 --- animations/pixmaptransition.cpp | 98 +++++++++++++++++++++++++++++++++ animations/pixmaptransition_p.h | 86 +++++++++++++++++++++++++++++ animator.cpp | 5 ++ animator.h | 3 +- 4 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 animations/pixmaptransition.cpp create mode 100644 animations/pixmaptransition_p.h diff --git a/animations/pixmaptransition.cpp b/animations/pixmaptransition.cpp new file mode 100644 index 000000000..9a9a839d6 --- /dev/null +++ b/animations/pixmaptransition.cpp @@ -0,0 +1,98 @@ +/* + * Copyright 2009 Mehmet Ali Akmanalp + * Copyright 2010 Marco Martin + * + * 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 "pixmaptransition_p.h" + +#include + +#include + +#include "paintutils.h" + +namespace Plasma +{ + +PixmapTransition::PixmapTransition(QObject *parent) + : Animation(parent) +{ +} + +PixmapTransition::~PixmapTransition() +{ +} + +void PixmapTransition::setStartPixmap(const QPixmap &pixmap) +{ + m_startPixmap = pixmap; + m_currentPixmap = pixmap; +} + +QPixmap PixmapTransition::startPixmap() const +{ + return m_startPixmap; +} + +void PixmapTransition::setTargetPixmap(const QPixmap &pixmap) +{ + m_targetPixmap = pixmap; +} + +QPixmap PixmapTransition::targetPixmap() const +{ + return m_targetPixmap; +} + +QPixmap PixmapTransition::currentPixmap() const +{ + return m_currentPixmap; +} + +void PixmapTransition::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) +{ + QGraphicsWidget *w = targetWidget(); + if (!w) { + return; + } + + if (oldState == Stopped && newState == Running) { + m_currentPixmap = (direction() == Forward ? m_startPixmap : m_targetPixmap); + } else if (newState == Stopped) { + m_currentPixmap = (direction() == Forward ? m_targetPixmap : m_startPixmap); + } + + w->update(); +} + +void PixmapTransition::updateCurrentTime(int currentTime) +{ + QGraphicsWidget *w = targetWidget(); + if (w) { + qreal delta = currentTime / qreal(duration()); + delta *= Animation::easingCurve().valueForProgress(delta); + m_currentPixmap = Plasma::PaintUtils::transition(m_startPixmap, m_targetPixmap, delta); + } + + Animation::updateCurrentTime(currentTime); + if (w) { + w->update(); + } +} + +} //namespace Plasma diff --git a/animations/pixmaptransition_p.h b/animations/pixmaptransition_p.h new file mode 100644 index 000000000..055d7c681 --- /dev/null +++ b/animations/pixmaptransition_p.h @@ -0,0 +1,86 @@ +/* + * Copyright 2009 Mehmet Ali Akmanalp + * Copyright 2010 Marco Martin + * + * 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. + */ + +/** + * @file This file contains the definition for the PixmapTransition effect. + */ + +#ifndef PLASMA_ANIMATIONS_PIXMAPTRANSITION_P_H +#define PLASMA_ANIMATIONS_PIXMAPTRANSITION_P_H + +#include +#include + +namespace Plasma +{ + +/** + * @class Fade plasma/animations/pixmaptransition.h + * @short PixmapTransition effect + * + * Effect that paints a transition between two pixmaps + */ +class PixmapTransition : public Animation +{ + Q_OBJECT + Q_PROPERTY(QPixmap startPixmap READ startPixmap WRITE setStartPixmap) + Q_PROPERTY(QPixmap targetPixmap READ targetPixmap WRITE setTargetPixmap) + Q_PROPERTY(QPixmap currentPixmap READ currentPixmap) + +public: + explicit PixmapTransition(QObject *parent = 0); + + virtual ~PixmapTransition(); + + /** + * The first pixmap of the animation + */ + QPixmap startPixmap() const; + + /** + * Set the first pixmap of the animation + */ + void setStartPixmap(const QPixmap &); + + /** + * The pixmap the animation will evolve to + */ + QPixmap targetPixmap() const; + + /** + * Set the pixmap the animation will evolve to + */ + void setTargetPixmap(const QPixmap &); + + QPixmap currentPixmap() const; + +protected: + void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState); + void updateCurrentTime(int currentTime); + +private: + QPixmap m_startPixmap; + QPixmap m_targetPixmap; + QPixmap m_currentPixmap; +}; + +} + +#endif // PLASMA_ANIMATIONS_PIXMAPTRANSITION_P_H diff --git a/animator.cpp b/animator.cpp index f2f5ef608..ad2dec16b 100644 --- a/animator.cpp +++ b/animator.cpp @@ -29,6 +29,7 @@ #include "animations/rotationstacked_p.h" #include "animations/geometry_p.h" #include "animations/zoom_p.h" +#include "animations/pixmaptransition_p.h" namespace Plasma { @@ -70,6 +71,10 @@ Plasma::Animation* Animator::create(Animator::Animation type, QObject *parent) result = new Plasma::ZoomAnimation(parent); break; + case PixmapTransitionAnimation: + result = new Plasma::PixmapTransition(parent); + break; + default: kDebug() << "Unsupported animation type."; } diff --git a/animator.h b/animator.h index ca9fd922d..d61f20120 100644 --- a/animator.h +++ b/animator.h @@ -65,7 +65,8 @@ public: RotationStackedAnimation, /*<< for flipping one object with another */ SlideAnimation, /*<< Move the position of animated object */ GeometryAnimation, /*<< Geometry animation*/ - ZoomAnimation /*<