If you've got a modern machine, turn on the eyecandy bool in contextview.cpp for...
[amarok.git] / src / contextview / graphicsitemfader.cpp
blobbceb2269ae1de040d482228c1788a781e222428b
1 /***************************************************************************
2 * Copyright (c) 2007 Nikolaj Hald Nielsen <nhnFreespirit@gmail.com> *
3 * 2007 Jeff Mitchell <kde-dev@emailgoeshere.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "contextbox.h"
22 #include "graphicsitemfader.h"
24 #include "debug.h"
26 #include <QPen>
27 #include <QTimer>
29 using namespace Context;
32 GraphicsItemFader::GraphicsItemFader( ContextBox *item )
33 : QObject()
34 , m_startAlpha( 0 )
35 , m_targetAlpha( 255 )
36 , m_fps( 0 )
37 , m_duration( 5000 )
38 , m_delay( 0 )
40 m_contentItem = item;
42 //m_fadeColor = QColor ( 255, 255, 255, 0 );
43 m_fadeColor = m_contentItem->brush().color();
45 m_timeLine = new QTimeLine( m_duration, this );
46 m_timeLine->setCurveShape( QTimeLine::LinearCurve );
47 connect( m_timeLine, SIGNAL( frameChanged( int ) ), this, SLOT( fadeSlot( int ) ) );
48 connect( m_timeLine, SIGNAL( finished() ), this, SLOT( fadeFinished() ) );
51 GraphicsItemFader::~GraphicsItemFader()
53 m_timeLine->stop();
54 fadeSlot( m_animationSteps );
57 void GraphicsItemFader::setStartAlpha(int alpha)
59 m_startAlpha = alpha;
60 m_fadeColor.setAlpha( m_startAlpha );
63 void GraphicsItemFader::setTargetAlpha(int alpha)
65 m_targetAlpha = alpha;
68 void GraphicsItemFader::setDuration(int ms)
70 m_duration = ms;
73 void GraphicsItemFader::setFPS(int fps)
75 m_fps = fps;
78 void GraphicsItemFader::fadeSlot(int step)
80 debug() << "step # " << step << " of " << m_animationSteps << endl;
81 qreal newAlpha = m_startAlpha + ( step * m_alphaStep );
82 m_fadeColor.setAlpha( (int)newAlpha );
84 QPen fadePen = m_contentItem->pen();
85 QColor penColor = fadePen.color();
86 penColor.setAlpha( (int)newAlpha );
87 fadePen.setColor( penColor );
88 m_contentItem->setPen( fadePen );
90 m_contentItem->titleItem()->setDefaultTextColor( QColor( 255, 255, 255, (int)newAlpha ) );
92 QLinearGradient titleBarRectGradient( QPointF( 0, 0 ), QPointF( 0, m_contentItem->titleBarRect()->boundingRect().height() ) );
93 titleBarRectGradient.setColorAt( 0, QColor( 200, 200, 255, (int)newAlpha ) );
94 titleBarRectGradient.setColorAt( 1, QColor( 50, 50, 255, (int)newAlpha ) );
95 m_contentItem->titleBarRect()->setBrush( QBrush( titleBarRectGradient ) );
97 QLinearGradient contentRectGradient( QPointF( 0, 0 ), QPointF( 0, 10) );
98 contentRectGradient.setColorAt( 0, QColor( 150, 150, 150, (int)newAlpha ) );
99 contentRectGradient.setColorAt( 1, QColor( 255, 255, 255, (int)newAlpha ) );
100 m_contentItem->contentRect()->setBrush( QBrush( contentRectGradient ) );
102 m_contentItem->update();
105 void GraphicsItemFader::fadeFinished()
107 DEBUG_BLOCK
108 emit( animationComplete() );
111 void GraphicsItemFader::startFading()
113 QTimer::singleShot( m_delay, this, SLOT(begin()) );
116 void GraphicsItemFader::begin()
118 if( m_timeLine->state() != QTimeLine::NotRunning )
119 m_timeLine->stop();
121 int fps = ( m_fps ? m_fps : 25 );
123 //total number of animation steps;
124 m_animationSteps = (int) ( fps * ( ( qreal ) m_duration / 1000.0 ) );
126 if( m_animationSteps == 0 )
127 m_animationSteps = 1;
129 //how much should alpha change each step
130 m_alphaStep = ( ( qreal ) ( m_targetAlpha - m_startAlpha ) ) / ( qreal ) m_animationSteps;
132 debug() << "Start fading, animationSteps = " << m_animationSteps << " over " << m_duration << " mseconds, alphaStep = " << m_alphaStep << endl;
134 m_timeLine->setDuration( m_duration );
135 m_timeLine->setFrameRange( 0, m_animationSteps );
136 m_timeLine->start();
139 #include "graphicsitemfader.moc"