1 /***************************************************************************
2 * Copyright (c) 2007 Nikolaj Hald Nielsen <nhnFreespirit@gmail.com> *
3 * 2007 Jeff Mitchell <kde-dev@emailgoeshere.com> *
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. *
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. *
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"
30 using namespace Context
;
32 GraphicsItemScaler::GraphicsItemScaler( ContextBox
*item
)
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() )
40 , m_negativeWidth( false )
41 , m_negativeHeight( false )
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()
57 scaleSlot( m_animationSteps
);
60 void GraphicsItemScaler::setTargetSize(int width
, int height
)
66 void GraphicsItemScaler::setDuration(int ms
)
71 void GraphicsItemScaler::setFPS(int 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
) )
81 qreal percent
= ( qreal
) step
/ m_animationSteps
;
83 qreal desiredWidth
, desiredHeight
;
85 desiredWidth
= m_originalWidth
+ ( percent
* ( m_tgtWidth
- m_originalWidth
) );
87 desiredWidth
= percent
* m_tgtWidth
;
89 if( m_negativeHeight
)
90 desiredHeight
= m_originalHeight
+ ( percent
* ( m_tgtHeight
- m_originalHeight
) );
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()
106 scaleSlot( m_animationSteps
);
107 emit( animationComplete() );
110 void GraphicsItemScaler::startScaling()
112 QTimer::singleShot( m_delay
, this, SLOT(begin()) );
115 void GraphicsItemScaler::begin()
118 if( m_timeLine
->state() != QTimeLine::NotRunning
)
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
);
144 #include "graphicsitemscaler.moc"