/****************************************************************************
** 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: chapter management
//
// Description: Functions (backend) for editing subtitles
//
//
// Author: Mikael Böhm <micko@bag.org>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//

#include "mandvd_global.h"
#include <subtitle.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>
#include <qdatetimeedit.h> 



	/* Subtitle: */	
	void
	mainfrm::bt_gest_sub_clicked () {
	//
	QListViewItem *i = lst_video_1->selectedItem ();
	
	if (i != 0 && i->text (2) != "9")
	{
	if (m_pMPlayerWidget->isPlaying () == true)
		{
		m_pMPlayerWidget->slotStop ();
		}
	lineEdit11_2->setText ("");
	tmp_001 = i->text (4);
	rc_001 = tmp_001.find ("$$$", 0);
	if (rc_001 != -1)
		{
		tmp_002 =
		tmp_001.mid (rc_001 + 3, tmp_001.length () - (rc_001 + 3));
		lineEdit11_2->setText (tmp_002);
		}
	//
	Etape_02->setDisabled (true);
	tmp_008 = i->text (1);
	m_pMPlayerWidget->setFilename (tmp_008);
	gestsubtitle->setGeometry (294, 295, 411, 110);
	gestsubtitle->show ();
	gestsubtitle->raise ();
	}
	}
	
	/* Subtitle: */
	void
	mainfrm::toolButton72_2_2_clicked () {
	gestsubtitle->hide ();
	Etape_02->setEnabled (true);
	}
	
	/* Import subtilte ? */
	void
	mainfrm::toolButton72_3_clicked () {
	// *** Sauvegarde du sous titre ***
	if (lineEdit11_2->text () != "")
	{
	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_001 = i->text (4);
		int j = tmp_001.find ("$$$");
		if (j == -1)
		{
		tmp_001 = tmp_001 + "$$$" + lineEdit11_2->text ();
		}
		else
		{
		if (j > 0)
			{
			tmp_002 = tmp_001.mid (0, j);
			}
		else
			{
			tmp_002 = "";
			}
		tmp_002 = tmp_002 + "$$$" + lineEdit11_2->text ();
		tmp_001 = tmp_002;
		}
		i->setText (4, tmp_001);
		i->setText (2, "1");
		i->setPixmap (0, wl_pix1);
		}
	}
	//
	gestsubtitle->hide ();
	Etape_02->setEnabled (true);
	}
	
	/* Import subtitle something */
	void
	mainfrm::toolButton71_2_clicked () {
	// *********************************
	// *** Paramtrage du dossier de travail ***
	// *********************************
	
	QString myfic = QFileDialog::getOpenFileName (dossier_travail_2,
							"Videos (*.srt *.SRT )",
							this, table_message[65], table_message[72]);
	if (myfic.isNull ())
	{
	}
	else
	{
	lineEdit11_2->setText (myfic);
	}
	}


	/* Subttle ??*/	
	void
	mainfrm::deplace_h_valueChanged (int) {
	depl_coupe ();
	}


	/* Subttle ??*/		
	void
	mainfrm::deplace_w_valueChanged (int) {
	depl_coupe ();
	}
	
	void
	mainfrm::bteditsstitre_clicked () {
	listdessub->clear ();
	if (lineEdit11_2->text () == "")
	{
	rc_003 = 0;
	}
	else
	{
	rc_003 = 1;
	//
	QFile myfic (lineEdit11_2->text ());
	QTextStream mystream (&myfic);
	if (myfic.open (IO_ReadOnly))
		{
		QString myrec = mystream.readLine ();
		while (mystream.atEnd () == false)
		{
	
		// *** Timing ***
		myrec = mystream.readLine ();
		tmp_001 = myrec.mid (0, 8);
		tmp_002 = myrec.mid (17, 8);
		// *** Texte ***
		myrec = mystream.readLine ();
		// *** Ajout  la listview ***
		// QListViewItem *i = new QListViewItem(listdessub, tmp_001, tmp_002, myrec ); Warningfix MB
		// *** Ligne blanche ***
		myrec = mystream.readLine ();	// *** Position ***
		myrec = mystream.readLine ();
		}
		}
	}
	newsubt->setDisabled (true);
	createsubtitle->setGeometry (15, 140, 970, 420);
	createsubtitle->show ();
	createsubtitle->raise ();
	mediabar->setGeometry (25, 460, 341, 30);
	mediabar->show ();
	mediabar->raise ();
	vframe->setGeometry (25, 190, 341, 273);
	vframe->show ();
	vframe->raise ();
	}

	/* Subttle */	
	void
	mainfrm::btcloseedit_clicked () {
	if (m_pMPlayerWidget->isPlaying () == true)
	{
	m_pMPlayerWidget->slotStop ();
	}
	listdessub->setSortColumn (1);
	listdessub->setSortOrder (Qt::Ascending);
	listdessub->sort ();
	if (rc_003 == 0)
	{
	QString myfic = QFileDialog::getSaveFileName (path_out->text (),
							"Subtitle (*.srt)",
							this, table_message[65], "Subtitle export" );
	if (myfic.isNull ())
		{
		}
	else
		{
		if (myfic.right (4) != ".srt")
		{
		myfic = myfic + ".srt";
		}
		save_sub (myfic);
		}
	}
	else
	{
	save_sub (lineEdit11_2->text ());
	}
	createsubtitle->hide ();
	mediabar->hide ();
	vframe->setGeometry (31, 125, 115, 71);
	vframe->show ();
	vframe->raise ();
	}

	/* Subttle */		
	void
	mainfrm::save_sub (QString locpathname) {
	QFile myfic (locpathname);
	int cpt_001 = 1;
	int cpt_002 = 0;
	if (myfic.open (IO_WriteOnly))
	{
	QListViewItem *lvi = listdessub->firstChild ();
	cpt_002 = listdessub->childCount ();
	while (cpt_001 <= cpt_002)
		{
		tmp_001 = lvi->text (0);
		tmp_002 = lvi->text (1);
		tmp_003 = lvi->text (2);
	
		tmp_004 = QString::number (cpt_001) + "\n";
		myfic.writeBlock (tmp_004, tmp_004.length ());
	
		tmp_004 = tmp_001 + ",000 --> " + tmp_002 + ",000\n";
		myfic.writeBlock (tmp_004, tmp_004.length ());
	
		tmp_004 = tmp_003 + "\n";
		myfic.writeBlock (tmp_004, tmp_004.length ());
	
		tmp_004 = "\n";
		myfic.writeBlock (tmp_004, tmp_004.length ());
	
		lvi = lvi->nextSibling ();
		cpt_001 = cpt_001 + 1;
		}
	myfic.close ();
	lineEdit11_2->setText (locpathname);
	}
	else
	{
	QMessageBox::warning (this, MANDVDVER, table_message[8]);
	}
	}

	/* Subttle ??*/		
	void
	mainfrm::bt_deb_sub_clicked () {
	if (m_pMPlayerWidget->isPlaying () == true)
	{
	int iHour;
	int iMin;
	int iSec;
	QTime mytime;
	float fElapsedTime =
		m_pMPlayerWidget->m_rxParseElapsedTime.cap (1).toFloat ();
	iHour = (int) (fElapsedTime / 3600.0);
	iMin = (int) ((fElapsedTime - iHour * 3600) / 60.0);
	iSec = (int) (fElapsedTime - iHour * 3600 - iMin * 60);
	mytime.setHMS (iHour, iMin, iSec);
	hre_deb_sub->setTime (mytime);
	}
	}

	
	void
	mainfrm::bt_gest_sub_2_clicked () {
	QListViewItem *i = lst_video_1->selectedItem ();
	
	if (i != 0 && i->text (2) != "9")
	{
	
	int cpt1;
	int cpt2;
	
	if (m_pMPlayerWidget->isPlaying () == true)
		{
		m_pMPlayerWidget->slotStop ();
		}
	
	nomfilm = i->text (1);
	QString lstfiltres = i->text (2);
	
	if (lstfiltres.find ("eq=", 0, false) == -1)
		{
		btreglages->setChecked (false);
		sl_lum->setValue (0);
		sl_con->setValue (0);
		}
	else
		{
		int cpt1 = lstfiltres.find ("eq=", 0, false) + 3;
		int cpt2 = 0;		// Warningfix MB
		tmp_001 = "";
		tmp_002 = "";
	
		while (lstfiltres.mid (cpt1, 1) != ","
			&& lstfiltres.mid (cpt1, 1) != " "
			&& cpt1 < lstfiltres.length ())
		{
		if (lstfiltres.mid (cpt1, 1) != ":")
			{
			if (cpt2 == 0)
			{
			tmp_001 = tmp_001 + lstfiltres.mid (cpt1, 1);
			}
			else
			{
			tmp_002 = tmp_002 + lstfiltres.mid (cpt1, 1);
			}
			}
		else
			{
			cpt2 = 1;
			}
		cpt1 = cpt1 + 1;
		}
		//
		rc_001 = tmp_001.toInt (&ok, 10);
		if (ok == true)
		{
		sl_lum->setValue (rc_001);
		}
		else
		{
		sl_lum->setValue (0);
		}
		//
		rc_001 = tmp_002.toInt (&ok, 10);
		if (ok == true)
		{
		sl_con->setValue (rc_001);
		}
		else
		{
		sl_con->setValue (0);
		}
		//
		btreglages->setChecked (true);
		}
	
	if (lstfiltres.find ("delogo=", 0, false) == -1)
		{
		btdelogo->setChecked (false);
		spx->setValue (0);
		spy->setValue (0);
		spl->setValue (0);
		sph->setValue (0);
		}
	else
		{
		cpt1 = lstfiltres.find ("delogo=", 0, false) + 7;
		cpt2 = 0;
		tmp_001 = "";
		tmp_002 = "";
		tmp_003 = "";
		tmp_004 = "";
	
		while (lstfiltres.mid (cpt1, 1) != ","
			&& lstfiltres.mid (cpt1, 1) != " "
			&& cpt1 < lstfiltres.length ())
		{
		if (lstfiltres.mid (cpt1, 1) != ":")
			{
			if (cpt2 == 0)
			{
			tmp_001 = tmp_001 + lstfiltres.mid (cpt1, 1);
			}
			else
			{
			if (cpt2 == 1)
				{
				tmp_002 = tmp_002 + lstfiltres.mid (cpt1, 1);
				}
			else
				{
				if (cpt2 == 2)
				{
				tmp_003 = tmp_003 + lstfiltres.mid (cpt1, 1);
				}
				else
				{
				if (cpt2 == 3)
					{
					tmp_004 =
					tmp_004 + lstfiltres.mid (cpt1, 1);
					}
				}
				}
			}
			}
		else
			{
			cpt2 = cpt2 + 1;
			}
		cpt1 = cpt1 + 1;
		}
		//
		rc_001 = tmp_001.toInt (&ok, 10);
		if (ok == true)
		{
		spx->setValue (rc_001);
		}
		else
		{
		spx->setValue (0);
		}
		//
		rc_001 = tmp_002.toInt (&ok, 10);
		if (ok == true)
		{
		spy->setValue (rc_001);
		}
		else
		{
		spy->setValue (0);
		}
		//
		rc_001 = tmp_003.toInt (&ok, 10);
		if (ok == true)
		{
		spl->setValue (rc_001);
		}
		else
		{
		spl->setValue (0);
		}
		//
		rc_001 = tmp_004.toInt (&ok, 10);
		if (ok == true)
		{
		sph->setValue (rc_001);
		}
		else
		{
		sph->setValue (0);
		}
		//
		btdelogo->setChecked (true);
		}
	
	if (lstfiltres.find ("mirror", 0, false) == -1)
		{
		btmirroir->setChecked (false);
		}
	else
		{
		btmirroir->setChecked (true);
		}
	
	if (lstfiltres.find ("denoise3d=", 0, false) == -1)
		{
		btdenoise->setChecked (false);
		}
	else
		{
		btdenoise->setChecked (true);
		}
	
	Etape_02->setDisabled (true);
	addfilter->setGeometry (55, 104, 890, 619);
	addfilter->show ();
	addfilter->raise ();
	}
	}
