/*****************************************************************************
 *
 *  encoderconfiguredialog.cpp -- Input encoding parameters.
 * 
 *  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 "encoderconfiguredialog.h"

#include <QTextDocument>
#include <QLineEdit>
#include <QDebug>

EncoderConfigureDialog::EncoderConfigureDialog(Configurator *configurator, QWidget *parent)
	: QDialog(parent)
{
	config = configurator;

	// Controls
	verticalLayout = new QVBoxLayout(this);

	// Global buttons
	buttonBox = new QDialogButtonBox(this);

	buttonBox->setOrientation(Qt::Horizontal);
	buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);

	// Build all tabs
	tabWidget = new QTabWidget(this);
	tabWidget->setMovable(false);

	verticalLayout->addWidget(tabWidget);
	verticalLayout->addWidget(buttonBox);

	tabWidget->setCurrentIndex(0);

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

	// Window size
	int charWidth = buttonBox->fontMetrics().averageCharWidth();

#ifdef WIN32
	setMinimumWidth( 90 * charWidth );
#else
	setMinimumWidth( 60 * charWidth );
#endif

	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	setAttribute(Qt::WA_AlwaysShowToolTips);
}

void EncoderConfigureDialog::selectProfile(bool checked)
{
	if ( !checked ) return;

	QRadioButton *profileSelection = (QRadioButton *) QObject::sender();
	
	if ( !listLevel.contains(profileSelection) ) return;

	Configurator::ProfileInterface *pi = config->getProfileInterface();

	QListIterator<Configurator::Profile> p(pi->profiles);
	while (p.hasNext())
	{
		Configurator::Profile profile = p.next();

		if (profile.level != listLevel.value(profileSelection)) continue;

		profileDescriptionLabel->setText( profile.description );
		pi->selected = profile.level;
		break;
	}
}

void EncoderConfigureDialog::setUserInterface(Configurator::ProfileInterface *pi)
{
	setWindowTitle( pi->title );

	QWidget *widget = new QWidget();
	QVBoxLayout *widgetVLayout = new QVBoxLayout(widget);

	tabWidget->addTab(widget, pi->panelTitle);

	QGroupBox *descriptionBox = new QGroupBox( tr("Selected profile") );
	QHBoxLayout *descriptionHLayout = new QHBoxLayout(descriptionBox);
	
	profileDescriptionLabel = new QLabel( descriptionBox );
	descriptionHLayout->addWidget(profileDescriptionLabel);

	profilesBox = new QGroupBox( tr("Profiles") );
	profilesVLayout = new QVBoxLayout(profilesBox);

	QListIterator<Configurator::Profile> p(pi->profiles);
	while (p.hasNext())
	{
		Configurator::Profile profile = p.next();

		QRadioButton *profileSelection = new QRadioButton( profile.title, profilesBox );
		profileSelection->setChecked( false );

		if ( pi->selected == profile.level )
		{
			profileSelection->setChecked( true );
			profileDescriptionLabel->setText( profile.description );
			pi->selected = profile.level;
		}
					
		listLevel.insert( profileSelection, profile.level);

		QObject::connect( profileSelection, SIGNAL(clicked(bool)),
						this, SLOT(selectProfile(bool)) );

		profilesVLayout->addWidget(profileSelection);
	}

	widgetVLayout->addWidget(descriptionBox);
	widgetVLayout->addWidget(profilesBox);
	
	QSpacerItem *verticalSpacer = new QSpacerItem(20, 1, QSizePolicy::Minimum, QSizePolicy::Expanding);
	widgetVLayout->addItem(verticalSpacer);

	tabWidget->setCurrentIndex(0);
}
