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

Program:		Range Imaging Toolkit (RITK)
Module:			ritkVisualizationUnit.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 "ritkVisualizationUnit.h"

namespace ritk
{


VisualizationUnit::VisualizationUnit(QWidget* parent, Qt::WindowFlags flags)
	: QMainWindow(parent, flags)
{
	// To accept key events
	setFocusPolicy(Qt::StrongFocus);
	this->setWindowFlags(Qt::Widget);
	this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

	m_ShowControls = false;

	// to display the VisualizationUnit in Full Screen
	m_FullscreenWindow = new FullscreenWindow(this);
	m_FullscreenWindow->setVisible(false);

	m_VisualizationWidget = new OpenGLVisualizationWidget(this);

	m_VisualizationWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	
	this->setCentralWidget(m_VisualizationWidget);
	
	m_VisualizationWidget->update();
	m_VisualizationWidget->show();

	m_FullScreen = false;
	
	
}


//----------------------------------------------------------------------------
VisualizationUnit::~VisualizationUnit()
{
	delete m_VisualizationWidget;
	delete m_FullscreenWindow;
}


//----------------------------------------------------------------------------
const OpenGLVisualizationWidget*
VisualizationUnit::GetVisualizationWidget() const
{
	return m_VisualizationWidget;
}


//----------------------------------------------------------------------------
OpenGLVisualizationWidget*
VisualizationUnit::GetVisualizationWidget()
{
	return m_VisualizationWidget;
}


//----------------------------------------------------------------------------
bool
VisualizationUnit::AddEntity(OpenGLEntity *e)
{
	return m_VisualizationWidget->AddEntity(e);
}


//----------------------------------------------------------------------------
bool
VisualizationUnit::RemoveEntity(OpenGLEntity *e)
{
	return m_VisualizationWidget->RemoveEntity(e);
}

//----------------------------------------------------------------------------
bool
VisualizationUnit::SetPrimaryEntity(OpenGLEntity *e)
{
	return m_VisualizationWidget->SetPrimaryEntity(e);
}


//----------------------------------------------------------------------------
bool
VisualizationUnit::RemovePrimaryEntity()
{
	return m_VisualizationWidget->RemovePrimaryEntity();
}

//----------------------------------------------------------------------------
void
VisualizationUnit::SetRender3D(bool enable3D)
{
	m_VisualizationWidget->SetRender3D(enable3D);
}

//----------------------------------------------------------------------------
void
VisualizationUnit::SetRenderModeFixed(bool enabled)
{
	m_VisualizationWidget->SetRenderModeFixed(enabled);
}

//----------------------------------------------------------------------------
void
VisualizationUnit::UpdateWidget()
{
	m_VisualizationWidget->Update();
}

//----------------------------------------------------------------------------
void 
VisualizationUnit::SetFullScreenMode(bool enabled){

	std::cout << "Fullscreen: " << (enabled?"True":"False") << std::endl;

	if(m_FullScreen == enabled) return;

	if(enabled){

		m_VisualizationWidget->setParent(m_FullscreenWindow);
		
		m_FullscreenWindow->setCentralWidget(m_VisualizationWidget);
	
		m_VisualizationWidget->update();
		m_VisualizationWidget->show();
		
		m_FullscreenWindow->setVisible(true);
		m_FullscreenWindow->showFullScreen();
		
	} else {
		
		m_FullscreenWindow->setVisible(false);
		
		m_VisualizationWidget->setParent(this);
		this->setCentralWidget(m_VisualizationWidget);
	
		
		m_VisualizationWidget->update();
		m_VisualizationWidget->show();

		show();
	}
		
	m_FullScreen = enabled;

	attachControlWidgets();

}

//----------------------------------------------------------------------------
void 
VisualizationUnit::keyPressEvent(QKeyEvent *e)
{
	switch(e->key()){
		
		case Qt::Key_C:
			{
				m_ShowControls = !m_ShowControls;

				attachControlWidgets();

				/*if(m_ShowControls)
				{
					this->addDockWidget(Qt::LeftDockWidgetArea, m_VisualizationWidget->GetCamera()->GetControlUnit());
					m_VisualizationWidget->GetCamera()->GetControlUnit()->show();
				}
				else
					this->removeDockWidget(m_VisualizationWidget->GetCamera()->GetControlUnit());*/
			}
			e->accept();
			break;
		
		case Qt::Key_F:
			
			SetFullScreenMode(!m_FullScreen);

			e->accept();
			break;

		case Qt::Key_Escape:

			if(m_FullScreen){
				SetFullScreenMode(false);
			}

			e->accept();
			break;

		default:
			break;

	}
}

//----------------------------------------------------------------------------
void 
VisualizationUnit::attachControlWidgets(){

	bool needRemoveNormal = m_controlWidgetsAttachedNormal;
	bool needRemoveFullscreen = m_controlWidgetsAttachedFullscreen;
	
	m_controlWidgetsAttachedFullscreen = false;
	m_controlWidgetsAttachedNormal = false;

	if ( m_ShowControls )
	{
		if(m_FullScreen){
			m_controlWidgetsAttachedFullscreen = true;
		} else {
			m_controlWidgetsAttachedNormal = true;
		}
	}


	// Get the entities from the internal visualization widget
	const OpenGLVisualizationWidget::EntityContainerType &Container = m_VisualizationWidget->GetEntities();

	// Iterate over all entities and query their control units
	for ( OpenGLVisualizationWidget::EntityContainerType::const_iterator it = Container.begin(); it != Container.end(); ++it )
	{
		// remove all controlWidgets
		if(needRemoveNormal){
			this->removeDockWidget((*it)->GetControlUnit());
		}
		if(needRemoveFullscreen){
			m_FullscreenWindow->removeDockWidget((*it)->GetControlUnit());
		}


		if(m_controlWidgetsAttachedFullscreen){
			m_FullscreenWindow->addDockWidget(Qt::LeftDockWidgetArea, (*it)->GetControlUnit());
			(*it)->GetControlUnit()->show();
		}
		if(m_controlWidgetsAttachedNormal) {
			this->addDockWidget(Qt::LeftDockWidgetArea, (*it)->GetControlUnit());
			(*it)->GetControlUnit()->show();
		}
	}



}

//----------------------------------------------------------------------------
void
VisualizationUnit::FullscreenWindow::keyPressEvent(QKeyEvent *e){
	
	((VisualizationUnit *)(this->parentWidget()))->keyPressEvent(e);
}


} // end namespace ritk
