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

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkMainWindow.cxx
	Version:		$Revision: 2480 $
	Date:			$Date: 2012-01-25 21:10:14 +0100 (Mi, 25 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 "ritkMainWindow.h"
//#include "DebugManager.h"

#include <QInputDialog>
#include <QFileDialog>
#include <QString>
#include <QPluginLoader>
#include <QDirIterator>
#include <QMessageBox>
#include <QCloseEvent>

#include "ritkManager.h"
#include "ritkPluginFactories.h"


namespace ritk
{


MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags flags)
	: QMainWindow(parent, flags)
{
	// create gui elements defined in the Ui_MainWindow class
	setupUi(this);

	// Connect signals and slots
	connect(this->m_ActionPluginCreator,		SIGNAL(triggered()),			this,	SLOT(ShowPluginCreator())	);
	connect(this->m_ActionPluginManager,		SIGNAL(triggered()),			this,	SLOT(ShowPluginManager())	);
	connect(this->m_ActionLoadPluginConfig,		SIGNAL(triggered()),			this,	SLOT(ShowPluginConfig())	);
	connect(this->m_ActionAboutRITK,			SIGNAL(triggered()),			this,	SLOT(AboutRITK())			);
	connect(this->m_ActionHotkeys,				SIGNAL(triggered()),			this,	SLOT(ShowHotkeys())				);
	connect(this->m_FilterTabs,					SIGNAL(tabCloseRequested(int)), this,	SLOT(FilterPluginTabCloseRequested(int)));
	connect(this->m_PluginTabs,					SIGNAL(tabCloseRequested(int)), this,	SLOT(ApplicationPluginTabCloseRequested(int)));

	// Add toggle actions for the views
	this->menuView->addAction(m_PlayerControlDockWidget->toggleViewAction());
	this->menuView->addAction(m_PreProcessingDockWidget->toggleViewAction());
	this->menuView->addAction(m_LogDockWidget->toggleViewAction());
	m_LogDockWidget->hide();

	
	// Shortcut to open the PluginManager
	m_PluginManagerShortcut = new QShortcut(QKeySequence(tr("Ctrl+M", "PluginManager|Open")), this);
	connect(this->m_PluginManagerShortcut,		SIGNAL(activated()),	this,	SLOT(ShowPluginManager())	);	

	// Set up the group for the players
	m_PlayerActionGroup = new QActionGroup(this);

	// Our wunderful about dialog and hotkeys
	m_AboutDialog = 0;
	m_HotkeysDialog = 0;
}


MainWindow::~MainWindow()
{
	delete m_PlayerActionGroup;

	if ( m_AboutDialog )
		delete m_AboutDialog;
}


//----------------------------------------------------------------------------
void
MainWindow::closeEvent(QCloseEvent *event)
 {
	emit ShutDown();
	event->accept();
 }


//----------------------------------------------------------------------------
void 
MainWindow::ShowPluginManager()
{
	emit ShowPluginManagerDialog();
}


//----------------------------------------------------------------------------
void 
MainWindow::ShowPluginCreator()
{
	emit ShowPluginCreatorDialog();
}


//----------------------------------------------------------------------------
void 
MainWindow::ShowPluginConfig()
{
	emit ShowPluginConfigDialog();
}


//----------------------------------------------------------------------------
void 
MainWindow::AboutRITK()
{
	if ( !m_AboutDialog )
		m_AboutDialog = new AboutDialog(this);
	m_AboutDialog->show();
}


//----------------------------------------------------------------------------
void 
MainWindow::ShowHotkeys()
{
	if ( !m_HotkeysDialog )
		m_HotkeysDialog = new HotkeysDialog(this);
	m_HotkeysDialog->show();
}


//----------------------------------------------------------------------------
void 
MainWindow::AddPlayer(SourcePlugin *Player, QString Name)
{
	QAction *Action = this->menuPlayer_mode->addAction(Name);
	Action->setCheckable(true);
	m_PlayerActionGroup->addAction(Action);
	connect(Action, SIGNAL(triggered()), Player, SLOT(StartSlot()));
	PlayerActionStruct Struct;
	Struct.Action = Action;
	Struct.Player = Player;
	m_Players.push_back(Struct);
}


//----------------------------------------------------------------------------
void 
MainWindow::RemovePlayer(SourcePlugin *Player)
{
	typedef QList<PlayerActionStruct>::iterator IteratorType;
	for ( IteratorType it = m_Players.begin(); it != m_Players.end(); ++it )
	{
		if ( (*it).Player == Player )
		{
			this->menuPlayer_mode->removeAction((*it).Action);
			this->m_PlayerActionGroup->removeAction((*it).Action);
			m_Players.erase(it);
			return;
		}
	}		
}


//----------------------------------------------------------------------------
void 
MainWindow::FilterPluginTabCloseRequested(int TabId)
{
	emit ClosePlugin(TabId, PluginManager::Filter);
}


//----------------------------------------------------------------------------
void 
MainWindow::ApplicationPluginTabCloseRequested(int TabId)
{
	emit ClosePlugin(TabId, PluginManager::Application);
}


} // end namespace ritk
