/*======================================================================================

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkOpenGLPointPickerLinearEntity.cxx
	Version:		$Revision: 2786 $
	Date:			$Date: 2012-03-13 17:51:50 +0100 (Di, 13 Mrz 2012) $

	Copyright (c) 2010-2011
	Pattern Recognition Lab, 
	University Erlangen-Nuremberg, Germany
	All rights reserved.

	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 RITKCopyright.txt for more information.

	http://www5.informatik.uni-erlangen.de/research/software/ritk

========================================================================================*/

#include "ritkOpenGLPointPickerLinearEntity.h"



namespace ritk
{

//----------------------------------------------------------------------------
/// Constructor
OpenGLPointPickerLinearEntity::OpenGLPointPickerLinearEntity(){
	m_ControlUnit = new OpenGLPointPickerLinearControlUnit();

	connect(this,	SIGNAL(Point1Changed(QVector3D)),	this->m_ControlUnit,	SLOT(UpdatePoint1(QVector3D)));
	connect(this,	SIGNAL(Point2Changed(QVector3D)),	this->m_ControlUnit,	SLOT(UpdatePoint2(QVector3D)));
	connect(this,	SIGNAL(DistanceChanged(float)),		this->m_ControlUnit,	SLOT(UpdateDistance(float)));
	connect(this->m_ControlUnit, SIGNAL(SizeChanged(float)),	this,	SLOT(SetSphereSize(float)));

}

//----------------------------------------------------------------------------
/// Destructor
OpenGLPointPickerLinearEntity::~OpenGLPointPickerLinearEntity(){
	if(m_ControlUnit){
		delete m_ControlUnit;
		m_ControlUnit = NULL;
	}
}

//----------------------------------------------------------------------------
bool
OpenGLPointPickerLinearEntity::Event_NewPoint(PickedPoint p){
	m_PointList.prepend(p);
	while(m_PointList.size() > 2)
		m_PointList.removeLast();

	updateLineList();

	emitSignals();

	return true;
}


//----------------------------------------------------------------------------
void
OpenGLPointPickerLinearEntity::emitSignals(){

	if(m_PointList.size() > 0){
		emit Point1Changed(m_PointList[0].coord3D);
	} else {
		emit Point1Changed(QVector3D(0,0,0));
	}
	if(m_PointList.size() > 1){
		emit Point2Changed(m_PointList[1].coord3D);
		emit DistanceChanged(GetDistance(0,1));
	} else {
		emit Point2Changed(QVector3D(0,0,0));
		emit DistanceChanged(0);
	}


}

//----------------------------------------------------------------------------
bool
OpenGLPointPickerLinearEntity::Event_PointReleased(int index){

	return false;
}

//----------------------------------------------------------------------------
bool
OpenGLPointPickerLinearEntity::Event_DeleteLine(int index){
	return false;
}

//----------------------------------------------------------------------------
bool
OpenGLPointPickerLinearEntity::Event_DeletePoint(int index){
	m_PointList.removeAt(index);
	updateLineList();
	emitSignals();
	return true;
}


//----------------------------------------------------------------------------
bool
OpenGLPointPickerLinearEntity::Event_PointClicked(int index){
	
	return false;
}

//----------------------------------------------------------------------------
bool
OpenGLPointPickerLinearEntity::Event_LineClicked(int index){
	
	return false;
}

//----------------------------------------------------------------------------
bool
OpenGLPointPickerLinearEntity::Event_PointDragged(int index, PickedPoint newPos){
	m_PointList[index] = newPos;

	emitSignals();
	return true;
}

//----------------------------------------------------------------------------
bool
OpenGLPointPickerLinearEntity::Event_ResetAll(){
	m_PointList.clear();
	updateLineList();
	emitSignals();
	return true;
}

//----------------------------------------------------------------------------
void
OpenGLPointPickerLinearEntity::updateLineList(){
	m_LineList.clear();
	if(m_PointList.length() > 1)
		m_LineList.append(PickedLine(&m_PointList[0], &m_PointList[1]));

}



} //namespace ritk