/*
 * Copyright 2009-2010  Stefan Gehn <stefan@srcbox.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.
 * 
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include "rawvaluegroup.hpp"

#include <qapplication.h>
#include <qdebug.h>
#include <qgraphicsscene.h>

using namespace IrTouch;


RawValueGroup::RawValueGroup()
{
	mBrush = QColor(128, 128, 128, 20);
}


RawValueGroup::~RawValueGroup()
{
}


void RawValueGroup::update(const IrTouch::TouchData &data)
{
	QGraphicsRectItem *rectItem;
	QGraphicsSimpleTextItem *textItem;

	clear();

	Q_FOREACH(const RawValue &rv, data.rawXValues())
	{
		QRectF rect(rv.value(), 1, rv.extent(), scene()->height() - 4);
		rectItem = new QGraphicsRectItem(rect, this);
		rectItem->setBrush(mBrush);

		textItem = new QGraphicsSimpleTextItem(mTextGroup);
		textItem->setText(QString("raw (%2->%3)").arg(rv.value()).arg(rv.extent()));
		textItem->setFont(mFont);
		textItem->setPos(QPointF(
				qMax(0.0, rect.center().x() - (textItem->boundingRect().width()/2)),
				rect.bottom() - 128
				));
	}

	Q_FOREACH(const RawValue &rv, data.rawYValues())
	{
		QRectF rect(1, rv.value(), scene()->width() - 4, rv.extent());
		rectItem = new QGraphicsRectItem(rect, this);
		rectItem->setBrush(mBrush);

		textItem = new QGraphicsSimpleTextItem(mTextGroup);
		textItem->setText(QString("raw (%2->%3)").arg(rv.value()).arg(rv.extent()));
		textItem->setFont(mFont);
		textItem->setPos(QPointF(
				rect.right() - textItem->boundingRect().width() - 25,
				qMax(0.0, rect.center().y() - (textItem->boundingRect().height()/2))
				));
	}
}
