/*****************************************************************************
 *
 *  userdefinedrezdialog.cpp -- Input user defined picture rezolution.
 *
 *  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 "userdefinedrezdialog.h"
#include "infoboxdialog.h"
#include <QtGui>

UserDefinedRezDialog::UserDefinedRezDialog(QSize *size, 
		const QSize &sizeMin, const QSize &sizeMax,
		QWidget *parent)
	: QDialog(parent)
{
	setWindowTitle( tr("Set user defined size") );

	QVBoxLayout *mainLayout = new QVBoxLayout(this);
	QGroupBox *groupBox = new QGroupBox(this);
	QHBoxLayout *horizontalLayout = new QHBoxLayout(groupBox);
	QLabel *widthLabel = new QLabel(groupBox);
	QLabel *heightLabel = new QLabel(groupBox);

	widthInput = new QLineEdit(groupBox);
	heightInput = new QLineEdit(groupBox);

	horizontalLayout->addWidget(widthLabel);
	horizontalLayout->addWidget(widthInput);
	horizontalLayout->addWidget(heightLabel);
	horizontalLayout->addWidget(heightInput);

	widthInput->setMaxLength(4);
	widthLabel->setText( tr("Width:") );

	heightInput->setMaxLength(4);
	heightLabel->setText( tr("x  Height:") );

	buttonBox = new QDialogButtonBox(this);
	buttonBox->setOrientation(Qt::Horizontal);
	buttonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
	buttonBox->setCenterButtons(true);

	mainLayout->addWidget(groupBox);
	mainLayout->addWidget(buttonBox);

	setLayout(mainLayout);

	connect(buttonBox, SIGNAL(rejected()), this, SLOT(close()));
	connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));

	setAttribute(Qt::WA_AlwaysShowToolTips);

	int widthMin = sizeMin.width();
	int widthMax = sizeMax.width();

	int heightMin = sizeMin.height();
	int heightMax = sizeMax.height();

	groupBox->setTitle( tr("Enter output video resolution") );
	groupBox->setToolTip(tr("Minimum %1x%2\n"
		"Maximum %3x%4")
		.arg(widthMin).arg(heightMin)
		.arg(widthMax).arg(heightMax));

	widthValidator = new QIntValidator( widthMin, widthMax, this );
	heightValidator = new QIntValidator( heightMin, heightMax, this );

	widthInput->setText(QString::number(size->width()));
	widthInput->setValidator( widthValidator );
	widthInput->setAlignment(Qt::AlignHCenter);
	widthInput->setMinimumWidth(60);
	widthInput->setMaximumWidth(100);

	heightInput->setText(QString::number(size->height()));
	heightInput->setValidator( heightValidator );
	heightInput->setAlignment(Qt::AlignHCenter);
	heightInput->setMinimumWidth(60);
	heightInput->setMaximumWidth(100);

	userDefinedSize = size;  // to store the user input before closing
}

void UserDefinedRezDialog::accept()
{
	QSize size;
	int pos = 0;
	QString str;
	bool accept = true;

	// Input can be less than minimum so re-validate the mininum
	str = widthInput->text();
	if ( widthValidator->validate( str, pos ) == QValidator::Acceptable )
	{
		size.setWidth( widthInput->text().toInt() );
	}
	else
	{
		widthInput->setText(QString::number( widthValidator->bottom() ));
		accept = false;
	}

	str = heightInput->text();
	if ( heightValidator->validate( str, pos ) == QValidator::Acceptable )
	{
		size.setHeight( heightInput->text().toInt() );
	}
	else
	{
		heightInput->setText(QString::number( heightValidator->bottom() ));
		accept = false;
	}

	if ( !accept )
	{
		InfoBoxDialog infoBox(this);
		infoBox.setText( tr( "Minimum acceptable rezolution is %1x%2." )
			.arg( widthValidator->bottom() ).arg( heightValidator->bottom() ) );
		infoBox.setInformativeText(tr("Please enter acceptable Width x Height values."));
		infoBox.exec();

		// so don't accept
		return;
	}

	// we're done, X and Y both validated
	*userDefinedSize = size;

	QDialog::accept();
}
