Plasma
grow.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright 2009 Mehmet Ali Akmanalp <makmanalp@wpi.edu> 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU Library General Public License as 00006 * published by the Free Software Foundation; either version 2, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details 00013 * 00014 * You should have received a copy of the GNU Library General Public 00015 * License along with this program; if not, write to the 00016 * Free Software Foundation, Inc., 00017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "grow_p.h" 00021 00022 #include <QRect> 00023 #include <kdebug.h> 00024 00025 namespace Plasma 00026 { 00027 00028 GrowAnimation::GrowAnimation(QObject *parent, qreal factor) 00029 : EasingAnimation(parent), m_animFactor(factor) 00030 { 00031 } 00032 00033 void GrowAnimation::setFactor(const qreal factor) 00034 { 00035 m_animFactor = qMax(qreal(0.0), factor); 00036 } 00037 00038 qreal GrowAnimation::factor() const 00039 { 00040 return m_animFactor; 00041 } 00042 00043 void GrowAnimation::updateEffectiveTime(int currentTime) 00044 { 00045 QGraphicsWidget *w = targetWidget(); 00046 if (w && state() == QAbstractAnimation::Running) { 00047 const qreal delta = currentTime / qreal(duration()); 00048 QRectF geometry; 00049 geometry.setTopLeft(m_startGeometry.topLeft() * (1-delta) + (m_targetGeometry.topLeft() * delta)); 00050 geometry.setSize(m_startGeometry.size() * (1-delta) + (m_targetGeometry.size() * delta)); 00051 w->setGeometry(geometry); 00052 } 00053 } 00054 00055 void GrowAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) 00056 { 00057 if (oldState == QAbstractAnimation::Stopped && newState == QAbstractAnimation::Running) { 00058 QGraphicsWidget *widget = targetWidget(); 00059 if (!widget) { 00060 return; 00061 } 00062 00063 QSizeF minimum = widget->effectiveSizeHint(Qt::MinimumSize); 00064 QSizeF maximum = widget->effectiveSizeHint(Qt::MaximumSize); 00065 m_startGeometry = widget->geometry(); 00066 qreal w = m_startGeometry.width(); 00067 qreal h = m_startGeometry.height(); 00068 qreal factor = m_animFactor; 00069 00070 //compute new geometry values 00071 qreal newWidth; 00072 qreal newHeight; 00073 if (direction() == QAbstractAnimation::Forward) { 00074 newWidth = qBound(minimum.width(), w * factor, maximum.width()); 00075 newHeight = qBound(minimum.height(), h * factor, maximum.height()); 00076 } else { 00077 newWidth = qBound(minimum.width(), w / factor, maximum.width()); 00078 newHeight = qBound(minimum.height(), h / factor, maximum.height()); 00079 } 00080 00081 qreal newX; 00082 qreal newY; 00083 newX = m_startGeometry.x() - (newWidth - w)/2; 00084 newY = m_startGeometry.y() - (newHeight - h)/2; 00085 00086 if (direction() == QAbstractAnimation::Forward) { 00087 //the end geometry gets rounded to prevent things looking ugly 00088 m_targetGeometry = QRect(newX, newY, newWidth, newHeight); 00089 } else { 00090 m_targetGeometry = m_startGeometry; 00091 m_startGeometry = QRectF(newX, newY, newWidth, newHeight); 00092 } 00093 } 00094 } 00095 00096 } //namespace Plasma 00097 00098 #include "grow_p.moc" 00099
KDE 4.6 API Reference