#include <QSize>
#include <QRect>
#include <QColor>
#include <QSignalTransition>
#include <QEasingCurve>
#include <QPainter>
#include <QBrush>
#include <QPen>
#include <QFontMetrics>
#include "Widget.h"

FlyInVanishEffect::FlyInVanishEffect( QWidget* pWidget ):
    m_TextPixmap( 1, 1 ), m_pWidget( pWidget )
{
    // 创建实例
    m_pMachine = new QStateMachine( this );
    m_pInitialState = new QState( m_pMachine );
    m_pMiddleState1 = new QState( m_pMachine );
    m_pMiddleState2 = new QState( m_pMachine );
    m_pFinalState = new QFinalState( m_pMachine );
    m_pFlyInAnim = new QPropertyAnimation( this );
    m_pVanishAnim = new QPropertyAnimation( this );
    m_pPauseAnim = new QPauseAnimation( this );

    // 设置状态之间的转换
    m_pInitialState->addTransition( this, SIGNAL( Start( ) ), m_pMiddleState1 )
            ->addAnimation( m_pFlyInAnim );
    m_pMiddleState1->addTransition( m_pMiddleState1, SIGNAL( entered( ) ), m_pMiddleState2 )
            ->addAnimation( m_pVanishAnim );
    m_pMiddleState2->addTransition( m_pMiddleState2, SIGNAL( entered( ) ), m_pFinalState )
            ->addAnimation( m_pPauseAnim );

    // 设置动画参数
    m_pFlyInAnim->setPropertyName( "Pos" );
    m_pFlyInAnim->setTargetObject( this );
    m_pPauseAnim->setDuration( 1000 );

    // 启动状态机
    m_pMachine->setInitialState( m_pInitialState );
    m_pMachine->start( );
}

void FlyInVanishEffect::RenderPixmap( const QString& text )
{
    QSize size = m_pWidget->fontMetrics( ).size( Qt::TextSingleLine, text );
    m_TextPixmap = m_TextPixmap.scaled( size );
    m_TextPixmap.fill( Qt::transparent );
    QPainter painter( &m_TextPixmap );

    // 绘制底纹
    painter.save( );
    painter.setPen( Qt::NoPen );
    painter.setBrush( QBrush( QColor( 255, 255, 0, 125 ) ) );// 黄色
    painter.drawRect( 0, 0, size.width( ), size.height( ) );

    // 绘制文字
    painter.restore( );
    painter.drawText( 0, m_pWidget->fontMetrics( ).ascent( ), text );
    painter.end( );


}

QPixmap& FlyInVanishEffect::Pixmap( void )
{
    return m_TextPixmap;
}

void FlyInVanishEffect::StartEffect( void )
{
    // 设置动画
    m_pFlyInAnim->setEasingCurve( QEasingCurve::InOutBack );
    m_pFlyInAnim->setStartValue( QPointF( m_pWidget->width( ), m_Pos.y( ) ) );
    m_pFlyInAnim->setEndValue( QPointF( ( m_pWidget->width( ) -
                                          m_TextPixmap.width( ) ) / 2, m_Pos.y( ) ) );
    m_pFlyInAnim->setDuration( 2000 );

    emit Start( );
}

Widget::Widget( QWidget* pParent ):
    QWidget( pParent ), m_Effect( this )
{
    m_Effect.RenderPixmap( tr( "Greatest challenge" ) );
    m_Effect.setPos( QPointF( 60, 300 ) );

    resize( 640, 360 );
}

void Widget::paintEvent( QPaintEvent* pEvent )
{
    Q_UNUSED( pEvent );
    QPainter painter( this );
        painter.drawText( 320, 307, "Good Example" );
    painter.drawPixmap( m_Effect.Pos( ).x( ),
                        m_Effect.Pos( ).y( ), m_Effect.Pixmap( ) );
    update( );
}

void Widget::keyPressEvent( QKeyEvent* pEvent )
{
    Q_UNUSED( pEvent );
    m_Effect.StartEffect( );

}
