#include "CUDAMedianImageFilterPlugin.h"
#include "ritkManager.h"
#include "ritkCudaContext.h"

CUDAMedianImageFilterPlugin::CUDAMedianImageFilterPlugin()
{
	// We need to initialize the CUDA context
	ritk::CudaContext::GetInstance();

	// Initialize filter
	m_Filter = CUDAMedianFilterType::New();

	// Create GUI widget
	m_Widget = new CUDAMedianFilterWidget();
	connect(m_Widget, SIGNAL(RangeDataCheckBoxChanged(bool)), this, SLOT(SetRangeFilterMode(bool)));
	connect(m_Widget, SIGNAL(RadiusChanged(unsigned int)), this, SLOT(SetRadius(unsigned int)));

	// Range filtering by default
	this->SetFilterMode(ritk::RImageFilterWrapperF2::RANGE);
}


CUDAMedianImageFilterPlugin::~CUDAMedianImageFilterPlugin()
{
	delete m_Widget;
}


//----------------------------------------------------------------------------
QWidget*
CUDAMedianImageFilterPlugin::GetPluginGUI()
{
	return m_Widget;
}


//----------------------------------------------------------------------------
CUDAMedianImageFilterPlugin::FilterPlugin::FilterPointer 
CUDAMedianImageFilterPlugin::GetFilter()
{	
	return (FilterPlugin::FilterType*)m_Filter;
}


//----------------------------------------------------------------------------
void
CUDAMedianImageFilterPlugin::SetRadius(unsigned int Radius)
{
	if ( Radius == m_Radius )
		return;

	LockPlugin();


	// Update member
	m_Radius = Radius;

	// Lock the pipeline (we're altering an element of it...)
	LockPipeline();

	// Update the filter
	m_Filter->SetKernelRadius(Radius);

	// Unlock the pipeline
	UnlockPipeline();

	UnlockPlugin();
}
