1 /***************************************************************************
2 * Copyright (c) 2007 Nikolaj Hald Nielsen <nhnFreespirit@gmail.com> *
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. *
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. *
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"
26 using namespace Context
;
29 GraphicsItemFader::GraphicsItemFader( QGraphicsItem
* item
, QGraphicsItem
* parent
)
31 , QGraphicsItem( parent
)
33 , m_targetAlpha( 255 )
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()
64 fadeSlot( m_animationSteps
);
65 m_contentItem
->setParentItem( m_itemPreviousParent
);
68 void GraphicsItemFader::setStartAlpha(int 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
)
85 void GraphicsItemFader::setFPS(int fps
)
90 QRectF
Context::GraphicsItemFader::boundingRect() const
92 return QRect( x(), y(), m_width
, m_height
);
96 void GraphicsItemFader::setFadeColor(const QColor
& 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()
118 emit( animationComplete() );
121 void GraphicsItemFader::startFading()
123 if( m_timeLine
->state() != QTimeLine::NotRunning
)
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
);
138 void GraphicsItemFader::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
145 #include "graphicsitemfader.moc"