Plasma
pixmaptransition.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright 2009 Mehmet Ali Akmanalp <makmanalp@wpi.edu> 00003 * Copyright 2010 Marco Martin <notmart@gmail.com> 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU Library General Public License as 00007 * published by the Free Software Foundation; either version 2, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details 00014 * 00015 * You should have received a copy of the GNU Library General Public 00016 * License along with this program; if not, write to the 00017 * Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "pixmaptransition_p.h" 00022 00023 #include <QPainter> 00024 #include <QPixmap> 00025 00026 #include <kdebug.h> 00027 00028 #include "paintutils.h" 00029 00030 namespace Plasma 00031 { 00032 00033 PixmapTransition::PixmapTransition(QObject *parent) 00034 : EasingAnimation(parent), 00035 m_cache(false), 00036 m_dirty(false) 00037 { 00038 } 00039 00040 PixmapTransition::~PixmapTransition() 00041 { 00042 } 00043 00044 void PixmapTransition::setStartPixmap(const QPixmap &pixmap) 00045 { 00046 if (state() == Running) { 00047 stop(); 00048 } 00049 00050 m_startPixmap = pixmap; 00051 00052 //this will center the pixmaps if needed 00053 updateEffectiveTime(0); 00054 } 00055 00056 QPixmap PixmapTransition::startPixmap() const 00057 { 00058 return m_startPixmap; 00059 } 00060 00061 void PixmapTransition::setTargetPixmap(const QPixmap &pixmap) 00062 { 00063 if (state() == Running) { 00064 stop(); 00065 } 00066 00067 m_targetPixmap = pixmap; 00068 00069 updateEffectiveTime(0); 00070 } 00071 00072 void PixmapTransition::setUsesCache(bool cache) 00073 { 00074 m_cache = cache; 00075 } 00076 00077 bool PixmapTransition::usesCache() const 00078 { 00079 return m_cache; 00080 } 00081 00082 QPixmap PixmapTransition::targetPixmap() const 00083 { 00084 return m_targetPixmap; 00085 } 00086 00087 QPixmap PixmapTransition::currentPixmap() const 00088 { 00089 if (m_cache && !m_dirty) { 00090 return m_currentPixmap; 00091 } 00092 00093 QPixmap currentPixmap; 00094 qreal delta = currentTime() / qreal(duration()); 00095 if (!m_startPixmap.isNull() && !m_targetPixmap.isNull()) { 00096 //kDebug() << "transitioning"; 00097 currentPixmap = Plasma::PaintUtils::transition(m_startPixmap, m_targetPixmap, delta); 00098 } else if (m_startPixmap.isNull()) { 00099 if (qFuzzyCompare(delta, qreal(1.0))) { 00100 currentPixmap = alignedTargetPixmap(); 00101 return currentPixmap; 00102 } 00103 00104 if (currentPixmap.isNull()) { 00105 currentPixmap = QPixmap(m_pixmapSize); 00106 } 00107 00108 currentPixmap.fill(QColor(0, 0, 0, (int)(((qreal)255)*delta))); 00109 QPainter p(¤tPixmap); 00110 p.setCompositionMode(QPainter::CompositionMode_SourceIn); 00111 //kDebug() << "painting" << m_targetPixmap.rect() << "into" << m_targetRect << "in size" << currentPixmap.size(); 00112 p.drawPixmap(m_targetRect, m_targetPixmap); 00113 p.end(); 00114 } else if (m_targetPixmap.isNull()) { 00115 currentPixmap = alignedStartPixmap(); 00116 if (qFuzzyCompare(delta, qreal(1.0))) { 00117 return m_currentPixmap; 00118 } 00119 //kDebug() << "painting" << m_startPixmap.rect() << "into" << m_targetRect << "in size" << currentPixmap.size(); 00120 QPainter p(¤tPixmap); 00121 p.setCompositionMode(QPainter::CompositionMode_DestinationIn); 00122 p.fillRect(currentPixmap.rect(), QColor(0, 0, 0, (int)(254 - ((qreal)254)*delta))); 00123 p.end(); 00124 } 00125 00126 if (m_cache) { 00127 const_cast<PixmapTransition *>(this)->m_currentPixmap = currentPixmap; 00128 } 00129 00130 return currentPixmap; 00131 } 00132 00133 QPixmap PixmapTransition::alignedTargetPixmap() const 00134 { 00135 QPixmap pm(m_pixmapSize); 00136 pm.fill(Qt::transparent); 00137 QPainter p(&pm); 00138 p.drawPixmap(m_targetRect, m_targetPixmap); 00139 return pm; 00140 } 00141 00142 QPixmap PixmapTransition::alignedStartPixmap() const 00143 { 00144 QPixmap pm(m_pixmapSize); 00145 pm.fill(Qt::transparent); 00146 QPainter p(&pm); 00147 p.drawPixmap(m_startRect, m_startPixmap); 00148 return pm; 00149 } 00150 00151 void PixmapTransition::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) 00152 { 00153 if (oldState == Stopped && newState == Running) { 00154 m_targetRect = m_targetPixmap.rect(); 00155 m_startRect = m_startPixmap.rect(); 00156 m_pixmapSize = m_startRect.size().expandedTo(m_targetRect.size()); 00157 QRect actualRect = QRect(QPoint(0,0), m_pixmapSize); 00158 m_targetRect.moveCenter(actualRect.center()); 00159 m_startRect.moveCenter(actualRect.center()); 00160 } else if (QGraphicsWidget *w = targetWidget()) { 00161 w->update(); 00162 } 00163 00164 m_dirty = true; 00165 } 00166 00167 void PixmapTransition::updateEffectiveTime(int currentTime) 00168 { 00169 m_dirty = true; 00170 QGraphicsWidget *w = targetWidget(); 00171 if (w) { 00172 w->update(); 00173 } 00174 } 00175 00176 } //namespace Plasma 00177 00178 #include "pixmaptransition_p.moc"
KDE 4.6 API Reference