/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
    Description:
	Page permettant de visualiser le fichier ZTLog.txt

*/

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

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

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


#include "ZTLogViewerPage.h"
#include <QPainter>
#include "GlobalDefine.h"
#include <QDialog>
#include <QMainWindow>
#include "Zonetest.h"
#include <QFont>
#include <QGraphicsProxyWidget>

#include "ZTLog.h"



CZTLogViewerPage::CZTLogViewerPage(QGraphicsWidget *Parent)
{
	Q_UNUSED(Parent)


	mProgramHandle = 0;

    mBackgroundRect = new QGraphicsRectItem(boundingRect(), this);
	QBrush BackgroundBrush(QColor(245, 245, 255));
	mBackgroundRect->setBrush(BackgroundBrush);

	QGraphicsTextItem *Title = new QGraphicsTextItem("Paramètres généraux",this);
	QFont font;
	font.setPointSize(18);
	Title->setFont(font);
	Title->setPos(40,10);

	Title = new QGraphicsTextItem("Ajuster date & heure",this);
	Title->setFont(font);
	Title->setPos(270,130);

    mCancelButton = new CTextButtonWidget("Fermer");
	mCancelButton->setParentItem(this);
    mCancelButton->setPos(700,550);
	connect(mCancelButton,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));


	mDestroyZTLogBtn = new CTextButtonWidget("Détruire ZTLog");
	mDestroyZTLogBtn->setParentItem(this);
	mDestroyZTLogBtn->setPos(400,550);
	connect(mDestroyZTLogBtn,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));

    mDestroyZTLogBtn->hide();


	mZTLogTextZone = new QTextEdit();
	QGraphicsProxyWidget *Proxy = new QGraphicsProxyWidget(this);
	Proxy->setWidget(mZTLogTextZone);
	Proxy->setPos(10,10);
	mZTLogTextZone->resize(780,500);
    mZTLogTextZone->setReadOnly(true);
	mZTLogTextZone->setWordWrapMode(QTextOption::NoWrap);

}

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


	mBackgroundRect->setRect(boundingRect());
}

void CZTLogViewerPage::ButtonClicked(CTextButtonWidget *BtnPtr)
{
    if(BtnPtr == mCancelButton)
	{
		mProgramHandle->HideZTLogViewerPage();
		hide();
	}
	if(BtnPtr == mDestroyZTLogBtn)
	{
		CZTLog::instance()->DeleteLogFile();
		LoadAndDisplayZTLog();

	}
}

void CZTLogViewerPage::LoadAndDisplayZTLog()
{
	mZTLogTextZone->clear();

	mZTLogTextZone->setPlainText(CZTLog::instance()->GetEntireLogFile());

    QTextCursor Cursor = mZTLogTextZone->textCursor();
    Cursor.movePosition(QTextCursor::End);
    mZTLogTextZone->setTextCursor(Cursor);
    mZTLogTextZone->ensureCursorVisible();
}

void CZTLogViewerPage::showEvent(QShowEvent *event)
{
	LoadAndDisplayZTLog();
	return;

	Q_UNUSED(event)
}

//Grab the mouse if the user clicks outside buttons
void CZTLogViewerPage::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
	Q_UNUSED(event)
}
void CZTLogViewerPage::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
	Q_UNUSED(event)
}
