/****************************************************************************
** ManDVD
**
** Created: Mon May 08 14:00:00 2006
**      by: Gibault Stéphane
**	Hacked on by Mikael Böhm micko@bag.org
**
** This is a tool for simply creating DVD Video
** 
****************************************************************************/
//
// C++ Implementation: cutting movies
//
// Description: Functions (backend) for editing chapters
//
//
// Author: Mikael Böhm <micko@bag.org>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//

#include "mandvd_global.h"
#include <cut.h>
#include <mainfrm.h>

#include <qlabel.h>
#include <qtoolbutton.h>
#include <qbuttongroup.h>
#include <qlineedit.h>
#include <qcombobox.h>
#include <qslider.h>
#include <qtextedit.h>
#include <qspinbox.h>
#include <qcheckbox.h>
#include <qradiobutton.h>
#include <qmessagebox.h> 
#include <qlistview.h>


	/* Cutting Movie */	
	void
	mainfrm::bt_cut_film_clicked () {
	QListViewItem *i = lst_video_1->selectedItem ();
	if (i != 0 && i->text (2) != "9")
	{
	if (m_pMPlayerWidget->isPlaying () == true)
		{
		m_pMPlayerWidget->slotStop ();
		}
	path_video = i->text (1);
	m_pMPlayerWidget->setFilename (path_video);
	m_pMPlayerWidget->slotPlay ();
	durfilmchap = 0;
	
	while (durfilmchap == 0)
		{
		if (m_pMPlayerWidget->isPlaying () == false)
		{
		durfilmchap = 1;
		}
		else
		{
		m_pMPlayerWidget->slotReadStdout ();
		durfilmchap = m_pMPlayerWidget->m_fLength;
		}
		}
	if (m_pMPlayerWidget->isPlaying () == true)
		{
		m_pMPlayerWidget->slotStop ();
		}
	rc_001 = 0;
	rc_002 = 0;
	bt_cutting->setDisabled (true);
	lstcut->clear ();
	debtxt->setText ("0");
	endtxt->setText ("0");
	Etape_02->setDisabled (true);
	cutfilm->setGeometry (180, 145, 640, 410);
	cutfilm->show ();
	cutfilm->raise ();
	mediabar->setGeometry (190, 428, 341, 30);
	mediabar->show ();
	mediabar->raise ();
	vframe->setGeometry (190, 155, 341, 273);
	vframe->show ();
	vframe->raise ();
	tmp_004 = i->text (2);
	rc_003 = tmp_004.find ("//EDL//", 0);
	if (rc_003 != -1)
		{
		rc_004 = tmp_004.find ("/*EDL/*", 0);
		tmp_001 = tmp_004.mid (rc_003 + 7, rc_004 - (rc_003 + 7));
		tmp_010 = tmp_001;
		openedl (tmp_001);
		rc_004 = 1;
		}
	else
		{
		rc_004 = 0;
		}
	}
	}

	/* Cutting Movie */		
	void
	mainfrm::bt_fermer_cutfilm_clicked () {
	if (m_pMPlayerWidget->isPlaying () == true)
	{
	m_pMPlayerWidget->slotStop ();
	}
	cutfilm->hide ();
	mediabar->hide ();
	vframe->setGeometry (31, 125, 115, 71);
	Etape_02->setEnabled (true);
	vframe->show ();
	vframe->raise ();
	}

	/* Cutting Movie */	
	void
	mainfrm::deb_pnt_cut_clicked () {
	int zcalcul;
	QTime mytime;
	float fElapsedTime =
	m_pMPlayerWidget->m_rxParseElapsedTime.cap (1).toFloat ();
	zcalcul = (int) fElapsedTime;
	debtxt->setText (QString::number (zcalcul));
	rc_001 = 1;
	if (rc_001 == 1 && rc_002 == 1)
	{
	bt_cutting->setEnabled (true);
	}
	}

	/* Cutting Movie */	
	void
	mainfrm::fin_pnt_cut_clicked () {
	int zcalcul;
	QTime mytime;
	float fElapsedTime =
	m_pMPlayerWidget->m_rxParseElapsedTime.cap (1).toFloat ();
	zcalcul = (int) fElapsedTime;
	endtxt->setText (QString::number (zcalcul));
	rc_002 = 1;
	if (rc_001 == 1 && rc_002 == 1)
	{
	bt_cutting->setEnabled (true);
	}
	}

	/* Cutting Movie */	
	void
	mainfrm::bt_cutting_clicked () {
	int thestartint;
	int theendint;
	QString thedeb = debtxt->text ();
	QString theend = endtxt->text ();
	thestartint = thedeb.toInt (&ok, 10);
	if (ok != true)
	{
	thestartint = 0;
	}
	theendint = theend.toInt (&ok, 10);
	if (ok != true)
	{
	theendint = 0;
	}
	if (thestartint < theendint)
	{
	QTime time_1 (0, 0, 0);
	QTime time_2 (0, 0, 0);
	QTime time_3 (0, 0, 0);
	QTime time_4 (0, 0, 0);
	time_3 = time_1.addSecs (thestartint);
	time_4 = time_2.addSecs (theendint);
	QListViewItem *j;
	j =
		new QListViewItem (lstcut, time_3.toString ("hh:mm:ss"),
				time_4.toString ("hh:mm:ss"));
	lstcut->setSortColumn (1);
	lstcut->sort ();
	bt_cutting->setDisabled (true);
	debtxt->setText ("0");
	endtxt->setText ("0");
	rc_001 = 0;
	rc_002 = 0;
	}
	else
	{
	QMessageBox::warning (this, MANDVDVER, table_message[29]);
	}
	}

	/* Cutting Movie */	
	void
	mainfrm::bt_remove_cutting_clicked () {
	QListViewItem *i = lstcut->selectedItem ();
	if (i != 0)
	{
	lstcut->takeItem (i);
	}
	}

	/* Cutting Movie */	
	void
	mainfrm::bt_valid_cutfilm_clicked () {
	if (rc_004 == 0 && lstcut->childCount () > 0)
	{
	QString myfic = QFileDialog::getSaveFileName (path_out->text (),
							"EDL file (*.edl)",
							this,
							"Sauvegarde EDL",
							"EDL export");
	if (myfic.isNull ())
		{
		}
	else
		{
		if (myfic.right (4) != ".edl")
		{
		myfic = myfic + ".edl";
		}
		save_edl (myfic);
		}
	}
	else
	{
	save_edl (tmp_010);
	}
	}

	/* Cutting Movie */	
	void
	mainfrm::save_edl (QString myfic) {
	lstcut->setSortColumn (1);
	lstcut->sort ();
	QFile myfichier (myfic);
	if (myfichier.open (IO_WriteOnly))
	{
	int cpt1 = 0;
	int cpt2 = lstcut->childCount ();
	QListViewItem *lvi = lstcut->firstChild ();
	int secondes;
	int minutes;
	int heures;
	QString thedeb;
	QString theend;
	int cumul_1;
	int cumul_2;
	while (cpt1 < cpt2)
		{
		thedeb = lvi->text (0);
		theend = lvi->text (1);
		//
		tmp_001 = thedeb.mid (0, 2);
		heures = tmp_001.toInt (&ok, 10);
		if (ok != true)
		{
		heures = 0;
		}
		tmp_001 = thedeb.mid (3, 2);
		minutes = tmp_001.toInt (&ok, 10);
		if (ok != true)
		{
		minutes = 0;
		}
	
		tmp_001 = thedeb.mid (6, 2);
		secondes = tmp_001.toInt (&ok, 10);
		if (ok != true)
		{
		secondes = 0;
		}
		cumul_1 = (heures * 3600) + (minutes * 60) + secondes;
		//
		tmp_001 = theend.mid (0, 2);
		heures = tmp_001.toInt (&ok, 10);
		if (ok != true)
		{
		heures = 0;
		}
	
		tmp_001 = theend.mid (3, 2);
		minutes = tmp_001.toInt (&ok, 10);
		if (ok != true)
		{
		minutes = 0;
		}
	
		tmp_001 = theend.mid (6, 2);
		secondes = tmp_001.toInt (&ok, 10);
		if (ok != true)
		{
		secondes = 0;
		}
		cumul_2 = (heures * 3600) + (minutes * 60) + secondes;
		//
		tmp_001 =
		QString::number (cumul_1) + " " + QString::number (cumul_2) +
		" 0\n";
		myfichier.writeBlock (tmp_001, tmp_001.length ());
	
		cpt1 = cpt1 + 1;
		lvi = lvi->nextSibling ();
		}
	myfichier.close ();
	bt_fermer_cutfilm_clicked ();
	QListViewItem *i = lst_video_1->selectedItem ();
	
	if (i != 0)
		{
		QImage wl_img1 = i->pixmap (0)->convertToImage ();
		QPixmap wl_pix1 (wl_img1);
		QImage wl_img2 = previcon_3->pixmap ()->convertToImage ();
		QPixmap wl_pix2 (wl_img2);
		::bitBlt (&wl_pix1, 3, 20, &wl_pix2, 0, 0, -1, -1, Qt::CopyROP,
			false);
		tmp_004 = i->text (2);
		tmp_005 = "";
		rc_003 = tmp_004.find ("//EDL//", 0);
		if (rc_003 == -1)
		{
		tmp_005 =
			"1//EDL//" + myfic + "/*EDL/*" + tmp_004.mid (1,
								tmp_004.length
								() - 1);
		i->setText (2, tmp_005);
		}
		i->setPixmap (0, wl_pix1);
		bt_force->setDisabled (true);
		}
	}
	else
	{
	QMessageBox::warning (this, MANDVDVER, table_message[8]);
	}
	}

	/* Cutting Movie */	
	void
	mainfrm::bt_import_cutfilm_clicked () {
	QString myfic = QFileDialog::getOpenFileName 
		(dossier_travail_2, "EDL (*.edl)", this, table_message[65], "Sï¿½lectionnez le fichier EDL importer");
	if (myfic.isNull ())
	{
	}
	else
	{
	openedl (myfic);
	rc_001 = 0;
	rc_002 = 0;
	}
	}
	
 