#include <QtGui>
#include "mainfrm.h"

void mainfrm::f_render()
{
	wfrom = from_encoding->value();
	wto = to_encoding->value();
	myconsole = "";
	thread_tra->scaleimg(size_dia_x,size_dia_y);
	pntmygl->affiche_grille = 0;
	pntmygl->frame_encoding = 0;
	current_diapo = wfrom - 1;
	poursuite_1 = 0;
	pntmygl->poursuite = 0;
	gorender = 0;
	nbre_comment = 0;
	affichage_diapo();
	if ( comboqualite->currentIndex() == 0 )
	{
		pntmygl->wg_qualite = 100;
	}
	else
	{
		if ( comboqualite->currentIndex() == 1 )
		{
			pntmygl->wg_qualite = 80;
		}
		else
		{
			if ( comboqualite->currentIndex() == 2 )
			{
				pntmygl->wg_qualite = 60;
			}
			else
			{
				pntmygl->wg_qualite = 30;
			}
		}
	}
	//
	f_render_bcle_lecture();
}

void mainfrm::f_render_bcle_lecture()
{
	if ( gorender == 0 )
	{
		if ( table_photos[current_diapo][24] != "" )
		{
			nbre_comment++;
		}
		pntmygl->wg_duree = table_photos[current_diapo][16].toInt();
		pntmygl->start_opacity = table_photos[current_diapo][25].toFloat() / 100;
		pntmygl->end_opacity = table_photos[current_diapo][26].toFloat() / 100;
		pntmygl->wg_percent_slide = table_photos[current_diapo][11].toFloat() / 100;
		//
		pntmygl->progress = 0;
	    	pntmygl->rotationX = pntmygl->debut_rx;
	    	pntmygl->rotationY = pntmygl->debut_ry;
	    	pntmygl->rotationZ = pntmygl->debut_rz;
	    	pntmygl->deplacementX = pntmygl->debut_px;
	    	pntmygl->deplacementY = pntmygl->debut_py;
		pntmygl->zoom = pntmygl->debut_pz;
		pntmygl->delta_opacity = (pntmygl->end_opacity - pntmygl->start_opacity) / (pntmygl->duree-1);
		pntmygl->wg_percent = pntmygl->start_opacity;
		pntmygl->start_fog_opacity = table_photos[current_diapo][15].toFloat();
		pntmygl->end_fog_opacity = table_photos[current_diapo][28].toFloat();
		pntmygl->isfog = table_photos[current_diapo][14].toFloat();
		pntmygl->delta_fog_opacity = (pntmygl->end_fog_opacity - pntmygl->start_fog_opacity) / (pntmygl->duree-1);
		pntmygl->wg_percent_fog = pntmygl->start_fog_opacity;
		pntmygl->bcle_tempo = 0;

		if ( pntmygl->isdiapoanim == 1 )
		{
			pntmygl->movie_object->setCacheMode( QMovie::CacheAll );
			pntmygl->movie_object->jumpToFrame(0);
		}

		if ( pntmygl->fadels == 0 )
		{
			pntmygl->wg_percent_trans = 1.0f;
		}
		else
		{
			pntmygl->wg_percent_trans = 0.0f;
		}
		if ( pntmygl->iseffect > 0 )
		{
			pntmygl->bcle_tempo = 0;
			pntmygl->movie->setCacheMode( QMovie::CacheAll );
			pntmygl->movie->jumpToFrame(0);
		}
		if ( current_diapo == 0 && table_photos[current_diapo][20] != "0" )
		{
			table_preview_transition[0].load(":/new/prefix2/Interface/Theme/nullpixmap.png");
			table_preview_transition[0] = table_preview_transition[0].scaled( size_dia_x, size_dia_y, Qt::IgnoreAspectRatio, Qt::FastTransformation );
			for (int i = 1; i < table_photos[current_diapo][21].toInt() * 25 ; i++)
			{
				table_preview_transition[i] = table_preview_transition[0];
			}
			poursuite_1 = table_photos[current_diapo][21].toInt() * 25;
		}
		//else
		/*{
			if ( table_photos[current_diapo][20] != "0" )
			{
				poursuite_1 = table_photos[current_diapo][21].toInt() * 25;
			}
		}*/

		if ( poursuite_1 > 0 )
		{
			tmpint_5 = 1;
			thread_tra->invert = table_photos[current_diapo][6];
			if ( table_photos[current_diapo][6] == "0" )
			{
				thread_tra->sequence = 0;
				thread_tra->invert = "0";
			}
			else
			{
				thread_tra->sequence = 100;
				thread_tra->invert = "2";
			}
			thread_tra->increment_sequence = 100 / (table_photos[current_diapo][21].toInt()*25);
	
			adjust_transition = (float)((float)100 / (float)(table_photos[current_diapo][21].toInt()*25)) - ((100 / (table_photos[current_diapo][21].toInt()*25)));
			compute_adjust_transition = 0;
		}
		else
		{
			tmpint_5 = 0;
		}

		if ( current_diapo+1 <= nbre_photos && tmpint_5 == 0 )
		{
			if ( table_photos[current_diapo+1][20] != "0" )
			{
				poursuite_1 = table_photos[current_diapo+1][21].toInt()*25;
			}
			else
			{
				poursuite_1 = 0;
			}
		}
		if ( table_photos[current_diapo][20].toInt() > 1 )
		{
			thread_tra->srcImage3 = table_image_transition[table_photos[current_diapo][20].toInt() - 2].scaled( size_dia_x, size_dia_y, Qt::IgnoreAspectRatio, Qt::FastTransformation );
		}

		ispreview = 0;
		QTimer::singleShot(2, this, SLOT(f_render_updateMove()));
	}
	else
	{
		frame_encode->show();
		frame_encode->raise();
		pb_encode->setValue(0);
		pb_encode->setMaximum(100);
		QTimer::singleShot(2, this, SLOT(end_encoding()));
	}
}

void mainfrm::f_render_updateMove()
{
	if ( pntmygl->progress > pntmygl->duree+1 )
	{
		current_diapo++;
		if ( current_diapo > wto-1 )
		{
			current_diapo = wfrom-1;
			gorender = 1;
			f_render_bcle_lecture();
			return;
		}
		else
		{
			affichage_diapo();
			f_render_bcle_lecture();
			return;
		}
	}
	else
	{
		if ( pntmygl->isdiapoanim == 1 )
		{
			if ( pntmygl->movie_object->jumpToNextFrame() == false )
			{
				pntmygl->movie_object->setCacheMode( QMovie::CacheAll );
				pntmygl->movie_object->jumpToFrame(0);
			}
			pntmygl->b = pntmygl->movie_object->currentImage();

			QImage wl_puis_001(1024,1024,QImage::Format_ARGB32_Premultiplied);
			wl_puis_001 = pntmygl->b.copy ( 0, 0, 1024, 1024);
    			pntmygl->f = QGLWidget::convertToGLFormat( wl_puis_001 );
    			glGenTextures( 1, &pntmygl->texture2 );
			glBindTexture( GL_TEXTURE_2D, pntmygl->texture2 );
			glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, pntmygl->f.width(), pntmygl->f.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, pntmygl->f.bits() );
			glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
		}
	    	pntmygl->rotationX = pntstamp->table_coo[pntmygl->progress][0];
	    	pntmygl->rotationY = pntstamp->table_coo[pntmygl->progress][1];
	    	pntmygl->rotationZ = pntstamp->table_coo[pntmygl->progress][2];
	    	pntmygl->deplacementX = pntstamp->table_coo[pntmygl->progress][3];
	    	pntmygl->deplacementY = pntstamp->table_coo[pntmygl->progress][4];
		pntmygl->zoom = pntstamp->table_coo[pntmygl->progress][5];
		//
		pntmygl->wg_percent = pntmygl->wg_percent + pntmygl->delta_opacity;
		pntmygl->wg_percent_fog = pntmygl->wg_percent_fog + pntmygl->delta_fog_opacity;
		if ( pntmygl->state_mode == 1 )
		{
			if ( pntmygl->fadels == 1 )
			{
				if ( pntmygl->progress < 10 )
				{
					pntmygl->wg_percent_trans = pntmygl->wg_percent_trans + 0.1;
				}
			}
			if ( pntmygl->fadele == 1 )
			{
				if ( pntmygl->progress >= pntmygl->wg_cpt_002 - 10 )
				{
					pntmygl->wg_percent_trans = pntmygl->wg_percent_trans - 0.1;
				}
			}
		}
	}

	if ( pntmygl->iseffect > 0 )
	{
		if ( pntmygl->long_effet != 1 )
		{
			if ( pntmygl->movie->jumpToNextFrame() == false )
			{
				pntmygl->movie->setCacheMode( QMovie::CacheAll );
				pntmygl->movie->jumpToFrame(0);
			}
			pntmygl->a = pntmygl->movie->currentImage();
			pntmygl->wg_puis_001 = pntmygl->a.copy ( 0, 0, pntmygl->size_anim, pntmygl->size_anim);
			pntmygl->d = QGLWidget::convertToGLFormat( pntmygl->wg_puis_001 );
			glDeleteTextures( 1, &pntmygl->texture3 );
			glGenTextures( 1, &pntmygl->texture3 );
			glBindTexture( GL_TEXTURE_2D, pntmygl->texture3 );
			glTexImage2D( GL_TEXTURE_2D, 0, 4, pntmygl->d.width(), pntmygl->d.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, pntmygl->d.bits() );
			glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
		}
		else
		{
			if ( pntmygl->bcle_tempo == 0 )
			{
				pntmygl->movie->jumpToFrame(0);
				pntmygl->a = pntmygl->movie->currentImage();
				pntmygl->wg_puis_001 = pntmygl->a.copy ( 0, 0, pntmygl->size_anim, pntmygl->size_anim);
				pntmygl->d = QGLWidget::convertToGLFormat( pntmygl->wg_puis_001 );
				glDeleteTextures( 1, &pntmygl->texture3 );
				glGenTextures( 1, &pntmygl->texture3 );
				glBindTexture( GL_TEXTURE_2D, pntmygl->texture3 );
				glTexImage2D( GL_TEXTURE_2D, 0, 4, pntmygl->d.width(), pntmygl->d.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, pntmygl->d.bits() );
				glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
				pntmygl->bcle_tempo = 1;
			}
		}
	}

	if ( pntmygl->isfog == 1 )
	{
		pntmygl->sl_opacity(pntmygl->wg_percent_fog);
	}

	if ( table_photos[current_diapo][17] != "" )
	{
		pntmygl->posxleg = pntmygl->startx_legani - ( pntmygl->progress * ((pntmygl->startx_legani - pntmygl->endx_legani) / (pntmygl->duree-1)));
		pntmygl->posyleg = pntmygl->starty_legani - ( pntmygl->progress * ((pntmygl->starty_legani - pntmygl->endy_legani) / (pntmygl->duree-1)));
	}

	//if ( pntmygl->poursuite == 0 )
	//{
		pntmygl->progress++;
		pntmygl->updateGL();
		if ( tmpint_5 == 0 )
		{
			if ( pntmygl->progress < (pntmygl->duree+1) - poursuite_1 )
			{
				wg_tmp_004 = "0000000000";
				wg_grab_render = pntmygl->grabFrameBuffer ( false );
				wg_tmp_005 = QString::number(pntmygl->frame_encoding);
				wg_tmp_004.replace( 10 - wg_tmp_005.length(), wg_tmp_005.length(), wg_tmp_005);
				wg_grab_render.save(pntmygl->path_render + "/Slide_xxx_" + wg_tmp_004 + ".jpg","JPEG",pntmygl->wg_qualite);
				pntmygl->frame_encoding++;
				f_render_updateMove();
			}
			else
			{
				table_preview_transition[ispreview] = pntmygl->grabFrameBuffer( false );
				ispreview++;
				f_render_updateMove();
			}
		}
		else
		{
			thread_tra->srcImage = table_preview_transition[ispreview].scaled(size_dia_x,size_dia_y,Qt::IgnoreAspectRatio, Qt::FastTransformation);
			thread_tra->srcImage2 = pntmygl->grabFrameBuffer( false );
			thread_tra->numtrans = table_photos[current_diapo][20].toInt();
			ispreview++;
			thread_tra->run();
		}
	//}
}

void mainfrm::f_render_end_ptransition()
{
	wg_tmp_004 = "0000000000";
	wg_tmp_005 = QString::number(pntmygl->frame_encoding);
	wg_tmp_004.replace( 10 - wg_tmp_005.length(), wg_tmp_005.length(), wg_tmp_005);
	thread_tra->dstImage.save(pntmygl->path_render + "/Slide_xxx_" + wg_tmp_004 + ".jpg","JPEG",pntmygl->wg_qualite);
	table_preview_transition[ispreview-1] = thread_ptra->srcImage2;
	pntmygl->frame_encoding++;

	if ( table_photos[current_diapo][6] == "0" )
	{
		if ( thread_tra->sequence + thread_tra->increment_sequence <= 100 )
		{
			thread_tra->sequence = thread_tra->sequence + thread_tra->increment_sequence;
		}
		else
		{
			thread_tra->sequence = 100;
		}
	}
	else
	{
		if ( thread_tra->sequence - thread_tra->increment_sequence >= 0 )
		{
			thread_tra->sequence = thread_tra->sequence - thread_tra->increment_sequence;
		}
		else
		{
			thread_tra->sequence = 0;
		}
	}	

	compute_adjust_transition = compute_adjust_transition + adjust_transition;
	if ( compute_adjust_transition >= 0.9 )
	{
		compute_adjust_transition = compute_adjust_transition-0.9;
		if ( table_photos[current_diapo][6] == "0" )
		{
			if ( thread_tra->sequence + thread_tra->increment_sequence <= 100 )
			{
				thread_tra->sequence = thread_tra->sequence + thread_tra->increment_sequence;
			}
			else
			{
				thread_tra->sequence = 100;
			}
		}
		else
		{
			if ( thread_tra->sequence - thread_tra->increment_sequence >= 0 )
			{
				thread_tra->sequence = thread_tra->sequence - thread_tra->increment_sequence;
			}
			else
			{
				thread_tra->sequence = 0;
			}
		}	
	}
	
	if ( ispreview >= table_photos[current_diapo][21].toInt()*25 )
	{
		tmpint_5 = 0;
		ispreview = 0;
		if ( current_diapo+1 <= nbre_photos )
		{
			if ( table_photos[current_diapo+1][20] != "0" )
			{
				poursuite_1 = table_photos[current_diapo+1][21].toInt()*25;
			}
			else
			{
				poursuite_1 = 0;
			}
		}
		else
		{
			poursuite_1 = 0;
		}
	}
	QTimer::singleShot(2, this, SLOT(f_render_updateMove()));
}

void mainfrm::end_encoding()
{
	// *** GENERATION DE LA BANDE SON ***
       	QFile myfic(wpathprojet + "/trtaudio.sh");

        if ( !myfic.open( QIODevice::WriteOnly ))
        {
		qDebug() << "ERROR CREATING SOUNDTRACK";
		this->setEnabled(true);
		return;
	}
	else
	{
		QTextStream out(&myfic);
		#ifdef Q_WS_WIN
		#else
			out << "#!/bin/bash" << endl;
		#endif
		out << "cd \"" + wpathprojet + "\"" << endl;
		// *** BANDE SON VIDE SUPPORT AUDIO ***
		if ( total_diapo > 0 )
		{
			out << "sox -n -t ogg -c 2 -r 44100 silent.ogg trim 0 " << QString::number(total_diapo)  << endl;
		}
		else
		{
			qDebug() << "STRANGE SLIDESHOW HAS 0 LENGTH";
			out << "sox -n -t ogg -c 2 -r 44100 silent.ogg trim 0 1"  << endl;
		}

		int tpsavant = 0;
		int tpsapres = 0;
		int i = 0;
		int j = zik_1->position;
		if ( zik_number > 0 )
		{ 
			if ( j < i )
			{
				j=0;
			}
			while ( i < j && i < 400 )
			{
				if ( table_photos[i][20] == "0" )
				{
					tpsavant = tpsavant + table_photos[i][16].toInt();
				}
				else
				{
					tpsavant = tpsavant + (table_photos[i][16].toInt() - table_photos[i][21].toInt());
				}
				i++;
			}
			if ( tpsavant + table_zik[0][1].toInt() < total_diapo )
			{
				tpsapres = total_diapo - (tpsavant + table_zik[0][1].toInt());
			}
			if ( tpsavant > 0 )
			{
				out << "sox silent.ogg -t ogg -c 2 -r 44100 debut.ogg trim 0:0:0.0 " << QString::number(tpsavant) << endl;
			}
			if ( tpsapres > 0 )
			{
				out << "sox silent.ogg -t ogg -c 2 -r 44100 fin.ogg trim " << QString::number(tpsavant + table_zik[0][1].toInt()) << endl;
			}
			out << "sox -S \"" << table_zik[0][0] << "\" -t ogg -c 2 -r 44100 melange.ogg trim 0:0:0.0 " << QString::number(total_diapo-(tpsavant+tpsapres)) << " fade t 0.5 " << QString::number((total_diapo-(tpsavant+tpsapres))-0.7) << " 0.5" << endl;
			QString path_audio_chain = "";
			if ( tpsavant > 0 )
			{
				path_audio_chain = "debut.ogg ";
			}
			path_audio_chain = path_audio_chain + "melange.ogg";
			if ( tpsapres > 0 )
			{
				path_audio_chain = path_audio_chain + " fin.ogg";
			}
			out << "sox -S " + path_audio_chain + " bandeson.ogg" << endl;
		}
		else
		{
			out << "sox silent.ogg -t ogg -r 44100 bandeson.ogg" << endl;
		}
		
		// *** CALCUL LA DUREE DU DIAPO ENTRE DEBUT ET FIN ***
		i = from_encoding->value() - 1;
		j = to_encoding->value() - 1;
		int durdiap = 0;
		while ( i <= j ) 
		{
			if ( i > 0 )
			{
				if ( table_photos[i][20] == "0" )
				{
					durdiap = durdiap + table_photos[i][16].toInt();
				}
				else
				{
					durdiap = durdiap + (table_photos[i][16].toInt() - table_photos[i][21].toInt());
				}
			}
			else
			{
				durdiap = durdiap + table_photos[i][16].toInt();
			}
			i++;
		}
		i = from_encoding->value() - 1;
		j = 0;
		tpsavant = 0;
		while ( i < j )
		{
			if ( i > 0 )
			{
				if ( table_photos[j][20] == "0" )
				{
					tpsavant = tpsavant + table_photos[j][16].toInt();
				}
				else
				{
					tpsavant = tpsavant + (table_photos[j][16].toInt() - table_photos[j][21].toInt());
				}
			}
			else
			{
				tpsavant = tpsavant + table_photos[j][16].toInt();
			}
			j++;
		} 
		out << "sox -S bandeson.ogg -t wav -r 44100 bandeson2.ogg trim " << QString::number(tpsavant) << " " << QString::number(durdiap) << endl;

		// *** TRAITEMENT DES LEGENDES AUDIO ***
		i = from_encoding->value() - 1;
		int dur_cur_diapo = 0;
		int pos_flux = 0;
		int compte_comment = 0;
		while ( i <= to_encoding->value() - 1 && nbre_comment != 0 )
		{
			if ( table_photos[i][24] != "" )
			{
				dur_cur_diapo = table_photos[i][16].toInt();
				out << "sox -S bandeson2.ogg -t ogg -c 2 -r 44100 extrait.ogg trim " << QString::number(pos_flux) << " " << QString::number(dur_cur_diapo) << endl;
				out << "sox -S \"" << table_photos[i][24] << "\" -t ogg -c 2 -r 44100 extrait2.ogg trim 0 " << QString::number(dur_cur_diapo) << endl;
				out << "sox -mix -S -v 0.4 extrait.ogg extrait2.ogg -t ogg -r 44100 melange.ogg" << endl;
				if ( pos_flux > 0 )
				{
					out << "sox -S bandeson2.ogg -t ogg -c 2 -r 44100 debut.ogg trim 0:0:0.0 " << QString::number(pos_flux) << endl;
				}
				out << "sox bandeson2.ogg -t ogg -c 2 fin.ogg trim " << QString::number(pos_flux + dur_cur_diapo) << endl;
				if ( pos_flux > 0 )
				{
					out << "sox -S debut.ogg melange.ogg fin.ogg  bandeson2.ogg" << endl;
				}
				else
				{
					out << "sox -S melange.ogg fin.ogg  bandeson2.ogg" << endl;
				}
				compte_comment++;
				if ( compte_comment >= nbre_comment )
				{
					nbre_comment = 0;
				}
			}
			if ( table_photos[i][20] == "0" )
			{
				pos_flux = pos_flux + table_photos[i][16].toInt();
			}
			else
			{
				pos_flux = pos_flux + (table_photos[i][16].toInt() - table_photos[i][21].toInt());
			}
			i++;
		}
		//
		out << "sox -S bandeson2.ogg -t wav -r 44100 bandeson.wav" << endl;

		out << "exit";
		myfic.close();

		// *** LANCEMENT DU PROCESSUS ***
		label_14->setText(tr("Encodage de l'audio ..."));
		pb_encode->setValue(0);
		pb_encode->setMaximum(100);
		// *** Lancement du process de generation de l'audio ***
		#ifdef Q_WS_WIN
			QString program = "cmd";
		#else
			QString program = "bash";
		#endif
		QStringList arguments;
       		arguments << wpathprojet + "/trtaudio.sh";
		myprocess_3.setProcessChannelMode ( QProcess::MergedChannels );
		myprocess_3.start( program, arguments );
	}
}

void mainfrm::end_encoding_audio(int, QProcess::ExitStatus)
{
	myprocess_3.terminate();
	label_14->setText(tr("Finalisation du projet ..."));
       	QFile myfic(wpathprojet + "/audio.sh");

        if ( !myfic.open( QIODevice::WriteOnly ))
        {
		this->setEnabled(true);
		return;
	}
	else
	{
		QTextStream out(&myfic);
		#ifdef Q_WS_WIN
		#else
			out << "#!/bin/bash" << endl;
		#endif
		out << "cd \"" + wpathprojet + "\"" << endl;
		QString wl_fps="25"; 
		QString string_format = "";

		if ( is43 == 1 )
		{
			string_format = "-lavcopts aspect=4/3";
		}
		else
		{
			if ( is43 == 0 )
			{
				string_format = "-vf scale=720:576 -lavcopts aspect=16/9";
			}
			else
			{
				wl_fps="30000/1001";
				string_format = "-lavcopts aspect=4/3";
			}
		}

		if ( combocodec->currentIndex() == 0 )
		{ // *** MPEG 2 ***
			if ( is43 == 1 )
			{
				out << "mencoder \"mf://Slide_xxx_*.jpg\" -noskip -mf w=720:h=576:fps=" + wl_fps + ":type=jpg -o \"" + path_out_render->text() + "/" + name_encoding->text() + "\" -oac lavc -ovc lavc -vf harddup -of mpeg -mpegopts format=dvd:tsaf:vbitrate=" + QString::number((uint)((pntmygl->wg_qualite - 20)* 100)) + ":muxrate=7500  -srate 48000 -af lavcresample=48000 -lavcopts vcodec=mpeg2video:aspect=4/3:vrc_buf_size=1835:vrc_maxrate=9800:threads=" + QString::number(nbre_threads) + ":vbitrate=" + QString::number((uint)((pntmygl->wg_qualite - 20)* 100)) + ":keyint=12:vstrict=0:acodec=ac3:abitrate=192 " + string_format + " -cache 8192 -audiofile bandeson.wav" << endl; // :trell:mbd=2:sc_threshold=1000000000:cgop:vstrict=0
			}
			else
			{
				out << "mencoder \"mf://Slide_xxx_*.jpg\" -noskip -mf w=720:h=576:fps=" + wl_fps + ":type=jpg -o \"" + path_out_render->text() + "/" + name_encoding->text() + "\" -oac lavc -ovc lavc -vf harddup -of mpeg -mpegopts format=dvd:tsaf:vbitrate=" + QString::number((uint)((pntmygl->wg_qualite - 20)* 100)) + ":muxrate=7500 -srate 48000 -af lavcresample=48000 -lavcopts vcodec=mpeg2video:aspect=16/9:vrc_buf_size=1835:vrc_maxrate=9800:threads=" + QString::number(nbre_threads) + ":vbitrate=" + QString::number((uint)((pntmygl->wg_qualite - 20)* 100)) + ":keyint=12:vstrict=0:acodec=ac3:abitrate=192 " + string_format + " -cache 8192 -audiofile bandeson.wav" << endl; // :trell:mbd=2:sc_threshold=1000000000:cgop:vstrict=0
			}
		}
		else
		{
			if ( combocodec->currentIndex() == 1 )
			{ // *** XVID *** -ovc xvid -oac mp3lame -xvidencopts bitrate=687
				if ( is43 == 1 )
				{
					out << "mencoder \"mf://Slide_xxx_*.jpg\" -noskip -mf w=720:h=576:fps=" + wl_fps + ":type=jpg -o \"" + path_out_render->text() + "/" + name_encoding->text() + "\" -oac mp3lame -ovc xvid " + string_format + " -xvidencopts bitrate=" + QString::number((uint)((pntmygl->wg_qualite - 20)* 10)) + ":chroma_opt:vhq=4:bvhq=1:quant_type=mpeg:par=pal43 -audiofile bandeson.wav" << endl;
				}
				else
				{
					out << "mencoder \"mf://Slide_xxx_*.jpg\" -noskip -mf w=720:h=576:fps=" + wl_fps + ":type=jpg -o \"" + path_out_render->text() + "/" + name_encoding->text() + "\" -oac mp3lame -ovc xvid " + string_format + " -xvidencopts bitrate=" + QString::number((uint)((pntmygl->wg_qualite - 20)* 10)) + ":chroma_opt:vhq=4:bvhq=1:quant_type=mpeg:par=pal169 -audiofile bandeson.wav" << endl;
				}
			}
			else
			{
				if ( combocodec->currentIndex() == 2 )
				{ // *** DV ***
					if ( is43 == 1 )
					{
						out << "mencoder \"mf://Slide_xxx_*.jpg\" -noskip -mf w=720:h=576:fps=" + wl_fps + ":type=jpg -o \"" + path_out_render->text() + "/" + name_encoding->text() + "\" " + string_format + " -oac pcm -aspect 4:3 -ovc libdv -audiofile bandeson.wav" << endl;
					}
					else
					{
						out << "mencoder \"mf://Slide_xxx_*.jpg\" -noskip -mf w=720:h=576:fps=" + wl_fps + ":type=jpg -o \"" + path_out_render->text() + "/" + name_encoding->text() + "\" " + string_format + " -oac pcm -aspect 16:9 -ovc libdv -audiofile bandeson.wav" << endl;
					}
				}
				else
				{ // *** FLV ***
					if ( is43 == 1 )
					{
						out << "mencoder \"mf://Slide_xxx_*.jpg\" -noskip -mf w=720:h=576:fps=" + wl_fps + ":type=jpg -o \"" + path_out_render->text() + "/" + name_encoding->text() + "\" -of lavf -oac mp3lame -lameopts abr:br=56 -srate 22050 -ovc lavc -lavcopts vcodec=flv:aspect=4/3:vbitrate=" + QString::number((uint)((pntmygl->wg_qualite - 20)* 100)) + ":mbd=2:mv0:trell:v4mv:cbp:last_pred=3 " + string_format + " -audiofile bandeson.wav" << endl;
					}
					else
					{
						out << "mencoder \"mf://Slide_xxx_*.jpg\" -noskip -mf w=720:h=576:fps=" + wl_fps + ":type=jpg -o \"" + path_out_render->text() + "/" + name_encoding->text() + "\" -of lavf -oac mp3lame -lameopts abr:br=56 -srate 22050 -ovc lavc -lavcopts vcodec=flv:aspect=16/9:vbitrate=" + QString::number((uint)((pntmygl->wg_qualite - 20)* 100)) + ":mbd=2:mv0:trell:v4mv:cbp:last_pred=3 " + string_format + " -audiofile bandeson.wav" << endl;
					}
				}

			}

		}

		myfic.close();
		// *** Lancement du process de generation de l'audio ***
		pb_encode->setValue(0);
		pb_encode->setMaximum(100);
		#ifdef Q_WS_WIN
			QString program = "cmd";
		#else
			QString program = "bash";
		#endif
       		QStringList arguments;
       		arguments << wpathprojet + "/audio.sh";
		myprocess_4.start( program, arguments );
	}
}

void mainfrm::end_all_audio(int, QProcess::ExitStatus)
{
	myprocess_4.terminate();
	fin_fin_encoding();
}

void mainfrm::lecture_console_out_audio_encoding()
{
	newData = myprocess_3.readAllStandardOutput();
	wg_tmp_002 = QString::fromLocal8Bit(newData);
	myconsole = myconsole + wg_tmp_002;
        tmpint_3 = wg_tmp_002.indexOf("%", 0);
        if ( tmpint_3 != -1 )
        {
		int valueprog=0;
		int j = tmpint_3;
		while ( wg_tmp_002.mid(tmpint_3,1) != "(" && tmpint_3 > 0 )
		{
			tmpint_3 = tmpint_3 - 1;
		}
		if ( tmpint_3 > 0 )
		{
			valueprog = wg_tmp_002.mid(tmpint_3 + 1, j - (tmpint_3 + 3)).toInt();
		}
		pb_encode->setValue(valueprog);
	}
}

void mainfrm::lecture_console_out_mencoder()
{
	newData = myprocess_4.readAllStandardOutput();
	wg_tmp_002 = QString::fromLocal8Bit(newData);
	myconsole = myconsole + wg_tmp_002;
        tmpint_3 = wg_tmp_002.indexOf("%", 0);
        if ( tmpint_3 != -1 )
        {
		int valueprog;
       		wg_tmp_001 = wg_tmp_002.mid((tmpint_3 - 2),2);
       		valueprog = wg_tmp_001.toInt();
		pb_encode->setValue(valueprog);
	}
}

void mainfrm::lecture_console_outerr_mencoder()
{
	newData = myprocess_4.readAllStandardError();
	wg_tmp_002 = QString::fromLocal8Bit(newData);
	myconsole = myconsole + wg_tmp_002;
}

void mainfrm::fin_fin_encoding()
{
	QString program;
	QStringList arguments;

	frame_encode->hide();
	this->setEnabled(true);
	contain_timeline->setEnabled(true);
	tabmenus->setEnabled(true);
	if ( tabmenus->currentIndex() < 1 )
	{
		toolsmenu->show();
	}
	frame_design->setEnabled(true);
	toolsmenu->show();

	frame_player->show();
	frame_player->raise();
	frame_ctrl_player->raise();			
	QFile myfic(wpathprojet + "/eraseall.sh");

	if ( myfic.open( QIODevice::WriteOnly ))
	{
		QTextStream out(&myfic);
		#ifdef Q_WS_WIN
			out << "cd \"" + wpathprojet + "\"" << endl;
			out << "del /Q bandeson.mpa" << endl;
			out << "del /Q bandeson.wav" << endl;
			out << "del /Q fin.ogg" << endl;
			out << "del /Q trtaudio.sh" << endl;
			out << "del /Q audio.sh" << endl;
			out << "del /Q bandeson.ogg" << endl;
			out << "del /Q bandeson2.ogg" << endl;
			out << "del /Q extrait.ogg" << endl;
			out << "del /Q extrait2.ogg" << endl;
			out << "del /Q silent.ogg" << endl;
			out << "del /Q silent.mp3" << endl;
			out << "del /Q Slide_xxx_*.jpg" << endl;
			out << "del /Q Manslide_fdlastphoto*.jpg" << endl;
			out << "del /Q Manslide_fdnoir.jpg" << endl;
			out << "del /Q " + name_encoding->text();
			out << "del /Q *.sh" << endl;
			//
			out << "del /Q extrait.ogg" << endl;
			out << "del /Q extrait2.ogg" << endl;
			out << "del /Q melange.ogg" << endl;
			out << "del /Q debut.ogg" << endl;
			out << "del /Q fin.ogg" << endl;
		#else
			out << "#!/bin/bash" << endl;
			out << "cd \"" + wpathprojet + "\"" << endl;
			out << "rm -f bandeson.mpa" << endl;
			out << "rm -f bandeson.wav" << endl;
			out << "rm -f fin.ogg" << endl;
			out << "rm -f trtaudio.sh" << endl;
			out << "rm -f audio.sh" << endl;
			out << "rm -f bandeson.ogg" << endl;
			out << "rm -f bandeson2.ogg" << endl;
			out << "rm -f extrait.ogg" << endl;
			out << "rm -f extrait2.ogg" << endl;
			out << "rm -f silent.ogg" << endl;
			out << "rm -f silent.mp3" << endl;
			out << "rm -f Slide_xxx_*.jpg" << endl;
			out << "rm -f Manslide_fdlastphoto*.jpg" << endl;
			out << "rm -f Manslide_fdnoir.jpg" << endl;
			//
			out << "rm -f extrait.ogg" << endl;
			out << "rm -f extrait2.ogg" << endl;
			out << "rm -f melange.ogg" << endl;
			out << "rm -f debut.ogg" << endl;
			out << "rm -f fin.ogg" << endl;
			out << "rm -f " + name_encoding->text();
			out << "rm -f *.sh" << endl;
		#endif
		//
		out << "exit" << endl;
		myfic.close();
		//
		#ifdef Q_WS_WIN
			QString program = "cmd";
		#else
			QString program = "bash";
		#endif
		QStringList arguments;
       		arguments << wpathprojet + "/eraseall.sh";
		ispreview = 9;
		myprocess_9.start( program, arguments );
	}
	affichage_diapo();
	QFile myfic2(QDir::homePath() + "/.logsmile.txt");

	if ( myfic2.open( QIODevice::WriteOnly ))
	{
		QTextStream out2(&myfic2);
		out2 << myconsole;
		myfic2.close();
	}
	QTimer::singleShot(30, this, SLOT(f_play_result()));
}

void mainfrm::f_play_result()
{
	player_open_video( path_out_render->text() + "/" + name_encoding->text() );
}

void mainfrm::f_close_param_rendering()
{
	contain_timeline->setEnabled(true);
	tabmenus->setEnabled(true);
	frame_design->setEnabled(true);
	menubar->setEnabled(true);
	toolBar->setEnabled(true);
	toolsmenu->show();
	param_encoding->hide();
}

void mainfrm::f_param_rendering()
{
	contain_timeline->setDisabled(true);
	tabmenus->setDisabled(true);
	frame_design->setDisabled(true);
	menubar->setDisabled(true);
	toolBar->setDisabled(true);
	from_encoding->setValue(1);
	from_encoding->setMinimum(1);
	from_encoding->setMaximum(nbre_photos+1);
	to_encoding->setMinimum(1);
	to_encoding->setMaximum(nbre_photos+1);
	to_encoding->setValue(nbre_photos+1);
	thread_ani->scr_width = this->width();
	thread_ani->scr_height = this->height();
	thread_ani->hdlfra = param_encoding;
	thread_ani->wlwidth = param_encoding->width();
	thread_ani->wlheight = param_encoding->height();
	thread_ani->type_ani = 0;
	thread_ani->y = (param_encoding->height()*-1)+(menubar->height() + toolBar->height());
	param_encoding->setGeometry((this->width() - param_encoding->width())/2,param_encoding->height()*-1,param_encoding->width(),param_encoding->height());
	toolsmenu->hide();
	param_encoding->show();
	if ( name_encoding->text().right(4) == ".MPG" || name_encoding->text().right(4) == ".mpg" || name_encoding->text().right(4) == ".avi" || name_encoding->text().right(4) == ".AVI" || name_encoding->text().right(4) == ".flv" || name_encoding->text().right(4) == ".FLV" )
	{
		name_encoding->setText(name_encoding->text().mid(0,name_encoding->text().length()-4));
	}
	QTimer::singleShot(10, this, SLOT(f_startani()));
}

void mainfrm::f_startani()
{
	thread_ani->run();
}

void mainfrm::f_generer_diapo()
{
	if ( name_encoding->text() == "" )
	{
 		QMessageBox::information(this, "SMILE", tr("Vous devez choisir un titre pour votre diaporama.\n"));
	}
	else
	{
		if ( path_out_render->text() != "" )
		{
			if ( from_encoding->value() > to_encoding->value() )
			{
				QMessageBox::information(this, "SMILE", tr("La diapositive de fin doit etre situee apres la diapositive de debut.\n"));
			}
			else
			{
				QString wl_tmp_001 = name_encoding->text();
				if ( combocodec->currentIndex() == 0 )
				{
					if ( wl_tmp_001.right(4) != ".mpg" && wl_tmp_001.right(4) != ".mpg" && wl_tmp_001.right(5) != ".MPEG" && wl_tmp_001.right(5) != ".mpeg" )
					{
						wl_tmp_001 = wl_tmp_001 + ".mpg";
						name_encoding->setText(wl_tmp_001);
					}
				}
				else
				{
					if ( combocodec->currentIndex() == 1 || combocodec->currentIndex() == 2 )
					{
						if ( wl_tmp_001.right(4) != ".avi" && wl_tmp_001.right(4) != ".AVI" )
						{
							wl_tmp_001 = wl_tmp_001 + ".avi";
							name_encoding->setText(wl_tmp_001);
						}
					}
					else
					{
						if ( wl_tmp_001.right(4) != ".flv" && wl_tmp_001.right(4) != ".FLV" )
						{
							wl_tmp_001 = wl_tmp_001 + ".flv";
							name_encoding->setText(wl_tmp_001);
						}
					}
				}
				param_encoding->hide();
				QTimer::singleShot(10, this, SLOT(f_render()));
			}
		}
		else
		{
			QMessageBox::information(this, "SMILE", tr("Vous devez choir le dossier de destination pour votre diaporama.\n"));
		}
	}
}
