Begin implementation of PUD in CV. Some enhancements to GraphicsItemFader as well...
[amarok.git] / src / contextview / graphicsitemfader.cpp
blobdef8e7a84751ae443571e42c8ebefcb1039986fa
1 /***************************************************************************
2 * Copyright (c) 2007 Nikolaj Hald Nielsen <nhnFreespirit@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
20 #include "graphicsitemfader.h"
22 #include "debug.h"
24 #include <QPen>
26 using namespace Context;
29 GraphicsItemFader::GraphicsItemFader( QGraphicsItem * item, QGraphicsItem * parent )
30 : QObject()
31 , QGraphicsItem( parent )
32 , m_startAlpha( 0 )
33 , m_targetAlpha( 255 )
34 , m_fps ( 30 )
35 , m_duration( 5000 )
37 m_contentItem = item;
38 m_itemPreviousParent = m_contentItem->parentItem();
39 m_contentItem->setParentItem( this );
40 m_contentItem->setZValue( 1 );
41 m_contentItem->setPos( 1, 1 );
43 m_shadeRectItem = new QGraphicsRectItem( this );
45 m_width = m_contentItem->boundingRect().width() + 2;
46 m_height = m_contentItem->boundingRect().height() + 2;
48 m_shadeRectItem->setRect( 0, 0, m_width, m_height );
49 m_shadeRectItem->setPos( 0, 0 ); // needs a slight offset to cover frames on m_contentItem
50 m_shadeRectItem->setPen( Qt::NoPen );
51 m_shadeRectItem->setZValue ( 2 );
53 m_fadeColor = QColor ( 255, 255, 255, 0 );
54 m_shadeRectItem->setBrush( QBrush ( m_fadeColor ) );
56 m_timeLine = new QTimeLine( m_duration, this );
57 connect( m_timeLine, SIGNAL( frameChanged( int ) ), this, SLOT( fadeSlot( int ) ) );
58 connect( m_timeLine, SIGNAL( finished() ), this, SLOT( fadeFinished() ) );
61 GraphicsItemFader::~GraphicsItemFader()
63 m_timeLine->stop();
64 fadeSlot( m_animationSteps );
65 m_contentItem->setParentItem( m_itemPreviousParent );
68 void GraphicsItemFader::setStartAlpha(int alpha)
70 m_startAlpha = alpha;
71 m_fadeColor.setAlpha( m_startAlpha );
72 m_shadeRectItem->setBrush( QBrush ( m_fadeColor ) );
75 void GraphicsItemFader::setTargetAlpha(int alpha)
77 m_targetAlpha = alpha;
80 void GraphicsItemFader::setDuration(int ms)
82 m_duration = ms;
85 void GraphicsItemFader::setFPS(int fps)
87 m_fps = fps;
90 QRectF Context::GraphicsItemFader::boundingRect() const
92 return QRect( x(), y(), m_width, m_height );
96 void GraphicsItemFader::setFadeColor(const QColor & color)
98 m_fadeColor = color;
99 m_fadeColor.setAlpha( m_startAlpha );
100 m_shadeRectItem->setBrush( QBrush ( m_fadeColor ) );
103 void GraphicsItemFader::fadeSlot(int step)
105 int newAlpha = m_startAlpha + ( step * m_alphaStep );
106 m_fadeColor.setAlpha( newAlpha );
107 m_shadeRectItem->setBrush( QBrush ( m_fadeColor ) );
109 if ( newAlpha == m_targetAlpha )
110 emit( animationComplete() );
112 debug() << "fading, new alpha = " << newAlpha << endl;
115 void GraphicsItemFader::fadeFinished()
117 DEBUG_BLOCK
118 emit( animationComplete() );
121 void GraphicsItemFader::startFading()
123 if( m_timeLine->state() != QTimeLine::NotRunning )
124 m_timeLine->stop();
125 //total number of animation steps;
126 m_animationSteps = m_fps * ( ( float ) m_duration / 1000.0 );
128 //how much should alpha change each step
129 m_alphaStep = ( ( float ) ( m_targetAlpha - m_startAlpha ) ) / ( float ) m_animationSteps;
131 debug() << "Start fading, animationSteps = " << m_animationSteps << " over " << m_duration << " mseconds, alphaStep = " << m_alphaStep << endl;
133 m_timeLine->setDuration( m_duration );
134 m_timeLine->setFrameRange( 0, m_animationSteps );
135 m_timeLine->start();
138 void GraphicsItemFader::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
140 Q_UNUSED( painter );
141 Q_UNUSED( option );
142 Q_UNUSED( widget );
145 #include "graphicsitemfader.moc"