#include "RecordPluginWidget.h"

RecordPluginWidget::RecordPluginWidget(QWidget *Parent)
: QWidget(Parent)
{
	setupUi(this);
	// To accept key events
	setFocusPolicy(Qt::StrongFocus);

	// Not recording by default
	m_CurrentState = Disabled;

	m_RImage = ritk::OpenGLRImageEntity::New();
	m_VisualizationUnit->AddEntity(m_RImage);

	// Connect
	connect(this->m_RecordButton, SIGNAL(clicked()), this, SLOT(RecordButtonClicked()));
	connect(this->m_PreviewCheckBox, SIGNAL(clicked()), this, SLOT(PreviewChanged()));
	connect(this->m_radioButtonNoPostfix, SIGNAL(clicked()), this, SLOT(SetCustomFilename()));
	connect(this->m_radioButtonPostfix, SIGNAL(clicked()), this, SLOT(SetCustomFilename()));
	connect(this->m_radioButtonPrefix, SIGNAL(clicked()), this, SLOT(SetCustomFilename()));
	connect(this->m_FilenameEdit, SIGNAL(textChanged(const QString &)), this, SLOT(SetCustomFilenameEvent(const QString &)));
}


RecordPluginWidget::~RecordPluginWidget()
{
}


//----------------------------------------------------------------------------
void
RecordPluginWidget::RecordButtonClicked()
{
	switch ( m_CurrentState )
	{
	case Enabled:
		m_CurrentState = Disabled;
		m_RecordButton->setText("Start recording");
		emit StopRecording();
		break;
	case Disabled:
		m_CurrentState = Enabled;
		m_RecordButton->setText("Stop recording");
		emit StartRecording();
		break;
	}
}


//----------------------------------------------------------------------------
void 
RecordPluginWidget::SetRangeData(ritk::RImageF2::ConstPointer Data)
{
	m_Mutex.lock();

	m_CurrentFrame = Data;

	m_RImage->SetRangeData(m_CurrentFrame);

	emit UpdateWidget();

	m_Mutex.unlock();
}


//----------------------------------------------------------------------------
void
RecordPluginWidget::PreviewChanged()
{
	emit PreviewChanged(this->m_PreviewCheckBox->isChecked());
}


//----------------------------------------------------------------------------
void
RecordPluginWidget::SetFrameCounter(long Value)
{
	m_FramesRecordedCounter->display((int)Value);
}


//----------------------------------------------------------------------------
void
RecordPluginWidget::SetCustomFilename()
{
	int state = 0;

	if (this->m_radioButtonNoPostfix->isChecked())
	{
		state = 0;
	}
	else if (this->m_radioButtonPrefix->isChecked())
	{
		state = 1;
	}
	else if (this->m_radioButtonPostfix->isChecked())
	{
		state = 2;
	}

	emit CustomFilenamePrefixChanged(state);
}

void 
RecordPluginWidget::SetCustomFilenameEvent(const QString filename)
{
	emit CustomFilenameChanged(filename);
}


//----------------------------------------------------------------------------
void 
RecordPluginWidget::keyPressEvent(QKeyEvent *e)
{
	if ( e->key() == Qt::Key_F2 )
	{
		RecordButtonClicked();
		e->accept();
	}
}