/*
 * 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 "touchvaluedistance.hpp"

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

using namespace IrTouch;


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


void TouchValueDistance::update(const IrTouch::TouchData &data)
{
	QGraphicsSimpleTextItem *textItem;

	clear();

	if (data.xValues().count() > 1)
	{
		for (int i = 0; i < data.xValues().count() - 1; i++)
		{
			double left = data.xValues().at(i).end();
			double right = data.xValues().at(i+1).start();
			new QGraphicsLineItem(left + 8, 25, right - 8, 25, this);

			textItem = new QGraphicsSimpleTextItem(mTextGroup);
			textItem->setText(QString("dist %2mm") .arg((right - left) / sPpmX, 0, 'f', 1));
			textItem->setFont(mFont);
			double xPos = (left + ((right - left)/2.0)) - (textItem->boundingRect().width() / 2.0);
			textItem->setPos(xPos, 35);
		}
	}

	if (data.yValues().count() > 1)
	{
		for (int i = 0; i < data.yValues().count() - 1; i++)
		{
			double top = data.yValues().at(i).end();
			double bottom = data.yValues().at(i+1).start();
			new QGraphicsLineItem(25, top + 8, 25, bottom - 8, this);

			textItem = new QGraphicsSimpleTextItem(mTextGroup);
			textItem->setText(QString("%2mm") .arg((bottom - top) / sPpmY, 0, 'f', 1));
			textItem->setFont(mFont);
			double yPos = (top + ((bottom - top)/2.0)) - (textItem->boundingRect().height()/2.0);
			textItem->setPos(35, yPos);
		}
	}
}

