#include "controlsview.h"
#include <qgraphicsitem.h>

//Button

OurButton::OurButton(QGraphicsItem* parent, QGraphicsScene* scene)
	: QGraphicsEllipseItem(QRectF(0,0,30,30), parent, scene)
{
	setFlag(ItemIsSelectable);
	setActivated(false);
}

void OurButton::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
	QGraphicsEllipseItem::mouseReleaseEvent(event);
	emit clicked(data(0).toInt());
}

void OurButton::setActivated(bool state)
{
	if(state)
		setBrush(Qt::red);
	else
		setBrush(Qt::blue);
}

static const int MAX=10;
//View
ControlsView::ControlsView(QWidget* parent): QGraphicsView(parent)
{
	setScene(new QGraphicsScene(this));
	int space=350/MAX;
	
	for(int i=0; i<MAX; i++) {
		OurButton* element=new OurButton;
		element->setData(0, i);
		element->setPos(20+i*space, 20);
		m_elements.append(element);
		scene()->addItem(element);
		
		connect(element, SIGNAL(clicked(int)), SLOT(changeCurrent(int)));
	}
	setCurrent(0);
}

void ControlsView::setCurrent(qreal fraction)
{
	int until=fraction*MAX;
	int i=0;
	foreach(OurButton* e, m_elements) {
		e->setActivated(i<=until);
		i++;
	}
}

void ControlsView::changeCurrent(int i)
{
	qreal value=qreal(i)/MAX;
	setCurrent(value);
	emit progressChanged(value);
}

#include "controlsview.moc"