/*****************************************************************************
 *
 * confaudmp3mencoderdialog.cpp -- Input the MP3 audio codec parameters.
 *
 * Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *
 *****************************************************************************/

#include "../video/video_functions.h"

#include "confaudmp3mencoderdialog.h"
#include "../encoder/encoder.h"
#include "macros.h"
#include <QtGui>

ConfAudMp3MEncoderDialog::ConfAudMp3MEncoderDialog(encoder_audio_settings_t *settings, 
	audio_t *track, QWidget *parent)
	: QDialog(parent),
	bitrateMin( 1 ),
	bitrateMax( 1024 ),
	frequenceMin( -1 ),
	frequenceMax( 24000 )
{
	callerSettings = settings;
	audioSource = track;

	// Local copy allows to cancel changes
	encSettings = new encoder_audio_settings_t( *settings );

	setMinimumSize( 340, 460 );
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

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

	mainTab = new QWidget();
	advancedTab = new QWidget();

	tabWidget->addTab(mainTab, QString());
	tabWidget->addTab(advancedTab, QString());

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

	tabWidget->setCurrentIndex(0);

	// Main tab 
	mainTabVLayout = new QVBoxLayout(mainTab);
	mainTabVLayout->setSpacing(12);

	// - Preset
	presetGroupBox = new QGroupBox(mainTab);
	presetVLayout = new QVBoxLayout(presetGroupBox);
	presetMediumRadioButton = new QRadioButton(presetGroupBox);
	presetStandardRadioButton = new QRadioButton(presetGroupBox);
	presetExtremeRadioButton = new QRadioButton(presetGroupBox);
	presetInsaneRadioButton = new QRadioButton(presetGroupBox);
	presetAbrRadioButton = new QRadioButton(presetGroupBox);
	presetNoneRadioButton = new QRadioButton(presetGroupBox);
	
	presetVLayout->addWidget(presetMediumRadioButton);
	presetVLayout->addWidget(presetStandardRadioButton);
	presetVLayout->addWidget(presetExtremeRadioButton);
	presetVLayout->addWidget(presetInsaneRadioButton);
	presetVLayout->addWidget(presetAbrRadioButton);
	presetVLayout->addWidget(presetNoneRadioButton);

	mainTabVLayout->addWidget(presetGroupBox);

	// - Encoding method
	methodHLayout = new QHBoxLayout();
	methodLabel = new QLabel(mainTab);
	methodComboBox = new QComboBox(mainTab);
	methodComboBox->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);

	methodHLayout->addWidget(methodLabel);
	methodHLayout->addWidget(methodComboBox);

	mainTabVLayout->addItem(methodHLayout);

	// - CBR/ABR bitrate
	bitrateGroupBox = new QGroupBox(mainTab);
	bitrateHLayout = new QHBoxLayout(bitrateGroupBox);
	bitrateSlider = new QSlider(bitrateGroupBox);
	bitrateLineEdit = new QLineEdit(bitrateGroupBox);

	bitrateSlider->setOrientation(Qt::Horizontal);
	bitrateSlider->setInvertedAppearance(false);
	bitrateSlider->setTickPosition(QSlider::NoTicks);
	bitrateSlider->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

	bitrateLineEdit->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	bitrateLineEdit->setMaxLength(4);
	bitrateLineEdit->setMaximumWidth(50);
		
	bitrateHLayout->addWidget(bitrateSlider);
	bitrateHLayout->addWidget(bitrateLineEdit);

	mainTabVLayout->addWidget(bitrateGroupBox);

	// - VBR options
	vbr_optionsGroupBox = new QGroupBox(mainTab);
	vbr_qualityGLayout = new QGridLayout(vbr_optionsGroupBox);

	vbr_qLowLabel = new QLabel(vbr_optionsGroupBox);
	vbr_qHighLabel = new QLabel(vbr_optionsGroupBox);
	vbr_qSlider = new QSlider(vbr_optionsGroupBox);

	vbr_qLowLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
	vbr_qHighLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

	vbr_qSlider->setOrientation(Qt::Horizontal);
	vbr_qSlider->setInvertedAppearance(false);
	vbr_qSlider->setTickPosition(QSlider::TicksAbove);
	vbr_qSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
	vbr_qSlider->setRange( 0, 9 );
	vbr_qSlider->setTickInterval( 1 );
	vbr_qSlider->setSingleStep( 1 );

	vbr_aqLowLabel = new QLabel(vbr_optionsGroupBox);
	vbr_aqHighLabel = new QLabel(vbr_optionsGroupBox);
	vbr_aqSlider = new QSlider(vbr_optionsGroupBox);

	vbr_aqLowLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
	vbr_aqHighLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

	vbr_aqSlider->setOrientation(Qt::Horizontal);
	vbr_aqSlider->setInvertedAppearance(false);
	vbr_aqSlider->setTickPosition(QSlider::TicksAbove);
	vbr_aqSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
	vbr_aqSlider->setRange( 0, 9 );
	vbr_aqSlider->setTickInterval( 1 );
	vbr_aqSlider->setSingleStep( 1 );

	vbr_qualityGLayout->addWidget(vbr_qLowLabel,	0, 0);
	vbr_qualityGLayout->addWidget(vbr_qSlider,		0, 1);
	vbr_qualityGLayout->addWidget(vbr_qHighLabel,	0, 2);
	vbr_qualityGLayout->addWidget(vbr_aqLowLabel,	1, 0);
	vbr_qualityGLayout->addWidget(vbr_aqSlider,		1, 1);
	vbr_qualityGLayout->addWidget(vbr_aqHighLabel,	1, 2);
	
	mainTabVLayout->addWidget(vbr_optionsGroupBox);

	// - Presets only options
	preset_optsGroupBox = new QGroupBox(mainTab);
	preset_optsVLayout = new QVBoxLayout(preset_optsGroupBox);

	fastCheckBox = new QCheckBox(preset_optsGroupBox);
	force_cbrCheckBox = new QCheckBox(preset_optsGroupBox);
	
	preset_optsVLayout->addWidget(fastCheckBox);
	preset_optsVLayout->addWidget(force_cbrCheckBox);

	mainTabVLayout->addWidget(preset_optsGroupBox);

	// - Spacer
	mainTabVSpacer = new QSpacerItem(20, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);

	mainTabVLayout->addItem(mainTabVSpacer);

	// - Default settings
	defaultHLayout = new QHBoxLayout();
	defaultPushButton = new QPushButton(mainTab);
	defaultHSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	defaultHLayout->addWidget(defaultPushButton);
	defaultHLayout->addItem(defaultHSpacer);

	mainTabVLayout->addLayout(defaultHLayout);

	// Advanced tab 
	advancedTabVLayout = new QVBoxLayout(advancedTab);
	advancedTabVLayout->setSpacing(12);

	// - Sampling rate
	srateGroupBox = new QGroupBox(advancedTab);
	srateHLayout = new QHBoxLayout(srateGroupBox);
	srateComboBox = new QComboBox(srateGroupBox);
	srateHSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	srateHLayout->addWidget(srateComboBox);
	srateHLayout->addItem(srateHSpacer);

	advancedTabVLayout->addWidget(srateGroupBox);

	// - Bandwidth filter
	filterGroupBox = new QGroupBox(advancedTab);

	highpassCheckBox = new QCheckBox(filterGroupBox);
	highpassSlider = new QSlider(filterGroupBox);
	highpassLineEdit = new QLineEdit(filterGroupBox);
	
	highpassSlider->setOrientation(Qt::Horizontal);
	highpassSlider->setInvertedAppearance(false);
	highpassSlider->setTickPosition(QSlider::NoTicks);
	highpassSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
	highpassSlider->setSingleStep( 100 );  // to pass from -1 (disabled) to 0 (LAME auto)
	highpassSlider->setRange( frequenceMin, frequenceMax );

	highpassLineEdit->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	highpassLineEdit->setMaxLength(5);
	highpassLineEdit->setMaximumWidth(40);

	lowpassCheckBox = new QCheckBox(filterGroupBox);
	lowpassSlider = new QSlider(filterGroupBox);
	lowpassLineEdit = new QLineEdit(filterGroupBox);
	
	lowpassSlider->setOrientation(Qt::Horizontal);
	lowpassSlider->setInvertedAppearance(false);
	lowpassSlider->setTickPosition(QSlider::NoTicks);
	lowpassSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
	lowpassSlider->setSingleStep( 100 );  // to pass from -1 (disabled) to 0 (LAME auto)
	lowpassSlider->setRange( frequenceMin, frequenceMax );

	lowpassLineEdit->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	lowpassLineEdit->setMaxLength(5);
	lowpassLineEdit->setMaximumWidth(40);

	filterGLayout = new QGridLayout(filterGroupBox);

	filterGLayout->addWidget(highpassCheckBox,	0, 0);
	filterGLayout->addWidget(highpassSlider,	0, 1);
	filterGLayout->addWidget(highpassLineEdit,	0, 2);
	filterGLayout->addWidget(lowpassCheckBox,	1, 0);
	filterGLayout->addWidget(lowpassSlider,		1, 1);
	filterGLayout->addWidget(lowpassLineEdit,	1, 2);

	advancedTabVLayout->addWidget(filterGroupBox);

	// - More advance options
	extraGroupBox = new QGroupBox(advancedTab);
	extraGLayout = new QGridLayout(extraGroupBox);

	stereoLabel = new QLabel(extraGroupBox);
	stereoComboBox = new QComboBox(extraGroupBox);

	paddingLabel = new QLabel(extraGroupBox);
	paddingComboBox = new QComboBox(extraGroupBox);

	ratioCheckBox = new QCheckBox(extraGroupBox);
	ratioSlider = new QSlider(extraGroupBox);
	
	ratioSlider->setOrientation(Qt::Horizontal);
	ratioSlider->setInvertedAppearance(false);
	ratioSlider->setTickPosition(QSlider::TicksAbove);
	ratioSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
	ratioSlider->setSingleStep( 10 );  // to pass from -1 (disabled) to 0 (LAME auto)
	ratioSlider->setRange( 0, 100 );
	ratioSlider->setTickInterval( 10 );

	extraGLayout->addWidget(stereoLabel,		0, 0);
	extraGLayout->addWidget(stereoComboBox,		0, 1);
	extraGLayout->addWidget(paddingLabel,		1, 0);
	extraGLayout->addWidget(paddingComboBox,	1, 1);
	extraGLayout->addWidget(ratioCheckBox,		2, 0);
	extraGLayout->addWidget(ratioSlider,		2, 1);
	
	advancedTabVLayout->addWidget(extraGroupBox);

	// - Spacer
	advancedTabVSpacer = new QSpacerItem(20, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);

	advancedTabVLayout->addItem(advancedTabVSpacer);

	// Captions
	setWindowTitle( tr("MP3 audio encoder") );

	tabWidget->setTabText(tabWidget->indexOf(mainTab), tr("Audio settings") );
	tabWidget->setTabText(tabWidget->indexOf(advancedTab), tr("Advanced") );

	presetGroupBox->setTitle( tr(" MP3 audio presets ") );
	presetNoneRadioButton->setText( tr("None") );
	presetMediumRadioButton->setText( tr("Medium (Good quality VBR)") );
	presetStandardRadioButton->setText( tr("Standard (High quality VBR)") );
	presetExtremeRadioButton->setText( tr("Extreme (Very high quality VBR)") );
	presetInsaneRadioButton->setText( tr("Insane (Highest quality CBR)") );
	presetAbrRadioButton->setText( tr("Average Bit Rate (Preset ABR)") );

	methodLabel->setText( tr("Encoding method: ") );
	
	bitrateGroupBox->setTitle( tr(" CBR/ABR bitrate ( kbps ) ") );

	vbr_optionsGroupBox->setTitle( tr(" VBR options ") );
	vbr_qLowLabel->setText( tr("Low ") );
	vbr_qHighLabel->setText( tr(" High quality") );
	vbr_aqLowLabel->setText( tr("Fast ") );
	vbr_aqHighLabel->setText( tr(" Slow algoritmh") );

	preset_optsGroupBox->setTitle( tr(" Presets options ") );
	fastCheckBox->setText( tr("Enable fast VBR encoding") );
	force_cbrCheckBox->setText( tr("Forced constant bitrate") );

	filterGroupBox->setTitle( tr(" Bandwidth filter ") );
	highpassCheckBox->setText( tr("High pass (Hz)") );
	lowpassCheckBox->setText( tr("Low pass (Hz)") );

	srateGroupBox->setTitle( tr(" Sampling rate ") );

	extraGroupBox->setTitle( tr(" Advanced options ") );

	stereoLabel->setText( tr("Stereo: ") );
	paddingLabel->setText( tr("Padding: ") );
	ratioCheckBox->setText( tr("Ratio:") );

	defaultPushButton->setText( tr("Default settings") );

	// Tool tips and What's this
	setToolTip();

	setAttribute(Qt::WA_AlwaysShowToolTips);

	bitrateValidator = new QIntValidator( this );
	bitrateLineEdit->setValidator( bitrateValidator );
	
	highpassValidator = new QIntValidator( this );
	highpassLineEdit->setValidator( highpassValidator );
	highpassValidator->setRange(  frequenceMin, frequenceMax );

	lowpassValidator = new QIntValidator( this );
	lowpassLineEdit->setValidator( lowpassValidator );
	lowpassValidator->setRange(  frequenceMin, frequenceMax );

	populateMethod();
	populateSampling();
	populateStereo();
	populatePadding();

	// Signals which don't change settings
	connect(bitrateLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(bitrateLineEditChanged(QString)));
	connect(bitrateSlider, SIGNAL(valueChanged(int)), 
		this, SLOT(bitrateSliderChanged(int)));

	connect(highpassLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(highpassLineEditChanged(QString)));
	connect(highpassSlider, SIGNAL(valueChanged(int)), 
		this, SLOT(highpassSliderChanged(int)));

	connect(lowpassLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(lowpassLineEditChanged(QString)));
	connect(lowpassSlider, SIGNAL(valueChanged(int)), 
		this, SLOT(lowpassSliderChanged(int)));

	connect(ratioSlider, SIGNAL(valueChanged(int)), 
		this, SLOT(ratioSliderChanged(int)));
	connect(vbr_qSlider, SIGNAL(valueChanged(int)), 
		this, SLOT(vbr_qSliderChanged(int)));
	connect(vbr_aqSlider, SIGNAL(valueChanged(int)), 
		this, SLOT(vbr_aqSliderChanged(int)));

	// Now connect other signals (which controls are up-to-date)
	connect(fastCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(toggleFast()));
	connect(force_cbrCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(toggleForce_Cbr()));

	connect(highpassCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(toggleHighPass()));
	connect(lowpassCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(toggleLowPass()));

	connect(ratioCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(toggleRatio()));

	connect(methodComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectMethod(int)));
	connect(srateComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectSampling(int)));
	connect(paddingComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectPadding(int)));
	connect(stereoComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectStereo(int)));

	connect(presetMediumRadioButton, SIGNAL(clicked()), 
		this, SLOT(selectPreset()));
	connect(presetStandardRadioButton, SIGNAL(clicked()), 
		this, SLOT(selectPreset()));
	connect(presetExtremeRadioButton, SIGNAL(clicked()), 
		this, SLOT(selectPreset()));
	connect(presetInsaneRadioButton, SIGNAL(clicked()), 
		this, SLOT(selectPreset()));
	connect(presetAbrRadioButton, SIGNAL(clicked()), 
		this, SLOT(selectPreset()));
	connect(presetNoneRadioButton, SIGNAL(clicked()), 
		this, SLOT(selectPreset()));

	connect(defaultPushButton, SIGNAL(clicked()), 
		this, SLOT(defaultPushButtonClicked()));

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

	updateControls();
}

void ConfAudMp3MEncoderDialog::accept()
{
	*callerSettings = *encSettings;

	QDialog::accept();
}

void ConfAudMp3MEncoderDialog::updateControls()
{
	int index;

	// Presets
	if ( encSettings->mp3.preset == "medium" )
	{
		presetMediumRadioButton->setChecked(true);
		fastCheckBox->setEnabled(true);
		force_cbrCheckBox->setEnabled(false);
		force_cbrCheckBox->setChecked(false);
		encSettings->mp3.cbr = false;
	}
	else if ( encSettings->mp3.preset == "standard" )
	{
		presetStandardRadioButton->setChecked(true);
		fastCheckBox->setEnabled(true);
		force_cbrCheckBox->setEnabled(false);
		force_cbrCheckBox->setChecked(false);
		encSettings->mp3.cbr = false;
	}
	else if ( encSettings->mp3.preset == "extreme" )
	{
		presetExtremeRadioButton->setChecked(true);
		fastCheckBox->setEnabled(true);
		force_cbrCheckBox->setEnabled(false);
		force_cbrCheckBox->setChecked(false);
		encSettings->mp3.cbr = false;
	}
	else if ( encSettings->mp3.preset == "insane" )
	{
		presetInsaneRadioButton->setChecked(true);
		fastCheckBox->setEnabled(false);
		fastCheckBox->setChecked(false);
		encSettings->mp3.fast = false;
		force_cbrCheckBox->setEnabled(false);
		force_cbrCheckBox->setChecked(false);
		encSettings->mp3.cbr = false;
	}
	else if ( encSettings->mp3.preset.toInt() >= 8 && encSettings->mp3.preset.toInt() <= 320 )
	{
		presetAbrRadioButton->setChecked(true);
		fastCheckBox->setEnabled(false);
		fastCheckBox->setChecked(false);
		encSettings->mp3.fast = false;
		force_cbrCheckBox->setEnabled(true);
	}
	else
	{
		// No preset
		presetNoneRadioButton->setChecked(true);
		fastCheckBox->setEnabled(false);
		fastCheckBox->setChecked(false);
		encSettings->mp3.fast = false;
		force_cbrCheckBox->setEnabled(false);
		force_cbrCheckBox->setChecked(false);
		encSettings->mp3.cbr = false;
		encSettings->mp3.preset.clear();  // avoid non expected value
	}

	// Arrange consistency CBR vs. VBR vs. presets
	if ( encSettings->mp3.preset.toInt() >= 8 && encSettings->mp3.preset.toInt() <= 320 )
	{
		bitrateSlider->setSingleStep( 16 );
		bitrateSlider->setRange( 8, 320 );
		bitrateValidator->setRange( 8, 320 );

		bitrateSlider->setValue( encSettings->mp3.preset.toInt() );
		bitrateLineEdit->setText( encSettings->mp3.preset );
		
		bitrateGroupBox->setTitle( tr(" Preset ABR bitrate ( kbps ) ") );
		bitrateGroupBox->setToolTip( boxToolTip("preset", tr("160"), QString("<8-320>"),
						tr("ABR encoding at average given kbps bitrate.")) );

		bitrateGroupBox->setEnabled(true);

		vbr_optionsGroupBox->setEnabled(false);
		vbr_qSlider->setToolTip( QString() );
		vbr_aqSlider->setToolTip( QString() );

		methodComboBox->setEnabled(false);
		methodLabel->setToolTip( QString() );
		methodComboBox->setToolTip( QString() );
	}
	else
	{
		// Preset or not?
		if ( encSettings->mp3.preset.isEmpty() )
		{
			methodLabel->setToolTip( boxToolTip("method", "cbr", "<cbr | vbr | abr | mt | rh | mtrh>",
				tr("Encoding method: cbr (constant bitrate), mt (Mark Taylor VBR algorithm), "
"rh (Robert Hegemann VBR algorithm), abr (average bitrate), "
"mtrh (Mark Taylor Robert Hegemann VBR algorithm). Setting to cbr also "
"forces CBR mode encoding on subsequent ABR presets modes.")) );
			methodComboBox->setToolTip( methodLabel->toolTip() );

			methodComboBox->setEnabled(true);

			// Encoding method
			if ( encSettings->mp3.method == "mt"
				|| encSettings->mp3.method == "rh"
				|| encSettings->mp3.method == "mtrh" )
			{
				vbr_qLowLabel->setToolTip( boxToolTip("vbr_q", "4", "<0-9>",
									tr("Quality (0 - highest, 9 - lowest).")) );
				vbr_qHighLabel->setToolTip( vbr_qLowLabel->toolTip() );

				vbr_aqLowLabel->setToolTip( boxToolTip("vbr_aq", "4", "<0-9>",
									tr("Algorithmic quality (0 - best/slowest, 9 - worst/fastest).")) );
				vbr_aqHighLabel->setToolTip( vbr_aqLowLabel->toolTip() );

				vbr_qSlider->setToolTip( QString("<b>%1</b>").arg( encSettings->mp3.vbr_q ) );
				vbr_aqSlider->setToolTip( QString("<b>%1</b>").arg( encSettings->mp3.vbr_aq ) );

				bitrateGroupBox->setToolTip( QString() );
				bitrateGroupBox->setEnabled(false);
				vbr_optionsGroupBox->setEnabled(true);
			}
			else
			{
				int br = encSettings->mp3.bitrate;

				bitrateSlider->setSingleStep( 16 );
				bitrateSlider->setRange( bitrateMin, bitrateMax );
				bitrateValidator->setRange( bitrateMin, bitrateMax );

				bitrateSlider->setValue( br );
				bitrateLineEdit->setText( QString::number(br) );

				bitrateGroupBox->setTitle( tr(" CBR/ABR bitrate ( kbps ) ") );
				bitrateGroupBox->setToolTip( boxToolTip("br", tr("160"), QString("<%1-%2>").arg( bitrateMin ).arg( bitrateMax ),
									tr("Bitrate in kbps (CBR and ABR only).")) );

				vbr_optionsGroupBox->setEnabled(false);
				vbr_qSlider->setToolTip( QString() );
				vbr_aqSlider->setToolTip( QString() );
				bitrateGroupBox->setEnabled(true);
			}
		}
		else
		{
			methodLabel->setToolTip( QString() );
			methodComboBox->setToolTip( QString() );
			bitrateGroupBox->setToolTip( QString() );
			vbr_optionsGroupBox->setToolTip( QString() );
			vbr_qSlider->setToolTip( QString() );
			vbr_aqSlider->setToolTip( QString() );

			methodComboBox->setEnabled(false);
			bitrateGroupBox->setEnabled(false);
			vbr_optionsGroupBox->setEnabled(false);
		}
	}

	ratioSlider->setValue( encSettings->mp3.ratio );
	if ( encSettings->mp3.ratio == 0 )
	{
		ratioSlider->setToolTip( tr("<b>Default</b>") );
	}
	else
	{
		ratioSlider->setToolTip( QString("<b>%1%</b>").arg( encSettings->mp3.ratio ) );
	}

	fastCheckBox->setChecked( encSettings->mp3.fast );
	force_cbrCheckBox->setChecked( encSettings->mp3.cbr );

	highpassCheckBox->setChecked( encSettings->mp3.highpass != -1 );
	highpassSlider->setValue( encSettings->mp3.highpass );
	highpassLineEdit->setText( QString::number(encSettings->mp3.highpass) );

	lowpassCheckBox->setChecked( encSettings->mp3.lowpass != -1 );
	lowpassSlider->setValue( encSettings->mp3.lowpass );
	lowpassLineEdit->setText( QString::number(encSettings->mp3.lowpass) );

	index = methodComboBox->findData( encSettings->mp3.method );

	if ( index == -1 ) // should not happened
	{
		methodComboBox->setCurrentIndex( 0 );
		encSettings->mp3.method = methodComboBox->itemData( 0 ).toInt();
	}
	else
	{
		methodComboBox->setCurrentIndex( index );
	}

	vbr_qSlider->setValue( encSettings->mp3.vbr_q );
	vbr_aqSlider->setValue( encSettings->mp3.vbr_aq );

	index = srateComboBox->findData( encSettings->mp3.srate );

	if ( index == -1 ) // should not happened
	{
		srateComboBox->setCurrentIndex( 0 );
		encSettings->mp3.srate = srateComboBox->itemData( 0 ).toInt();
	}
	else
	{
		srateComboBox->setCurrentIndex( index );
	}

	index = stereoComboBox->findData( encSettings->mp3.stereo );

	if ( index == -1 ) // should not happened
	{
		stereoComboBox->setCurrentIndex( 0 );
		encSettings->mp3.stereo = stereoComboBox->itemData( 0 ).toInt();
	}
	else
	{
		stereoComboBox->setCurrentIndex( index );
	}

	index = paddingComboBox->findData( encSettings->mp3.padding );

	if ( index == -1 ) // should not happened
	{
		paddingComboBox->setCurrentIndex( 0 );
		encSettings->mp3.padding = paddingComboBox->itemData( 0 ).toInt();
	}
	else
	{
		paddingComboBox->setCurrentIndex( index );
	}
}

void ConfAudMp3MEncoderDialog::populateMethod()
{
	methodComboBox->clear();

	QMap<QString, QString> method;
	method.insert( "cbr", tr("CBR") );  // default for better a/v synchro
	method.insert( "mt", tr("VBR mt (Mark Taylor)") );
	method.insert( "rh", tr("VBR rh (Robert Hegemann)") );
	method.insert( "abr", tr("ABR") );
	method.insert( "mtrh", tr("VBR mtrh") );

	QMapIterator<QString, QString> i(method);
	while ( i.hasNext() )
	{
		i.next();

		methodComboBox->addItem( i.value(), i.key() );
	}

	int index = methodComboBox->findData( encSettings->mp3.method );

	if ( index == -1 ) // should not happened
	{
		methodComboBox->setCurrentIndex( 0 );
		encSettings->mp3.method = methodComboBox->itemData( 0 ).toString();
	}
	else
	{
		methodComboBox->setCurrentIndex( index );
	}
}

void ConfAudMp3MEncoderDialog::populateSampling()
{
	srateComboBox->clear();

	QMap<int, QString> sampling;
	sampling.insert( 0, tr("(input rate)") );
	sampling.insert( 11025, tr("11,025 Hz") );
	sampling.insert( 22050, tr("22,050 Hz") );
	sampling.insert( 44100, tr("44,100 Hz") );
	sampling.insert( 48000, tr("48,000 Hz") );
	sampling.insert( 96000, tr("96,000 Hz") );

	QMapIterator<int, QString> i(sampling);
	while ( i.hasNext() )
	{
		i.next();

		srateComboBox->addItem( i.value(), i.key() );
	}

	int index = srateComboBox->findData( encSettings->mp2.srate );

	if ( index == -1 ) // should not happened
	{
		srateComboBox->setCurrentIndex( 0 );
		encSettings->mp2.srate = srateComboBox->itemData( 0 ).toInt();
	}
	else
	{
		srateComboBox->setCurrentIndex( index );
	}
}

void ConfAudMp3MEncoderDialog::populateStereo()
{
	stereoComboBox->clear();

	QMap<QString, QString> stereo;
	stereo.insert( "unset", tr("Default: auto") );
	stereo.insert( "stereo", tr("Stereo") );
	stereo.insert( "jstereo", tr("Joint-Stereo") );
	stereo.insert( "dual", tr("Dual-Channel") );
	stereo.insert( "mono", tr("Mono") );

	QMapIterator<QString, QString> i(stereo);
	while ( i.hasNext() )
	{
		i.next();

		stereoComboBox->addItem( i.value(), i.key() );
	}

	int index = stereoComboBox->findData( encSettings->mp3.stereo );

	if ( index == -1 ) // should not happened
	{
		stereoComboBox->setCurrentIndex( 0 );
		encSettings->mp3.stereo = stereoComboBox->itemData( 0 ).toInt();
	}
	else
	{
		stereoComboBox->setCurrentIndex( index );
	}
}

void ConfAudMp3MEncoderDialog::populatePadding()
{
	paddingComboBox->clear();

	QMap<int, QString> padding;
	padding.insert( 3, tr("Default") );
	padding.insert( 0, tr("None") );
	padding.insert( 1, tr("All") );
	padding.insert( 2, tr("Adjust") );

	QMapIterator<int, QString> i(padding);
	while ( i.hasNext() )
	{
		i.next();

		paddingComboBox->addItem( i.value(), i.key() );
	}

	int index = paddingComboBox->findData( encSettings->mp3.padding );

	if ( index == -1 ) // should not happened
	{
		paddingComboBox->setCurrentIndex( 0 );
		encSettings->mp3.padding = paddingComboBox->itemData( 0 ).toInt();
	}
	else
	{
		paddingComboBox->setCurrentIndex( index );
	}
}

void ConfAudMp3MEncoderDialog::bitrateLineEditChanged(const QString &value)
{
	encSettings->mp3.bitrate = value.toInt();

	bitrateSlider->setValue( encSettings->mp3.bitrate );
}

void ConfAudMp3MEncoderDialog::bitrateSliderChanged(const int value)
{
	encSettings->mp3.bitrate = value;

	bitrateLineEdit->setText( QString::number(value) );
}

void ConfAudMp3MEncoderDialog::vbr_qSliderChanged(const int value)
{
	encSettings->mp3.vbr_q = value;

	vbr_qSlider->setToolTip( QString("<b>%1</b>").arg( value ) );
}

void ConfAudMp3MEncoderDialog::vbr_aqSliderChanged(const int value)
{
	encSettings->mp3.vbr_aq = value;

	vbr_aqSlider->setToolTip( QString("<b>%1</b>").arg( value ) );
}

void ConfAudMp3MEncoderDialog::toggleHighPass()
{
	if ( highpassCheckBox->isChecked() )
	{
		if ( encSettings->mp3.highpass == -1 )
		{
			highpassSlider->setValue( 0 );

			encSettings->mp3.highpass = 0;  // "LAME choose values automatically"
		}
	}
	else
	{
		highpassSlider->setValue( -1 );

		encSettings->mp3.highpass = -1;  // "Not Set"
	}
}

void ConfAudMp3MEncoderDialog::highpassLineEditChanged(const QString &value)
{
	encSettings->mp3.highpass = value.toInt();

	highpassSlider->setValue( encSettings->mp3.highpass );

	if ( encSettings->mp3.highpass > 0 && encSettings->mp3.lowpass > 0
		&& encSettings->mp3.highpass >= encSettings->mp3.lowpass )
	{
		lowpassLineEdit->setText( QString::number( qMin(encSettings->mp3.highpass, lowpassSlider->maximum()) ) );
	}
}

void ConfAudMp3MEncoderDialog::highpassSliderChanged(const int value)
{
	encSettings->mp3.highpass = value;

	if ( encSettings->mp3.highpass == -1 )
	{
		highpassCheckBox->setChecked(false);
	}
	else
	{
		highpassCheckBox->setChecked(true);
	}

	highpassLineEdit->setText( QString::number(value) );

	if ( encSettings->mp3.highpass > 0 && encSettings->mp3.lowpass > 0
		&& encSettings->mp3.highpass >= encSettings->mp3.lowpass )
	{
		lowpassSlider->setValue( qMin(encSettings->mp3.highpass, lowpassSlider->maximum()) );
	}
}

void ConfAudMp3MEncoderDialog::toggleLowPass()
{
	if ( lowpassCheckBox->isChecked() )
	{
		if ( encSettings->mp3.lowpass == -1 )
		{
			lowpassSlider->setValue( 0 );

			encSettings->mp3.lowpass = 0;  // "LAME choose values automatically"
		}
	}
	else
	{
		lowpassSlider->setValue( -1 );

		encSettings->mp3.lowpass = -1;  // "Not Set"
	}
}

void ConfAudMp3MEncoderDialog::lowpassLineEditChanged(const QString &value)
{
	encSettings->mp3.lowpass = value.toInt();

	lowpassSlider->setValue( encSettings->mp3.lowpass );

	if ( encSettings->mp3.highpass > 0 && encSettings->mp3.lowpass > 0
		&& encSettings->mp3.highpass >= encSettings->mp3.lowpass )
	{
		highpassLineEdit->setText( QString::number( qMax(encSettings->mp3.lowpass, highpassSlider->minimum()) ) );
	}
}

void ConfAudMp3MEncoderDialog::lowpassSliderChanged(const int value)
{
	encSettings->mp3.lowpass = value;

	if ( encSettings->mp3.lowpass == -1 )
	{
		lowpassCheckBox->setChecked(false);
	}
	else
	{
		lowpassCheckBox->setChecked(true);
	}

	lowpassLineEdit->setText( QString::number(value) );

	if ( encSettings->mp3.highpass > 0 && encSettings->mp3.lowpass > 0
		&& encSettings->mp3.highpass >= encSettings->mp3.lowpass )
	{
		highpassSlider->setValue( qMax(encSettings->mp3.lowpass, highpassSlider->minimum()) );
	}
}

void ConfAudMp3MEncoderDialog::toggleFast()
{
	encSettings->mp3.fast = fastCheckBox->isChecked();
}

void ConfAudMp3MEncoderDialog::toggleForce_Cbr()
{
	encSettings->mp3.cbr = force_cbrCheckBox->isChecked();
}

void ConfAudMp3MEncoderDialog::toggleRatio()
{
	if ( ratioCheckBox->isChecked() )
	{
		if ( encSettings->mp3.ratio == 0 )
		{
			ratioSlider->setValue( 1 );

			encSettings->mp3.ratio = 1;
		}
	}
	else
	{
		ratioSlider->setValue( 0 );

		encSettings->mp3.ratio = 0;  // "Not Set"
	}
}

void ConfAudMp3MEncoderDialog::ratioSliderChanged(const int value)
{
	encSettings->mp3.ratio = value;

	if ( encSettings->mp3.ratio == 0 )
	{
		ratioCheckBox->setChecked(false);
		ratioSlider->setToolTip( tr("<b>Default</b>") );
	}
	else
	{
		ratioCheckBox->setChecked(true);
		ratioSlider->setToolTip( QString("<b>%1%</b>").arg( value ) );
	}
}

void ConfAudMp3MEncoderDialog::selectMethod(const int index)
{
	// Do nothing if combobox is empty or reset
	if (index < 0)
	{
		return;
	}

	encSettings->mp3.method = methodComboBox->itemData( index ).toInt();

	updateControls();
}

void ConfAudMp3MEncoderDialog::selectPadding(const int index)
{
	// Do nothing if combobox is empty or reset
	if (index < 0)
	{
		return;
	}

	encSettings->mp3.padding = paddingComboBox->itemData( index ).toInt();

	updateControls();
}

void ConfAudMp3MEncoderDialog::selectSampling(const int index)
{
	// Do nothing if combobox is empty or reset
	if (index < 0)
	{
		return;
	}

	encSettings->mp3.srate = srateComboBox->itemData( index ).toInt();
}

void ConfAudMp3MEncoderDialog::selectStereo(const int index)
{
	// Do nothing if combobox is empty or reset
	if (index < 0)
	{
		return;
	}

	encSettings->mp3.stereo = stereoComboBox->itemData( index ).toInt();

	updateControls();
}

void ConfAudMp3MEncoderDialog::selectPreset()
{
	// Then proceed for each preset
	if ( presetMediumRadioButton->isChecked() )
	{
		encSettings->mp3.preset = "medium";
	}
	else if ( presetStandardRadioButton->isChecked() )
	{
		encSettings->mp3.preset = "standard";
	}
	else if ( presetExtremeRadioButton->isChecked() )
	{
		encSettings->mp3.preset = "extreme";
	}
	else if ( presetInsaneRadioButton->isChecked() )
	{
		encSettings->mp3.preset = "insane";
	}
	else if ( presetAbrRadioButton->isChecked() )
	{
		encSettings->mp3.preset = "160";
	}
	else
	{
		encSettings->mp3.preset.clear();
	}

	updateControls();
}

void ConfAudMp3MEncoderDialog::defaultPushButtonClicked()
{
	encSettings->mp3.clear();

	updateControls();
}

void ConfAudMp3MEncoderDialog::setToolTip()
{
	fastCheckBox->setToolTip( boxToolTip("fast", "Not Set", "",
						tr("Switch on faster encoding on subsequent VBR presets modes. This "
"results in slightly lower quality and higher bitrates.")) );
				
	force_cbrCheckBox->setToolTip( boxToolTip("cbr", "Not Set", "",
						tr("Forces CBR mode encoding on subsequent ABR presets modes.")) );

	highpassCheckBox->setToolTip( boxToolTip("highpass", "-1 (disable)", "<freq>",
						tr("Set a highpass filtering frequency in Hz. Frequencies below the "
"specified one will be cut off. A value of -1 will disable filtering, a value of 0 will let "
"LAME choose values automatically.")) );
	highpassSlider->setToolTip( highpassCheckBox->toolTip() );
	highpassLineEdit->setToolTip( highpassCheckBox->toolTip() );

	lowpassCheckBox->setToolTip( boxToolTip("lowpass", "-1 (disable)", "<freq>",
						tr("Set a lowpass filtering frequency in Hz. Frequencies above the "
"specified one will be cut off. A value of -1 will disable filtering, a value of 0 will let "
"LAME choose values automatically.")) );
	lowpassSlider->setToolTip( lowpassCheckBox->toolTip() );
	lowpassLineEdit->setToolTip( lowpassCheckBox->toolTip() );

	stereoLabel->setToolTip( boxToolTip("mode", "auto", "",
						tr("Channel mode.")) );
	stereoComboBox->setToolTip( stereoLabel->toolTip() );

	paddingLabel->setToolTip( boxToolTip("padding", "auto", "<none | all | adjust>",
						tr("The encoder is supposed to use padding in selected frames to keep "
"the average bitrate as close as possible to the specified bitrate.")) );
	paddingComboBox->setToolTip( paddingLabel->toolTip() );

	ratioCheckBox->setToolTip( boxToolTip("ratio", "disable", "1-100%",
						tr("Compression ratio.")) );
}

QString ConfAudMp3MEncoderDialog::labelToolTip( const QString &defaults, const QString &details )
{
	QString strDefault( tr("Default: ") ); 

	return tr(	"<p><b>%1</b> %2</p>"
				"<p></p>"
				"<p>%3</p>" )
				.arg(Qt::escape(strDefault)).arg(Qt::escape(defaults))
				.arg(Qt::escape(details));
}

QString ConfAudMp3MEncoderDialog::editToolTip( const QString &name, const QString &range )
{
	return tr("<b>%1</b> %2").arg( Qt::escape(name) ).arg( Qt::escape(range) );
}

QString ConfAudMp3MEncoderDialog::boxToolTip( const QString &name, const QString &defaults, const QString &range, const QString &details )
{
	QString strDefault( tr("Default: ") ); 

	return tr(	"<p><b>%1</b> %2</p>"
				"<p></p>"
				"<p><b>%3</b> %4</p>"
				"<p></p>"
				"<p>%5</p>" )
				.arg(Qt::escape(name)).arg(Qt::escape(range))
				.arg(Qt::escape(strDefault)).arg(Qt::escape(defaults))
				.arg(Qt::escape(details));
}
