/* ============================================================
 * 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 <QDropEvent>
#include "timeline.h"

timeline::timeline ( QWidget * parent )
    : QFrame(parent)
{
	myparent = parent;
	this->setAcceptDrops(true);
	this->setMouseTracking (true);
	QLabel *winsert = new QLabel(this);
	pntinsert = winsert;
	max_diapo = -1;
	lengthtimeline = 0;
	postimeline = 0;
	tabposition = 0;
	typeimport = 0;

	insert_movie = new QMovie(":/new/prefix2/Interface/Theme/insertion.gif");
	if ( insert_movie->isValid() == false )
	{
		qDebug() << "ERROR LOADING PICTURE INSERT IN TIMELINE";
	}
	else
	{
		insert_movie->setCacheMode( QMovie::CacheAll );
		insert_movie->jumpToFrame( 0 );
	}
	pntinsert->setGeometry( 100, 3, 32, 32);
	pntinsert->setMovie ( insert_movie );
	pntinsert->hide();
	//
	removeDiapo = new QAction(tr("Supprimer la diapositive selectionnee"), this);
	removeDiapo->setIcon ( QIcon(":/new/prefix2/Interface/Theme/trash_empty.png") );
	editDiapo = new QAction(tr("Editer la diapositive"), this);
	editDiapo->setIcon ( QIcon(":/new/prefix2/Interface/Theme/applications_graphics.png") );
	copyDiapo = new QAction(tr("Copier les coordonnees"), this);
	copyDiapo->setIcon ( QIcon(":/new/prefix2/Interface/Theme/copy.png") );
	pasteDiapo = new QAction(tr("Coller les coordonnees"), this);
	pasteDiapo->setIcon ( QIcon(":/new/prefix2/Interface/Theme/editpaste.png") );
	addaudioDiapo = new QAction(tr("Ajouter une piste audio"), this);
	addaudioDiapo->setIcon ( QIcon(":/new/prefix2/Interface/Theme/document_open.png") );
	synaudioDiapo = new QAction(tr("Ajuster la duree du diaporama sur celle de la piste audio"), this);
	synaudioDiapo->setIcon ( QIcon(":/new/prefix2/Interface/Theme/tip.png") );
	delaudioDiapo = new QAction(tr("Supprimer la piste audio"), this);
	delaudioDiapo->setIcon ( QIcon(":/new/prefix2/Interface/Theme/trash_empty.png") );
	stopaudioDiapo = new QAction(tr("Terminer la piste audio a la fin de cette diapositive"), this);
	stopaudioDiapo->setIcon ( QIcon(":/new/prefix2/Interface/Theme/flag_red.png") );
	melangeDiapo = new QAction(tr("Melanger automatiquement les diapositives"), this);
	melangeDiapo->setIcon ( QIcon(":/new/prefix2/Interface/Theme/roll.png") );
	duplicateDiapo = new QAction(tr("Dupliquer la diapositive courante"), this);
	duplicateDiapo->setIcon ( QIcon(":/new/prefix2/Interface/Theme/tab_duplicate.png") );

	commentDiapo = new QAction(tr("Ajouter un commentaire audio a la diapositive"), this);
	commentDiapo->setIcon ( QIcon(":/new/prefix2/Interface/Theme/comment.png") );
	delcommentDiapo = new QAction(tr("Supprimer le commentaire audio"), this);
	delcommentDiapo->setIcon ( QIcon(":/new/prefix2/Interface/Theme/trash_empty.png") );

	connect(removeDiapo, SIGNAL(triggered()), this, SLOT(deletethis()));
	connect(editDiapo, SIGNAL(triggered()), this, SLOT(editthis()));
	connect(copyDiapo, SIGNAL(triggered()), this, SLOT(copythis()));
	connect(pasteDiapo, SIGNAL(triggered()), this, SLOT(pastethis()));
	connect(addaudioDiapo, SIGNAL(triggered()), this, SLOT(addaudio()));
	connect(synaudioDiapo, SIGNAL(triggered()), this, SLOT(syncaudio()));
	connect(delaudioDiapo, SIGNAL(triggered()), this, SLOT(delaudio()));
	connect(stopaudioDiapo, SIGNAL(triggered()), this, SLOT(stopaudio()));
	connect(melangeDiapo, SIGNAL(triggered()), this, SLOT(melange()));
	connect(duplicateDiapo, SIGNAL(triggered()), this, SLOT(duplicate()));
	connect(commentDiapo, SIGNAL(triggered()), this, SLOT(comment()));
	connect(delcommentDiapo, SIGNAL(triggered()), this, SLOT(delcomment()));
}

void timeline::comment()
{
	emit dia_comment();
}

void timeline::delcomment()
{
	emit dia_delcomment();
}

void timeline::duplicate()
{
	emit dia_duplicate();
}

void timeline::melange()
{
	emit dia_melange();
}

void timeline::deletethis()
{
	emit dia_delete();
}

void timeline::editthis()
{
	emit dia_edit();
}

void timeline::copythis()
{
	emit dia_copy();
}

void timeline::pastethis()
{
	emit dia_paste();
}

void timeline::addaudio()
{
	emit s_addaudio();
}

void timeline::syncaudio()
{
	emit s_syncaudio();
}

void timeline::delaudio()
{
	emit s_delaudio();
}

void timeline::stopaudio()
{
	emit s_stopaudio();
}

void timeline::contextMenuEvent(QContextMenuEvent *event)
{
	if ( max_diapo >= 0 && tabposition == 0 )
	{
		if ( zik_number == 0 )
		{
			delaudioDiapo->setDisabled(true);
			synaudioDiapo->setDisabled(true);
			addaudioDiapo->setEnabled(true);
		}
		else
		{
			delaudioDiapo->setEnabled(true);
			synaudioDiapo->setEnabled(true);
			addaudioDiapo->setDisabled(true);
		}
		if ( iscomment == 0 )
		{
			commentDiapo->setEnabled(true);
			delcommentDiapo->setDisabled(true);
		}
		else
		{
			commentDiapo->setDisabled(true);
			delcommentDiapo->setEnabled(true);
		}
		QMenu menu(this);
		menu.addAction(removeDiapo);
		menu.addAction(duplicateDiapo);
		menu.addAction(editDiapo);
		menu.addAction(copyDiapo);
		menu.addAction(pasteDiapo);
		menu.addSeparator();
		menu.addAction(melangeDiapo);
		menu.addSeparator();
		menu.addAction(addaudioDiapo);
		if ( zik_1->current < zik_1->fin && zik_1->current >= zik_1->position && zik_number > 0 )
		{
			menu.addAction(stopaudioDiapo);
		}
		menu.addAction(delaudioDiapo);
		menu.addAction(synaudioDiapo);
		menu.addSeparator();
		menu.addAction(commentDiapo);
		menu.addAction(delcommentDiapo);

		menu.exec(event->globalPos());
	}
}

void timeline::dragEnterEvent(QDragEnterEvent *event)
{
	event->acceptProposedAction();
}

void timeline::wheelEvent(QWheelEvent *event)
{
	if ( event->delta() > 0 )
	{
		emit slice(-25);
	}
	else
	{
		emit slice(25);
	}
}

void timeline::dragMoveEvent( QDragMoveEvent * event ) 
{
	if ( event->pos().x() < (max_diapo+1) * 107 && max_diapo > -1 )
	{
		pntinsert->setGeometry( (int)(((event->pos().x() / 107)*107))-5, 0, 22, 22);
	}
	else
	{
		pntinsert->setGeometry( (int)(((max_diapo+1)*107))-5, 0, 22, 22);
	}
	if ( pntinsert->isVisible() == false )
	{
		pntinsert->raise();
		pntinsert->show();
		insert_movie->start();
	}
	if ( (event->pos().x() - postimeline) > (lengthtimeline - 100) )
	{
		QCursor::setPos(QCursor::pos().x() + 1,QCursor::pos().y());
		emit move_left();
		QCursor::setPos(QCursor::pos().x() - 1,QCursor::pos().y());
	}  
	if ( (event->pos().x() - postimeline) < 100 )
	{
		QCursor::setPos(QCursor::pos().x() + 1,QCursor::pos().y());
		emit move_right();
		QCursor::setPos(QCursor::pos().x() - 1,QCursor::pos().y());
	} 
}

void timeline::dropEvent(QDropEvent *event)
{
	drop_x = event->pos().x();
	pntinsert->hide();
	insert_movie->stop();

	if ( event->mimeData()->hasUrls() == true )
	{
		myurl = event->mimeData()->urls();
		wl_importation = 0;
		typeimport = 1;
		emit prepare_drop();

		while ( wl_importation <= myurl.count() - 1 )
		{
			if ( myurl[wl_importation].toString(QUrl::None).right(4) == ".jpg" || myurl[wl_importation].toString(QUrl::None).right(5) == ".jpeg" || myurl[wl_importation].toString(QUrl::None).right(4) == ".JPG" || myurl[wl_importation].toString(QUrl::None).right(5) == ".JPEG" || myurl[wl_importation].toString(QUrl::None).right(4) == ".png" || myurl[wl_importation].toString(QUrl::None).right(4) == ".PNG")
			{
				wl_pathname = myurl[wl_importation].toString(QUrl::None);
				wl_importation++;
				event->acceptProposedAction();
				emit godrop();
				drop_x = drop_x + 117;
			}
			else
			{
				wl_importation++;
				event->acceptProposedAction();
			}
		}
		typeimport = 0;
	}
	else
	{
		if (event->mimeData()->hasText())
		{
			wl_pathname = event->mimeData()->text();
			event->acceptProposedAction();
			emit godrop();	
		}
	}
}

void timeline::dragLeaveEvent ( QDragLeaveEvent * event )
{
	pntinsert->hide();
	insert_movie->stop();
}

Qt::DropActions timeline::supportedDropActions () const
{
	// returns what actions are supported when dropping
	return Qt::CopyAction | Qt::MoveAction;
}

