/*****************************************************************************
 *
 *  progresswidget.cpp -- ProgressWidget class extends ProgressWidget
 *
 *  A widget to show processing progress.
 *
 *  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 "mainwindow.h"
extern MainWindow *mainWin;

#include "progresswidget.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QProgressBar>
#include <QString>
#include <QDesktopWidget>
#include <QDebug>

ProgressWidget::ProgressWidget(QString title, QString info, 
							   const int start, const int end, 
							   QWidget *refConsole)
	:QWidget(0)
{
	mainConsole = refConsole;  // reference for centering

	setWindowTitle(title);

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

	QLabel *infoText = new QLabel(info);

	pBar = new QProgressBar();
	pBar->setRange(start, end);
	pBar->setValue(start);

	QVBoxLayout *layout = new QVBoxLayout;
	layout->addWidget(infoText);
	layout->addWidget(pBar);
	setLayout(layout);

	center();
}

void ProgressWidget::reset(const int start, const int end, const int value)
{
	pBar->setRange(start, end);
	pBar->setValue(value);
}

void ProgressWidget::setRemainingStep(const int estimated)
{
	//qDebug() << "ProgressWidget::setRemainingStep value:" << pBar->value() << "estimated remaining:" << estimated;

	pBar->setMaximum( pBar->value() + estimated );
}

void ProgressWidget::stepUpdate()
{
	//qDebug() << "ProgressWidget::setRemainingStep value:" << pBar->value() << "estimated total:" << pBar->maximum();

	pBar->setValue( pBar->value() + 1 );
}

int ProgressWidget::value()
{
	return pBar->value();
}

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

		// Arbitrary widget size (Qt size is 1163x733, pBar size is 640x480)
		int w(240), h(120);

		qDebug() << "Progress Widget: console& =" << mainConsole; 
		qDebug() << "Progress Widget: console size =" << mainConsole->size() \
			<< "console position =" << mainConsole->pos();
		qDebug() << "Console screen number :" << desktop.screenNumber(mainConsole);
		qDebug() << "Console screen available geometry:" << dkrect;
		qDebug() << "Progress Widget: progress bar& =" << pBar; 
		qDebug() << "Progress Widget: progress bar size =" << pBar->size();
		qDebug() << "Progress Widget: widget& =" << this; 
		qDebug() << "Progress Widget: widget size =" << this->size();
		qDebug() << "Progress Widget: widget arbitrary size =" << w << "x" << h;

		int	x = mainConsole->pos().x() + ( mainConsole->size().width() - w ) / 2;
		int	y = mainConsole->pos().y() + ( mainConsole->size().height() - h ) / 2;
		qDebug() << "x:" << x;
		qDebug() << "y:" << y;
		
		move( x, y );
	}
}

void ProgressWidget::closeEvent(QCloseEvent *ev)
{
	emit closedByUser();

	ev->accept();
}
