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

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

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

#include "SwitchCDVItem.h"
#include <QPainter>
#include "ZTData.h"
#include <QGraphicsSceneMouseEvent>
#include "CDV.h"
#include "SwitchCDV.h"

CSwitchCDVItem::CSwitchCDVItem(CCDV *CDVPtr,QGraphicsItem *Parent)
	:CCDVItem(CDVPtr,Parent)
{
	mSwitchCDV = (CSwitchCDV*)CDVPtr;
	mSwitchPosition = mSwitchCDV->GetSwitchState();

    mNormalCDVRect = rect();
	mNormalPainterPath.addRect(rect());




    qreal nibble = rect().width()*0.25;

    int shim = 3;
    int WaysSpascing = rect().height() + 10;
    mReversedPainterPath.moveTo(0,pos().y()+WaysSpascing);
    mReversedPainterPath.lineTo(nibble-shim,pos().y()+WaysSpascing);                    // --
    mReversedPainterPath.lineTo(rect().width()-nibble-shim,0);                          // /
    mReversedPainterPath.lineTo(rect().width(),0);                                      // --
    mReversedPainterPath.lineTo(rect().width(),rect().height());                        //   |
    mReversedPainterPath.lineTo(rect().width()-nibble+shim,rect().height());            //  __
    mReversedPainterPath.lineTo(nibble+shim,pos().y()+WaysSpascing+rect().height());    // /
    mReversedPainterPath.lineTo(0,pos().y()+WaysSpascing+rect().height());              // __
    mReversedPainterPath.lineTo(0,pos().y()+WaysSpascing);                              // |


    this->setGeometry(0,0,90,70); //Change geometry so we can draw the reversed switch



}

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


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

    if(mSwitchPosition == SWITCH_CDV_NORMAL_POSITION)
    {
        painter->drawPath(mNormalPainterPath);
    }
    else
    {
        painter->drawPath(mReversedPainterPath);
    }

//	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(mNormalCDVRect, flags, mCDVLabel);

}

unsigned int CSwitchCDVItem::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;
		}
	}

	mSwitchPosition = mSwitchCDV->GetSwitchState();


	update(rect());

	return RET_OK;
}

//void CSwitchCDVItem::resizeEvent()
//{
////	mNormalPainterPath = QPainterPath();
////	mNormalPainterPath.addRect(rect());
////	mReversedPainterPath = QPainterPath();
////	mReversedPainterPath.addRect(rect());
//}

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

//void CSwitchCDVItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
//{
//	Q_UNUSED(event)
//}

