/*******************************************************************************
*                                                                              *
* 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 <QFont>
#include <QGraphicsProxyWidget>
#include <QTextBlock>
#include <QScrollBar>
#include <QGraphicsSceneResizeEvent>




CZTLogViewerPage::CZTLogViewerPage(QGraphicsWidget *Parent, bool Embedded)
{
	Q_UNUSED(Parent)

    mZTLogText.clear();
    mIsEmbedded = Embedded;



//	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);

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

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


    //mZTLogTextZone = new QTextEdit();
    mZTLogTextZone = new QPlainTextEdit();
	QGraphicsProxyWidget *Proxy = new QGraphicsProxyWidget(this);
	Proxy->setWidget(mZTLogTextZone);
	Proxy->setPos(10,10);
	mZTLogTextZone->resize(780,500);
	mZTLogTextZone->setReadOnly(true);
	mZTLogTextZone->setWordWrapMode(QTextOption::NoWrap);
    QPalette p = mZTLogTextZone->palette();
    p.setColor(QPalette::Highlight,QColor(200,100,100));
    mZTLogTextZone->setPalette(p);

}

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

    if(mIsEmbedded)
    {
        mZTLogTextZone->resize(event->newSize().toSize());
    }
    else
    {
        mBackgroundRect->setRect(boundingRect());
    }
}

void CZTLogViewerPage::SetZTLogText(QString Text, QString FocusText)
{
    mZTLogText = Text;
    LoadAndDisplayZTLog();

    if(FocusText.length() != 0)
    {
        QTextCursor cursor = mZTLogTextZone->document()->find(FocusText);
        cursor.select(QTextCursor::LineUnderCursor);
        mZTLogTextZone->setFocus();
        mZTLogTextZone->setTextCursor(cursor);
        QScrollBar *VerticalSB = mZTLogTextZone->verticalScrollBar();
        mZTLogTextZone->centerCursor();
        //VerticalSB->setValue();
    }
}

void CZTLogViewerPage::ButtonClicked(CTextButtonWidget *BtnPtr)
{
    if(BtnPtr == mCancelButton)
	{
        mZTLogTextZone->clear();
		hide();
	}
}

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

    mZTLogTextZone->setPlainText(mZTLogText);
}

void CZTLogViewerPage::ScrollToLine(int Line)
{
    QTextBlock TextBlock = mZTLogTextZone->document()->findBlockByLineNumber(Line-1);
    QTextCursor cursor = mZTLogTextZone->textCursor();
    cursor.setPosition(TextBlock.position());
    mZTLogTextZone->setFocus();
    mZTLogTextZone->setTextCursor(cursor);
    mZTLogTextZone->centerCursor();
}

void CZTLogViewerPage::showEvent(QShowEvent *event)
{



	return;

	Q_UNUSED(event)
}

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