/***************************************************************************
                         kfbutton.cpp  -  description
                            -------------------
   begin                : Tue Dec 11 2001
   copyright            : (C) 2001 by Christian Hubinger
   email                : 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.                                   *
 *                                                                         *
 ***************************************************************************/
#include "kfbutton.h"

// QT includes
#include <qpixmap.h>
#include <qbitmap.h>
#include <qpainter.h>
#include <qimage.h>
#include <qfont.h>
#include <qdrawutil.h>

// KDE includes
#include <kdebug.h>
#include <kglobal.h>
#include <kstandarddirs.h>

KFButton::KFButton( QWidget *parent, const char *name ) : QWidget( parent, name ) {
	m_text = "No Text Set";
	ang = 0;
	state = 0;
	buttonStyle = -1;
	KStandardDirs stddir;

	left_normal = stddir.findResource("data", "kfreerings/required_pics/kfbutton_left.png");
	left_over = stddir.findResource("data", "kfreerings/required_pics/kfbutton_left_over.png");
	left_active = stddir.findResource("data", "kfreerings/required_pics/kfbutton_left_active.png");
	left_mask = stddir.findResource("data", "kfreerings/required_pics/kfbutton_left_mask.png");

	right_normal = stddir.findResource("data", "kfreerings/required_pics/kfbutton_right.png");
	right_over = stddir.findResource("data", "kfreerings/required_pics/kfbutton_right_over.png");
	right_active = stddir.findResource("data", "kfreerings/required_pics/kfbutton_right_active.png");
	right_mask = stddir.findResource("data", "kfreerings/required_pics/kfbutton_right_mask.png");
	repaint();
}

KFButton::~KFButton() {}

void KFButton::setText( const QString& txt ) {
	m_text = txt;

}

void KFButton::setAngel( int a ) {
	ang = a;
	state = 0;
	setMouseTracking(true);
}

void KFButton::setBackground( QPixmap *pic, int x, int y ) {
	QImage base ( pic->convertToImage() /*800, 600, 32*/ );
	QImage m( base.copy( x, y, width(), height() ) );
	QPixmap pix;
	pix.convertFromImage( m, AutoColor );
	setPaletteBackgroundPixmap( pix );
	setBackgroundPixmap( pix );
// 	kdDebug() << "X: " << x <<  " Y: " << y <<  endl;
}

void KFButton::setButtonStyle( int butStyle ) {
	buttonStyle = butStyle;
}

QPixmap KFButton::paintButton( int mode ) {
	QPainter p;
	QString file = "";
	if ( buttonStyle == LEFT ) {
		switch ( mode ) {
			case 0:
			file = left_normal;
			break;
			case 1:
			file = left_over;
			break;
			case 2:
			file = left_active;
			break;
		}
	} else if ( buttonStyle == RIGHT ) {
		switch ( mode ) {
			case 0:
			file = right_normal;
			break;
			case 1:
			file = right_over;
			break;
			case 2:
			file = right_active;
			break;
		}
	}
	QPixmap butt( file );
	QWMatrix rot;
	rot.translate( -butt.width() / 2, -butt.height() / 2 );
	rot.rotate( ang );
	QPixmap out = butt.xForm( rot );
	return out;
}

void KFButton::mouseReleaseEvent( QMouseEvent *ev ) {
	if ( ev->button() == LeftButton ) {
		emit clicked();
		state = 0;
		repaint();
	}
}

void KFButton::mousePressEvent( QMouseEvent * ) {
	emit pressed();
	if ( state != 2 ) {
		state = 2;
		repaint();
	}
}

void KFButton::enterEvent( QEvent * ) {
	emit over();
	if ( state != 1 ) {
		state = 1;
		repaint();
	}
}

void KFButton::leaveEvent( QEvent * ) {
	emit out();
	if ( state != 0 ) {
		state = 0;
		repaint();
	}
}

void KFButton::repaintButtons() {
	repaint();
}

void KFButton::paintEvent ( QPaintEvent *q ) {
	QPainter p( this );
	if ( buttonStyle == TEXT ) {
		QFont f( "Helvetica", 10, QFont::Bold );
		p.setFont( f );
		p.setBrush( QBrush::NoBrush );
		const QColorGroup& cg =QColorGroup( colorGroup() );
// 		const QColor& col = QColor( "darkGray" );
		switch ( state ) {
			case 0:
// 			qDrawPlainRect( &p,0,0,width(), height(), col ,  1, 0 );
			p.setPen( QColor( "darkGray") );
			break;
			case 1:
			qDrawShadePanel( &p,0,0,width(), height(), cg , false, 1,0 );
			p.setPen( QColor( "lightGray") );

			break;
			case 2:
			qDrawShadePanel( &p,0,0,width(), height(), cg , true, 1,0 );
			p.setPen( QColor( "white") );
			break;
		}
		int fWidth = fontMetrics().width( m_text );
		int fHeight = fontMetrics().height();
		
		
		
		p.drawText( QRect( ( width() - fWidth ) / 2 , ( height() - fHeight ) / 2 ,fWidth, fHeight ), AlignCenter, m_text );
	} else {
		p.drawPixmap( 0, 0, paintButton( state ) );
	}
	QWidget::paintEvent( q );
}





