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

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkPluginCreatorDialog.cxx
	Version:		$Revision: 2444 $
	Date:			$Date: 2012-01-23 10:02:50 +0100 (Mo, 23 Jan 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 "ritkPluginCreatorDialog.h"
#include "ritkPluginCreator.h"
#include <QMessageBox>

#include <iostream>

namespace ritk
{

PluginCreatorDialog::PluginCreatorDialog(QWidget *parent) :
    QDialog(parent)  
{
	setupUi(this);

	connect(this->m_PushButtonCreatePlugin, SIGNAL(clicked()), this, SLOT(CreatePlugin()));
}

PluginCreatorDialog::~PluginCreatorDialog()
{}

void PluginCreatorDialog::CreatePlugin()
{
	PluginCreator m_PluginCreator;

	// read out GUI form
	QString Name = this->m_LineEditPluginName->text();
	QString Description = this->m_LineEditPluginDescription->text();

    // cancel if no plugin name is given
	if(Name.isEmpty()) 
	{
		QMessageBox::warning(NULL, "PluginCreator", "No plugin name given.", QMessageBox::Ok);
	}
	else
	{
		QStringList libraries;
		QStringList finds;
		if(this->m_CheckBoxRitkCommon->isChecked() || this->m_CheckBoxRitkCore->isChecked() || this->m_CheckBoxRitkCuda->isChecked() || this->m_CheckBoxRitkTools->isChecked() || this->m_CheckBoxRitkVisualization->isChecked() || this->m_CheckBoxRitkVTK->isChecked())
		{
			finds.push_back("# RITK\nFIND_PACKAGE(RITK REQUIRED)\nIF(RITK_FOUND)\n	INCLUDE_DIRECTORIES(${RITK_INCLUDE_DIRS})\n	INCLUDE(${RITK_USE_FILE})\nELSE()\n	MESSAGE(\"RITK not found, but needed.\")\nENDIF(RITK_FOUND)\n");
			if(this->m_CheckBoxRitkCommon->isChecked())
				libraries.push_back("	debug ${RITK_COMMON_LIBRARY_DEBUG}\n	optimized ${RITK_COMMON_LIBRARY}\n");
			if(this->m_CheckBoxRitkCore->isChecked())
				libraries.push_back("	debug ${RITK_CORE_LIBRARY_DEBUG}\n	optimized ${RITK_CORE_LIBRARY}\n");
			if(this->m_CheckBoxRitkCuda->isChecked())
			{
				libraries.push_back("	debug ${RITK_CUDA_LIBRARY_DEBUG}\n	optimized ${RITK_CUDA_LIBRARY}\n");

				finds.push_back("# CUDA\nFIND_PACKAGE(CUDA)\nIF(CUDA_FOUND)\n	SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} \"--ptxas-options=-v\")\nELSE()\n	MESSAGE(FATAL_ERROR \"CUDA not found, but needed.\")\nENDIF(CUDA_FOUND)\n");
				libraries.push_back("	${CUDA_CUDA_LIBRARY}\n	${CUDA_CUDART_LIBRARY}\n");
			}
			if(this->m_CheckBoxRitkTools->isChecked())
				libraries.push_back("	debug ${RITK_TOOLS_LIBRARY_DEBUG}\n	optimized ${RITK_TOOLS_LIBRARY}\n");
			if(this->m_CheckBoxRitkVisualization->isChecked())
				libraries.push_back("	debug ${RITK_VISUALIZATION_LIBRARY_DEBUG}\n	optimized ${RITK_VISUALIZATION_LIBRARY}\n");
			if(this->m_CheckBoxRitkVTK->isChecked())
				libraries.push_back("	debug ${RITK_VTK_LIBRARY_DEBUG}\n	optimized ${RITK_VTK_LIBRARY}\n");
		}
		if(this->m_CheckBoxOpenCV->isChecked())
		{
			finds.push_back("# OPENCV\nFIND_PACKAGE(OPENCV REQUIRED)\nIF(OPENCV_FOUND)\n	INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIR})\n	MESSAGE(\"OPENCV not found, but needed.\")\nENDIF(OPENCV_FOUND)\n");
			libraries.push_back("	${OpenCV_LIBS}\n");
		}
		if(this->m_CheckBoxQWT->isChecked())
		{
			finds.push_back("# QWT\n# Uncomment for windows\nFIND_PACKAGE(QWT REQUIRED)\nIF(QWT_FOUND)\n	INCLUDE_DIRECTORIES(${QWT_INCLUDE_DIR})\n	INCLUDE(${QWT_USE_FILE})	\nELSE()\n	MESSAGE(FATAL_ERROR \"QWT not found, but needed.\")\nENDIF(QWT_FOUND)\n");
			libraries.push_back("	debug ${QWT_LIBRARY_DEBUG}\n	optimized ${QWT_LIBRARY}\n");
		}
		if(this->m_CheckBoxITK->isChecked())
		{
			finds.push_back("# ITK\nFIND_PACKAGE(ITK REQUIRED)\nIF(ITK_FOUND)\n	INCLUDE_DIRECTORIES(${ITK_INCLUDE_DIRS})\n	INCLUDE(${ITK_USE_FILE})\nELSE()\n	MESSAGE(\"ITK not found, but needed.\")\nENDIF(ITK_FOUND)\n");
			libraries.push_back("	${ITK_LIBRARIES}\n");
		}
		if(this->m_CheckBoxVTK->isChecked())
		{
			finds.push_back("# VTK\nFIND_PACKAGE(VTK REQUIRED)\nIF(VTK_FOUND)\n	INCLUDE(${VTK_USE_FILE})\n	INCLUDE_DIRECTORIES(${VTK_INCLUDE_DIRS})\nELSE()\n	MESSAGE(\"VTK not found, but needed.\")\nENDIF(VTK_FOUND)\nSET(VTK_LIBRARIES\n	vtkCommon\n	vtkWidgets\n	QVTK\n)\n");
			libraries.push_back("	${VTK_LIBRARIES}\n");
		}
		if(this->m_CheckBoxQT->isChecked())
		{
			finds.push_back("# QT4\nFIND_PACKAGE(Qt4 REQUIRED)\nIF(QT4_FOUND)\n	INCLUDE_DIRECTORIES(${QT_INCLUDE_DIRS})\n	INCLUDE(${QT_USE_FILE})\nELSE()\n	MESSAGE(\"QT4 not found, but needed.\")\nENDIF(QT4_FOUND)\n");
			libraries.push_back("	${QT_LIBRARIES}\n");
		}

		m_PluginCreator.SetLibrariesCMake(libraries);
		m_PluginCreator.SetFindCMake(finds);

		if(this->m_RadioButtonFilterPlugin->isChecked())
		{
			m_PluginCreator.SetPluginType(PluginCreator::FilterPlugin, this->m_RadioButtonOpenSource->isChecked());
			m_PluginCreator.SetPluginName(Name);
			m_PluginCreator.SetPluginDescription(Description);
			m_PluginCreator.CreatePlugin(PluginCreator::FilterPlugin);
		}
		else if(this->m_RadioButtonApplicationPlugin->isChecked())
		{
			m_PluginCreator.SetPluginType(PluginCreator::ApplicationPlugin, this->m_RadioButtonOpenSource->isChecked());
			m_PluginCreator.SetPluginName(Name);
			m_PluginCreator.SetPluginDescription(Description);
			m_PluginCreator.CreatePlugin(PluginCreator::ApplicationPlugin);
		}
		else if (this->m_RadioButtonSourcePlugin->isChecked())
		{
			m_PluginCreator.SetPluginType(PluginCreator::SourcePlugin, this->m_RadioButtonOpenSource->isChecked());
			m_PluginCreator.SetPluginName(Name);
			m_PluginCreator.SetPluginDescription(Description);
			m_PluginCreator.CreatePlugin(PluginCreator::SourcePlugin);
		}

		this->close();
	}

}


} // end namespace ritk
