/*****************************************************************************
 *
 *  customsplitdialog.cpp -- Input the custom size of volumes to split output.
 * 
 *  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 "customsplitdialog.h"
#include "infoboxdialog.h"
#include <QtGui>

CustomSplitDialog::CustomSplitDialog(int *split, QWidget *parent)
	: QDialog(parent)
{
	setWindowTitle( tr("Set custom split size") );

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

	splitInput = new QLineEdit(groupBox);

	horizontalLayout->addWidget(splitPreLabel);
	horizontalLayout->addWidget(splitInput);
	horizontalLayout->addWidget(splitPostLabel);
	horizontalLayout->addItem(splitSpacer);

	splitPreLabel->setText( tr("Volume size:") );
	splitPostLabel->setText( tr("MBytes") );

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

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

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

	setLayout(mainLayout);

	setAttribute(Qt::WA_AlwaysShowToolTips);

	groupBox->setTitle( tr("Enter output volume size") );
	groupBox->setToolTip(tr("Minimum 650 MBytes\n"
		"Maximum 9500 MBytes"));

	splitValidator = new QIntValidator( 650, 9500, this );

	splitInput->setValidator( splitValidator );
	splitInput->setText(QString::number(*split));
	splitInput->setAlignment(Qt::AlignHCenter);
	splitInput->setMinimumWidth(60);
	splitInput->setMaximumWidth(100);

	customSplit = split;  // to store the user input before closing
}

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

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

		splitInput->setText(QString::number( splitValidator->bottom() ));

		// so don't accept
		return;
	}

	*customSplit = splitInput->text().toInt();

	QDialog::accept();
}
