/* ============================================================
 * File  : tetrahedron.cpp
 * Author: Stephane Gibault
 * Date  : Sun Feb 4 2007
 * Description :
 *
 * 
 *  (C) 2007 by Stephane Gibault
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * ============================================================ */
//#include <QtGui>
#include "timestamp.h"

timestamp::timestamp ( QWidget * parent )
    : QLabel(parent)
{
	this->setMouseTracking(true);
	this->setAutoFillBackground(true);
	//this->setBackgroundRole( QPalette::Light );
	//this->setFrameShape( QFrame::Box );
	//
	mouse_position_x = 0;
	mouse_real_x = 0;
	wgisgrabbing = 0;
	initpos();
}

void timestamp::initpos()
{
	for (int i=0; i < 20; i++)
	{
		table_markpos[i] = 0;
	}
	this->repaint();
}

void timestamp::charge_frames()
{
	int i = 0;
	int j = 0;
	int k = 0;
	QString wl_str1;
	while ( paraframes.mid(i,1) != "$" && i <= paraframes.length() )
	{
		if ( paraframes.mid(i,1) == "|" )
		{
			i++;
			wl_str1 = ""; 
			while ( paraframes.mid(i,1) != ">" && i <= paraframes.length() )
			{
				wl_str1 = wl_str1 + paraframes.mid(i,1);
				i++;
			}
			j = wl_str1.toInt();
			k = 0;
			i++;
			wl_str1 = "";
			while( paraframes.mid(i,1) != "|" && paraframes.mid(i,1) != "$" )
			{
				if ( paraframes.mid(i,1) != " " )
				{
					wl_str1 = wl_str1 + paraframes.mid(i,1);
				}
				else
				{
					if (j > 0)
					{
						table_coo[(j*25)-1][k] = wl_str1.toFloat();
					}
					else
					{
						table_coo[0][k] = wl_str1.toFloat();
					}
					k++;
					wl_str1 = "";
				}
				i++;
			}
			if (j > 0)
			{
				table_coo[(j*25)-1][k] = wl_str1.toFloat();
			}
			else
			{
				table_coo[0][k] = wl_str1.toFloat();
			}
			table_markpos[j] = 1;
		}
		else
		{
			i++;
		}
	}
	if (paraframes.mid(1,1) != "0")
	{
	       	table_coo[0][0] = 0;
	       	table_coo[0][1] = 0;
	       	table_coo[0][2] = 0;
	       	table_coo[0][3] = 0;
	       	table_coo[0][4] = 0;
	       	table_coo[0][5] = -3.8;
		table_markpos[0] = 1;
	}
	int sch = paraframes.lastIndexOf( "|" + QString::number(duree_diapo) );
	if ( sch < 0 )
	{
	       	table_coo[(duree_diapo*25)-1][0] = 0;
	       	table_coo[(duree_diapo*25)-1][1] = 0;
	       	table_coo[(duree_diapo*25)-1][2] = 0;
	       	table_coo[(duree_diapo*25)-1][3] = 0;
	       	table_coo[(duree_diapo*25)-1][4] = 0;
	       	table_coo[(duree_diapo*25)-1][5] = -3.8;
		table_markpos[duree_diapo] = 1;
	}
}

void timestamp::mouseMoveEvent ( QMouseEvent * event )
{
	mouse_position_x = ((event->x() / ((this->width()-10) / duree_diapo))*((this->width()-10) / duree_diapo))+5;
	if (event->buttons() & Qt::LeftButton) 
	{
		if ( event->x() > 0 && event->x() <= this->width() )
		{
			mouse_real_x = event->x();
			emit follow((float)((float)event->x()/(float)((float)this->width() / (float)duree_diapo))*25);
		}
	}
	this->repaint();
}

void timestamp::mousePressEvent ( QMouseEvent * event )
{
	if (event->buttons() & Qt::RightButton) 
	{
		if ( table_markpos[event->x()/(this->width() / (duree_diapo+1))] == 0 )
		{
			table_markpos[event->x()/(this->width() / (duree_diapo+1))] = 1;
			emit mark_coo((event->x()/(this->width() / (duree_diapo+1)))*25);
		}
		else
		{
			table_markpos[event->x()/(this->width() / (duree_diapo+1))] = 0;
			emit umark_coo();
		}
		this->repaint();
	}
	else
	{
		if ( event->x() && event->x() <= this->width() )
		{
			emit follow((float)((float)event->x()/(float)((float)this->width() / (float)duree_diapo))*25);
			mouse_real_x = event->x();
			wgisgrabbing = 1;
		}
	}	
}

void timestamp::mouseReleaseEvent ( QMouseEvent * event )
{
	wgisgrabbing = 0;
	mouse_real_x = event->x();
}

void timestamp::paintEvent(QPaintEvent *)
{
	QPainter p;
	p.begin(this);
	p.setRenderHint(QPainter::Antialiasing,true);
	p.setPen(Qt::NoPen);
	QPalette mypal(QApplication::palette());
	QBrush mybrush_1 = mypal.window();
	QBrush mybrush_3 = mypal.dark();
	QLinearGradient Gradient1(0.0, this->height()/2,0.0,0.0);
	Gradient1.setColorAt(0.0, mybrush_1.color());
	Gradient1.setColorAt(1.0, mybrush_3.color());
	p.fillRect(0,0,this->width(),this->height(),QBrush( Gradient1 ));

	p.setBrush(QColor(150,150,150));
	p.drawRect(0,0,this->width(),20);
	p.setBrush(QColor(200,200,200));
	p.setPen(QColor(120,120,120));

	QFont serifFont("Times", 9, QFont::Bold);
	p.setPen(Qt::DotLine); //Qt::SolidLine);
	p.setPen(QColor(120,120,120));
	for ( int i=0; i <= duree_diapo; i++ )
	{
		//p.drawLine(position_draw+5, 20, position_draw+5, this->height());
		position_draw = i * ((this->width() - 10) / duree_diapo);
		if ( table_markpos[i] == 1 )
		{
			//p.setBrush(QColor(255,0,0));
			//p.drawEllipse ( position_draw, (this->height() - 10)/2, 10, 10 );
			if ( i == duree_diapo )
			{
				p.drawImage ( position_draw-24, (this->height() - 10)/2, QImage(":/new/prefix2/Interface/Theme/flag_red.png").mirrored(true,false), 0, 0, 32, 32  );

			}
			else
			{
				p.drawImage ( position_draw, (this->height() - 10)/2, QImage(":/new/prefix2/Interface/Theme/flag_green.png"), 0, 0, 32, 32  );
			}
		}
		else
		{
			if ( i == duree_diapo )
			{
				p.drawImage ( position_draw-24, (this->height() - 10)/2, QImage(":/new/prefix2/Interface/Theme/flag_grey.png").mirrored(true,false), 0, 0, 32, 32  );

			}
			else
			{
				p.drawImage ( position_draw, (this->height() - 10)/2, QImage(":/new/prefix2/Interface/Theme/flag_grey.png"), 0, 0, 32, 32  );
			}
			//p.setBrush(QColor(200,200,200));
			//p.drawEllipse ( position_draw, (this->height() - 10)/2, 10, 10 );
		}
		p.setFont(serifFont);
		p.setPen(QColor(80,80,80));
		p.drawText ( position_draw + 2, 15, QString::number(i) );
		p.setPen(QColor(120,120,120));
	}
	int laststop = (mouse_position_x/(this->width() / (duree_diapo+1)));
	laststop--;
	while (table_markpos[laststop] == 0 && laststop > 0)
	{
		laststop--;
	}
	if ( wgisgrabbing == 0 )
	{
		p.setPen ( QColor(255,0,0) );
		p.fillRect ( (laststop * ((this->width()-10) / duree_diapo))+5, 20, mouse_position_x - (laststop * ((this->width()-10) / duree_diapo))-5, this->height() - 20,QBrush( QColor(255,0,0,100) ) );
		p.drawLine ( mouse_position_x, 20, mouse_position_x, this->height() );
	}
	if (wgisgrabbing == 1)
	{
		p.setPen ( QColor(0,255,0) );
		p.drawLine ( mouse_real_x, 20, mouse_real_x, this->height() );
	}
	p.end();
}


