/***************************************************************************
 *   Copyright (C) 2004 by Christian Hubinger                              *
 *   e9806056@student.tuwien.ac.at                                         *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   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.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "kfdialog.h"

// QT includes
#include <qlayout.h>
#include <qlabel.h>
#include <qwidget.h>
#include <qpixmap.h>
#include <qimage.h>
#include <qpainter.h>
#include <qcolor.h>
#include <qslider.h>
#include <qlcdnumber.h>
#include <qtextedit.h>
#include <qbuttongroup.h>
#include <qradiobutton.h>

// KDE includes
#include <kdebug.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <kpushbutton.h>
#include <klocale.h>
#include <kpixmapeffect.h>
#include <kpixmap.h>
#include <qdrawutil.h>


// Project includes
#include "kfconfiguration.h"
#include "kfbutton.h"
#include "kfbuttongroup.h"

KFDialog::KFDialog(QWidget *parent, const char *name, int dialogType, const QString& text)
		: QWidget(parent, name, Qt::WStyle_Customize | QWidget::WStyle_NoBorder | Qt::WDestructiveClose |  Qt::WShowModal | Qt::WType_Dialog ) {
// 	setFixedSize(300,150);
	m_childWidgets = new QPtrList<QWidget>;
	m_dialogType = dialogType;
	m_text = text;
	setMinimumWidth( 300 );
	m_textLabel = new QTextEdit( this );
	m_childWidgets->append( m_textLabel );
	m_textLabel->setReadOnly( true );
	m_textLabel->setWordWrap( QTextEdit::WidgetWidth );
	m_textLabel->setTextFormat( Qt::RichText );
	m_textLabel->setText( text );
	m_textLabel->setFrameStyle( QFrame::NoFrame );
	m_textLabel->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred, true );
	m_textLabel->setMinimumHeight( 50 );
	QVBoxLayout *m_layout = new QVBoxLayout(this, 10,10 );

	if ( m_dialogType == YESNO ) {
		m_ok = new KFButton( this, "m_cancel"  );
		m_childWidgets->append( m_ok );
		m_ok->setButtonStyle( KFButton::TEXT );
		m_ok->setFixedHeight( 30 );
		m_ok->setText( i18n("Ok") );
// 		m_ok->setGeometry( 4, 4,fontMetrics().width( i18n("Ok") ) + 10, 30 );
		m_ok->setFixedWidth( 100 );

		connect( m_ok, SIGNAL( clicked() ), this, SLOT( slotAccept() ) );
		m_cancel = new KFButton( this, "m_cancel"  );
		m_childWidgets->append( m_cancel );
		m_cancel->setButtonStyle( KFButton::TEXT );
		m_cancel->setFixedHeight( 30 );
		m_cancel->setText( i18n("Cancel") );
// 		m_cancel->setGeometry( 4, 4,fontMetrics().width( i18n("Cancel") ) + 10, 30 );
		m_cancel->setFixedWidth( 100 );
		
		connect( m_cancel, SIGNAL( clicked() ), this, SLOT( slotReject() ) );

		m_layout->addWidget( m_textLabel );
		QHBoxLayout *vbox = new QHBoxLayout( m_layout );
		vbox->addItem( new QSpacerItem( 10, 1 ) );
		vbox->addWidget( m_ok );
		vbox->addItem( new QSpacerItem( 10, 1 ) );
		vbox->addWidget( m_cancel );
		vbox->addItem( new QSpacerItem( 10, 1 ) );
		// m_layout->addLayout( vbox );
		m_layout->addItem( new QSpacerItem( 1, 10 ) );

	} else if (m_dialogType == INFORMATION) {
		m_ok = new KFButton(  this, "m_cancel" );
		m_childWidgets->append( m_ok );
		m_ok->setButtonStyle( KFButton::TEXT );
		m_ok->setFixedHeight( 30 );
		m_ok->setText( i18n("Close Window") );
		m_ok->setFixedWidth( 150 );
		connect( m_ok, SIGNAL( clicked() ), this, SLOT( slotAccept() ) );

		m_layout->addWidget( m_textLabel );
		QHBoxLayout *vbox = new QHBoxLayout( m_layout );
		vbox->addItem( new QSpacerItem( 10, 1 ) );
		vbox->addWidget( m_ok );
		vbox->addItem( new QSpacerItem( 10, 1 ) );
		// m_layout->addLayout( vbox );
		m_layout->addItem( new QSpacerItem( 1, 10 ) );

	} else if (m_dialogType == SETLEVEL) {
		m_ok = new KFButton(  this, "m_cancel" );
		m_childWidgets->append( m_ok );
		m_ok->setButtonStyle( KFButton::TEXT );
		m_ok->setFixedHeight( 30 );
		m_ok->setText( i18n("Ok") );
		m_ok->setFixedWidth( 100 );
		connect( m_ok, SIGNAL( clicked() ), this, SLOT( slotAccept() ) );
		
		m_cancel = new KFButton( this, "m_cancel"  );
		m_childWidgets->append( m_cancel );
		m_cancel->setButtonStyle( KFButton::TEXT );
		m_cancel->setFixedHeight( 30 );
		m_cancel->setText( i18n("Cancel") );
// 		m_cancel->setGeometry( 4, 4,fontMetrics().width( i18n("Cancel") ) + 10, 30 );
		m_cancel->setFixedWidth( 100 );
		connect( m_cancel, SIGNAL( clicked() ), this, SLOT( slotReject() ) );
		
		m_bgroup = new KFButtonGroup( this, "m_bGroup" );
		
		m_easy = new QRadioButton( i18n("&Child"), m_bgroup );
		m_bgroup->insert( m_easy );
		m_childWidgets->append( m_easy );
		
		m_medium = new QRadioButton( i18n("&Beginner"), m_bgroup );
		m_bgroup->insert( m_medium );
		m_childWidgets->append( m_medium );
		
		m_hard = new QRadioButton( i18n("&Advanced"), m_bgroup );
		m_bgroup->insert( m_hard );
		m_childWidgets->append( m_hard );
		
		m_hardCore = new QRadioButton( i18n("&Expert"), m_bgroup );
		m_bgroup->insert( m_hardCore );
		m_childWidgets->append( m_hardCore );
		
		m_childWidgets->append( m_bgroup );
		
		m_layout->addWidget( m_textLabel );
		m_layout->addWidget( m_bgroup );
		
		
		
		
		
		
		QHBoxLayout *vbox = new QHBoxLayout( m_layout );
		vbox->addItem( new QSpacerItem( 10, 1 ) );
		vbox->addWidget( m_ok );
		vbox->addItem( new QSpacerItem( 10, 1 ) );
		vbox->addWidget( m_cancel );
		vbox->addItem( new QSpacerItem( 10, 1 ) );
		// m_layout->addLayout( vbox );
		m_layout->addItem( new QSpacerItem( 1, 10 ) );

	} else if ( m_dialogType == SETTRAININGSLEVEL ) {
// 		m_ok = new KPushButton( i18n("&OK"), this );
		
		m_ok = new KFButton( this, "m_newGame" );
		m_childWidgets->append( m_ok );
		m_ok->setButtonStyle( KFButton::TEXT );
		m_ok->setFixedHeight( 30 );
		m_ok->setText( i18n("Ok") );
// 		m_ok->setGeometry( 4, 4,fontMetrics().width( i18n("Game") ) + 10, 30 );
		m_ok->setFixedWidth( 100 );
		
		
		connect( m_ok, SIGNAL( clicked() ), this, SLOT( slotAccept() ) );
		m_cancel = new KFButton( this, "m_cancel"  );
		m_childWidgets->append( m_cancel );
		m_cancel->setButtonStyle( KFButton::TEXT );
		m_cancel->setFixedHeight( 30 );
		m_cancel->setText( i18n("Cancel") );
// 		m_cancel->setGeometry( 4, 4,fontMetrics().width( i18n("Cancel") ) + 10, 30 );
		m_cancel->setFixedWidth( 100 );
		
		
		connect( m_cancel, SIGNAL( clicked() ), this, SLOT( slotReject() ) );
		m_slider = new QSlider( 1 , 100, 10, 20, Qt::Horizontal, this, "slider" );
		m_childWidgets->append( m_slider );
		m_slider->setTickmarks( QSlider::Above );
		m_slider->setTickInterval( 10 );
		m_slider->setValue( Config().m_Level );
		m_slider->setSteps( 10, 10 );
		
		m_number = new QLCDNumber( 3, this, "lcd" );
		m_childWidgets->append( m_number );
		m_number->display( Config().m_Level  );

		m_layout->addWidget( m_textLabel );
		QHBoxLayout *vbox1 = new QHBoxLayout( m_layout );
		vbox1->addItem( new QSpacerItem( 10, 1 ) );
		vbox1->addWidget( m_slider );
		vbox1->addWidget( m_number );
		vbox1->addItem( new QSpacerItem( 10, 1 ) );

		// m_layout->addLayout( vbox1 );

		QHBoxLayout *vbox = new QHBoxLayout( m_layout );
		vbox->addItem( new QSpacerItem( 10, 1 ) );
		vbox->addWidget( m_ok );
		vbox->addItem( new QSpacerItem( 10, 1 ) );
		vbox->addWidget( m_cancel );
		vbox->addItem( new QSpacerItem( 10, 1 ) );
		// m_layout->addLayout( vbox );
		m_layout->addItem( new QSpacerItem( 1, 10 ) );
		connect( m_slider, SIGNAL( valueChanged( int ) ), m_number, SLOT( display( int ) ) );
	}
	adjustSize();
}


KFDialog::~KFDialog() {}

void KFDialog::setPaletteBackgroundPixmap( const QPixmap& bg ) {
	kdDebug() << "KFDialog::setPaletteBackgroundPixmap( const QPixmap& bg )" << endl;
	QPainter p( &bg );
	const QColorGroup& cg =QColorGroup( colorGroup() );
	qDrawShadeRect( &p,0,0, width(), height() , cg , false, 1,2,0 );
	
	QPtrListIterator<QWidget> it( *m_childWidgets );
	while ( it.current() ) {
		QWidget *wid = *it;
		++it;
		setChildBackground( wid, bg );
	}
	
	QWidget::setPaletteBackgroundPixmap( bg );
}
void KFDialog::setFont( const QFont & new_font ) {
	kdDebug() << "KFDialog::setFont( const QFont & new_font )" << endl;
	QWidget::setFont( new_font );
	QPtrListIterator<QWidget> it( *m_childWidgets );
	while ( it.current() ) {
		QWidget *wid = *it;
		++it;
		wid->setFont( new_font );
	}
}

void KFDialog::setPaletteForegroundColor( const QColor& new_color ) {
	kdDebug() << "KFDialog::setPaletteForegroundColor( const QColor& new_color )" << endl;
	QWidget::setPaletteForegroundColor( new_color );
	QPtrListIterator<QWidget> it( *m_childWidgets );
	while ( it.current() ) {
		QWidget *wid = *it;
		++it;
		wid->setPaletteForegroundColor( new_color );
	}
}

void KFDialog::setChildBackground( QWidget *child, QPixmap pic ) {
	QImage base ( width(), height(), 32 );
	base = pic.convertToImage();
	
	QImage tmpimage( base.copy(  child->x(), child->y(), child->width(), child->height() ) );
		
	QPixmap pix;
	pix.convertFromImage( tmpimage, AutoColor );
	child->setPaletteBackgroundPixmap( pix );
}




void KFDialog::slotAccept() {
	if ( m_dialogType == SETTRAININGSLEVEL ) {
		kdDebug() << "Saving Trainings Level" << endl;
		Config().m_Level = m_slider->value();
		Config().write();
	} else if ( m_dialogType == SETLEVEL ) {
		kdDebug() << "Saving Level" << endl;
		if ( m_easy->isChecked() ) {
			Config().m_GameLevel = 0;
		} else if ( m_medium->isChecked() ) {
			Config().m_GameLevel = 1;
		} else if ( m_hard->isChecked() ) {
			Config().m_GameLevel = 2;
		} else if ( m_hardCore->isChecked() ) {
			Config().m_GameLevel = 3;
		}  
		Config().write();
	} 

	emit sigAccepted();
	hide();
}

void KFDialog::slotReject() {
	emit sigRejected();
	hide();
}

void KFDialog::show() {
	kdDebug() << "void KFDialog::show()" << endl;
	if ( m_dialogType == SETLEVEL ) {
		switch ( Config().m_GameLevel ) {
			case 0:
			m_easy->setChecked(true);
			break;
			case 1:
			m_medium->setChecked(true);
			break;
			case 2:
			m_hard->setChecked(true);
			break;
			case 3:
			m_hardCore->setChecked(true);
			break;
		}
	}
	QWidget::show();
	
}

#include "kfdialog.moc"
