If you've got a modern machine, turn on the eyecandy bool in contextview.cpp for...
[amarok.git] / src / contextview / graphicsitemscaler.cpp
blob94fab636ffdbe65ac2489517740aeeea5f2acecf
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 "graphicsitemscaler.h"
24 #include <math.h>
25 #include "debug.h"
27 #include <QPen>
28 #include <QTimer>
30 using namespace Context;
32 GraphicsItemScaler::GraphicsItemScaler( ContextBox *item )
33 : QObject()
34 , m_currWidth( item->boundingRect().width() )
35 , m_currHeight( item->boundingRect().height() )
36 , m_originalWidth( (int) item->boundingRect().width() )
37 , m_originalHeight( (int) item->boundingRect().height() )
38 , m_tgtWidth( 0 )
39 , m_tgtHeight( 0 )
40 , m_negativeWidth( false )
41 , m_negativeHeight( false )
42 , m_fps( 0 )
43 , m_duration( 5000 )
44 , m_delay( 0 )
46 DEBUG_BLOCK
47 m_contentItem = item;
49 m_timeLine = new QTimeLine( m_duration, this );
50 connect( m_timeLine, SIGNAL( frameChanged( int ) ), this, SLOT( scaleSlot( int ) ) );
51 connect( m_timeLine, SIGNAL( finished() ), this, SLOT( scaleFinished() ) );
54 GraphicsItemScaler::~GraphicsItemScaler()
56 m_timeLine->stop();
57 scaleSlot( m_animationSteps );
60 void GraphicsItemScaler::setTargetSize(int width, int height)
62 m_tgtWidth = width;
63 m_tgtHeight = height;
66 void GraphicsItemScaler::setDuration(int ms)
68 m_duration = ms;
71 void GraphicsItemScaler::setFPS(int fps)
73 m_fps = fps;
76 void GraphicsItemScaler::scaleSlot(int step)
78 debug() << "step # " << step << " of " << m_animationSteps << endl;
79 if( step == 0 || ( m_currWidth == m_tgtWidth && m_currHeight == m_tgtHeight ) )
80 return;
81 qreal percent = ( qreal ) step / m_animationSteps;
83 qreal desiredWidth, desiredHeight;
84 if( m_negativeWidth )
85 desiredWidth = m_originalWidth + ( percent * ( m_tgtWidth - m_originalWidth ) );
86 else
87 desiredWidth = percent * m_tgtWidth;
89 if( m_negativeHeight )
90 desiredHeight = m_originalHeight + ( percent * ( m_tgtHeight - m_originalHeight ) );
91 else
92 desiredHeight = percent * m_tgtHeight;
94 qreal scalefactorWidth = desiredWidth / m_currWidth;
95 qreal scalefactorHeight = desiredHeight / m_currHeight;
97 m_contentItem->scale( scalefactorWidth, scalefactorHeight );
98 m_currWidth = desiredWidth;
99 m_currHeight = desiredHeight;
100 m_contentItem->update();
103 void GraphicsItemScaler::scaleFinished()
105 DEBUG_BLOCK
106 scaleSlot( m_animationSteps );
107 emit( animationComplete() );
110 void GraphicsItemScaler::startScaling()
112 QTimer::singleShot( m_delay, this, SLOT(begin()) );
115 void GraphicsItemScaler::begin()
117 DEBUG_BLOCK
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) round( ( fps * ( ( qreal ) m_duration / 1000.0 ) ) );
126 if( m_animationSteps == 0 )
127 m_animationSteps = 1;
129 m_originalWidth = (int) m_currWidth;
130 m_originalHeight = (int) m_currHeight;
132 if( m_tgtWidth - m_currWidth < 0 )
133 m_negativeWidth = true;
134 if( m_tgtHeight - m_currHeight < 0 )
135 m_negativeHeight = true;
137 debug() << "Start scaling, animationSteps = " << m_animationSteps << " over " << m_duration << " mseconds" << endl;
139 m_timeLine->setDuration( m_duration );
140 m_timeLine->setFrameRange( 0, m_animationSteps );
141 m_timeLine->start();
144 #include "graphicsitemscaler.moc"