/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Élément graphique qui affiche un CDV.  L'affichage change en fonction de l'état
	du CDV (libre, occupé, itinéraire commandé).
	
*/

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

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

#include "CDVItem.h"
#include <QPainter>
#include "ZTData.h"
#include <QGraphicsSceneMouseEvent>
#include "CDV.h"



CCDVItem::CCDVItem(CCDV *CDVPtr, QGraphicsItem *Parent)
{
	if(Parent != 0)
		setParentItem(Parent);

	mCDVPtr = CDVPtr;
	mCDVFreeBrush = new QBrush(Qt::gray);
	mCDVITICommandedBrush = new QBrush(Qt::darkGreen);
    mCDVOccupiedBrush = new QBrush(QColor(255,128,0));

	mCDVState = CDV_STATE_FREE;
	mCurBrush = mCDVFreeBrush;

    setGeometry(0,0,90,20);
}

CCDVItem::~CCDVItem()
{
	delete mCDVFreeBrush;
	delete mCDVITICommandedBrush;
	delete mCDVOccupiedBrush;
}

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

	QRectF CDVRect = rect();

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

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

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

	QFont font;
//	font.setPointSizeF(9.5);
	font.setPixelSize(13);
	painter->setPen(Qt::black);
	painter->setFont(font);
	painter->drawText(CDVRect, flags, mCDVLabel);

}

unsigned int CCDVItem::SetParameters(QString Label/*, unsigned int posx, unsigned int posy*/)
{
	mCDVLabel = Label;
//	setPos(posx,posy);
	return RET_OK;
}

unsigned int CCDVItem::UpdateState()
{
	return SetState(mCDVPtr->GetCDVState());
}

unsigned int CCDVItem::SetState(unsigned int State)
{
	if(State >= CDV_STATE_UNKNOWN)
		return RET_ERROR;
	if(mCDVState == State)
		return RET_OK;

	mCDVState = State;
	switch(State)
	{
		case CDV_STATE_OCCUPIED:
		{
			mCurBrush = mCDVOccupiedBrush;
			break;
		}
		case CDV_STATE_FREE:
		{
			mCurBrush = mCDVFreeBrush;
			break;
		}
		case CDV_STATE_ITI_CMD:
		{
			mCurBrush = mCDVITICommandedBrush;
			break;
		}
	}
	update(rect());

	return RET_OK;
}

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

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

CCDV *CCDVItem::GetCDV()
{
	return mCDVPtr;
}
