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

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkRImageFilePlayerWidget.cxx
	Version:		$Revision: 2734 $
	Date:			$Date: 2012-03-07 13:17:13 +0100 (Mi, 07 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 "ritkRImageFilePlayerWidget.h"
#include "ritkDebugManager.h"
#include <QInputDialog>
#include <QFileDialog>
#include <QThread>

namespace ritk
{


RImageFilePlayerWidget::RImageFilePlayerWidget(QWidget *Parent)
: QWidget(Parent)
{
	setupUi(this);

	// Set state and update GUI
	SetState(ClosedState);
	UpdateGUI();

	// Connect the signals and slots
	connect(this->m_PushButtonOpenFile,					SIGNAL(clicked()),						this,	SLOT(SelectFile()));
	connect(this->m_PushButtonPause,					SIGNAL(clicked()),						this,	SIGNAL(PauseEvent()));
	connect(this->m_PushButtonPlayBackward,				SIGNAL(clicked()),						this,	SIGNAL(PlayBackwardEvent()));
	connect(this->m_PushButtonPlayForward,				SIGNAL(clicked()),						this,	SIGNAL(PlayForwardEvent()));
	connect(this->m_PushButtonStepBackward,				SIGNAL(clicked()),						this,	SIGNAL(StepBackwardEvent()));
	connect(this->m_PushButtonStepForward,				SIGNAL(clicked()),						this,	SIGNAL(StepForwardEvent()));
	connect(this->m_PushButtonStop,						SIGNAL(clicked()),						this,	SIGNAL(StopEvent()));
	//connect(this->m_PushButtonGoToFrame,				SIGNAL(clicked()),						this,	SLOT(EnterFrameIndex()));
	connect(this->m_CheckBoxLoop,						SIGNAL(stateChanged(int)),				this,	SLOT(LoopEvent()));
	connect(this->m_CheckBoxTimestampDelivery,			SIGNAL(stateChanged(int)),				this,	SLOT(TimestampDeliveryEvent()));
	connect(this->m_FrameSlider,						SIGNAL(sliderMoved(int)),				this,	SLOT(SliderValueChanged(int)));
	connect(this->m_SliderFrameInterval,				SIGNAL(sliderMoved(int)),				this,	SLOT(SliderFrameIntervalChanged(int)));
	connect(this->m_LineEditInterval,					SIGNAL(textChanged(const QString&)),	this,	SLOT(LineEditFrameIntervalChanged(const QString &)));

	m_CurrentFileName = tr("");
	m_NumberOfFrames = 0;
	m_CurrentFrameID = 0;
}

RImageFilePlayerWidget::~RImageFilePlayerWidget()
{
}


//----------------------------------------------------------------------------
void 
RImageFilePlayerWidget::SetFileName(QString FileName)
{
	QMutexLocker locker(&m_StateMutex);
	m_CurrentFileName = FileName;
}


//----------------------------------------------------------------------------
void 
RImageFilePlayerWidget::SetNumberOfFrames(unsigned long NumFrames)
{
	QMutexLocker locker(&m_StateMutex);
	m_NumberOfFrames = NumFrames;
}


//----------------------------------------------------------------------------
void 
RImageFilePlayerWidget::SetFrameID(unsigned long ID)
{
	QMutexLocker locker(&m_StateMutex);
	m_CurrentFrameID = ID;
}


//----------------------------------------------------------------------------
void 
RImageFilePlayerWidget::SetImageResolution(unsigned int SX, unsigned int SY)
{
	m_ImageResolution[0] = SX;
	m_ImageResolution[1] = SY;
}


//----------------------------------------------------------------------------
void 
RImageFilePlayerWidget::UpdateGUI()
{
	switch ( GetState() )
	{
	case ClosedState:
		m_PushButtonStop->setEnabled(false);
		m_PushButtonPause->setEnabled(false);
		m_PushButtonStepForward->setEnabled(false);
		m_PushButtonStepBackward->setEnabled(false);
		m_PushButtonPlayForward->setEnabled(false);
		m_PushButtonPlayBackward->setEnabled(false);
		m_CheckBoxLoop->setEnabled(false);
		m_CheckBoxTimestampDelivery->setEnabled(false);
		m_FrameSlider->setEnabled(false);
		m_LabelFileName->setText(tr("N/A"));
		m_LabelFrames->setText(tr("N/A"));
		m_LabelResolution->setText(tr("N/A"));
		break;

	case OpenedState:
		m_PushButtonStop->setEnabled(false);
		m_PushButtonPause->setEnabled(false);
		m_PushButtonStepForward->setEnabled(true);
		m_PushButtonStepBackward->setEnabled(true);
		m_PushButtonPlayForward->setEnabled(true);
		m_PushButtonPlayBackward->setEnabled(true);
		m_CheckBoxLoop->setEnabled(true);
		m_CheckBoxTimestampDelivery->setEnabled(true);
		m_FrameSlider->setEnabled(true);
		m_FrameSlider->setMinimum(0);
		m_FrameSlider->setMaximum(m_NumberOfFrames-1);
		m_FrameSlider->setValue(m_CurrentFrameID);
		m_LabelFileName->setText(m_CurrentFileName);
		m_LabelFileName->setToolTip(m_CurrentFileName);
		m_LabelFrames->setText(QString::number(m_NumberOfFrames));
		m_LabelResolution->setText(QString::number(m_ImageResolution[0]) + tr(" x ") + QString::number(m_ImageResolution[1]));
		m_LCDFrameNumber->display((int)m_CurrentFrameID);
		break;

	case PlayForwardState:
		m_PushButtonStop->setEnabled(true);
		m_PushButtonPause->setEnabled(true);
		m_PushButtonStepForward->setEnabled(false);
		m_PushButtonStepBackward->setEnabled(false);
		m_PushButtonPlayForward->setEnabled(false);
		m_PushButtonPlayBackward->setEnabled(false);
		m_CheckBoxLoop->setEnabled(true);
		m_CheckBoxTimestampDelivery->setEnabled(true);
		m_FrameSlider->setValue(m_CurrentFrameID);
		m_LCDFrameNumber->display((int)m_CurrentFrameID);
		break;

	case PlayBackwardState:
		m_PushButtonStop->setEnabled(true);
		m_PushButtonPause->setEnabled(true);
		m_PushButtonStepForward->setEnabled(false);
		m_PushButtonStepBackward->setEnabled(false);
		m_PushButtonPlayForward->setEnabled(false);
		m_PushButtonPlayBackward->setEnabled(false);
		m_CheckBoxLoop->setEnabled(true);
		m_CheckBoxTimestampDelivery->setEnabled(true);
		m_FrameSlider->setValue(m_CurrentFrameID);
		m_LCDFrameNumber->display((int)m_CurrentFrameID);
		break;
	}
	//this->update();
}


//----------------------------------------------------------------------------
void 
RImageFilePlayerWidget::SetState(RImageFilePlayerWidget::FileInterfaceStateType State)
{
	QMutexLocker locker(&m_StateMutex);
	m_CurrentState = State;
}


//----------------------------------------------------------------------------
RImageFilePlayerWidget::FileInterfaceStateType
RImageFilePlayerWidget::GetState()
{
	QMutexLocker locker(&m_StateMutex);
	return m_CurrentState;
}


//----------------------------------------------------------------------------
void
RImageFilePlayerWidget::SelectFile()
{
	QString FileName = QFileDialog::getOpenFileName(this, "Open RI File", "C:/Projects/RITK/Data", "RIM files (*.ris *.ri)");		
	//QString FileName = QFileDialog::getOpenFileName(this, "Open RIS File", "F:/Work/RITK/Data", "RIS files (*.ris)");		
	if ( !FileName.isEmpty() ) 
	{
		m_CurrentFileName = FileName;
		emit OpenFileEvent(m_CurrentFileName);
	}
}



//----------------------------------------------------------------------------
void
RImageFilePlayerWidget::EnterFrameIndex()
{
	QString Text = tr("Enter frame number (0-") + QString::number(m_NumberOfFrames-1) + tr(")");
	bool OK;
	long Index = QInputDialog::getInteger(this, "GO TO", Text, 0, 0,m_NumberOfFrames,1,&OK);
	if ( OK )
	{
		LOG_DEB("Goto frame #" << Index << " requested");
		emit GoToFrameEvent(Index);
	}
}


//----------------------------------------------------------------------------
void
RImageFilePlayerWidget::LoopEvent()
{
	if ( m_CheckBoxLoop->checkState() == Qt::Checked )
		ToggleLoopEvent(true);
	else
		ToggleLoopEvent(false);
}


//----------------------------------------------------------------------------
void
RImageFilePlayerWidget::TimestampDeliveryEvent()
{
	if ( m_CheckBoxTimestampDelivery->checkState() == Qt::Checked )
		ToggleTimestampDeliveryEvent(true);
	else
		ToggleTimestampDeliveryEvent(false);
}


//----------------------------------------------------------------------------
void
RImageFilePlayerWidget::SliderValueChanged(int value)
{
	emit GoToFrameEvent((unsigned long)value);
}

//----------------------------------------------------------------------------
void
RImageFilePlayerWidget::SliderFrameIntervalChanged(int value)
{
	this->m_LineEditInterval->setText(QString::number(value));
	emit FrameIntervalEvent((unsigned long)value);
}


//----------------------------------------------------------------------------
void
RImageFilePlayerWidget::LineEditFrameIntervalChanged(const QString &Text)
{
	// Convert
	bool ok;
	int value = Text.toInt(&ok);
	if ( !ok )
		return;

	emit FrameIntervalEvent((unsigned long)value);
	m_SliderFrameInterval->setSliderPosition(value);
}

} // end namespace ritk
