/*****************************************************************************
 *
 *  jobmanagerdialog.cpp -- Wrap a JobManagerWidget in a window.
 * 
 *  Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *  All rights reserved.
 *
 *  This file is part of the Gui4 module of the DVDx 4 application.
 *
 *  GNU Lesser General Public License Usage
 *  This file may be used under the terms of the GNU Lesser General
 *  Public License version 2.1 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.LGPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU Lesser General Public License version 2.1 requirements
 *  will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 *
 *  GNU General Public License Usage
 *  Alternatively, this file may be used under the terms of the GNU
 *  General Public License version 3.0 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.GPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU General Public License version 3.0 requirements will be
 *  met: http://www.gnu.org/copyleft/gpl.html.
 *
 *****************************************************************************/

#include "jobmanagerdialog.h"
#include "infoboxdialog.h"
#include <QtGui>
#include <QFontMetrics>

JobManagerDialog::JobManagerDialog(const QString &title, QWidget *refConsole)
	: finished(false)
{
	this->mainConsole = refConsole;  // reference for centering

	setWindowFlags(Qt::CustomizeWindowHint);  // Turns off the default window title hints.
	setWindowFlags(Qt::WindowTitleHint);      // Gives the window a title bar.
	setWindowFlags(Qt::WindowStaysOnTopHint);

	QString str = title.isEmpty() ? tr("Job manager") : title;
	setWindowTitle( str );

	jobManagerWidget = new JobManagerWidget(this);
	jobManagerWidget->setNoConsoleButton(!showConsoleButton);
	
#ifdef WIN32
	jobManagerWidget->setMinimumWidth( 100 * jobManagerWidget->fontMetrics().averageCharWidth() );
#else
	jobManagerWidget->setMinimumWidth( 70 * jobManagerWidget->fontMetrics().averageCharWidth() );
#endif

	mainLayout = new QVBoxLayout(this);
	mainLayout->setAlignment(Qt::AlignHCenter | Qt::AlignTop);

	mainLayout->addWidget(jobManagerWidget);
	
	center();
}

void JobManagerDialog::hideEvent(QHideEvent *ev)
{
	QWidget::hideEvent(ev);

	emit hidden();
}

void JobManagerDialog::closeEvent(QCloseEvent *ev)
{
	if (finished) 
	{
		jobManagerWidget->abortProcesses();

		// Delete consoles
		jobManagerWidget->reset();
	}
	else
	{
		mainConsole->show();
	}

	ev->accept();
}

void JobManagerDialog::adjustGuiSize()
{
	adjustSize();
}

void JobManagerDialog::modalConsoleHidden()
{
	setModal(true);
	show();
}

void JobManagerDialog::center()
{
	if (mainConsole)
	{
		QDesktopWidget desktop;
		QRect dkrect = desktop.availableGeometry(desktop.screenNumber(mainConsole));

		int	x = mainConsole->pos().x() + ( mainConsole->size().width() - width() ) / 2;
		int	y = mainConsole->pos().y() + ( mainConsole->size().height() - height() ) / 2;
		
		move( x, y );
	}
}

bool JobManagerDialog::isIdle()
{
	return jobManagerWidget->isIdle();
}

void JobManagerDialog::setVerbosity(const int verbose)
{
	verbosity = verbose;

}

void JobManagerDialog::setNoConsoleButton(const bool no)
{
	showConsoleButton = !no;

	jobManagerWidget->setNoConsoleButton(no);
}
