/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Affiche un rectangle qui "flash" en rouge lorsqu'une fonction de détection
	est désactivée.  Le clignotement est une petite animation qui change la
	transparence de la couleur de fond.
	
*/

/* ************************************************************************** */
/* Revision:
### 20130524 JFM
	Verision d'origine.
	
### YYYYMMDD Description du besoin ou du bug
	Description du changement.
 */

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

#include "EventItem.h"
#include <QPainter>
#include "ZTData.h"
#include <QGraphicsSceneMouseEvent>
#include "CDV.h"
#include <QPoint>

CEventItem::CEventItem(QString Label, bool Animated, QGraphicsItem *Parent, int Color)
{
	setParentItem(Parent);


    if(Color != EVENT_ITEM_COLOR_RED && Color != EVENT_ITEM_COLOR_GREEN)
    {
        Color = EVENT_ITEM_COLOR_RED;
    }

    mColor = Color;

	setGeometry(0,0,40,25);
	mCurBrush = new QBrush(Qt::lightGray);
	mLabelFont.setPixelSize(12);
	mLabelFont.setBold(true);

	SetLabel(Label);

	mAnimated = Animated;
	if(mAnimated)
	{
		mAnimationTimeline = new QTimeLine(1000);
		connect(mAnimationTimeline,SIGNAL(valueChanged(qreal)),this,SLOT(UpdateAnimation(qreal)));
		connect(mAnimationTimeline,SIGNAL(finished()),this,SLOT(AnimationFinished()));
		mAnimationTimeline->start();
	}

	mAlpha = 0;
}

CEventItem::~CEventItem()
{
	if(mAnimated)
		delete mAnimationTimeline;
}

void CEventItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	Q_UNUSED(option)
	Q_UNUSED(widget)

	QRectF EventRect = rect();

	if(mAnimated)
    {
        if(mColor == EVENT_ITEM_COLOR_RED)
        {
            mCurBrush->setColor(QColor(255,mAlpha,mAlpha,255));
        }
        else
        {
            mCurBrush->setColor(QColor(mAlpha,255,mAlpha,255));
        }
    }

	painter->setBrush(*mCurBrush);
	painter->setPen(Qt::black);
	painter->drawRect(EventRect);

//	QRectF textRect(CDVRect.adjusted(-3,-3,-3,-3));
//	painter->drawRect(textRect);

	int flags = Qt::AlignHCenter | Qt::AlignVCenter/* | Qt::TextWordWrap*/;

	painter->setPen(Qt::black);
	painter->setFont(mLabelFont);
	painter->drawText(EventRect, flags, mLabel);

}

void CEventItem::SetLabel(QString Label)
{
	mLabel = Label;
	QSizeF Size;
	Size = size();
	QFontMetrics FontMetrics(mLabelFont);
	Size.setWidth(FontMetrics.boundingRect(mLabel).width() + 10);
	resize(Size);
}

void CEventItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
	if(event->button() == Qt::LeftButton)
	{
		emit EventItemRightClicked(this);
	}
}

void CEventItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
	Q_UNUSED(event)
}

void CEventItem::UpdateAnimation(qreal value)
{
	mAlpha = 255*value;


	//update(QGraphicsItem::pos().x(),QGraphicsItem::pos().x(),40,25);
	update();
}

void CEventItem::AnimationFinished()
{
	mAnimationTimeline->toggleDirection();
	mAnimationTimeline->start();
}
