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

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkSourcePlugin.cxx
	Version:		$Revision: 1761 $
	Date:			$Date: 2011-09-23 17:25:41 +0200 (Fr, 23 Sep 2011) $

	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 "ritkSourcePlugin.h"
#include "ritkDebugManager.h"

namespace ritk
{

SourcePlugin::SourcePlugin()
{
	// No acquisition pipeline and no source
	m_AcquisitionPipeline = NULL;
	m_Source = NULL;
}


SourcePlugin::~SourcePlugin()
{
}


//----------------------------------------------------------------------------
bool
SourcePlugin::StartPlugin(void *Pipeline)
{
	// Sanity checks
	if ( !Pipeline )
	{
		LOG_DEB("Pipeline pointer is NULL");
		return false;
	}

	AcquisitionPipeline* Pipe = dynamic_cast<AcquisitionPipeline*>((AcquisitionPipeline*)Pipeline);
	if ( !Pipe )
	{
		LOG_DEB("Pointer is not a pipeline!");
		return false;
	}
	
	if ( !GetSource() )
	{
		LOG_DEB("Invalid source object!");
		return false;
	}

	// Call superclass method (this should only set the flag)
	if ( !PluginBase::StartPlugin() )
		return false;

	// Update member
	m_AcquisitionPipeline = Pipe;

	// Set the source
	m_Source = GetSource();
	m_AcquisitionPipeline->SetSource(m_Source);

	// Connect the new frame signal
	connect(m_AcquisitionPipeline, SIGNAL(NewFrame()), this, SLOT(NewFrame()));

	// Call superclass method (this should only set the flag)
	return PluginBase::StartPlugin();
}


//----------------------------------------------------------------------------
bool
SourcePlugin::StopPlugin()
{
	// Sanity check
	if ( !Started() )
	{
		LOG_DEB("StopPlugin requested but plugin is not started!");
		return true;
	}

	// Lock the pipeline
	m_AcquisitionPipeline->Lock();

	// Disconnect the new frame signal
	disconnect(m_AcquisitionPipeline, SIGNAL(NewFrame()), this, SLOT(NewFrame()));

	// Unlock the pipeline
	m_AcquisitionPipeline->Unlock();

	// Update the pipeline
	m_AcquisitionPipeline->SetSource(NULL);

	// Update member
	m_AcquisitionPipeline = NULL;

	// Call superclass method (this should only set the flag)
	return PluginBase::StopPlugin();
}


//----------------------------------------------------------------------------
void 
SourcePlugin::GetRuntime(double *Runtime, double *RuntimeStdDeviation)
{
	// Sanity check
	if ( !m_Source )
	{
		*Runtime = 0;
		if ( RuntimeStdDeviation )
			*RuntimeStdDeviation = 0;
		return;
	}

	// Runtime computation is done by the wrapped filter
	m_Source->GetElapsedTime(Runtime,RuntimeStdDeviation);
}


//----------------------------------------------------------------------------
QString
SourcePlugin::GetNameForSelectionMenu()
{
	return tr("");
}


//----------------------------------------------------------------------------
QIcon
SourcePlugin::GetIconForSelectionMenu()
{
	return QIcon();
}


//----------------------------------------------------------------------------
void
SourcePlugin::NewFrame()
{
}


//----------------------------------------------------------------------------
void
SourcePlugin::StartSlot()
{
	// Only emit the signal if we're not started
	if ( !Started() )
		emit StartSignal(this);
}


} // end namespace ritk


