/*****************************************************************************
 *
 *  userdefinedfpsdialog.cpp -- Input user defined frame rate.
 *
 *  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 "userdefinedfpsdialog.h"
#include "infoboxdialog.h"
#include <QtGui>

UserDefinedFpsDialog::UserDefinedFpsDialog(double *fps,
		double fpsMin, double fpsMax,
		QWidget *parent)
	: QDialog(parent)
{
	setWindowTitle( tr("Set user defined frame rate") );

	QVBoxLayout *mainLayout = new QVBoxLayout(this);
	QGroupBox *groupBox = new QGroupBox(this);
	QHBoxLayout *horizontalLayout = new QHBoxLayout(groupBox);
	QLabel *fpsLabel = new QLabel(groupBox);
	QSpacerItem *fpsSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

	fpsInput = new QLineEdit(groupBox);

	horizontalLayout->addWidget(fpsInput);
	horizontalLayout->addWidget(fpsLabel);
	horizontalLayout->addItem(fpsSpacer);

	fpsLabel->setText( tr("FPS (images/second)") );

	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);

	groupBox->setTitle( tr("Enter output video frame rate") );
	groupBox->setToolTip(tr("Minimum %1 FPS\n"
		"Maximum %2 FPS")
		.arg(fpsMin).arg(fpsMax));

	fpsValidator = new QDoubleValidator( fpsMin, fpsMax, 3, this );  // 3 decimals

	fpsInput->setValidator( fpsValidator );
	fpsInput->setText(QString::number( *fps ));
	fpsInput->setAlignment(Qt::AlignHCenter);
	fpsInput->setMinimumWidth(60);
	fpsInput->setMaximumWidth(100);

	userDefinedFps = fps;  // to store the user input before closing
}

void UserDefinedFpsDialog::accept()
{
	QString str;
	int pos = 0;

	// Input can be less than minimum so re-validate the mininum
	str = fpsInput->text();
	if ( fpsValidator->validate( str, pos ) != QValidator::Acceptable )
	{
		InfoBoxDialog infoBox(this);
		infoBox.setText( tr( "Minimum acceptable frame rate is %1 FPS." ).arg( fpsValidator->bottom() ) );
		infoBox.setInformativeText(tr("Please enter an acceptable frame rate value."));
		infoBox.exec();

		fpsInput->setText(QString::number( fpsValidator->bottom() ));

		// so don't accept
		return;
	}

	*userDefinedFps = fpsInput->text().toDouble();

	QDialog::accept();
}


