/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2013                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Widget Bouton de style "toggle" qui affiche une image lorsqu'il est désactivé
	et une autre image lorsqu'il est activé.

*/

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

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

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

#include "ToggleButtonWidget.h"
#include <QPainter>
#include <QGraphicsSceneMouseEvent>

CToggleButtonWidget::CToggleButtonWidget(QString ONImageFilePath, QString OFFImageFilePath,QGraphicsItem *Parent, QString Label)
{
	setParentItem(Parent);
	mButtonState = TOGGLE_BUTTON_OFF;

	mONPixmap = new QGraphicsPixmapItem(QPixmap(ONImageFilePath), this);
	mONPixmap->hide();
	mOFFPixmap = new QGraphicsPixmapItem(QPixmap(OFFImageFilePath), this);
	mOFFPixmap->show();

    mButtonEnabled = true;
	mButtonData = 0;

    if(Label.isEmpty() == false)
    {
        QGraphicsTextItem *BtnLabel = new QGraphicsTextItem(this);
        BtnLabel->setPlainText(Label);
        BtnLabel->adjustSize();
        BtnLabel->setPos(-BtnLabel->boundingRect().width()-3,0);
    }
}


void CToggleButtonWidget::SetData(unsigned int data)
{
	mButtonData = data;
}
unsigned int CToggleButtonWidget::GetData()
{
	return mButtonData;
}

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

void CToggleButtonWidget::mouseReleaseEvent( QGraphicsSceneMouseEvent * event)
{
	Q_UNUSED(event)

    if(mButtonEnabled == false)
        return ;

	if(mButtonState == TOGGLE_BUTTON_ON)
		mButtonState = TOGGLE_BUTTON_OFF;
	else
		mButtonState = TOGGLE_BUTTON_ON;

	UpdateButton();
    emit ButtonToggled(this);
}

void CToggleButtonWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
{
		Q_UNUSED(event)


	mONPixmap->setPixmap(mONPixmap->pixmap().scaled(boundingRect().width(),boundingRect().height()));
	mOFFPixmap->setPixmap(mOFFPixmap->pixmap().scaled(boundingRect().width(),boundingRect().height()));
}

unsigned int CToggleButtonWidget::GetButtonState()
{
	return mButtonState;
}

unsigned int CToggleButtonWidget::SetButtonEnabled(bool Enabled)
{
    mButtonEnabled = Enabled;
	return RET_OK;
}

unsigned int CToggleButtonWidget::SetButtonState(unsigned int State)
{
	if(mButtonState != TOGGLE_BUTTON_OFF && mButtonState != TOGGLE_BUTTON_ON)
		return RET_ERROR;

	mButtonState = State;
	UpdateButton();

	return RET_OK;
}


void CToggleButtonWidget::UpdateButton()
{
	if(mButtonState == TOGGLE_BUTTON_OFF)
	{
		mOFFPixmap->show();
		mONPixmap->hide();
	}
	else
	{
		mOFFPixmap->hide();
		mONPixmap->show();
	}
}
