/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2013                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Élément graphique qui affiche une règle de graduation temporelle dans le bas
	de la zone d'affichage.

	Utilisée dans la page de visualisation des passages de train.

*/

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

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

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


#include "GraphRulerWidget.h"
#include <QPainter>
#include <QTextStream>
#include <QStyleOptionGraphicsItem>

CGraphRulerWidget::CGraphRulerWidget(qreal RulerPixelWidth, QGraphicsItem *Parent)
{
    setParentItem(Parent);
    mPixelWidth = RulerPixelWidth;
    mPixelPitch = RulerPixelWidth/RULER_NB_TICKS;

	mRulerPixmap = new QPixmap(RulerPixelWidth,boundingRect().height()+1);
}

CGraphRulerWidget::~CGraphRulerWidget()
{
	delete mRulerPixmap;
}

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

    painter->setClipRect(option->exposedRect);
	painter->drawPixmap(0,0,*mRulerPixmap);

//	static int toto = 0;
//	qDebug("CGraphRulerWidget::paint %d",toto++);

//    qreal x = 0;
//    QRectF TextRect(0,0,100,10);
//    QString TimeText;
//    qreal tick = (mEndTime - mStartTime)/mPixelWidth;
//	QFont font;
//	font.setPixelSize(12);
//	painter->setFont(font);
//    for(int i = 0; i <= RULER_NB_TICKS; i++)
//    {
//        QLine line;
//        if(i%20 == 0)
//        {
//            TimeText.clear();
//            line.setLine(x,0,x,10); //long line
//            TextRect.moveCenter(QPoint(x,15));
//            qreal time = (x*tick) + mStartTime;

//            if(time < 1000)
//            {
//                QTextStream(&TimeText) << time << " ns";
//            }
//            else
//            {
//                time /= 1000;
//                if(time < 1000)
//                {
//                    QTextStream(&TimeText) << time << " us";
//                }
//                else
//                {
//                    time /= 1000;
//                    if(time < 1000)
//                    {
//                        QTextStream(&TimeText) << time << " ms";
//                    }
//                    else
//                    {
//                        time /= 1000;
//                        if(time < 1000)
//                        {
//                            QTextStream(&TimeText) << time << " s";
//                        }
//                    }
//                }

//            }

//            painter->drawText(TextRect,Qt::AlignCenter,TimeText);

//        }
//        else
//        {
//            line.setLine(x,0,x,5);
//        }
//        painter->drawLine(line);
//        x += mPixelPitch;
//    }
}

unsigned int CGraphRulerWidget::SetRange(quint64 StartTime, quint64 EndTime)
{
    if(StartTime > EndTime)
        return false;

    mStartTime = StartTime;
    mEndTime = EndTime;

	Render();

    update();

    return RET_OK;
}

void CGraphRulerWidget::Render()
{
	qreal x = 0;
	QRectF TextRect(0,0,100,10);
	QString TimeText;
	qreal tick = (mEndTime - mStartTime)/mPixelWidth;
	QFont font;

	mRulerPixmap->fill(Qt::white);
	QPainter *painter = new QPainter(mRulerPixmap);

	font.setPixelSize(12);
	painter->setFont(font);
	for(int i = 0; i <= RULER_NB_TICKS; i++)
	{
		QLine line;
		if(i%20 == 0)
		{
			TimeText.clear();
			line.setLine(x,0,x,10); //long line

			TextRect.moveCenter(QPoint(x,15));
			if(i == 0)
				TextRect.moveLeft(x);
			else if(i == RULER_NB_TICKS)
				TextRect.moveRight(x);


			qreal time = (x*tick) + mStartTime;

			if(time < 1000)
			{
				QTextStream(&TimeText) << time << " ns";
			}
			else
			{
				time /= 1000;
				if(time < 1000)
				{
					QTextStream(&TimeText) << time << " us";
				}
				else
				{
					time /= 1000;
					if(time < 1000)
					{
						QTextStream(&TimeText) << time << " ms";
					}
					else
					{
						time /= 1000;
						if(time < 1000)
						{
							QTextStream(&TimeText) << time << " s";
						}
					}
				}

			}

            if(i == 0)
                painter->drawText(TextRect,Qt::AlignLeft | Qt::AlignVCenter,TimeText);
            else if(i == RULER_NB_TICKS)
                painter->drawText(TextRect,Qt::AlignRight | Qt::AlignVCenter,TimeText);
            else
				painter->drawText(TextRect,Qt::AlignCenter,TimeText);

		}
		else
		{
			line.setLine(x,0,x,5);
		}
		painter->drawLine(line);
		x += mPixelPitch;
	}

	delete painter;

}

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

	delete mRulerPixmap;
	mRulerPixmap = new QPixmap(mPixelWidth,boundingRect().height()+1);
}
