2010-01-21 08:23:18 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Mehmet Ali Akmanalp <makmanalp@wpi.edu>
|
|
|
|
* Copyright 2010 Marco Martin <notmart@gmail.com>
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2010-01-28 18:08:07 +01:00
|
|
|
#include <QPainter>
|
2010-01-21 08:23:18 +01:00
|
|
|
#include <QPixmap>
|
|
|
|
|
|
|
|
#include <kdebug.h>
|
|
|
|
|
|
|
|
#include "paintutils.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
PixmapTransition::PixmapTransition(QObject *parent)
|
|
|
|
: Animation(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PixmapTransition::~PixmapTransition()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PixmapTransition::setStartPixmap(const QPixmap &pixmap)
|
|
|
|
{
|
2010-02-03 13:46:54 +01:00
|
|
|
if (state() == Running) {
|
2010-01-22 00:31:45 +01:00
|
|
|
stop();
|
|
|
|
}
|
2010-02-03 13:46:54 +01:00
|
|
|
|
|
|
|
m_startPixmap = pixmap;
|
|
|
|
|
|
|
|
//this will center the pixmaps if needed
|
|
|
|
updateCurrentTime(0);
|
2010-01-21 08:23:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap PixmapTransition::startPixmap() const
|
|
|
|
{
|
|
|
|
return m_startPixmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PixmapTransition::setTargetPixmap(const QPixmap &pixmap)
|
|
|
|
{
|
2010-02-03 13:46:54 +01:00
|
|
|
if (state() == Running) {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2010-01-21 08:23:18 +01:00
|
|
|
m_targetPixmap = pixmap;
|
2010-02-03 13:46:54 +01:00
|
|
|
|
|
|
|
updateCurrentTime(0);
|
2010-01-21 08:23:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-01-28 18:08:07 +01:00
|
|
|
if (!m_startPixmap.isNull() && !m_targetPixmap.isNull()) {
|
|
|
|
if (oldState == Stopped && newState == Running) {
|
|
|
|
Plasma::PaintUtils::centerPixmaps(m_startPixmap, m_targetPixmap);
|
|
|
|
m_currentPixmap = (direction() == Forward ? m_startPixmap : m_targetPixmap);
|
|
|
|
} else if (newState == Stopped) {
|
|
|
|
m_currentPixmap = (direction() == Forward ? m_targetPixmap : m_startPixmap);
|
|
|
|
}
|
|
|
|
} else if (m_startPixmap.isNull()) {
|
|
|
|
if (oldState == Stopped && newState == Running) {
|
|
|
|
if (direction() == Forward) {
|
|
|
|
m_currentPixmap = QPixmap(m_targetPixmap.size());
|
|
|
|
m_currentPixmap.fill(Qt::transparent);
|
|
|
|
} else {
|
|
|
|
m_currentPixmap = m_targetPixmap;
|
|
|
|
}
|
|
|
|
} else if (newState == Stopped) {
|
|
|
|
if (direction() == Forward) {
|
|
|
|
m_currentPixmap = m_targetPixmap;
|
|
|
|
} else {
|
|
|
|
m_currentPixmap = QPixmap(m_targetPixmap.size());
|
|
|
|
m_currentPixmap.fill(Qt::transparent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (m_targetPixmap.isNull()) {
|
|
|
|
if (oldState == Stopped && newState == Running) {
|
|
|
|
if (direction() == Forward) {
|
|
|
|
m_currentPixmap = m_targetPixmap;
|
|
|
|
} else {
|
|
|
|
m_currentPixmap = QPixmap(m_targetPixmap.size());
|
|
|
|
m_currentPixmap.fill(Qt::transparent);
|
|
|
|
}
|
|
|
|
} else if (newState == Stopped) {
|
|
|
|
if (direction() == Forward) {
|
|
|
|
m_currentPixmap = QPixmap(m_targetPixmap.size());
|
|
|
|
m_currentPixmap.fill(Qt::transparent);
|
|
|
|
} else {
|
|
|
|
m_currentPixmap = m_targetPixmap;
|
|
|
|
}
|
|
|
|
}
|
2010-01-21 08:23:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
w->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PixmapTransition::updateCurrentTime(int currentTime)
|
|
|
|
{
|
|
|
|
QGraphicsWidget *w = targetWidget();
|
|
|
|
if (w) {
|
|
|
|
qreal delta = currentTime / qreal(duration());
|
2010-01-28 18:08:07 +01:00
|
|
|
delta *= Animation::easingCurve().valueForProgress(delta);
|
|
|
|
if (!m_startPixmap.isNull() && !m_targetPixmap.isNull()) {
|
|
|
|
m_currentPixmap = Plasma::PaintUtils::transition(m_startPixmap, m_targetPixmap, delta);
|
|
|
|
} else if (m_startPixmap.isNull()) {
|
2010-02-18 08:39:28 +01:00
|
|
|
if (qFuzzyCompare(delta, qreal(1.0))) {
|
2010-02-08 20:24:47 +01:00
|
|
|
m_currentPixmap = m_targetPixmap;
|
|
|
|
return;
|
|
|
|
}
|
2010-02-08 19:36:11 +01:00
|
|
|
if (m_currentPixmap.isNull()) {
|
|
|
|
m_currentPixmap = QPixmap(m_targetPixmap.size());
|
|
|
|
}
|
2010-02-10 22:54:12 +01:00
|
|
|
m_currentPixmap.fill(QColor(0, 0, 0, (int)(((qreal)255)*qMin((qreal)0.98, delta))));
|
2010-01-28 18:08:07 +01:00
|
|
|
QPainter p(&m_currentPixmap);
|
|
|
|
p.setCompositionMode(QPainter::CompositionMode_SourceIn);
|
|
|
|
p.drawPixmap(m_currentPixmap.rect(), m_targetPixmap, m_targetPixmap.rect());
|
|
|
|
p.end();
|
|
|
|
} else if (m_targetPixmap.isNull()) {
|
2010-02-08 19:36:11 +01:00
|
|
|
m_currentPixmap = m_startPixmap;
|
2010-01-28 18:08:07 +01:00
|
|
|
QPainter p(&m_currentPixmap);
|
|
|
|
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
2010-02-10 22:54:12 +01:00
|
|
|
p.fillRect(m_currentPixmap.rect(), QColor(0, 0, 0, (int)(((qreal)255)*qMin((qreal)0.98, delta))));
|
2010-01-28 18:08:07 +01:00
|
|
|
p.end();
|
|
|
|
}
|
2010-01-21 08:23:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Animation::updateCurrentTime(currentTime);
|
|
|
|
if (w) {
|
|
|
|
w->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} //namespace Plasma
|
2010-02-24 00:31:45 +01:00
|
|
|
|
|
|
|
#include <../pixmaptransition_p.moc>
|