/*
 * 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 "touchvaluegroup.hpp"
#include <qapplication.h>
#include <qdebug.h>
#include <qgraphicsscene.h>

using namespace IrTouch;


TouchValueGroup::TouchValueGroup()
{
	mBrush = QColor(0, 0, 255, 20);
}


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

	clear();

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

		textItem = new QGraphicsSimpleTextItem(mTextGroup);
		textItem->setText(QString("value %1 (%2mm wide)").arg(val.id()).arg(val.extent()/sPpmX, 0, 'f', 1));
		textItem->setFont(mFont);
		textItem->setPos(QPointF(
				qMax(0.0, rect.center().x() - (textItem->boundingRect().width()/2)),
				rect.top() + 96
				));
	}

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

		textItem = new QGraphicsSimpleTextItem(mTextGroup);
		textItem->setText(QString("value %1 (%2mm wide)").arg(val.id()).arg(val.extent()/sPpmY, 0, 'f', 1));
		textItem->setFont(mFont);
		textItem->setPos(QPointF(
				rect.left() + 96,
				qMax(0.0, rect.center().y() - (textItem->boundingRect().height()/2))
				));
	}
}
