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)
|
2010-09-22 21:35:47 +02:00
|
|
|
: EasingAnimation(parent),
|
|
|
|
m_cache(false),
|
|
|
|
m_dirty(false)
|
2010-01-21 08:23:18 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2010-04-08 19:07:33 +02:00
|
|
|
updateEffectiveTime(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
|
|
|
|
2010-04-08 19:07:33 +02:00
|
|
|
updateEffectiveTime(0);
|
2010-01-21 08:23:18 +01:00
|
|
|
}
|
|
|
|
|
2010-09-22 21:35:47 +02:00
|
|
|
void PixmapTransition::setUsesCache(bool cache)
|
2010-01-21 08:23:18 +01:00
|
|
|
{
|
2010-09-22 21:35:47 +02:00
|
|
|
m_cache = cache;
|
2010-01-21 08:23:18 +01:00
|
|
|
}
|
|
|
|
|
2010-09-22 21:35:47 +02:00
|
|
|
bool PixmapTransition::usesCache() const
|
2010-01-21 08:23:18 +01:00
|
|
|
{
|
2010-09-22 21:35:47 +02:00
|
|
|
return m_cache;
|
2010-01-21 08:23:18 +01:00
|
|
|
}
|
|
|
|
|
2010-09-22 21:35:47 +02:00
|
|
|
QPixmap PixmapTransition::targetPixmap() const
|
2010-01-21 08:23:18 +01:00
|
|
|
{
|
2010-09-22 21:35:47 +02:00
|
|
|
return m_targetPixmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap PixmapTransition::currentPixmap() const
|
|
|
|
{
|
|
|
|
if (m_cache && !m_dirty) {
|
|
|
|
return m_currentPixmap;
|
2010-01-21 08:23:18 +01:00
|
|
|
}
|
|
|
|
|
2010-09-22 21:35:47 +02:00
|
|
|
QPixmap currentPixmap;
|
|
|
|
qreal delta = currentTime() / qreal(duration());
|
2010-01-28 18:08:07 +01:00
|
|
|
if (!m_startPixmap.isNull() && !m_targetPixmap.isNull()) {
|
2010-09-22 21:35:47 +02:00
|
|
|
//kDebug() << "transitioning";
|
|
|
|
currentPixmap = Plasma::PaintUtils::transition(m_startPixmap, m_targetPixmap, delta);
|
2010-01-28 18:08:07 +01:00
|
|
|
} else if (m_startPixmap.isNull()) {
|
2010-09-22 21:35:47 +02:00
|
|
|
if (qFuzzyCompare(delta, qreal(1.0))) {
|
|
|
|
currentPixmap = alignedTargetPixmap();
|
|
|
|
return currentPixmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentPixmap.isNull()) {
|
|
|
|
currentPixmap = QPixmap(m_pixmapSize);
|
2010-01-28 18:08:07 +01:00
|
|
|
}
|
2010-09-22 21:35:47 +02:00
|
|
|
|
|
|
|
currentPixmap.fill(QColor(0, 0, 0, (int)(((qreal)255)*delta)));
|
|
|
|
QPainter p(¤tPixmap);
|
|
|
|
p.setCompositionMode(QPainter::CompositionMode_SourceIn);
|
|
|
|
//kDebug() << "painting" << m_targetPixmap.rect() << "into" << m_targetRect << "in size" << currentPixmap.size();
|
|
|
|
p.drawPixmap(m_targetRect, m_targetPixmap);
|
|
|
|
p.end();
|
2010-01-28 18:08:07 +01:00
|
|
|
} else if (m_targetPixmap.isNull()) {
|
2010-09-22 21:35:47 +02:00
|
|
|
currentPixmap = alignedStartPixmap();
|
|
|
|
if (qFuzzyCompare(delta, qreal(1.0))) {
|
2010-11-03 22:55:50 +01:00
|
|
|
return m_currentPixmap;
|
2010-01-28 18:08:07 +01:00
|
|
|
}
|
2010-09-22 21:35:47 +02:00
|
|
|
//kDebug() << "painting" << m_startPixmap.rect() << "into" << m_targetRect << "in size" << currentPixmap.size();
|
|
|
|
QPainter p(¤tPixmap);
|
|
|
|
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
2010-11-03 22:55:50 +01:00
|
|
|
p.fillRect(currentPixmap.rect(), QColor(0, 0, 0, (int)(254 - ((qreal)254)*delta)));
|
2010-09-22 21:35:47 +02:00
|
|
|
p.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_cache) {
|
|
|
|
const_cast<PixmapTransition *>(this)->m_currentPixmap = currentPixmap;
|
2010-01-21 08:23:18 +01:00
|
|
|
}
|
|
|
|
|
2010-09-22 21:35:47 +02:00
|
|
|
return currentPixmap;
|
2010-01-21 08:23:18 +01:00
|
|
|
}
|
|
|
|
|
2010-09-22 21:35:47 +02:00
|
|
|
QPixmap PixmapTransition::alignedTargetPixmap() const
|
2010-01-21 08:23:18 +01:00
|
|
|
{
|
2010-09-22 21:35:47 +02:00
|
|
|
QPixmap pm(m_pixmapSize);
|
|
|
|
pm.fill(Qt::transparent);
|
|
|
|
QPainter p(&pm);
|
|
|
|
p.drawPixmap(m_targetRect, m_targetPixmap);
|
|
|
|
return pm;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap PixmapTransition::alignedStartPixmap() const
|
|
|
|
{
|
|
|
|
QPixmap pm(m_pixmapSize);
|
|
|
|
pm.fill(Qt::transparent);
|
|
|
|
QPainter p(&pm);
|
|
|
|
p.drawPixmap(m_startRect, m_startPixmap);
|
|
|
|
return pm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PixmapTransition::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
|
|
|
|
{
|
|
|
|
if (oldState == Stopped && newState == Running) {
|
|
|
|
m_targetRect = m_targetPixmap.rect();
|
|
|
|
m_startRect = m_startPixmap.rect();
|
|
|
|
m_pixmapSize = m_startRect.size().expandedTo(m_targetRect.size());
|
|
|
|
QRect actualRect = QRect(QPoint(0,0), m_pixmapSize);
|
|
|
|
m_targetRect.moveCenter(actualRect.center());
|
|
|
|
m_startRect.moveCenter(actualRect.center());
|
|
|
|
} else if (QGraphicsWidget *w = targetWidget()) {
|
|
|
|
w->update();
|
2010-01-21 08:23:18 +01:00
|
|
|
}
|
|
|
|
|
2010-09-22 21:35:47 +02:00
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PixmapTransition::updateEffectiveTime(int currentTime)
|
|
|
|
{
|
2011-05-27 12:12:43 +02:00
|
|
|
Q_UNUSED(currentTime)
|
2010-09-22 21:35:47 +02:00
|
|
|
m_dirty = true;
|
|
|
|
QGraphicsWidget *w = targetWidget();
|
2010-01-21 08:23:18 +01:00
|
|
|
if (w) {
|
|
|
|
w->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} //namespace Plasma
|
2010-02-24 00:31:45 +01:00
|
|
|
|
2010-09-22 23:14:40 +02:00
|
|
|
#include "pixmaptransition_p.moc"
|