/***************************************************************************
                          dlg_settings.cpp  -  description
                             -------------------
    begin                : Mon Jul 16 2001
    copyright            : (C) 2003 by Troy Corbin Jr.
    email                : tcorbin@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kicontheme.h>
#include <kstandarddirs.h>
#include "definitions.h"
#include "dlg_settings.moc"
#include "setpagedisplay.h"
#include "setpageaudio.h"
#include "setpageengines.h"
#include "setpageservers.h"
#include "setpagegeneral.h"

///////////////////////////////////////
//
//	dlg_settings::Constructor
//
///////////////////////////////////////
dlg_settings::dlg_settings(TQWidget *parent, const char *name, resource *Rsrc ) :
	KDialogBase(	IconList,
								i18n("Configure"),
								Help|Ok|Apply|Cancel,
								Ok,
								parent,
								name,
								false,
								false )
{
	TQString buffer;
	Resource = Rsrc;
	Resource->ConfigWrite();

	/* General Page */
	FRAME_General = addPage(	i18n( "General" ),
														i18n( "Various aspects of Knights get configured here." ),
														TQPixmap( locate( "data", "knights/icon-general.png" ) ) );
	PAGE_General = new setPageGeneral( FRAME_General, Resource );
	connect( PAGE_General, TQ_SIGNAL( enableApply() ), this, TQ_SLOT( slotEnableApply() ) );

	/* Display Page */
	FRAME_Display = addPage(	i18n( "Display" ),
														i18n( "These settings determine how Knights displays itself to you." ),
														TQPixmap( locate( "data", "knights/icon-display.png" ) ) );
	PAGE_Display = new setPageDisplay( FRAME_Display, Resource );
	connect( PAGE_Display, TQ_SIGNAL( enableApply() ), this, TQ_SLOT( slotEnableApply() ) );

	/* Audio Page */
	FRAME_Audio = addPage(	i18n( "Audio" ),
														i18n( "These settings determine what Knights sounds like." ),
														Resource->LoadIcon( TQString("multimedia"), TDEIcon::Panel ) );
	PAGE_Audio = new setPageAudio( FRAME_Audio, Resource );
	connect( PAGE_Audio, TQ_SIGNAL( enableApply() ), this, TQ_SLOT( slotEnableApply() ) );

	/* Engines Page */
	FRAME_Engines = addPage(	i18n( "Computer Opponents" ),
														i18n( "Configure your chess engines here." ),
														Resource->LoadIcon( TQString("gear"), TDEIcon::Panel ) );
	PAGE_Engines = new setPageEngines( FRAME_Engines, Resource );
	connect( PAGE_Engines, TQ_SIGNAL( enableApply() ), this, TQ_SLOT( slotEnableApply() ) );

	/* Servers Page */
	FRAME_Servers = addPage(	i18n( "Chess Servers" ),
														i18n( "Configure your internet chess servers here." ),
														Resource->LoadIcon( TQString("network"), TDEIcon::Panel ) );
	PAGE_Servers = new setPageServers( FRAME_Servers, Resource );
	connect( PAGE_Servers, TQ_SIGNAL( enableApply() ), this, TQ_SLOT( slotEnableApply() ) );

	connect( this, TQ_SIGNAL( aboutToShowPage( TQWidget* ) ), this, TQ_SLOT( slotPageChanging( TQWidget* ) ) );

	/* Init the buttons */
	showButtonCancel( true );
	showButtonOK( true );
	showButtonApply( true );
	showButton( Help, true );

	enableButtonCancel( true );
	enableButtonOK( true );
	enableButtonApply( false );
	enableButton( Help, true );

	setHelp( TQString( "configure" ) );
	show();
}
///////////////////////////////////////
//
//	dlg_settings::Destructor
//
///////////////////////////////////////
dlg_settings::~dlg_settings()
{
}
///////////////////////////////////////
//
//	dlg_settings::slotOk
//
///////////////////////////////////////
void dlg_settings::slotOk( void )
{
	slotApply();
	slotDelayedDestruct();
}
///////////////////////////////////////
//
//	dlg_settings::slotApply
//
///////////////////////////////////////
void dlg_settings::slotApply( void )
{
	if( PAGE_Display->changeTheme )
	{
		PAGE_Display->changeTheme = false;
		emit themeChanged( PAGE_Display->NewBoards, PAGE_Display->NewChessmen );
	}
	if( PAGE_Display->refreshBoard )
	{
		PAGE_Display->refreshBoard = false;
		emit redrawBoard();
	}
	if( PAGE_Display->rebuildConsole )
	{
		Resource->buildStyle();
		PAGE_Display->rebuildConsole = false;
		emit rebuildConsole();
	}
	if( PAGE_Audio->changeTheme )
	{
		PAGE_Audio->changeTheme = false;
		Resource->setAudio( PAGE_Audio->NewSounds );
	}
	if( PAGE_Servers->resetServer )
	{
		PAGE_Servers->resetServer = false;
		emit resetServer();
	}
	Resource->ConfigWrite();
	enableButtonApply( false );
}
///////////////////////////////////////
//
//	dlg_settings::slotCancel
//
///////////////////////////////////////
void dlg_settings::slotCancel( void )
{
	Resource->ConfigRead();
	slotDelayedDestruct();
}
///////////////////////////////////////
//
//	dlg_settings::slotEnableApply
//
///////////////////////////////////////
void dlg_settings::slotEnableApply( void )
{
	enableButtonApply( true );
}
///////////////////////////////////////
//
//	dlg_settings::slotThemesAdded
//
///////////////////////////////////////
void dlg_settings::slotThemesAdded( void )
{
	PAGE_Display->buildThemeList();
	PAGE_Audio->buildThemeList();
}
///////////////////////////////////////
//
//	dlg_settings::slotPageChanging
//
///////////////////////////////////////
void dlg_settings::slotPageChanging( TQWidget *page )
{
	if( page == FRAME_General )
		setHelp( TQString( "configure-general" ) );
	else if( page == FRAME_Display )
		setHelp( TQString( "configure-display" ) );
	else if( page == FRAME_Audio )
		setHelp( TQString( "configure-audio" ) );
	else if( page == FRAME_Engines )
		setHelp( TQString( "configure-engines" ) );
	else if( page == FRAME_Servers )
		setHelp( TQString( "configure-servers" ) );
}



