#include "selectionWidget.h"
#include <cassert>
#include <iostream>
#include <qpainter.h>
#include <qstyle.h>
#include <qrect.h>
#include <qpalette.h>
#include <algorithm>


void MyProgressBar::paintEvent(QPaintEvent *e)
{
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing, true);

	painter.fillRect( 0, 0, (this->width()-2) * this->percentageProgress / 100.0, this->height() , this->palette().highlightedText() );

	QLabel::paintEvent( e );
}

void MyProgressBar::progressSlot( unsigned int percentageProgress )
{
	this->percentageProgress = percentageProgress;
	this->repaint();
}

//######################################################################

SelectionWidget::SelectionWidget(  QWidget *parent ): QWidget( parent )
{
	
}

void SelectionWidget::init( unsigned int maxButtons, unsigned int buttonSizeX, unsigned int buttonSizeY, unsigned int space )
{
	this->clear();
	this->itemArray.resize( maxButtons );

	for ( int i=0; i< maxButtons; ++i)
	{
		this->itemArray[i] = new MyPushButton( i, this ); // ( id, parent )
		this->itemArray[i]->setEnabled( true );
		this->itemArray[i]->setGeometry(  QRect( (buttonSizeX+space) * i, 0, buttonSizeX, buttonSizeY ));

		bool connectionOk =  QObject::connect( this->itemArray[i], SIGNAL( clickedSignal(unsigned int) ), this, SLOT( buttonSelectedSlot(unsigned int)) );
		assert( connectionOk && "Signal & slot connection failed!" );
	}

}

void SelectionWidget::buttonSelectedSlot( unsigned int id )
{
	if ( id < this->itemArray.size() )
	{
		emit selectedItem( id );
	}
}

void SelectionWidget::clear( void )
{
	for ( int i=0; i< this->itemArray.size(); ++i)
	{
		delete this->itemArray[i];
	}
}

SelectionWidget::~SelectionWidget()
{
	this->clear();
}

MyPushButton& SelectionWidget::getItem(unsigned int id )
{
	assert( id < this->itemArray.size() && "Out of range" );

	return *this->itemArray[ id ];
}

void MyPushButton::paintEvent(QPaintEvent *e)
{
	QPushButton::paintEvent( e );

	QPainter paint(this);
            
	paint.drawText(QPoint( 5, this->height() * 0.95 ), this->myText );
 
}

unsigned int SelectionWidget::getArraySize(void)
{
	return this->itemArray.size();
}

