/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
    Description:
	Widget animé qui affiche une boîte contenant du texte.

*/

/* ************************************************************************** */
/* Revision:
### 20121220 JFM
    Verision d'origine.

### YYYYMMDD Description du besoin ou du bug
    Description du changement.
 */

/* ************************************************************************** */


#include "SimpleTextBoxWidget.h"

#include <QtGui/QtGui>
#include <QRectF>

CSimpleTextBoxWidget::CSimpleTextBoxWidget(QGraphicsItem *parent)
   : QGraphicsWidget(parent)
{
    opacity = 1.0;
    hide();
    //mText = "TEst";

    
    timeLine = new QTimeLine(500);
    timeLine->setCurveShape(QTimeLine::EaseOutCurve);
    connect(timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(setValue(qreal)));

    resize(SCREEN_RES_WIDTH, SCREEN_RES_HEIGHT);

    QTransform transform;

  //  transform.translate(160,120);
    transform.translate(SCREEN_RES_WIDTH/2,SCREEN_RES_HEIGHT/2);
    transform.scale(0,0);
    //transform.translate(-160,-120);
    transform.translate(-SCREEN_RES_WIDTH/2,-SCREEN_RES_HEIGHT/2);
    setTransform(transform);
}

CSimpleTextBoxWidget::~CSimpleTextBoxWidget()
{
	delete timeLine;
}

void CSimpleTextBoxWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    QRectF BoxRect;
    BoxRect.setWidth(SCREEN_RES_WIDTH - 200);
    BoxRect.setHeight(SCREEN_RES_HEIGHT - 200);
    BoxRect.moveCenter(QPoint(SCREEN_RES_WIDTH/2,SCREEN_RES_HEIGHT/2));
 //   painter->setOpacity(opacity);
    painter->setPen(QPen(Qt::black, 2));
    painter->setBrush(QColor(245, 245, 255, 200));
    painter->setClipRect(rect());
   // painter->drawRoundRect(25, 10, SCREEN_RES_WIDTH - 200, SCREEN_RES_HEIGHT - 200);
    painter->drawRoundRect(BoxRect);

    QRectF textRect(BoxRect);
 //   textRect.moveCenter(QPoint(160,120));
    int flags = Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextWordWrap;

    QFont font;
    font.setPixelSize(30);
    painter->setPen(Qt::black);
    painter->setFont(font);
    QString text = "Ceci est un test\nTexte multiligne";
    painter->drawText(textRect, flags, mText);

    //textRect.moveTop(textRect.top()+15);
    //painter->drawText(textRect, flags, mCallerNumber);
}


//void CSimpleTextBoxWidget::mousePressEvent(QGraphicsSceneMouseEvent * event )
//{
//    if(isVisible())
//    {
//	HideCallerID();
//	event->accept();
//    }
//    else
//    {
//	event->ignore();
//    }
//}


void CSimpleTextBoxWidget::setValue(qreal value)
{
    QTransform transform;


    //QRectF BoxRect = rect();
    transform.translate(SCREEN_RES_WIDTH/2,SCREEN_RES_HEIGHT/2);
    transform.scale(value,value);
    transform.translate(-SCREEN_RES_WIDTH/2,-SCREEN_RES_HEIGHT/2);
    setTransform(transform);

    if(value == 0 && timeLine->direction()== QTimeLine::Backward)
    {
        hide();
    }
}

void CSimpleTextBoxWidget::ShowTextBox()
{
    show();
    timeLine->setDirection(QTimeLine::Forward);
    timeLine->start();

}

void CSimpleTextBoxWidget::HideTextBox()
{
    timeLine->setDirection(QTimeLine::Backward);
    timeLine->start();
    //hide();
}

void CSimpleTextBoxWidget::InsertString(QString TextLine)
{
    if(!mText.isEmpty())
        mText += '\n';
    mText += TextLine;
    update(rect());
}

void CSimpleTextBoxWidget::ClearText()
{
    mText.clear();
}
