/***************************************************************************
                          setpagedisplay.cpp  -  description
                             -------------------
    begin                : Thu Aug 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 "setpagedisplay.moc"
#include <kstddirs.h>
#include <tdemessagebox.h>
#include <kcolordialog.h>
#include <tdefontdialog.h>
#include <tqstyle.h>
#include <tqregexp.h>
#include <kiconloader.h>
#include <tdefiledialog.h>
#include <tdeio/netaccess.h>

setPageDisplay::setPageDisplay(TQWidget *parent, resource *Rsrc  ) : TQVBoxLayout(parent)
{	
	TQStyle& Style = TQApplication::style();
	Resource = Rsrc;
	margin = Style.defaultFrameWidth();
	NewBoards = 0;
	NewChessmen = 0;
	changeTheme = FALSE;
	refreshBoard = FALSE;
	rebuildConsole = FALSE;

	TabParent = new TQTabWidget( parent, "setPageDisplay" );
	initTab1();
	initTab2();
	addWidget( TabParent );
}
setPageDisplay::~setPageDisplay()
{
	delete Show_Splashscreen;
	delete Board_Orientation;
	delete GROUP_General_Graphics;
	delete Size_Max;
	delete Size_Min;
	delete Current_Theme_Size;
	delete Current_Boards;
	delete Current_Chessmen;
	delete GROUP_Boards;
	delete GROUP_Chessmen;
	delete BOX_Themes;
	delete Tab1;
	delete Tab2;
	delete TabParent;
}
///////////////////////////////////////
//
//	setPageDisplay::initTab1
//
///////////////////////////////////////
void setPageDisplay::initTab1( void )
{
	Tab1 = new TQVBox( TabParent );
	Tab1->setMargin( margin );
	BOX_Themes = new TQHBox( Tab1 );
	GROUP_Boards = new TQGroupBox( 1,
																TQt::Vertical,
																i18n( "Board Themes" ),
																BOX_Themes );
	Current_Boards = new KComboBox ( GROUP_Boards );
	GROUP_Chessmen = new TQGroupBox( 1,
																TQt::Vertical,
																i18n( "Chessman Themes" ),
																BOX_Themes );
	Current_Chessmen = new KComboBox ( GROUP_Chessmen );
	
	GROUP_Theme_Size = new TQGroupBox( 3,
																TQt::Horizontal,
																i18n( "Theme Size" ),
																Tab1 );
	
	Size_Min = new TQLabel( i18n( "Small" ), GROUP_Theme_Size );
	Current_Theme_Size = new TQSlider ( 	0,
																			14,
																			2,
																			( Resource->ThemeSize - IMAGE_MIN ) / 8 ,
																			TQt::Horizontal,
																			GROUP_Theme_Size );
	Size_Max = new TQLabel( i18n( "Large" ), GROUP_Theme_Size );
	
	GROUP_SCID_Images = new TQGroupBox( 2, TQt::Horizontal, i18n("Player Images File:"), Tab1 );
	EDIT_SCID_Images = new KLineEdit( GROUP_SCID_Images );
	EDIT_SCID_Images->setText( Resource->SCID_Image_Path );
	connect( EDIT_SCID_Images, TQ_SIGNAL( textChanged(const TQString&) ), this, TQ_SLOT( slot_SCID_Images(const TQString&) ) );
	BUTTON_SCID_Images = new TQPushButton( GROUP_SCID_Images );
	BUTTON_SCID_Images->setPixmap( Resource->LoadIcon( TQString( "document-open" ), TDEIcon::Toolbar ) );
	connect( BUTTON_SCID_Images, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_SCID_Images_Button() ) );

	GROUP_General_Graphics = new TQGroupBox( 4,
																TQt::Vertical,
																i18n( "Other Display Options" ),
																Tab1 );

	Board_Orientation = new TQCheckBox( i18n( "Reverse Board Orientation" ), GROUP_General_Graphics );
	Board_Orientation->setChecked( Resource->OPTION_Board_Orientation );
	connect(	Board_Orientation, TQ_SIGNAL( toggled(bool) ),
						this, TQ_SLOT( slotToggle_Board_Orientation(bool) ) );

	Show_Splashscreen = new TQCheckBox( i18n( "Display Startup Logo" ), GROUP_General_Graphics );
	Show_Splashscreen->setChecked( Resource->OPTION_Show_Splash );
	connect(	Show_Splashscreen, TQ_SIGNAL( toggled(bool) ),
						this, TQ_SLOT( slotToggle_Splashscreen(bool) ) );

	Auto_Preview = new TQCheckBox( i18n( "Automatic Preview" ), GROUP_General_Graphics );
	Auto_Preview->setChecked( Resource->OPTION_Auto_Preview );
	connect(	Auto_Preview, TQ_SIGNAL( toggled(bool) ),
						this, TQ_SLOT( slotToggle_Auto_Preview(bool) ) );

	Show_Last_Move = new TQCheckBox( i18n( "Show Last Move" ), GROUP_General_Graphics );
	Show_Last_Move->setChecked( Resource->OPTION_Show_Last_Move );
	connect(	Show_Last_Move, TQ_SIGNAL( toggled(bool) ),
						this, TQ_SLOT( slotToggle_Show_Last_Move(bool) ) );

	Animate_Move = new TQCheckBox( i18n( "Animate Moves" ), GROUP_General_Graphics );
	Animate_Move->setChecked( Resource->OPTION_Animate_Moves );
	connect(	Animate_Move, TQ_SIGNAL( toggled(bool) ),
						this, TQ_SLOT( slotToggle_Animate_Move(bool) ) );

	Show_Coord = new TQCheckBox( i18n( "Show Coordinates" ), GROUP_General_Graphics );
	Show_Coord->setChecked( Resource->OPTION_Show_Coord );
	connect(	Show_Coord, TQ_SIGNAL( toggled(bool) ),
						this, TQ_SLOT( slotToggle_Show_Coord(bool) ) );

	Current_Theme_Size->setTickmarks( TQSlider::Below );
	Current_Boards->setEditable( FALSE );
	Current_Chessmen->setEditable( FALSE );
	buildThemeList();
	connect(	Current_Boards, TQ_SIGNAL( activated(int) ),
						this, TQ_SLOT( slotCurrent_Boards(int) ) );
	connect(	Current_Chessmen, TQ_SIGNAL( activated(int) ),
						this, TQ_SLOT( slotCurrent_Chessmen(int) ) );
	connect(	Current_Theme_Size, TQ_SIGNAL( valueChanged(int) ),
						this, TQ_SLOT( slotCurrent_Theme_Size(int) ) );
	TabParent->addTab( Tab1, i18n("General") );
}
///////////////////////////////////////
//
//	setPageDisplay::initTab2
//
///////////////////////////////////////
void setPageDisplay::initTab2( void )
{
	TQPixmap ColorsIcon( Resource->LoadIcon( TQString("colorize"), TDEIcon::Toolbar ) );
	Tab2 = new TQFrame( TabParent );
	Tab2->setMargin( margin );

	GRID_Style = new TQGridLayout( Tab2, 11, 5, margin );

		Console_Sample = new TQTextView( Tab2 );
		Console_Sample->setTextFormat( TQt::RichText );
		GRID_Style->addMultiCellWidget( Console_Sample, 5, 10, 1, 1 );

		Default_Button = new TQPushButton( i18n("Restore Defaults"), Tab2 );
		connect( Default_Button, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_setDefault() ) );
		GRID_Style->addWidget( Default_Button, 11, 1 );

		Fonts_Button_Standard = new TQPushButton( i18n("Standard Font..."), Tab2 );
		connect( Fonts_Button_Standard, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_Font_Standard() ) );
		GRID_Style->addWidget( Fonts_Button_Standard, 5, 3 );

		Colors_Button_Standard = new TQPushButton( Tab2 );
		Colors_Button_Standard->setPixmap( ColorsIcon );
		connect( Colors_Button_Standard, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_Color_Standard() ) );
		GRID_Style->addWidget( Colors_Button_Standard, 5, 5 );

		Fonts_Button_PrivateTell = new TQPushButton( i18n("Private Font..."), Tab2 );
		connect( Fonts_Button_PrivateTell, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_Font_PrivateTell() ) );
		GRID_Style->addWidget( Fonts_Button_PrivateTell, 6, 3 );

		Colors_Button_PrivateTell = new TQPushButton( Tab2 );
		Colors_Button_PrivateTell->setPixmap( ColorsIcon );
		connect( Colors_Button_PrivateTell, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_Color_PrivateTell() ) );
		GRID_Style->addWidget( Colors_Button_PrivateTell, 6, 5 );

		Fonts_Button_ChannelTell = new TQPushButton( i18n("Channel Font..."), Tab2 );
		connect( Fonts_Button_ChannelTell, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_Font_ChannelTell() ) );
		GRID_Style->addWidget( Fonts_Button_ChannelTell, 7, 3 );

		Colors_Button_ChannelTell = new TQPushButton( Tab2 );
		Colors_Button_ChannelTell->setPixmap( ColorsIcon );
		connect( Colors_Button_ChannelTell, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_Color_ChannelTell() ) );
		GRID_Style->addWidget( Colors_Button_ChannelTell, 7, 5 );

		Fonts_Button_Shout = new TQPushButton( i18n("Shout Font..."), Tab2 );
		connect( Fonts_Button_Shout, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_Font_Shout() ) );
		GRID_Style->addWidget( Fonts_Button_Shout, 8, 3 );

		Colors_Button_Shout = new TQPushButton( Tab2 );
		Colors_Button_Shout->setPixmap( ColorsIcon );
		connect( Colors_Button_Shout, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_Color_Shout() ) );
		GRID_Style->addWidget( Colors_Button_Shout, 8, 5 );

		Fonts_Button_Whisper = new TQPushButton( i18n("Whisper Font..."), Tab2 );
		connect( Fonts_Button_Whisper, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_Font_Whisper() ) );
		GRID_Style->addWidget( Fonts_Button_Whisper, 9, 3 );

		Colors_Button_Whisper = new TQPushButton( Tab2 );
		Colors_Button_Whisper->setPixmap( ColorsIcon );
		connect( Colors_Button_Whisper, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_Color_Whisper() ) );
		GRID_Style->addWidget( Colors_Button_Whisper, 9, 5 );

		Fonts_Button_Notification = new TQPushButton( i18n("Notification Font..."), Tab2 );
		connect( Fonts_Button_Notification, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_Font_Notification() ) );
		GRID_Style->addWidget( Fonts_Button_Notification, 10, 3 );

		Colors_Button_Notification = new TQPushButton( Tab2 );
		Colors_Button_Notification->setPixmap( ColorsIcon );
		connect( Colors_Button_Notification, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_Color_Notification() ) );
		GRID_Style->addWidget( Colors_Button_Notification, 10, 5 );

		Colors_Button_Background = new TQPushButton( Tab2 );
		Colors_Button_Background->setPixmap( ColorsIcon );
		connect( Colors_Button_Background, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slot_Color_Background() ) );
		GRID_Style->addWidget( Colors_Button_Background, 11, 5 );

	resetSampleConsole();
	TabParent->addTab( Tab2, i18n("Console") );
}
///////////////////////////////////////
//
//	setPageDisplay::slot_setDefault
//
///////////////////////////////////////
void setPageDisplay::slot_setDefault(void)
{
	Resource->FONT_Standard = TDEGlobalSettings::fixedFont();
	Resource->FONT_PrivateTell = TDEGlobalSettings::fixedFont();
	Resource->FONT_ChannelTell = TDEGlobalSettings::fixedFont();
	Resource->FONT_Shout = TDEGlobalSettings::fixedFont();
	Resource->FONT_Whisper = TDEGlobalSettings::fixedFont();
	Resource->FONT_Notification = TDEGlobalSettings::fixedFont();
	Resource->COLOR_Background = Resource->COLOR_White;
	Resource->COLOR_Standard = Resource->COLOR_Black;
	Resource->COLOR_PrivateTell = TQColor( 128, 0, 128 );	// Purple
	Resource->COLOR_ChannelTell = TQColor( 0, 0, 160 );	// Blue
	Resource->COLOR_Shout = TQColor( 160, 0, 0 );	// Red
	Resource->COLOR_Whisper = TQColor( 0, 160, 0 );	// Green
	Resource->COLOR_Notification = TQColor( 0, 128, 128 );	// Cyan
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageDisplay::slot_Font_?????
//
///////////////////////////////////////
void setPageDisplay::slot_Font_Standard(void)
{
	int value = TDEFontDialog::getFont( Resource->FONT_Standard );
	if( value != TDEFontDialog::Accepted ) return;
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
void setPageDisplay::slot_Font_PrivateTell(void)
{
	int value = TDEFontDialog::getFont( Resource->FONT_PrivateTell );
	if( value != TDEFontDialog::Accepted ) return;
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
void setPageDisplay::slot_Font_ChannelTell(void)
{
	int value = TDEFontDialog::getFont( Resource->FONT_ChannelTell );
	if( value != TDEFontDialog::Accepted ) return;
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
void setPageDisplay::slot_Font_Shout(void)
{
	int value = TDEFontDialog::getFont( Resource->FONT_Shout );
	if( value != TDEFontDialog::Accepted ) return;
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
void setPageDisplay::slot_Font_Whisper(void)
{
	int value = TDEFontDialog::getFont( Resource->FONT_Whisper );
	if( value != TDEFontDialog::Accepted ) return;
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
void setPageDisplay::slot_Font_Notification(void)
{
	int value = TDEFontDialog::getFont( Resource->FONT_Notification );
	if( value != TDEFontDialog::Accepted ) return;
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageDisplay::slot_Color_?????
//
///////////////////////////////////////
void setPageDisplay::slot_Color_Background(void)
{
	int value = KColorDialog::getColor( Resource->COLOR_Background );
	if( value != KColorDialog::Accepted ) return;
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
void setPageDisplay::slot_Color_Standard(void)
{
	int value = KColorDialog::getColor( Resource->COLOR_Standard );
	if( value != KColorDialog::Accepted ) return;
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
void setPageDisplay::slot_Color_PrivateTell(void)
{
	int value = KColorDialog::getColor( Resource->COLOR_PrivateTell );
	if( value != KColorDialog::Accepted ) return;
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
void setPageDisplay::slot_Color_ChannelTell(void)
{
	int value = KColorDialog::getColor( Resource->COLOR_ChannelTell );
	if( value != KColorDialog::Accepted ) return;
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
void setPageDisplay::slot_Color_Shout(void)
{
	int value = KColorDialog::getColor( Resource->COLOR_Shout );
	if( value != KColorDialog::Accepted ) return;
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
void setPageDisplay::slot_Color_Whisper(void)
{
	int value = KColorDialog::getColor( Resource->COLOR_Whisper );
	if( value != KColorDialog::Accepted ) return;
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
void setPageDisplay::slot_Color_Notification(void)
{
	int value = KColorDialog::getColor( Resource->COLOR_Notification );
	if( value != KColorDialog::Accepted ) return;
	resetSampleConsole();
	rebuildConsole = TRUE;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageDisplay::slotCurrent_Boards
//
///////////////////////////////////////
void setPageDisplay::slotCurrent_Boards( int Index )
{
	NewBoards = Index;
	changeTheme = TRUE;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageDisplay::slotCurrent_Chessmen
//
///////////////////////////////////////
void setPageDisplay::slotCurrent_Chessmen( int Index )
{
	NewChessmen = Index;
	changeTheme = TRUE;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageDisplay::slotCurrent_Theme_Size
//
///////////////////////////////////////
void setPageDisplay::slotCurrent_Theme_Size( int Index )
{
	Resource->ThemeSize = 32 + ( Index * 8 );
	changeTheme = TRUE;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageDisplay::slotToggle_Board_Orientation
//
///////////////////////////////////////
void setPageDisplay::slotToggle_Board_Orientation( bool state )
{
	Resource->OPTION_Board_Orientation = state;
	refreshBoard = TRUE;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageDisplay::slotToggle_Splashscreen
//
///////////////////////////////////////
void setPageDisplay::slotToggle_Splashscreen( bool state )
{
	Resource->OPTION_Show_Splash = state;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageDisplay::slotToggle_Auto_Preview
//
///////////////////////////////////////
void setPageDisplay::slotToggle_Auto_Preview( bool state )
{
	Resource->OPTION_Auto_Preview = state;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageDisplay::slotToggle_Show_Last_Move
//
///////////////////////////////////////
void setPageDisplay::slotToggle_Show_Last_Move( bool state )
{
	Resource->OPTION_Show_Last_Move = state;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageDisplay::slotToggle_Animate_Move
//
///////////////////////////////////////
void setPageDisplay::slotToggle_Animate_Move( bool state )
{
	Resource->OPTION_Animate_Moves = state;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageDisplay::slotToggle_Show_Coord
//
///////////////////////////////////////
void setPageDisplay::slotToggle_Show_Coord( bool state )
{
	Resource->OPTION_Show_Coord = state;
	refreshBoard = TRUE;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageDisplay::buildThemeList
//
///////////////////////////////////////
void setPageDisplay::buildThemeList( void )
{
	TQString buffer;
	int tmp(0);

	Current_Boards->clear();
	Current_Chessmen->clear();
	Resource->readThemeDir();
	while(1)
	{
		buffer = Resource->getBoard( tmp );
		if( buffer.isEmpty() ) break;
		buffer.remove( 0, 2 );
		buffer.replace( TQRegExp("_"), " " );
		buffer.replace( TQRegExp(".tar"), "" );
		buffer.replace( TQRegExp(".gz"), "" );
		buffer.replace( TQRegExp(".bz2"), "" );
		Current_Boards->insertItem( buffer, tmp );
		if( Resource->getBoard() == Resource->getBoard( tmp ) )
		{
			Current_Boards->setCurrentItem( tmp );
			NewBoards = tmp;
		}
		tmp++;
	}
	/* ...and for Chessmen */
	tmp = 0;
	while(1)
	{
		buffer = Resource->getChessmen( tmp );
		if( buffer.isEmpty() ) break;
		buffer.remove( 0, 2 );
		buffer.replace( TQRegExp("_"), " " );
		buffer.replace( TQRegExp(".tar"), "" );
		buffer.replace( TQRegExp(".gz"), "" );
		buffer.replace( TQRegExp(".bz2"), "" );
		Current_Chessmen->insertItem( buffer, tmp );
		if( Resource->getChessmen() == Resource->getChessmen( tmp ) )
		{
			Current_Chessmen->setCurrentItem( tmp );
			NewChessmen = tmp;
		}
		tmp++;
	}
}
///////////////////////////////////////
//
//	setPageDisplay::resetSampleConsole
//
///////////////////////////////////////
void setPageDisplay::resetSampleConsole( void )
{
	Resource->buildStyle();
	Console_Sample->setPaper( TQBrush( Resource->COLOR_Background ) );
	Console_Sample->setText( TQString( "<K_STD>" + i18n( "Standard" ) + "</K_STD>\n" ) );
	Console_Sample->append( TQString( "<K_PVT>" + i18n( "Private Tell" ) + "</K_PVT>\n" ) );
	Console_Sample->append( TQString( "<K_CH>" + i18n( "Channel Tell" ) + "</K_CH>\n" ) );
	Console_Sample->append( TQString( "<K_SHT>" + i18n( "Shout" ) + "</K_SHT>\n" ) );
	Console_Sample->append( TQString( "<K_WSP>" + i18n( "Whisper" ) + "</K_WSP>\n" ) );
	Console_Sample->append( TQString( "<K_NOT>" + i18n( "Notification" ) + "</K_NOT>" ) );
}
///////////////////////////////////////
//
//	setPageDisplay::slot_SCID_Images
//
///////////////////////////////////////
void setPageDisplay::slot_SCID_Images( const TQString& string )
{
	Resource->SCID_Image_Path = string;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageDisplay::slot_SCID_Images_Button
//
///////////////////////////////////////
void setPageDisplay::slot_SCID_Images_Button( void )
{
	TQString temp;

	temp = KFileDialog::getOpenFileName(	TQString(),
																				TQString( "*" ),
																				Tab1,
																				i18n( "Find Player Images File..." ) );
	if( temp.isEmpty() ) return;
	EDIT_SCID_Images->setText( temp );
	Resource->SCID_Image_Path = temp;
	emit enableApply();
}


