/* ============================================================
 * File  : media_browser.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 "mainfrm.h"

void mainfrm::f_view_arborescence()
{
	if ( frame_tree->isVisible() == true )
	{
		top_window->setEnabled(true);
		frame_pro->setEnabled(true);

		menuProjet->setEnabled(true);
		menuMenu->setEnabled(true);
		menuG_n_ration->setEnabled(true);
		menuEdition->setEnabled(true);
		actionAfficher_cacher_les_r_gles->setEnabled(true);
		actionAfficher_cacher_les_coordonn_es->setEnabled(true);
		actionAfficher_cacher_les_zones_de_s_curit->setEnabled(true);
		actionAfficher_cacher_les_contours->setEnabled(true);
		menuA_propos->setEnabled(true);
		frame_tree->hide();
		menu_th *tmphdl = hdl_current_menuth;
		hdl_current_menuth = 0;
		special_delete = 1;
		f_select_menuth(tmphdl);
		special_delete = 0;
	}
	else
	{
		f_save_widgetonmenu();

		top_window->setDisabled(true);
		frame_pro->setDisabled(true);

		menuProjet->setDisabled(true);
		menuMenu->setDisabled(true);
		menuG_n_ration->setDisabled(true);
		menuEdition->setDisabled(true);
		actionAfficher_cacher_les_r_gles->setDisabled(true);
		actionAfficher_cacher_les_coordonn_es->setDisabled(true);
		actionAfficher_cacher_les_zones_de_s_curit->setDisabled(true);
		actionAfficher_cacher_les_contours->setDisabled(true);
		menuA_propos->setDisabled(true);

		int wl_int_001 = 0;
		int wl_int_002 = 0;
		frame_syno->num_actif = 999;

		// *** MINIATURE DES MENUS ***

		while ( wl_int_001 <= nbre_menuth )
		{
			frame_syno->tab_pnt_item[wl_int_001]->fond = table_hdl_menuth[wl_int_001]->draw_image;
			frame_syno->tab_pnt_item[wl_int_001]->nbre_menus = nbre_menuth;
			frame_syno->tab_pnt_item[wl_int_001]->nbre_medias = nbre_medias;
			if ( table_hdl_menuth[wl_int_001] == hdl_current_menuth )
			{
				frame_syno->num_actif = wl_int_001;
			}

			wl_int_002 = 0;
			while ( wl_int_002 < nbre_medias )
			{
				frame_syno->tab_pnt_item[wl_int_001]->table_medias[wl_int_002] = table_hdl_medias[wl_int_002]->pathname_media;
				wl_int_002++;
			}

			wl_int_001++;
		}

		// RECHERCHE DES RELATIONS ENTRE MENUS ***
		wl_int_001 = 0;
		wl_int_002 = 0;

		while ( wl_int_001 <= nbre_menuth )
		{
			wl_int_002 = 0;
			while ( table_menus[wl_int_001][wl_int_002][0] != "$$$" )
			{
				if ( table_menus[wl_int_001][wl_int_002][5].mid(0,3) == "!M!" )
				{
					if ( table_menus[wl_int_001][wl_int_002][5] != "!M!"+wg_str_int_003)
					{
						frame_syno->tab_rel[wl_int_001][wl_int_002] = table_menus[wl_int_001][wl_int_002][5].mid(8,table_menus[wl_int_001][wl_int_002][5].length()-8);
					}
					else
					{
						frame_syno->tab_rel[wl_int_001][wl_int_002] = "0";
					}
				}
				else
				{
					frame_syno->tab_rel[wl_int_001][wl_int_002] = " ";
				}
				wl_int_002++;
			}
			frame_syno->tab_rel[wl_int_001][wl_int_002] = "$$$";
			wl_int_001++;
		}

		// RECHERCHE DES RELATIONS ENTRE MENUS ET MEDIAS ***
		wl_int_001 = 0;
		wl_int_002 = 0;

		int wl_int_003;

		while ( wl_int_001 <= nbre_menuth )
		{
			wl_int_002 = 0;
			while ( table_menus[wl_int_001][wl_int_002][0] != "$$$" )
			{
				if ( table_menus[wl_int_001][wl_int_002][5].mid(0,3) != "!M!" && table_menus[wl_int_001][wl_int_002][5] != "" )
				{
					wl_int_003 = 0;

					while ( wl_int_003 < nbre_medias && table_menus[wl_int_001][wl_int_002][5] != table_hdl_medias[wl_int_003]->pathname_media )
					{
						wl_int_003++;
					}

					if ( wl_int_003 < nbre_medias )
					{
						frame_syno->tab_rel_media[wl_int_001][wl_int_002] = QString::number(wl_int_003);
						//qDebug() << wl_int_001 << " " << wl_int_002 << " " << wl_int_003;
					}
					else
					{
						qDebug() << "ERROR FINDING MEDIA FOR TREEVIEW : " << table_menus[wl_int_001][wl_int_002][5];
					}
				}
				else
				{
					frame_syno->tab_rel_media[wl_int_001][wl_int_002] = " ";
				}
				wl_int_002++;
			}
			frame_syno->tab_rel_media[wl_int_001][wl_int_002] = "$$$";
			wl_int_001++;
		}

		frame_syno->nbre_menus = nbre_menuth;
		frame_syno->repaint();
		frame_tree->show();
		frame_tree->raise();
	}
}

void mainfrm::f_item_move(item_menu*)
{
	frame_syno->repaint();
}

void mainfrm::f_item_select(item_menu* hdlm)
{
	int wl_int_001 = 0;
	while ( frame_syno->tab_pnt_item[wl_int_001] != hdlm )
	{
		wl_int_001++;
	}
	frame_syno->num_actif = wl_int_001;
	frame_syno->repaint();
}

void mainfrm::f_affiche_info_arbo(item_menu* hdlm)
{
	frame_nummenu->setGeometry(hdlm->geometry().x(),hdlm->geometry().y()-(frame_nummenu->height()+2),frame_nummenu->width(),77);
	frame_nummenu->wl_action = "FAKEFORARBO";
	hdlm->nbre_menus = nbre_menuth;

	int wl_int_001 = 0;
	while ( frame_syno->tab_pnt_item[wl_int_001] != hdlm )
	{
		wl_int_001++;
	}
	hdlm->num_menu = wl_int_001;
	if ( wl_int_001 == 0 )
	{
		label_text_posarbo->setText(wg_str_int_003);
	}
	else
	{
		label_text_posarbo->setText(tr("Menu")+" "+QString::number(wl_int_001));
	}
	frame_nummenu->show();
	frame_nummenu->raise();
}

void mainfrm::f_hide_info_arbo(item_menu*)
{
	frame_nummenu->hide();
}

void mainfrm::f_edit_menu_tree(item_menu* hdlm)
{
	f_view_arborescence();

	int wl_int_001 = 0;
	while ( frame_syno->tab_pnt_item[wl_int_001] != hdlm )
	{
		wl_int_001++;
	}

	f_select_menuth(table_hdl_menuth[wl_int_001]);
}

void mainfrm::f_affiche_info_mediaarbo(item_media*)
{
	/*frame_nummenu->setGeometry(hdlm->geometry().x(),hdlm->geometry().y()-(frame_nummenu->height()+2),frame_nummenu->width(),frame_nummenu->height());
	frame_nummenu->wl_action = "FAKEFORARBO";
	hdlm->nbre_menus = nbre_menuth;

	int wl_int_001 = 0;
	while ( frame_syno->tab_pnt_item[wl_int_001] != hdlm )
	{
		wl_int_001++;
	}
	hdlm->num_menu = wl_int_001;
	if ( wl_int_001 == 0 )
	{
		label_text_posarbo->setText(wg_str_int_003);
	}
	else
	{
		label_text_posarbo->setText(tr("Menu")+" "+QString::number(wl_int_001));
	}
	frame_nummenu->show();
	frame_nummenu->raise();*/
}

void mainfrm::f_hide_info_mediaarbo(item_media*)
{
	frame_nummenu->hide();
}

void mainfrm::f_add_link(item_menu* hdlm,int numlink)
{
	int wl_int_001 = 0;
	int wl_int_002 = 0;

	while ( frame_syno->tab_pnt_item[wl_int_002] != hdlm )
	{
		wl_int_002++;
	}

	while ( table_menus[wl_int_002][wl_int_001][0] != "$$$" )
	{
		wl_int_001++;
	}

	table_menus[wl_int_002][wl_int_001][0] = "100";
	table_menus[wl_int_002][wl_int_001][1] = "100";
	table_menus[wl_int_002][wl_int_001][2] = "128";
	table_menus[wl_int_002][wl_int_001][3] = "128";
	table_menus[wl_int_002][wl_int_001][4] = "";
	if ( numlink == 0 )
	{
		table_menus[wl_int_002][wl_int_001][5] = "!M!"+wg_str_int_003;
	}
	else
	{
		table_menus[wl_int_002][wl_int_001][5] = "!M!Menu " + QString::number(numlink);
	}
	table_menus[wl_int_002][wl_int_001][6] = "";
	table_menus[wl_int_002][wl_int_001][7] = "0";
	table_menus[wl_int_002][wl_int_001][8] = "128";
	table_menus[wl_int_002][wl_int_001][9] = "";
	table_menus[wl_int_002][wl_int_001][10] = "128";
	table_menus[wl_int_002][wl_int_001][11] = "0";
	table_menus[wl_int_002][wl_int_001][12] = "2";
	table_menus[wl_int_002][wl_int_001][13] = "";
	table_menus[wl_int_002][wl_int_001][14] = "0";
	table_menus[wl_int_002][wl_int_001][15] = QString::number(wl_int_001);
	table_menus[wl_int_002][wl_int_001][16] = QString::number(wl_int_001+1);
	table_menus[wl_int_002][wl_int_001][17] = "255";
	table_menus[wl_int_002][wl_int_001][18] = "0^0^0^$255$0$";
	table_menus[wl_int_002][wl_int_001][19] = "";
	table_menus[wl_int_002][wl_int_001][20] = "0";
	table_menus[wl_int_002][wl_int_001][21] = "0";
	table_menus[wl_int_002][wl_int_001][22] = "";
	table_menus[wl_int_002][wl_int_001][23] = "";
	table_menus[wl_int_002][wl_int_001][24] = "0001";

	table_menus[wl_int_002][wl_int_001+1][0] = "$$$";

	wl_int_001 = 0;
	while ( frame_syno->tab_rel[wl_int_002][wl_int_001] != "$$$" )
	{
		wl_int_001++;
	}

	frame_syno->tab_rel[wl_int_002][wl_int_001] = QString::number(numlink);
	frame_syno->tab_rel[wl_int_002][wl_int_001+1] = "$$$";

	frame_syno->repaint();
}

void mainfrm::f_itemmedia_move(item_media*)
{
	frame_syno->repaint();
}

void mainfrm::f_add_link_media(item_menu* hdlm,QString pathm)
{
	int wl_int_001 = 0;
	int wl_int_002 = 0;

	while ( frame_syno->tab_pnt_item[wl_int_002] != hdlm )
	{
		wl_int_002++;
	}

	while ( table_menus[wl_int_002][wl_int_001][0] != "$$$" )
	{
		wl_int_001++;
	}

	table_menus[wl_int_002][wl_int_001][0] = "100";
	table_menus[wl_int_002][wl_int_001][1] = "100";
	table_menus[wl_int_002][wl_int_001][2] = "128";
	table_menus[wl_int_002][wl_int_001][3] = "128";
	table_menus[wl_int_002][wl_int_001][4] = "";
	table_menus[wl_int_002][wl_int_001][5] = pathm;
	table_menus[wl_int_002][wl_int_001][6] = "";
	table_menus[wl_int_002][wl_int_001][7] = "0";
	table_menus[wl_int_002][wl_int_001][8] = "128";
	table_menus[wl_int_002][wl_int_001][9] = wg_str_int_001;
	table_menus[wl_int_002][wl_int_001][10] = "128";
	table_menus[wl_int_002][wl_int_001][11] = "0";
	table_menus[wl_int_002][wl_int_001][12] = "2";
	table_menus[wl_int_002][wl_int_001][13] = "";
	table_menus[wl_int_002][wl_int_001][14] = "0";
	table_menus[wl_int_002][wl_int_001][15] = QString::number(wl_int_001);
	table_menus[wl_int_002][wl_int_001][16] = QString::number(wl_int_001+1);
	table_menus[wl_int_002][wl_int_001][17] = "255";
	table_menus[wl_int_002][wl_int_001][18] = "0^0^0^$255$0$";
	table_menus[wl_int_002][wl_int_001][19] = wg_str_int_006;
	table_menus[wl_int_002][wl_int_001][20] = "0";
	table_menus[wl_int_002][wl_int_001][21] = "0";
	table_menus[wl_int_002][wl_int_001][22] = "";
	table_menus[wl_int_002][wl_int_001][23] = "";
	table_menus[wl_int_002][wl_int_001][24] = "0001";

	table_menus[wl_int_002][wl_int_001+1][0] = "$$$";

	// RECHERCHE DES RELATIONS ENTRE MENUS ET MEDIAS ***
	wl_int_001 = 0;
	wl_int_002 = 0;

	int wl_int_003;

	while ( wl_int_001 <= nbre_menuth )
	{
		wl_int_002 = 0;
		while ( table_menus[wl_int_001][wl_int_002][0] != "$$$" )
		{
			if ( table_menus[wl_int_001][wl_int_002][5].mid(0,3) != "!M!" && table_menus[wl_int_001][wl_int_002][5] != "" )
			{
				wl_int_003 = 0;

				while ( wl_int_003 < nbre_medias && table_menus[wl_int_001][wl_int_002][5] != table_hdl_medias[wl_int_003]->pathname_media )
				{
					wl_int_003++;
				}

				if ( wl_int_003 < nbre_medias )
				{
					frame_syno->tab_rel_media[wl_int_001][wl_int_002] = QString::number(wl_int_003);
					//qDebug() << wl_int_001 << " " << wl_int_002 << " " << wl_int_003;
				}
				else
				{
					qDebug() << "ERROR FINDING MEDIA FOR TREEVIEW : " << table_menus[wl_int_001][wl_int_002][5];
				}
			}
			else
			{
				frame_syno->tab_rel_media[wl_int_001][wl_int_002] = " ";
			}
			wl_int_002++;
		}
		frame_syno->tab_rel_media[wl_int_001][wl_int_002] = "$$$";
		wl_int_001++;
	}

	frame_syno->repaint();
}

void mainfrm::f_itemmedia_ren(media* hdlm)
{
	hdl_current_media = hdlm;
	f_encode_media(hdlm);
}


