/*****************************************************************************
 *
 *  confvidxvidmencoderdialog.cpp -- Input the Xvid codec 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 "confvidxvidmencoderdialog.h"
#include "userdefinedrezdialog.h"
#include "userdefinedfpsdialog.h"
#include "userdefinedtargetdialog.h"

#include "../video/video_struct.h"
#include "../encoder/encoder.h"
#include "macros.h"
#include <QtGui>

ConfVidXvidMEncoderDialog::ConfVidXvidMEncoderDialog(encoder_video_settings_t *settings, 
	video_t *track,  QWidget *parent)
	: QDialog(parent),
	fpsMin( 0.01 ),
	fpsMax( 200.0 )
{
	callerSettings = settings;
	videoSource = track;

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

	// Ensure some settings
	if (encSettings->user_defined.xvid.profile_set >= 0) 
	{
		encSettings->user_defined.xvid.profile_set = DivX;
	}

	if (encSettings->user_defined.xvid.resX < widthMin || encSettings->user_defined.xvid.resY < heightMin )
	{
		encSettings->user_defined.xvid.resX = track->resX;
		encSettings->user_defined.xvid.resY = track->resY;
	}

	if (encSettings->user_defined.xvid.fps < fpsMin || encSettings->user_defined.xvid.fps < fpsMax )
	{
		encSettings->user_defined.xvid.fps = track->fps;
	}

	/*experimental
#ifdef win32
	setMinimumSize( 360, 520 );
#else
	setMinimumSize( 360, 620 );
#endif
*/

	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();
	qualityTab = new QWidget();
	profileTab = new QWidget();

	tabWidget->addTab(mainTab, QString());
	tabWidget->addTab(profileTab, QString());
	tabWidget->addTab(qualityTab, QString());

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

	tabWidget->setCurrentIndex(0);

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

	// - Pass
	passHLayout = new QHBoxLayout();
	passLabel = new QLabel(mainTab);
	passComboBox = new QComboBox(mainTab);
	passLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	passComboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);

	passHLayout->addWidget(passLabel);
	passHLayout->addWidget(passComboBox);

	mainTabVLayout->addLayout(passHLayout);

	// - Main settings
	mainGroupBox = new QGroupBox(mainTab);
	mainVLayout = new QVBoxLayout(mainGroupBox);
	mainGrid1GLayout = new QGridLayout();
	mainGrid2GLayout = new QGridLayout();
	
	mainVLayout->addLayout(mainGrid1GLayout);
	mainVLayout->addLayout(mainGrid2GLayout);
	
	mainTabVLayout->addWidget(mainGroupBox);

	// -- Profile selection
	profileHLayout = new QHBoxLayout();
	profileLabel = new QLabel(mainGroupBox);
	profileComboBox = new QComboBox(mainGroupBox);
	horizontalSpacer_12 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

	profileLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	profileComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);

	mainGrid1GLayout->addWidget(profileLabel,		0, 0);
	mainGrid1GLayout->addWidget(profileComboBox,	0, 1);

	// -- Quality presets
	presetHLayout = new QHBoxLayout();
	presetLabel = new QLabel(mainGroupBox);
	presetComboBox = new QComboBox(mainGroupBox);
	horizontalSpacer_9 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

	presetLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	presetComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);

	mainGrid1GLayout->addWidget(presetLabel,		1, 0);
	mainGrid1GLayout->addWidget(presetComboBox,		1, 1);
	
	// -- Rez
	rezLabel = new QLabel(mainGroupBox);
	rezComboBox = new QComboBox(mainGroupBox);
	horizontalSpacer_10 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

	rezLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	rezComboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);

	mainGrid2GLayout->addWidget(rezLabel,			0, 0);
	mainGrid2GLayout->addWidget(rezComboBox,		0, 1);
	mainGrid2GLayout->addItem(horizontalSpacer_10,	0, 2);

	// -- Framerate
	fpsLabel = new QLabel(mainGroupBox);
	fpsComboBox = new QComboBox(mainGroupBox);
	horizontalSpacer_11 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

	fpsLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	fpsComboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);

	mainGrid2GLayout->addWidget(fpsLabel,			1, 0);
	mainGrid2GLayout->addWidget(fpsComboBox,		1, 1);
	mainGrid2GLayout->addItem(horizontalSpacer_11,	1, 2);

	// - Encoding mode
	enc_modeGroupBox = new QGroupBox(mainTab);
	enc_modeVLayout = new QVBoxLayout(enc_modeGroupBox);

	mainTabVLayout->addWidget(enc_modeGroupBox);

	// -- Bitrate
	bitrateHLayout = new QHBoxLayout();
	bitrateRadioButton = new QRadioButton(enc_modeGroupBox);
	bitrateSlider = new QSlider(enc_modeGroupBox);
	bitrateLineEdit = new QLineEdit(enc_modeGroupBox);

	bitrateSlider->setOrientation(Qt::Horizontal);
	bitrateSlider->setInvertedAppearance(false);
	bitrateSlider->setTickPosition(QSlider::NoTicks);

	bitrateSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
	bitrateLineEdit->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	bitrateLineEdit->setMaxLength(6);
	bitrateLineEdit->setMaximumWidth(50);

	bitrateHLayout->addWidget(bitrateRadioButton);
	bitrateHLayout->addWidget(bitrateSlider);
	bitrateHLayout->addWidget(bitrateLineEdit);

	enc_modeVLayout->addLayout(bitrateHLayout);

	// -- Constant quality
	quantizerHLayout = new QHBoxLayout();
	quantizerRadioButton = new QRadioButton(enc_modeGroupBox);
	quantizerSlider = new QSlider(enc_modeGroupBox);
	quantizerLabel = new QLabel(enc_modeGroupBox);

	quantizerSlider->setOrientation(Qt::Horizontal);
	quantizerSlider->setInvertedAppearance(false);
	quantizerSlider->setTickPosition(QSlider::NoTicks);
	quantizerSlider->setRange( 1, 31 );
	quantizerSlider->setInvertedAppearance(true);
	quantizerSlider->setInvertedControls(true);

	quantizerSlider->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

	quantizerHLayout->addWidget(quantizerRadioButton);
	quantizerHLayout->addWidget(quantizerSlider);
	quantizerHLayout->addWidget(quantizerLabel);

	enc_modeVLayout->addLayout(quantizerHLayout);

	// -- Target size
	targetHLayout = new QHBoxLayout();
	targetRadioButton = new QRadioButton(enc_modeGroupBox);
	targetComboBox = new QComboBox(enc_modeGroupBox);
	horizontalSpacer_7 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

	targetHLayout->addWidget(targetRadioButton);
	targetHLayout->addWidget(targetComboBox);
	targetHLayout->addItem(horizontalSpacer_7);

	enc_modeVLayout->addLayout(targetHLayout);

	// - Cartoon and turbo
	moreOptionsGroup = new QGroupBox(mainTab);
	moreOptionsVLayout = new QVBoxLayout(moreOptionsGroup);

	cartoonCheckBox = new QCheckBox(moreOptionsGroup);
	turboCheckBox = new QCheckBox(moreOptionsGroup);

	moreOptionsVLayout->addWidget(cartoonCheckBox);
	moreOptionsVLayout->addWidget(turboCheckBox);

	mainTabVLayout->addWidget(moreOptionsGroup);

	// - Spacer
	verticalSpacer = new QSpacerItem(20, 30, QSizePolicy::Minimum, QSizePolicy::Expanding);

	mainTabVLayout->addItem(verticalSpacer);

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

	defaultHLayout->setSizeConstraint(QLayout::SetMaximumSize);
	defaultHLayout->addWidget(defaultPushButton);
	defaultHLayout->addItem(horizontalSpacer);

	mainTabVLayout->addLayout(defaultHLayout);

	// Profile tab
	profileVLayout = new QVBoxLayout(profileTab);
	profileVLayout->setSpacing(12);

	// - Profile choice
	profileDetailsHLayout = new QHBoxLayout();
	profileLabel_2 = new QLabel();
	profileComboBox_2 = new QComboBox();
	profileLabel_2->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	profileComboBox_2->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);

	profileDetailsHLayout->addWidget(profileLabel_2);
	profileDetailsHLayout->addWidget(profileComboBox_2);

	profileVLayout->addLayout(profileDetailsHLayout);

	// - Profile resrictions
	profileDetailsGroupBox = new QGroupBox(profileTab);
	profileDetailsVLayout = new QVBoxLayout(profileDetailsGroupBox);

	profileVLayout->addWidget(profileDetailsGroupBox);

	// -- Rez / Framerate
	rezfps_2GLayout = new QGridLayout();

	rezLabel_2 = new QLabel(profileDetailsGroupBox);
	rezComboBox_2 = new QComboBox(profileDetailsGroupBox);
	horizontalSpacer_13 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

	rezLabel_2->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	rezComboBox_2->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);

	rezfps_2GLayout->addWidget(rezLabel_2,			0, 0);
	rezfps_2GLayout->addWidget(rezComboBox_2,		0, 1);
	rezfps_2GLayout->addItem(horizontalSpacer_13,	0, 2);

	fpsLabel_2 = new QLabel(profileDetailsGroupBox);
	fpsComboBox_2 = new QComboBox(profileDetailsGroupBox);
	horizontalSpacer_14 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

	fpsLabel_2->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	fpsComboBox_2->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);

	rezfps_2GLayout->addWidget(fpsLabel_2,			1, 0);
	rezfps_2GLayout->addWidget(fpsComboBox_2,		1, 1);
	rezfps_2GLayout->addItem(horizontalSpacer_14,	1, 2);

	profileDetailsVLayout->addLayout(rezfps_2GLayout);

	// -- Details checkboxes
	lumi_maskCheckBox = new QCheckBox(profileDetailsGroupBox);
	interlacingCheckBox = new QCheckBox(profileDetailsGroupBox);
	qpelCheckBox = new QCheckBox(profileDetailsGroupBox);
	gmcCheckBox = new QCheckBox(profileDetailsGroupBox);

	profileDetailsVLayout->addWidget(lumi_maskCheckBox);
	profileDetailsVLayout->addWidget(interlacingCheckBox);
	profileDetailsVLayout->addWidget(qpelCheckBox);
	profileDetailsVLayout->addWidget(gmcCheckBox);

	quant_typeHLayout = new QHBoxLayout();
	quant_typeLabel = new QLabel(profileDetailsGroupBox);
	quant_typeComboBox = new QComboBox(profileDetailsGroupBox);
	horizontalSpacer_5 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
	quant_typeHLayout->addWidget(quant_typeLabel);
	quant_typeHLayout->addWidget(quant_typeComboBox);
	quant_typeHLayout->addItem(horizontalSpacer_5);

	profileDetailsVLayout->addLayout(quant_typeHLayout);

	fourccHLayout = new QHBoxLayout();
	fourccLabel = new QLabel(profileDetailsGroupBox);
	fourccComboBox = new QComboBox(profileDetailsGroupBox);
	horizontalSpacer_7 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
	fourccHLayout->addWidget(fourccLabel);
	fourccHLayout->addWidget(fourccComboBox);
	fourccHLayout->addItem(horizontalSpacer_7);

	profileDetailsVLayout->addLayout(fourccHLayout);

	// - B-VOPs
	bvopGroupBox = new QGroupBox(profileTab);
	bvopGroupBox->setCheckable(true);
	bvopVLayout = new QVBoxLayout(bvopGroupBox);

	profileVLayout->addWidget(bvopGroupBox);

	// -- Details in HLayouts
	max_bframesHLayout = new QHBoxLayout();
	max_bframesLineEdit = new QLineEdit(bvopGroupBox);
	max_bframesLabel = new QLabel(bvopGroupBox);
	horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
	max_bframesHLayout->addWidget(max_bframesLineEdit);
	max_bframesHLayout->addWidget(max_bframesLabel);
	max_bframesHLayout->addItem(horizontalSpacer_2);
	max_bframesLineEdit->setMaxLength(6);
	max_bframesLineEdit->setMaximumWidth(50);

	bvopVLayout->addLayout(max_bframesHLayout);

	bquant_ratioHLayout = new QHBoxLayout();
	bquant_ratioLineEdit = new QLineEdit(bvopGroupBox);
	bquant_ratioLabel = new QLabel(bvopGroupBox);
	horizontalSpacer_3 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
	bquant_ratioHLayout->addWidget(bquant_ratioLineEdit);
	bquant_ratioHLayout->addWidget(bquant_ratioLabel);
	bquant_ratioHLayout->addItem(horizontalSpacer_3);
	bquant_ratioLineEdit->setMaxLength(6);
	bquant_ratioLineEdit->setMaximumWidth(50);

	bvopVLayout->addLayout(bquant_ratioHLayout);

	bquant_offsetHLayout = new QHBoxLayout();
	bquant_offsetLineEdit = new QLineEdit(bvopGroupBox);
	bquant_offsetLabel = new QLabel(bvopGroupBox);
	horizontalSpacer_4 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
	bquant_offsetHLayout->addWidget(bquant_offsetLineEdit);
	bquant_offsetHLayout->addWidget(bquant_offsetLabel);
	bquant_offsetHLayout->addItem(horizontalSpacer_4);
	bquant_offsetLineEdit->setMaxLength(6);
	bquant_offsetLineEdit->setMaximumWidth(50);

	bvopVLayout->addLayout(bquant_offsetHLayout);

	// - Spacer
	verticalSpacer_2 = new QSpacerItem(20, 30, QSizePolicy::Minimum, QSizePolicy::Expanding);

	profileVLayout->addItem(verticalSpacer_2);

	// - Default profile button
	profileDefaultHLayout = new QHBoxLayout();
	profileDefaultPushButton = new QPushButton(profileTab);
	horizontalSpacer_6 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

	profileDefaultHLayout->setSizeConstraint(QLayout::SetMaximumSize);
	profileDefaultHLayout->addWidget(profileDefaultPushButton);
	profileDefaultHLayout->addItem(horizontalSpacer_6);

	profileVLayout->addLayout(profileDefaultHLayout);

	// Quality tab
	qualityTabVLayout = new QVBoxLayout(qualityTab);
	qualityTabVLayout->setSpacing(12);

	// - Quality choice
	presetHLayout_2 = new QHBoxLayout();
	presetLabel_2 = new QLabel();
	presetComboBox_2 = new QComboBox();
	presetLabel_2->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	presetComboBox_2->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);

	presetHLayout_2->addWidget(presetLabel_2);
	presetHLayout_2->addWidget(presetComboBox_2);

	qualityTabVLayout->addLayout(presetHLayout_2);

	// - Activable quality controls
	qualityGroup = new QGroupBox(qualityTab);
	qualityVLayout = new QVBoxLayout(qualityGroup);

	qualityTabVLayout->addWidget(qualityGroup);

	// -- Activable lists
	me_qualityHLayout = new QHBoxLayout();
	me_qualityComboBox = new QComboBox(qualityGroup);
	me_qualityLabel = new QLabel(qualityGroup);
	me_qualityHLayout->addWidget(me_qualityComboBox);
	me_qualityHLayout->addWidget(me_qualityLabel);

	qualityVLayout->addLayout(me_qualityHLayout);

	vhqHLayout = new QHBoxLayout();
	vhqComboBox = new QComboBox(qualityGroup);
	vhqLabel = new QLabel(qualityGroup);
	vhqHLayout->addWidget(vhqComboBox);
	vhqHLayout->addWidget(vhqLabel);

	qualityVLayout->addLayout(vhqHLayout);

	// -- Activable checkboxes
	bvhqCheckBox = new QCheckBox(qualityGroup);
	hq_acCheckBox = new QCheckBox(qualityGroup);
	chroma_meCheckBox = new QCheckBox(qualityGroup);
	chroma_optCheckBox = new QCheckBox(qualityGroup);
	trellisCheckBox = new QCheckBox(qualityGroup);

	qualityVLayout->addWidget(bvhqCheckBox);
	qualityVLayout->addWidget(hq_acCheckBox);
	qualityVLayout->addWidget(chroma_meCheckBox);
	qualityVLayout->addWidget(chroma_optCheckBox);
	qualityVLayout->addWidget(trellisCheckBox);

	// - Spacer
	verticalSpacer_3 = new QSpacerItem(20, 30, QSizePolicy::Minimum, QSizePolicy::Expanding);
	qualityTabVLayout->addItem(verticalSpacer_3);

	// - Default preset button
	presetDefaultHLayout = new QHBoxLayout();
	presetDefaultPushButton = new QPushButton(qualityTab);
	horizontalSpacer_6 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

	presetDefaultHLayout->setSizeConstraint(QLayout::SetMaximumSize);
	presetDefaultHLayout->addWidget(presetDefaultPushButton);
	presetDefaultHLayout->addItem(horizontalSpacer_6);

	qualityTabVLayout->addLayout(presetDefaultHLayout);

	// Captions
	setWindowTitle( tr("Xvid encoder settings") );

	tabWidget->setTabText(tabWidget->indexOf(mainTab),  tr("General") );
	tabWidget->setTabText(tabWidget->indexOf(qualityTab),  tr("Quality") );
	tabWidget->setTabText(tabWidget->indexOf(profileTab),  tr("Profile") );

	enc_modeGroupBox->setTitle( tr(" Encoding mode ") );
	moreOptionsGroup->setTitle( QString() );

	passLabel->setText( tr("Pass:") );
	bitrateRadioButton->setText( tr("Bitrate CBR:") );
	quantizerRadioButton->setText( tr("Quantizer: (smallest file)") );
	quantizerLabel->setText( tr("(maximum quality)") );
	targetRadioButton->setText( tr("Output target:") );

	mainGroupBox->setTitle( QString() );
	profileLabel->setText( tr("Profile:") );
	rezLabel->setText( tr("Picture:") );
	fpsLabel->setText( tr("Frame rate:") );
	presetLabel->setText( tr("Quality:") );

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

	qualityGroup->setTitle( QString() );
	presetLabel_2->setText( tr("Preset:") );
	me_qualityLabel->setText( tr("Motion search precision") );
	vhqLabel->setText( tr("VHQ mode") );

	bvhqCheckBox->setText( tr("VHQ mode for B-Frames") );
	hq_acCheckBox->setText( tr("Better coefficient cost estimation method") );
	chroma_meCheckBox->setText( tr("Also use color information for motion estimation") );
	chroma_optCheckBox->setText( tr("Chroma optimization pre-filter") );
	trellisCheckBox->setText( tr("Quantization process optimization") );

	presetDefaultPushButton->setText( tr("Default quality settings") );

	cartoonCheckBox->setText( tr("Encode cartoon content") );
	turboCheckBox->setText( tr("Turbo first-pass encoding") );

	profileLabel_2->setText( tr(" Profile:") );

	rezLabel_2->setText( tr("Picture:") );
	fpsLabel_2->setText( tr("Frame rate:") );
	profileDetailsGroupBox->setTitle( QString() );
	lumi_maskCheckBox->setText( tr("Adaptive quantization") );
	interlacingCheckBox->setText( tr("Interlacing video") );
	qpelCheckBox->setText( tr("Quarter pixel") );
	gmcCheckBox->setText( tr("Global motion compensation") );
	quant_typeLabel->setText( tr(" Quantization type:") );
	fourccLabel->setText( tr(" FourCC:") );

	bvopGroupBox->setTitle( tr(" B-VOPs ") );
	max_bframesLabel->setText( tr("Max consecutive BVOPs") );
	bquant_ratioLabel->setText( tr("Quantizer ratio") );
	bquant_offsetLabel->setText( tr("Quantizer offset") );

	profileDefaultPushButton->setText( tr("Default profile settings") );

	bitrateValidator = new QIntValidator( this );
	bitrateLineEdit->setValidator( bitrateValidator );
	bitrateValidator->setRange( bitrateMin, bitrateMax );

	setToolTips();

	setAttribute(Qt::WA_AlwaysShowToolTips);

	populatePass();
	populateTarget();
	populateRez(rezComboBox);
	populateRez(rezComboBox_2);
	populateFps(fpsComboBox);
	populateFps(fpsComboBox_2);
	populatePreset( presetComboBox );
	populatePreset( presetComboBox_2 );
	populateProfiles( profileComboBox, encSettings->user_defined.xvid.profile_set );
	populateProfiles( profileComboBox_2, encSettings->user_defined.xvid.profile_set );
	populateQuantizationTypes( quant_typeComboBox );
	populateFourcc();
	populateMe_quality( me_qualityComboBox );
	populateVhq( vhqComboBox, encSettings->xvid.vhq );

	// Tab mirroring connections
	connect(profileComboBox, SIGNAL(currentIndexChanged(int)), 
		profileComboBox_2, SLOT(setCurrentIndex(int)));
	connect(profileComboBox_2, SIGNAL(currentIndexChanged(int)), 
		profileComboBox, SLOT(setCurrentIndex(int)));

	connect(presetComboBox, SIGNAL(currentIndexChanged(int)), 
		presetComboBox_2, SLOT(setCurrentIndex(int)));
	connect(presetComboBox_2, SIGNAL(currentIndexChanged(int)), 
		presetComboBox, SLOT(setCurrentIndex(int)));

	connect(rezComboBox, SIGNAL(currentIndexChanged(int)), 
		rezComboBox_2, SLOT(setCurrentIndex(int)));
	connect(rezComboBox_2, SIGNAL(currentIndexChanged(int)), 
		rezComboBox, SLOT(setCurrentIndex(int)));

	connect(fpsComboBox, SIGNAL(currentIndexChanged(int)), 
		fpsComboBox_2, SLOT(setCurrentIndex(int)));
	connect(fpsComboBox_2, SIGNAL(currentIndexChanged(int)), 
		fpsComboBox, SLOT(setCurrentIndex(int)));

	updateControls();

	// Processing connections
	connect(passComboBox, SIGNAL(currentIndexChanged(int)), 
		this, SLOT(selectPass(int)));

	connect(profileComboBox, SIGNAL(currentIndexChanged(int)), 
		this, SLOT(selectProfile(int)));
	connect(presetComboBox, SIGNAL(currentIndexChanged(int)), 
		this, SLOT(selectPreset(int)));
	connect(rezComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectRez(int)));
	connect(fpsComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectFps(int)));

	connect(bitrateRadioButton, SIGNAL(toggled(bool)), 
		this, SLOT(bitrateRadioButtonToggled(bool)));
	connect(bitrateSlider, SIGNAL(valueChanged(int)), 
		this, SLOT(bitrateSliderChanged(int)));
	connect(bitrateLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(bitrateLineEditChanged(QString)));

	connect(quantizerRadioButton, SIGNAL(toggled(bool)), 
		this, SLOT(quantizerRadioButtonToggled(bool)));
	connect(quantizerSlider, SIGNAL(valueChanged(int)), 
		this, SLOT(quantizerSliderChanged(int)));

	connect(targetRadioButton, SIGNAL(toggled(bool)), 
		this, SLOT(targetRadioButtonToggled(bool)));
	connect(targetComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectTarget(int)));
	
	connect(cartoonCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(cartoonCheckBoxToggled(bool)));	
	connect(turboCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(turboCheckBoxToggled(bool)));

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

	connect(profileComboBox_2, SIGNAL(currentIndexChanged(int)), 
		this, SLOT(selectProfile(int)));

	connect(lumi_maskCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(updateProfileSettings()));
	connect(interlacingCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(updateProfileSettings()));
	connect(qpelCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(updateProfileSettings()));
	connect(gmcCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(updateProfileSettings()));
	connect(quant_typeComboBox, SIGNAL(currentIndexChanged(int)), 
		this, SLOT(updateProfileSettings()));
	connect(fourccComboBox, SIGNAL(currentIndexChanged(int)), 
		this, SLOT(updateProfileSettings()));

	connect(bvopGroupBox, SIGNAL(clicked(bool)), 
		this, SLOT(bvopCheckBoxClicked(bool)));
	connect(max_bframesLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(updateProfileSettings()));
	connect(bquant_ratioLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(updateProfileSettings()));
	connect(bquant_offsetLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(updateProfileSettings()));

	connect(profileDefaultPushButton, SIGNAL(clicked()), 
		this, SLOT(profileDefaultPushButtonClicked()));

	connect(presetComboBox_2, SIGNAL(currentIndexChanged(int)), 
		this, SLOT(selectPreset(int)));

	connect(me_qualityComboBox, SIGNAL(currentIndexChanged(int)), 
		this, SLOT(updateQualitySettings()));
	connect(vhqComboBox, SIGNAL(currentIndexChanged(int)), 
		this, SLOT(updateQualitySettings()));
	connect(bvhqCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(updateQualitySettings()));
	connect(hq_acCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(updateQualitySettings()));
	connect(chroma_meCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(updateQualitySettings()));
	connect(chroma_optCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(updateQualitySettings()));
	connect(trellisCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(updateQualitySettings()));

	connect(presetDefaultPushButton, SIGNAL(clicked()), 
		this, SLOT(presetDefaultPushButtonClicked()));

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

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

	QDialog::accept();
}

void ConfVidXvidMEncoderDialog::bitrateRadioButtonToggled(bool checked)
{
	if (checked)
	{
		encSettings->xvid.method = 0;
	}

	updateControls();
}

void ConfVidXvidMEncoderDialog::quantizerRadioButtonToggled(bool checked)
{
	if (checked)
	{
		encSettings->xvid.method = 1;
	}

	updateControls();
}

void ConfVidXvidMEncoderDialog::targetRadioButtonToggled(bool checked)
{
	if (checked)
	{
		encSettings->xvid.method = 2;
	}

	updateControls();
}

void ConfVidXvidMEncoderDialog::applyProfileOnControls(const int profile)
{
	QString str;
	int index;

	// Update profile related settings from controls current state
	interlacingCheckBox->setChecked( encSettings->xvid.interlacing );
	lumi_maskCheckBox->setChecked( encSettings->xvid.lumi_mask );
	qpelCheckBox->setChecked( encSettings->xvid.qpel );
	gmcCheckBox->setChecked( encSettings->xvid.gmc );
	max_bframesLineEdit->setText( QString::number(encSettings->xvid.max_bframes) );
	bquant_ratioLineEdit->setText( str.sprintf("%.2f", (double)encSettings->xvid.bquant_ratio / 100.0) );
	bquant_offsetLineEdit->setText( str.sprintf("%.2f", (double)encSettings->xvid.bquant_offset / 100.0) );
	quant_typeComboBox->setCurrentIndex( quant_typeComboBox->findData( encSettings->xvid.quant_type ) );
	fourccComboBox->setCurrentIndex( fourccComboBox->findData( encSettings->xvid.fourcc ) );

	// Enable/disable profile controls
	switch ( profile )
	{
	case DivxHandheld:	// Handheld
	case DivxPortableNtsc:
	case DivxPortablePal:
		lumi_maskCheckBox->setEnabled(true);
		interlacingCheckBox->setEnabled(false);
		qpelCheckBox->setEnabled(false);
		gmcCheckBox->setEnabled(false);
		bvopGroupBox->setEnabled(false);
		quant_typeComboBox->setCurrentIndex(encSettings->xvid.quant_type);
		quant_typeComboBox->setEnabled(false);
		break;
	case DivxHomeTheatreNtsc:
	case DivxHomeTheatrePal:
	case DivxHighDef:	// HDTV
		lumi_maskCheckBox->setEnabled(true);
		interlacingCheckBox->setEnabled(true);
		qpelCheckBox->setEnabled(false);
		gmcCheckBox->setEnabled(false);
		bvopGroupBox->setEnabled(false);
		quant_typeComboBox->setCurrentIndex(encSettings->xvid.quant_type);
		quant_typeComboBox->setEnabled(false);
		break;
	case Simple0:
	case Simple1:
	case Simple2:
	case Simple3:
		lumi_maskCheckBox->setEnabled(false);
		interlacingCheckBox->setEnabled(false);
		qpelCheckBox->setEnabled(false);
		gmcCheckBox->setEnabled(false);
		bvopGroupBox->setEnabled(false);
		quant_typeComboBox->setCurrentIndex(encSettings->xvid.quant_type);
		quant_typeComboBox->setEnabled(false);
		break;
	case AdvancedSimple0:
	case AdvancedSimple1:
	case AdvancedSimple2:
	case AdvancedSimple3:
	case AdvancedSimple4:
	case AdvancedSimple5:
		lumi_maskCheckBox->setEnabled(true);
		interlacingCheckBox->setEnabled(true);
		qpelCheckBox->setEnabled(true);
		gmcCheckBox->setEnabled(true);
		bvopGroupBox->setEnabled(true);
		quant_typeComboBox->setCurrentIndex(encSettings->xvid.quant_type);
		quant_typeComboBox->setEnabled(true);
		break;
	case Free:
	default:
		lumi_maskCheckBox->setEnabled(true);
		interlacingCheckBox->setEnabled(true);
		qpelCheckBox->setEnabled(true);
		gmcCheckBox->setEnabled(true);
		bvopGroupBox->setEnabled(true);
		quant_typeComboBox->setCurrentIndex(encSettings->xvid.quant_type);
		quant_typeComboBox->setEnabled(true);
		break;
	}

	// B-VOPs
	bool bvop = (encSettings->xvid.max_bframes != 0);

	bvopGroupBox->setChecked(bvop);
	max_bframesLineEdit->setEnabled(bvop);
	bquant_ratioLineEdit->setEnabled(bvop);
	bquant_offsetLineEdit->setEnabled(bvop);

	// Re-populate and set rez with profile one
	populateRez(rezComboBox);
	populateRez(rezComboBox_2);
	QSize size( encSettings->xvid.resX, encSettings->xvid.resY );
	index = rezComboBox->findData( size );
	if (index == -1)
	{
		// Missing current rez in list is forced to be the user defined (item #1)
		index = 1;
		rezComboBox->setItemText( index, tr("%1x%2 (user defined)")
			.arg(size.width())
			.arg(size.height()));
		rezComboBox->setItemData( index, size);
	}
	rezComboBox->setCurrentIndex( index );

	// Set FPS with profile one
	populateFps(fpsComboBox);
	populateFps(fpsComboBox_2);
	index = fpsComboBox->findData( encSettings->xvid.fps );
	if (index == -1)
	{
		// Missing current fps in list is forced to be the user defined (item #1)
		index = 1;
		fpsComboBox->setItemText( index, tr("%1 FPS (user defined)")
			.arg(encSettings->xvid.fps));
		fpsComboBox->setItemData( index, encSettings->xvid.fps);
	}
	fpsComboBox->setCurrentIndex( index );

	// Bitrate
	switch ( profile )
	{
	case DivxHandheld:	// Handheld
		setMaxBitrate( 1334 );
		break;
	case DivxPortableNtsc:
	case DivxPortablePal:
	case DivxHomeTheatreNtsc:
	case DivxHomeTheatrePal:
		setMaxBitrate( 4854);
		bitrateSlider->setMaximum( 4854 );
		bitrateValidator->setTop( 4854 );
		break;
	case DivxHighDef:	// HDTV
		setMaxBitrate( 9708 );
		break;
	case Simple0:
	case Simple1:
		setMaxBitrate( 64 );
		break;
	case Simple2:
		setMaxBitrate( 128 );
		break;
	case Simple3:
		setMaxBitrate( 384 );
		break;
	case AdvancedSimple0:
	case AdvancedSimple1:
		setMaxBitrate( 128 );
		break;
	case AdvancedSimple2:
		setMaxBitrate( 384 );
		break;
	case AdvancedSimple3:
		setMaxBitrate( 768 );
		break;
	case AdvancedSimple4:
		setMaxBitrate( 3000 );
		break;
	case AdvancedSimple5:
		setMaxBitrate( 8000 );
		break;
	case SmallSmartphones:
		setMaxBitrate( 1000 );
		break;
	case LargeSmartphones:
		setMaxBitrate( 1500 );
		break;
	case Apple_iPhone:
	case Apple_iPad:
		setMaxBitrate( 1500 );
		break;
	case Apple_MacBook13:
	case Apple_MacBook15:
	case Apple_MacBook17:
		setMaxBitrate( 4000 );
		break;
	case Apple_iMac:
		setMaxBitrate( 9708 );
	case Archos7:
	case Archos10:
		setMaxBitrate( 1500 );
		break;
	case BlackBerry:
		setMaxBitrate( 1500 );
		break;
	case Netbook10:
	case Netbook12:
	case Laptop13:
	case Laptop15:
	case Laptop17:
		setMaxBitrate( 4000 );
		break;
	case Desktop:
		setMaxBitrate( 9708 );
		break;
	case Free:
	default:
		setMaxBitrate( bitrateMax );
	}

	if ( encSettings->xvid.method == 1 )
	{
		// Quantizer
		quantizerRadioButton->setChecked(true);

		bitrateSlider->setDisabled(true);
		bitrateLineEdit->setDisabled(true);
		quantizerSlider->setEnabled(true);
	}
	else if ( encSettings->xvid.method == 0 )
	{
		// CBR by default
		bitrateRadioButton->setChecked(true);

		bitrateSlider->setEnabled(true);
		bitrateLineEdit->setEnabled(true);
		quantizerSlider->setDisabled(true);
	}

	bitrateSlider->setValue( encSettings->xvid.bitrate );
	quantizerSlider->setValue( encSettings->xvid.fixed_quant );
}

void ConfVidXvidMEncoderDialog::applyProfileOnSettings(const int profile)
{
	double fps = videoSource->fps;						// input fps ?? 23.976, 29.97
	int interlaced = videoSource->interlaced;		// source
	bool assumingPAL = (videoSource->fps == 25.0) || (videoSource->fps == 50.0);
	bool assumingNTSC = (videoSource->fps == 23.976) || (videoSource->fps == 24.0)
						 || (videoSource->fps == 29.97) || (videoSource->fps == 30.0)
						 || (videoSource->fps == 60.0);
	bool assumingWide = (videoSource->display_aspect > 1.70);

	// Default bitrate, rezolution and FPS
	switch ( profile )
	{
	case DivxHandheld:	// Handheld
		encSettings->xvid.resX = 176;
		encSettings->xvid.resY = 144;
		encSettings->xvid.fps = 15.0;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 200;
		break;
	case DivxPortableNtsc:
		encSettings->xvid.resX = 640;
		encSettings->xvid.resY = 480;
		encSettings->xvid.fps = assumingNTSC ? fps : 30.0;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 768;
		break;
	case DivxPortablePal:
		encSettings->xvid.resX = 640;
		encSettings->xvid.resY = 480;
		encSettings->xvid.fps = assumingPAL ? fps : 25.0;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 768;
		break;
	case DivxHomeTheatreNtsc:
		encSettings->xvid.resX = 720;
		encSettings->xvid.resY = 480;
		encSettings->xvid.fps = assumingNTSC ? fps : 30.0;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 4000;
		break;
	case DivxHomeTheatrePal:
		encSettings->xvid.resX = 720;
		encSettings->xvid.resY = 576;
		encSettings->xvid.fps = assumingPAL ? fps : 25.0;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 4000;
		break;
	case DivxHighDef:	// HDTV
		encSettings->xvid.resX = 1280;
		encSettings->xvid.resY = 720;
		encSettings->xvid.fps = assumingNTSC || assumingPAL ? fps : 30.0;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 8000;
		break;
	case Simple0:
	case Simple1:
		encSettings->xvid.resX = 176;
		encSettings->xvid.resY = 144;
		encSettings->xvid.fps = 15.0;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 64;
		break;
	case Simple2:
		encSettings->xvid.resX = 352;
		encSettings->xvid.resY = assumingPAL ? 288 : 240;
		encSettings->xvid.fps = 15.0;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 128;
		break;
	case Simple3:
		encSettings->xvid.resX = 352;
		encSettings->xvid.resY = assumingPAL ? 288 : 240;
		encSettings->xvid.fps = 15.0;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 384;
		break;
	case AdvancedSimple0:
		encSettings->xvid.resX = 176;
		encSettings->xvid.resY = 144;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 128;
		break;
	case AdvancedSimple1:
		encSettings->xvid.resX = 176;
		encSettings->xvid.resY = 144;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 128;
		break;
	case AdvancedSimple2:
		encSettings->xvid.resX = 176;
		encSettings->xvid.resY = 144;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 384;
		break;
	case AdvancedSimple3:
		encSettings->xvid.resX = 352;
		encSettings->xvid.resY = assumingPAL ? 288 : 240;
		encSettings->xvid.fps = assumingNTSC || assumingPAL ? fps : 25.0;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 768;
		break;
	case AdvancedSimple4:
		encSettings->xvid.resX = 352;
		encSettings->xvid.resY = assumingPAL ? 576 : 480;
		encSettings->xvid.fps = assumingNTSC || assumingPAL ? fps : 25.0;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 3000;
		break;
	case AdvancedSimple5:
		encSettings->xvid.resX = 720;
		encSettings->xvid.resY = assumingPAL ? 576 : 480;
		encSettings->xvid.fps = assumingNTSC || assumingPAL ? fps : 25.0;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 8000;
		break;
	case SmallSmartphones:
		encSettings->xvid.resX = 320;		// 240x260, 240x320, 320x480
		encSettings->xvid.resY = 240;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 350;	// max 1000
		break;
	case LargeSmartphones:
		encSettings->xvid.resX = 400;		// 480x800
		encSettings->xvid.resY = 225;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 450;	// max 1500
		break;
	case Apple_iPhone:
		encSettings->xvid.resX = assumingWide ? 640 : 512;		// 480x320 960x640
		encSettings->xvid.resY = assumingWide ? 360 : 384;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 650;	// max 1500
		break;
	case Apple_iPad:
		encSettings->xvid.resX = assumingWide ? 640 : 512;		// 1024x768
		encSettings->xvid.resY = assumingWide ? 360 : 384;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 900;	// max 1500
		break;
	case Apple_MacBook13:
		encSettings->xvid.resX = assumingWide ? 640 : 512;		// 1 280 x 800 (native), 
			// 1 152 x 720, 1024 x 640 et 800 x 500 pixels au format 16:10 ; 
			// 1 024 x 768, 800 x 600 et 640 x 480 pixels au format 4:3 ; 
			// 1 024 x 768, 800 x 600 et 640 x 480 pixels au format 4:3 étendu ; 
			// 720 x 480 pixels au format 3:2 ; 720 x 480 pixels au format 3:2 étendu
		encSettings->xvid.resY = assumingWide ? 360 : 384;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 900;	// max 4000
		break;
	case Apple_MacBook15:
		encSettings->xvid.resX = assumingWide ? 640 : 512;		//  1 440 x 900 (native), 
			// 1 280 x 800, 1 152 x 720, 1 024 x 640 et 800 x 500 pixels au format 16:10 ;
			// 1 024 x 768, 800 x 600 et 640 x 480 pixels au format 4:3 ; 
			// 1 024 x 768, 800 x 600 et 640 x 480 pixels au format 4:3 étendu ; 
			// 720 x 480 pixels au format 3:2 ; 720 x 480 pixels au format 3:2 étendu
		encSettings->xvid.resY = assumingWide ? 360 : 384;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 1100;	// max 4000
		break;
	case Apple_MacBook17:
		encSettings->xvid.resX = assumingWide ? 960 : 720;		// 1 920 x 1 200 (native),
			// 1 680 x 1 050, 1 280 x 800, 1 152 x 720, 1 024 x 640 et 800 x 500 pixels au format 16:10 ;
			// 1 280 x 1 024 pixels au format 5:4 ; 1 280 x 1 024 pixels au format 5:4 étendu ;
			// 1 600 x 1 200, 1 024 x 768, 800 x 600 et 640 x 480 pixels au format 4:3 ; 
			// 1 600 x 1 200, 1 024 x 768, 800 x 600 et 640 x 480 pixels au format 4:3 étendu ; 
			// 720 x 480 pixels au format 3:2 ; 720 x 480 pixels au format 3:2 étendu
		encSettings->xvid.resY =  assumingWide ? 540 : 540;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 1;
		encSettings->xvid.fixed_quant = 4;
		break;
	case Apple_iMac:
		encSettings->xvid.resX = assumingWide ? 960 : 720;		// Toshiba 1280x800
		encSettings->xvid.resY = assumingWide ? 540 : 540;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 1;
		encSettings->xvid.fixed_quant = 4;
		break;
	case Archos7:
		encSettings->xvid.resX = assumingWide ? 640 : 512;		// 800x480 (720p)
		encSettings->xvid.resY = assumingWide ? 360 : 384;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 900;	// max 1500
		break;
	case Archos10:
		encSettings->xvid.resX = assumingWide ? 640 : 512;		// 1024x600 (720p)
		encSettings->xvid.resY = assumingWide ? 360 : 384;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 900;	// max 1500
		break;
	case BlackBerry:
		encSettings->xvid.resX = 400;		// 480x320, 480x360
		encSettings->xvid.resY = 225;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 450;	// max 1500
		break;
	case Netbook10:
		encSettings->xvid.resX = assumingWide ? 640 : 512;		// 10" 1024x600
		encSettings->xvid.resY = assumingWide ? 360 : 384;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 900;	// max 4000
		break;
	case Netbook12:
		encSettings->xvid.resX = assumingWide ? 640 : 512;		// 11" 1366x768	12" 1366x768
		encSettings->xvid.resY = assumingWide ? 360 : 384;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 900;	// max 4000
		break;
	case Laptop13:
		encSettings->xvid.resX = assumingWide ? 640 : 512;		// Toshiba 1280x800
		encSettings->xvid.resY = assumingWide ? 360 : 384;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 900;	// max 4000
		break;
	case Laptop15:
		encSettings->xvid.resX = assumingWide ? 640 : 512;		// HP, Toshiba 1366x768
		encSettings->xvid.resY = assumingWide ? 360 : 384;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 0;
		encSettings->xvid.bitrate = 1100;	// max 4000
		break;
	case Laptop17:
		encSettings->xvid.resX = assumingWide ? 960 : 720;		// Asus, Toshiba 1600x900
		encSettings->xvid.resY = assumingWide ? 540 : 540;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 1;
		encSettings->xvid.fixed_quant = 4;
		break;
	case Desktop:
		encSettings->xvid.resX = assumingWide ? 960 : 720;		// Toshiba 1280x800
		encSettings->xvid.resY = assumingWide ? 540 : 540;
		encSettings->xvid.fps = fps;
		encSettings->xvid.method = 1;
		encSettings->xvid.fixed_quant = 4;
		break;
	case Free:
	default:
		break;
	}

	// Default profile details
	encSettings->xvid.lumi_mask = false;
	encSettings->xvid.qpel = false;
	encSettings->xvid.gmc = false;

	switch ( profile )
	{
	case DivxHandheld:	// Handheld
		encSettings->xvid.fourcc = "DX50";
		encSettings->xvid.interlacing = false;
		encSettings->xvid.max_bframes = 0;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = H263;
		break;
	case DivxPortableNtsc:
	case DivxPortablePal:
		encSettings->xvid.fourcc = "DX50";
		encSettings->xvid.interlacing = false;
		encSettings->xvid.max_bframes = 1;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = H263;
		break;
	case DivxHomeTheatreNtsc:
	case DivxHomeTheatrePal:
		encSettings->xvid.fourcc = "DX50";
		encSettings->xvid.interlacing = interlaced;
		encSettings->xvid.max_bframes = 1;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = H263;
		break;
	case DivxHighDef:	// HDTV
		encSettings->xvid.fourcc = "DX50";
		encSettings->xvid.interlacing = interlaced;
		encSettings->xvid.max_bframes = 2;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = H263;
		break;
	case Simple0:
	case Simple1:
	case Simple2:
	case Simple3:
		encSettings->xvid.fourcc = "XVID";
		encSettings->xvid.interlacing = false;
		encSettings->xvid.max_bframes = 0;
		encSettings->xvid.bquant_ratio = 0;
		encSettings->xvid.bquant_offset = 0;
		encSettings->xvid.quant_type = H263;
		break;
	case AdvancedSimple0:
	case AdvancedSimple1:
	case AdvancedSimple2:
	case AdvancedSimple3:
		encSettings->xvid.fourcc = "XVID";
		encSettings->xvid.interlacing = interlaced;
		encSettings->xvid.max_bframes = 2;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = MPEG;
		break;
	case AdvancedSimple4:
	case AdvancedSimple5:
		encSettings->xvid.fourcc = "XVID";
		encSettings->xvid.interlacing = interlaced;
		encSettings->xvid.max_bframes = 2;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = MPEG;
		break;
	case SmallSmartphones:
	case LargeSmartphones:
		encSettings->xvid.fourcc = "DX50";
		encSettings->xvid.interlacing = false;
		encSettings->xvid.max_bframes = 1;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = H263;
		break;
	case Apple_iPhone:
		encSettings->xvid.fourcc = "DX50";
		encSettings->xvid.interlacing = false;
		encSettings->xvid.max_bframes = 1;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = H263;
		break;
	case Apple_iPad:
	case Apple_MacBook13:
	case Apple_MacBook15:
		encSettings->xvid.fourcc = "DX50";
		encSettings->xvid.interlacing = false;
		encSettings->xvid.max_bframes = 1;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = H263;
		break;
	case Apple_MacBook17:
	case Apple_iMac:
		encSettings->xvid.fourcc = "DX50";
		encSettings->xvid.interlacing = false;
		encSettings->xvid.max_bframes = 2;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = MPEG;
		break;
	case Archos7:
	case Archos10:
		encSettings->xvid.fourcc = "DX50";
		encSettings->xvid.interlacing = false;
		encSettings->xvid.max_bframes = 2;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = H263;
		break;
	case BlackBerry:
		encSettings->xvid.fourcc = "DX50";
		encSettings->xvid.interlacing = false;
		encSettings->xvid.max_bframes = 1;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = H263;
		break;
	case Netbook10:
	case Netbook12:
		encSettings->xvid.fourcc = "XVID";
		encSettings->xvid.interlacing = false;
		encSettings->xvid.max_bframes = 1;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = H263;
		break;
	case Laptop13:
	case Laptop15:
		encSettings->xvid.fourcc = "XVID";
		encSettings->xvid.interlacing = false;
		encSettings->xvid.max_bframes = 1;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = H263;
		break;
	case Laptop17:
	case Desktop:
		encSettings->xvid.fourcc = "XVID";
		encSettings->xvid.interlacing = false;
		encSettings->xvid.max_bframes = 2;
		encSettings->xvid.bquant_ratio = 150;
		encSettings->xvid.bquant_offset = 100;
		encSettings->xvid.quant_type = MPEG;
		break;
	case Free:
	default:
		break;
	}
}

void ConfVidXvidMEncoderDialog::setMaxBitrate(const int rate)
{
	bitrateSlider->setMaximum( rate );
	bitrateValidator->setTop( rate );

	bitrateRadioButton->setToolTip( formatedTip("bitrate", QString(), 
		QString("<%1-%2>").arg(bitrateMin).arg(rate),
		QStringList( tr("Encoding mode: constant bitrate (CBR).") )
		+ QStringList( tr("Sets the bitrate to be used in  kbits/second. In two-pass mode "
		"the resulting average bitrate will be closer to the expectation.") )));

	bitrateSlider->setToolTip( bitrateRadioButton->toolTip() );
	bitrateLineEdit->setToolTip( bitrateRadioButton->toolTip() );
}


void ConfVidXvidMEncoderDialog::cartoonCheckBoxToggled(const bool checked)
{
	cartoonCheckBox->setChecked( checked );
}

void ConfVidXvidMEncoderDialog::turboCheckBoxToggled(const bool checked)
{
	turboCheckBox->setChecked( checked );
}

void ConfVidXvidMEncoderDialog::bvopCheckBoxClicked(const bool checked)
{
	if ( !checked )
	{
		encSettings->xvid.max_bframes = 0;
		encSettings->xvid.bquant_ratio = 0;
		encSettings->xvid.bquant_offset = 0;

		max_bframesLineEdit->setText( QString::number(encSettings->xvid.max_bframes) );
		QString str;
		bquant_ratioLineEdit->setText( str.sprintf("%.2f", (double)encSettings->xvid.bquant_ratio / 100.0) );
		bquant_offsetLineEdit->setText( str.sprintf("%.2f", (double)encSettings->xvid.bquant_offset / 100.0) );

		return;  // done with disabling B-VOPs
	}

	int profile = profileComboBox->itemData( profileComboBox->currentIndex() ).toInt();

	if ( encSettings->xvid.max_bframes == 0  )
	{
		// Reload B-VOPs default
		switch ( profile )
		{
		case DivxHandheld:	// Handheld
		case DivxPortableNtsc:
		case DivxPortablePal:
		case DivxHomeTheatreNtsc:
		case DivxHomeTheatrePal:
		case DivxHighDef:	// HDTV
			encSettings->xvid.max_bframes = 1;
			encSettings->xvid.bquant_ratio = 150;
			encSettings->xvid.bquant_offset = 100;
			break;
		case Simple0:
		case Simple1:
		case Simple2:
		case Simple3:
			encSettings->xvid.max_bframes = 0;
			encSettings->xvid.bquant_ratio = 0;
			encSettings->xvid.bquant_offset = 0;
			break;
		case AdvancedSimple0:
		case AdvancedSimple1:
		case AdvancedSimple2:
		case AdvancedSimple3:
		case AdvancedSimple4:
		case AdvancedSimple5:
		case Free:
		default:
			encSettings->xvid.max_bframes = 2;
			encSettings->xvid.bquant_ratio = 150;
			encSettings->xvid.bquant_offset = 100;
			break;
		}

		applyProfileOnControls(profile);
	}
}

void ConfVidXvidMEncoderDialog::profileDefaultPushButtonClicked()
{
	applyProfileOnSettings(encSettings->xvid.profile);
	applyProfileOnControls(encSettings->xvid.profile);

	return;
}

void ConfVidXvidMEncoderDialog::presetDefaultPushButtonClicked()
{
	encSettings->xvid.preset = Free;

	// Real default
	encoder_video_settings_t defaultSettings;  // default settings by construction
	encSettings->xvid.me_quality = defaultSettings.xvid.me_quality;
	encSettings->xvid.vhq = defaultSettings.xvid.vhq;
	encSettings->xvid.bvhq = defaultSettings.xvid.bvhq;
	encSettings->xvid.hq_ac = defaultSettings.xvid.hq_ac;
	encSettings->xvid.chroma_me = defaultSettings.xvid.chroma_me;
	encSettings->xvid.chroma_opt = defaultSettings.xvid.chroma_opt;
	encSettings->xvid.trellis = defaultSettings.xvid.trellis;

	applyPresetOnControls(encSettings->xvid.preset);

	return;
}

void ConfVidXvidMEncoderDialog::updateControls()
{
	// Pass
	if ( encSettings->xvid.pass == 1 )
	{
		// Two-pass - 1st pass
		bitrateRadioButton->setAutoExclusive(false);
		quantizerRadioButton->setAutoExclusive(false);
		targetRadioButton->setAutoExclusive(false);

		bitrateRadioButton->setChecked(false);
		quantizerRadioButton->setChecked(false);
		targetRadioButton->setChecked(false);

		bitrateRadioButton->setDisabled(true);
		bitrateSlider->setDisabled(true);
		bitrateLineEdit->setDisabled(true);
		quantizerRadioButton->setDisabled(true);
		quantizerSlider->setDisabled(true);
		quantizerLabel->setDisabled(true);
		targetRadioButton->setDisabled(true);
		targetComboBox->setDisabled(true);
	}
	else if ( encSettings->xvid.pass == 2 )
	{
		// Two-pass - 2nd pass
		bitrateRadioButton->setAutoExclusive(true);
		quantizerRadioButton->setAutoExclusive(true);
		targetRadioButton->setAutoExclusive(true);

		bitrateRadioButton->setEnabled(true);
		bitrateSlider->setEnabled(true);
		bitrateLineEdit->setEnabled(true);
		targetRadioButton->setEnabled(true);
		targetComboBox->setEnabled(true);
		// No quantizer mode for 2nd pass
		quantizerRadioButton->setDisabled(true);
		quantizerSlider->setDisabled(true);
		quantizerLabel->setDisabled(true);

		if ( encSettings->xvid.method == 2 )
		{
			// Target size
			targetRadioButton->setChecked(true);

			bitrateSlider->setDisabled(true);
			bitrateLineEdit->setDisabled(true);
			targetComboBox->setEnabled(true);
		}
		else
		{
			// CBR by default
			bitrateRadioButton->setChecked(true);

			bitrateSlider->setEnabled(true);
			bitrateLineEdit->setEnabled(true);
			targetComboBox->setDisabled(true);
		}
	}
	else
	{
		// Single pass
		bitrateRadioButton->setAutoExclusive(true);
		quantizerRadioButton->setAutoExclusive(true);
		targetRadioButton->setAutoExclusive(true);

		bitrateRadioButton->setEnabled(true);
		bitrateSlider->setEnabled(true);
		bitrateLineEdit->setEnabled(true);
		quantizerRadioButton->setEnabled(true);
		quantizerSlider->setEnabled(true);
		quantizerLabel->setEnabled(true);
		// No target mode for single pass
		targetRadioButton->setDisabled(true);
		targetComboBox->setDisabled(true);

		if ( encSettings->xvid.method == 1 )
		{
			// Quantizer
			quantizerRadioButton->setChecked(true);

			bitrateSlider->setDisabled(true);
			bitrateLineEdit->setDisabled(true);
			quantizerSlider->setEnabled(true);
		}
		else
		{
			// CBR by default
			bitrateRadioButton->setChecked(true);

			bitrateSlider->setEnabled(true);
			bitrateLineEdit->setEnabled(true);
			quantizerSlider->setDisabled(true);
		}
	}

	bitrateSlider->setValue( encSettings->xvid.bitrate );
	bitrateLineEdit->setText( QString::number(encSettings->xvid.bitrate) );

	quantizerSlider->setValue( encSettings->xvid.fixed_quant );
	quantizerSlider->setToolTip( QString("<b>%1</b>").arg( encSettings->xvid.fixed_quant ) );
}

void ConfVidXvidMEncoderDialog::updateSettings()
{
	encSettings->codec = "xvid";

	encSettings->xvid.bitrate = bitrateLineEdit->text().toInt();
	encSettings->xvid.fixed_quant = quantizerSlider->value();

	QVariant s = targetComboBox->itemData( targetComboBox->currentIndex() );
	split_t *split = static_cast<split_t*>( (void *)&s );
	encSettings->user_defined.xvid.split.size = split->size;
	encSettings->user_defined.xvid.split.volumes = split->volumes;

	encSettings->xvid.cartoon = cartoonCheckBox->isChecked();
	encSettings->xvid.turbo = turboCheckBox->isChecked();

	updateProfileSettings();
	updateQualitySettings();
}

void ConfVidXvidMEncoderDialog::updateQualitySettings()
{
	encSettings->xvid.preset = presetComboBox->itemData( presetComboBox->currentIndex() ).toInt();

	encSettings->xvid.me_quality = me_qualityComboBox->itemData( me_qualityComboBox->currentIndex() ).toInt();
	encSettings->xvid.vhq = vhqComboBox->itemData( vhqComboBox->currentIndex() ).toInt();
	encSettings->xvid.bvhq = bvhqCheckBox->isChecked();
	encSettings->xvid.hq_ac = hq_acCheckBox->isChecked();
	encSettings->xvid.chroma_me = chroma_meCheckBox->isChecked();
	encSettings->xvid.chroma_opt = chroma_optCheckBox->isChecked();
	encSettings->xvid.trellis = trellisCheckBox->isChecked();
}

void ConfVidXvidMEncoderDialog::updateProfileSettings()
{
	encSettings->xvid.lumi_mask = lumi_maskCheckBox->isChecked();
	encSettings->xvid.interlacing = interlacingCheckBox->isChecked();
	encSettings->xvid.qpel = qpelCheckBox->isChecked();
	encSettings->xvid.gmc = gmcCheckBox->isChecked();
	encSettings->xvid.max_bframes = max_bframesLineEdit->text().toInt();;
	encSettings->xvid.bquant_ratio = (int)bquant_ratioLineEdit->text().toDouble() * 100;
	encSettings->xvid.bquant_offset = (int)bquant_offsetLineEdit->text().toDouble() * 100;
	encSettings->xvid.quant_type = quant_typeComboBox->itemData( quant_typeComboBox->currentIndex() ).toInt();
	encSettings->xvid.fourcc = fourccComboBox->itemData( fourccComboBox->currentIndex() ).toString();
}

void ConfVidXvidMEncoderDialog::enterUserDefinedFps()
{
	double fps = encSettings->user_defined.xvid.fps;
	
	UserDefinedFpsDialog *dlg = new UserDefinedFpsDialog(&fps, fpsMin, fpsMax, this);
	int res = dlg->exec();		
	Q_CHECK_PTR(dlg);
	delete(dlg);

	if ( res == QDialog::Rejected ) 
	{
		return;
	}

	encSettings->user_defined.xvid.fps = fps;

	// Update combo box
	fpsComboBox->setItemText( 1, tr("%1 FPS (user defined)")
		.arg(fps));
	fpsComboBox->setItemData( 1, fps);

	fpsComboBox_2->setItemText( 1, tr("%1 FPS (user defined)")
		.arg(fps));
	fpsComboBox_2->setItemData( 1, fps);
}

void ConfVidXvidMEncoderDialog::enterUserDefinedSize()
{
	QSize size(encSettings->user_defined.xvid.resX, encSettings->user_defined.xvid.resY);

	QSize min = QSize(widthMin, heightMin);
	QSize max = QSize(widthMax, heightMax);

	UserDefinedRezDialog *dlg = new UserDefinedRezDialog(&size, min, max, this);
	int res = dlg->exec();		
	Q_CHECK_PTR(dlg);
	delete(dlg);

	if ( res == QDialog::Rejected ) 
	{
		return;
	}

	encSettings->user_defined.xvid.resX = size.width();
	encSettings->user_defined.xvid.resY = size.height();

	// Update combo box
	rezComboBox->setItemText( 1, tr("%1x%2 (user defined)")
		.arg(size.width())
		.arg(size.height()));
	rezComboBox->setItemData( 1, size);

	rezComboBox_2->setItemText( 1, tr("%1x%2 (user defined)")
		.arg(size.width())
		.arg(size.height()));
	rezComboBox_2->setItemData( 1, size);
}

void ConfVidXvidMEncoderDialog::enterUserDefinedTarget()
{
	split_t split(encSettings->xvid.split.size, encSettings->xvid.split.volumes);

	UserDefinedTargetDialog *dlg = new UserDefinedTargetDialog(&split, this);
	int res = dlg->exec();		
	Q_CHECK_PTR(dlg);
	delete(dlg);

	if ( res == QDialog::Rejected ) 
	{
		return;
	}

	encSettings->xvid.split = split;
		
	targetComboBox->setItemText( 0, tr("%1MB x %2 (user defined)")
		.arg(split.size)
		.arg(split.volumes) );
	targetComboBox->setItemData( 0, QPoint(split.size, split.volumes) );
}

void ConfVidXvidMEncoderDialog::populateFps(QComboBox *fpsBox)
{
	fpsBox->clear();

	// Item #0 (first must be the recommended framerate)
	double fps = Encoder::recommendsFps(encSettings, videoSource);

	encSettings->recommendation.xvid.fps = fps;

	if (fps <= 0)
	{
		fps = 15.0;
	}

	fpsBox->addItem( tr("%1 FPS (recommended)")
		.arg(fps), fps);
	
	// Item #1 (second must be the user defined)
	fps = encSettings->user_defined.xvid.fps;

	if (fps <= 0)
	{
		fps = 15.0;
	}

	encSettings->user_defined.xvid.fps = fps;

	fpsBox->addItem( tr("%1 FPS (user defined)")
		.arg(fps) , fps);

	// Build a list of most common fps 
	QList<double> commonFps;
	commonFps << 15.0
		<< 23.976
		<< 24.0
		<< 25.0
		<< 29.970
		<< 30.0;

	// Item #2... (push every common rez) 
	while (!commonFps.isEmpty() )
	{
		fps = commonFps.takeFirst();

		fpsBox->addItem( tr("%1 FPS")
			.arg(fps) , fps);
	}

	// Select current rez
	fps =  encSettings->xvid.fps;

	if (fps <= 0.0)
	{
		fps = 15.0;
	}

	encSettings->xvid.fps = fps;

	int index = fpsBox->findData( fps );

	if (index == -1)
	{
		// Missing current fps in list is forced to be the user defined (item #1)
		index = 1;
		fpsBox->setItemText( index, tr("%1 FPS (user defined)")
			.arg(fps));
		fpsBox->setItemData( index, fps);
	}

	fpsBox->setCurrentIndex( index );
}

void ConfVidXvidMEncoderDialog::populateRez(QComboBox *rezBox)
{
	QSize size;
	
	disconnect(rezComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectRez(int)));

	rezBox->clear();

	// First must be the recommended size
	QSize sourceSize( videoSource->resX, videoSource->resY );

	size = Encoder::recommendsSize(encSettings, videoSource);

	if (size.width() <= 0) size.setWidth(320);
	if (size.height() <= 0) size.setHeight(240);

	encSettings->recommendation.xvid.resX = size.width();
	encSettings->recommendation.xvid.resY = size.height();

	// Item #0
	rezBox->addItem( tr("%1x%2 (recommended)")
		.arg(size.width())
		.arg(size.height()) , size);
	
	// Second must be the user defined
	size = QSize(encSettings->user_defined.xvid.resX, encSettings->user_defined.xvid.resY);

	if (size.width() <= 0)
	{
		size.setWidth(320);
	}

	if (size.height() <= 0)
	{
		size.setHeight(240);
	}

	encSettings->user_defined.xvid.resX = size.width();
	encSettings->user_defined.xvid.resY = size.height();

	// Item #1
	rezBox->addItem( tr("%1x%2 (user defined)")
		.arg(size.width())
		.arg(size.height()) , size);

	// Build a list of most common rez 
	QList<QSize> commonSizes;

	switch( encSettings->xvid.profile )
	{
	case DivxHandheld:	// Handheld
		commonSizes << QSize( 176, 144 )
			<< QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 );
		break;
	case DivxPortableNtsc:
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 480, 272 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 640, 480 )
			<< QSize( 352, 480 )
			<< QSize( 720, 480 );
		break;
	case DivxPortablePal:
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 640, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 576 );
		break;
	case DivxHomeTheatreNtsc:
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 640, 480 )
			<< QSize( 352, 480 )
			<< QSize( 720, 480 );
		break;
	case DivxHomeTheatrePal:
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 288 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 640, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 576 );
		break;
	case DivxHighDef:	// HDTV
		commonSizes << QSize( 1280, 720 )
			<< QSize( 1920, 1080 );
		break;
	case Simple0:
	case Simple1:
		commonSizes << QSize( 176, 144 )
			<< QSize( 320, 240 );
		break;
	case Simple2:
		commonSizes << QSize( 176, 144 )
			<< QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 );
		break;
	case Simple3:
		commonSizes << QSize( 176, 144 )
			<< QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 );
		break;
	case AdvancedSimple0:
	case AdvancedSimple1:
		commonSizes << QSize( 176, 144 )
			<< QSize( 320, 240 );
		break;
	case AdvancedSimple2:
		commonSizes << QSize( 176, 144 )
			<< QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 );
		break;
	case AdvancedSimple3:
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 640, 480 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 );
		break;
	case AdvancedSimple4:
		commonSizes << QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 640, 480 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1280, 720 );
		break;
	case AdvancedSimple5:
		commonSizes << QSize( 624, 352 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 640, 480 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1280, 720 )
			<< QSize( 1920, 1080 );
		break;
	case SmallSmartphones:
		// 240x260, 240x320, 320x480
		commonSizes << QSize( 176, 144 )
			<< QSize( 260, 240 )
			<< QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 624, 352 )
			<< QSize( 480, 320 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 640, 480 );
		break;
	case LargeSmartphones:
		// 480x800
		commonSizes << QSize( 176, 144 )
			<< QSize( 260, 240 )
			<< QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 400, 225 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 600, 450 )
			<< QSize( 800, 450 )
			<< QSize( 640, 480 )
			<< QSize( 800, 480 );
		break;
	case Apple_iPhone:
		// 480x320 960x640
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )		// iMovie mobile setting
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )		// iMovie medium settings
			<< QSize( 512, 384 )
			<< QSize( 600, 450 )
			<< QSize( 800, 450 )
			<< QSize( 640, 480 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 960, 640 )
			<< QSize( 1280, 720 );
		break;
	case Apple_iPad:
		// 1024x768
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 600, 450 )
			<< QSize( 800, 450 )
			<< QSize( 640, 480 )
			<< QSize( 960, 640 )
			<< QSize( 1024, 768 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1280, 720 );
		break;
	case Apple_MacBook13:
		// 1 280 x 800 (native), 
		// 1 152 x 720, 1024 x 640 et 800 x 500 pixels au format 16:10 ; 
		// 1 024 x 768, 800 x 600 et 640 x 480 pixels au format 4:3 ; 
		// 1 024 x 768, 800 x 600 et 640 x 480 pixels au format 4:3 étendu ; 
		// 720 x 480 pixels au format 3:2 ; 720 x 480 pixels au format 3:2 étendu
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 640, 480 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 800, 500 )
			<< QSize( 1024, 576 )
			<< QSize( 960, 640 )
			<< QSize( 1024, 640 )
			<< QSize( 1024, 768 )
			<< QSize( 1152, 720 )
			<< QSize( 1280, 720 )
			<< QSize( 1280, 800 )
			<< QSize( 1920, 1080 );
		break;
	case Apple_MacBook15:
		// 1 440 x 900 (native), 
		// 1 280 x 800, 1 152 x 720, 1 024 x 640 et 800 x 500 pixels au format 16:10 ;
		// 1 024 x 768, 800 x 600 et 640 x 480 pixels au format 4:3 ; 
		// 1 024 x 768, 800 x 600 et 640 x 480 pixels au format 4:3 étendu ; 
		// 720 x 480 pixels au format 3:2 ; 720 x 480 pixels au format 3:2 étendu
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 640, 480 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 800, 500 )
			<< QSize( 1024, 576 )
			<< QSize( 960, 640 )
			<< QSize( 1024, 640 )
			<< QSize( 1024, 768 )
			<< QSize( 1152, 720 )
			<< QSize( 1280, 720 )
			<< QSize( 1280, 800 )
			<< QSize( 1920, 1080 );
		break;
	case Apple_MacBook17:
		// 1 920 x 1 200 (native),
		// 1 680 x 1 050, 1 280 x 800, 1 152 x 720, 1 024 x 640 et 800 x 500 pixels au format 16:10 ;
		// 1 280 x 1 024 pixels au format 5:4 ; 1 280 x 1 024 pixels au format 5:4 étendu ;
		// 1 600 x 1 200, 1 024 x 768, 800 x 600 et 640 x 480 pixels au format 4:3 ; 
		// 1 600 x 1 200, 1 024 x 768, 800 x 600 et 640 x 480 pixels au format 4:3 étendu ; 
		// 720 x 480 pixels au format 3:2 ; 720 x 480 pixels au format 3:2 étendu
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 640, 480 )
			<< QSize( 800, 500 )
			<< QSize( 720, 540 )
			<< QSize( 960, 540 )
			<< QSize( 960, 640 )
			<< QSize( 1024, 640 )
			<< QSize( 1024, 768 )
			<< QSize( 1152, 720 )
			<< QSize( 1280, 800 )
			<< QSize( 1680, 1050 )
			<< QSize( 1600, 1200 )
			<< QSize( 1920, 1600 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1280, 720 )
			<< QSize( 1920, 1080 );
		break;
	case Apple_iMac:
		// 2560x1440
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 640, 480 )
			<< QSize( 800, 500 )
			<< QSize( 720, 540 )
			<< QSize( 960, 540 )
			<< QSize( 960, 640 )
			<< QSize( 1024, 640 )
			<< QSize( 1024, 768 )
			<< QSize( 1152, 720 )
			<< QSize( 1280, 800 )
			<< QSize( 1680, 1050 )
			<< QSize( 1600, 1200 )
			<< QSize( 2560, 1440 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1280, 720 )
			<< QSize( 1920, 1080 );
		break;
	case Archos7:
		// 800x480 (720p)
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 400, 225 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 600, 450 )
			<< QSize( 800, 450 )
			<< QSize( 640, 480 )
			<< QSize( 800, 480 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1280, 720 );
		break;
	case Archos10:
		// 1024x600 (720p)
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 400, 225 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 600, 450 )
			<< QSize( 800, 450 )
			<< QSize( 640, 480 )
			<< QSize( 800, 480 )
			<< QSize( 800, 600 )
			<< QSize( 1024, 600 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1280, 720 );
		break;
	case BlackBerry:
		// 480x320, 480x360
		commonSizes << QSize( 176, 144 )
			<< QSize( 260, 240 )
			<< QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 400, 225 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 640, 480 );
		break;
	case Netbook10:
		// 10" 1024x600
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 600, 450 )
			<< QSize( 800, 450 )
			<< QSize( 640, 480 )
			<< QSize( 800, 600 )
			<< QSize( 960, 640 )
			<< QSize( 1024, 600 )
			<< QSize( 1024, 768 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1280, 720 );
		break;
	case Netbook12:
		// 11" 1366x768	12" 1366x768
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 600, 450 )
			<< QSize( 800, 450 )
			<< QSize( 640, 480 )
			<< QSize( 960, 640 )
			<< QSize( 1024, 768 )
			<< QSize( 1366, 768 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1280, 720 );
		break;
	case Laptop13:
		// Toshiba 1280x800
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 600, 450 )
			<< QSize( 800, 450 )
			<< QSize( 640, 480 )
			<< QSize( 800, 500 )
			<< QSize( 960, 640 )
			<< QSize( 1024, 768 )
			<< QSize( 1152, 720 )
			<< QSize( 1280, 800 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1280, 720 );
		break;
	case Laptop15:
		// HP, Toshiba 1366x768
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 600, 450 )
			<< QSize( 800, 450 )
			<< QSize( 640, 480 )
			<< QSize( 800, 500 )
			<< QSize( 960, 640 )
			<< QSize( 1024, 768 )
			<< QSize( 1366, 768 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1280, 720 );
		break;
	case Laptop17:
		// Asus, Toshiba 1600x900
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 600, 450 )
			<< QSize( 800, 450 )
			<< QSize( 640, 480 )
			<< QSize( 800, 500 )
			<< QSize( 720, 540 )
			<< QSize( 960, 540 )
			<< QSize( 960, 640 )
			<< QSize( 1024, 768 )
			<< QSize( 1600, 900 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1280, 720 );
		break;
	case Desktop:
		commonSizes << QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 600, 450 )
			<< QSize( 800, 450 )
			<< QSize( 640, 480 )
			<< QSize( 800, 500 )
			<< QSize( 720, 540 )
			<< QSize( 960, 540 )
			<< QSize( 960, 640 )
			<< QSize( 1024, 768 )
			<< QSize( 1600, 900 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1280, 720 )
			<< QSize( 1920, 1080 );
	case Free:
	default:
		commonSizes << QSize( 176, 144 )
			<< QSize( 320, 240 )
			<< QSize( 352, 240 )
			<< QSize( 352, 288 )
			<< QSize( 480, 272 )
			<< QSize( 480, 320 )
			<< QSize( 624, 352 )
			<< QSize( 480, 360 )
			<< QSize( 640, 360 )
			<< QSize( 512, 384 )
			<< QSize( 600, 450 )
			<< QSize( 800, 450 )
			<< QSize( 640, 480 )
			<< QSize( 800, 500 )
			<< QSize( 720, 540 )
			<< QSize( 960, 540 )
			<< QSize( 960, 640 )
			<< QSize( 1024, 640 )
			<< QSize( 1024, 768 )
			<< QSize( 1280, 800 )
			<< QSize( 1600, 900 )
			<< QSize( 1680, 1050 )
			<< QSize( 1600, 1200 )
			<< QSize( 2560, 1440 )
			<< QSize( 352, 480 )
			<< QSize( 352, 576 )
			<< QSize( 720, 480 )
			<< QSize( 720, 576 )
			<< QSize( 848, 480 )
			<< QSize( 1024, 576 )
			<< QSize( 1152, 720 )
			<< QSize( 1280, 720 )
			<< QSize( 1920, 1080 );
		break;
	}

	// Somme user-friendly comments
	QMap<QString, QString> comments;  // QSize has no > member so can't be a QMap key
	comments.insert( QString( "352x240" ), tr("NTSC VCD") );
	comments.insert( QString( "352x288" ), tr("PAL VCD") );
	comments.insert( QString( "352x480" ), tr("NTSC China-VCD") );
	comments.insert( QString( "352x576" ), tr("PAL China-VCD") );
	comments.insert( QString( "480x480" ), tr("NTSC SVCD") );
	comments.insert( QString( "480x576" ), tr("PAL SVCD") );
	comments.insert( QString( "720x480" ), tr("NTSC DVD") );
	comments.insert( QString( "720x576" ), tr("PAL DVD") );
	comments.insert( QString( "848x480" ), tr("NTSC DVD 1:1") );
	comments.insert( QString( "1024x576" ), tr("PAL DVD 1:1") );
	comments.insert( QString( "480x272" ), tr("iMovie mobile") );
	comments.insert( QString( "480x360" ), tr("YouTube 4:3") );
	comments.insert( QString( "624x352" ), tr("P2P sharing 16:9") );
	comments.insert( QString( "640x360" ), tr("YouTube 16:9, iMovie medium") );
	comments.insert( QString( "512x384" ), tr("P2P sharing 4:3") );
	comments.insert( QString( "1280x720" ), tr("HD TV") );
	comments.insert( QString( "1920x1080" ), tr("HD TV") );

	// Item #2... (push every common rez) 
	while (!commonSizes.isEmpty() )
	{
		size = commonSizes.takeFirst();
		QString strSize = QString( "%1x%2" ).arg(size.width()).arg(size.height());

		if ( comments.contains( strSize ) )
		{
			rezBox->addItem( tr("%1x%2 (%3)")
				.arg(size.width())
				.arg(size.height())
				.arg(comments.value(strSize)), size);
		}
		else
		{
			rezBox->addItem( tr("%1x%2")
				.arg(size.width())
				.arg(size.height()) , size);
		}
	}

	// Select current rez
	size = QSize(encSettings->xvid.resX, encSettings->xvid.resY);

	if (size.width() <= 0)
	{
		size.setWidth(320);
	}

	if (size.height() <= 0)
	{
		size.setHeight(240);
	}

	encSettings->xvid.resX = size.width();
	encSettings->xvid.resY = size.height();

	int index = rezBox->findData( size );

	if (index == -1)
	{
		// Missing current rez in list is forced to be the user defined (item #1)
		index = 1;
		rezBox->setItemText( index, tr("%1x%2 (user defined)")
			.arg(size.width())
			.arg(size.height()));
		rezBox->setItemData( index, size);
	}

	connect(rezComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectRez(int)));

	rezBox->setCurrentIndex( index );
}

void ConfVidXvidMEncoderDialog::populatePass()
{
	passComboBox->clear();

	QMap<int, QString> preset;
	preset.insert( 0, tr("Single pass") );
	preset.insert( 1, tr("Two pass - 1st pass") );
	preset.insert( 2, tr("Two pass - 2nd pass") );

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

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

	int index = passComboBox->findData( encSettings->xvid.pass );

	if ( index == -1 ) // should not happened
	{
		passComboBox->setCurrentIndex( 0 );
		encSettings->xvid.pass = passComboBox->itemData( 0 ).toInt();
	}
	else
	{
		passComboBox->setCurrentIndex( index );
	}
}

void ConfVidXvidMEncoderDialog::populatePreset(QComboBox *presetBox)
{
	presetBox->clear();

	QMap<int, QString> preset;
	preset.insert( VeryFast, tr("Very fast encoding") );
	preset.insert( Fast, tr("Fast encoding - Good quality") );
	preset.insert( HQ, tr("High quality") );
	preset.insert( VeryHQ, tr("Very high quality") );
	preset.insert( CustomPreset, tr("Custom quality settings") );

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

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

	int index = presetBox->findData( encSettings->xvid.preset );

	if ( index == -1 ) // should not happened
	{
		presetBox->setCurrentIndex( 0 );
		encSettings->xvid.preset = presetBox->itemData( 0 ).toInt();
	}
	else
	{
		presetBox->setCurrentIndex( index );
	}
}

void ConfVidXvidMEncoderDialog::populateTarget()
{
	// From: http://www.osta.org/technology/cdqa7.htm
	// and http://www.osta.org/technology/dvdqa/dvdqa6.htm
	
	targetComboBox->clear();

	int userDefSize = encSettings->user_defined.xvid.split.size;
	int userDefVolumes = encSettings->user_defined.xvid.split.volumes;

	targetComboBox->addItem( tr("%1MB x %2 (user defined)")
		.arg(userDefSize).arg(userDefVolumes),			QPoint(userDefSize, userDefVolumes) );
	targetComboBox->addItem( tr("CD 210MB (8cm)"),		QPoint(193, 1) );	// 193,536,000 Bytes
	targetComboBox->addItem( tr("CD 650MB / 74Min"),	QPoint(681, 1) );	// 681,984,000 Bytes
	targetComboBox->addItem( tr("CD 700MB / 80Min"),	QPoint(737, 1) );	// 737,280,000 Bytes
	targetComboBox->addItem( tr("CD 800MB / 90Min"),	QPoint(829, 1) );	// 829,440,000 Bytes
	targetComboBox->addItem( tr("CD 900MB / 100Min"),	QPoint(912, 1) );	// 912,384,000 Bytes
	targetComboBox->addItem( tr("CD 700MB x 2"),		QPoint(737, 2) );	// 
	targetComboBox->addItem( tr("DVD 1.4GB - 8cm"),		QPoint(1463, 1) );	// 
	targetComboBox->addItem( tr("DVD 4.5GB"),			QPoint(4700, 1) );	// 4,700,372,992 Bytes (DVD+R)
	targetComboBox->addItem( tr("DVD 9GB"),				QPoint(8543, 1) );	// 8,543,666,176 Bytes (DVD-R DL)

	int index = targetComboBox->findData( QPoint(encSettings->xvid.split.size, encSettings->xvid.split.volumes) );

	if ( index == -1 )
	{
		split_t split(encSettings->xvid.split);
		
		targetComboBox->setItemText( 0, tr("%1MB x %2 (user defined)")
			.arg(split.size)
			.arg(split.volumes) );
		targetComboBox->setItemData( 0, QPoint(split.size, split.volumes) );

		targetComboBox->setCurrentIndex( 0 );
	}
	else
	{
		targetComboBox->setCurrentIndex( index );
	}
}

void ConfVidXvidMEncoderDialog::populateProfiles( QComboBox *profileBox, const int choice )
{
	profileBox->clear();

	profileBox->addItem( "(unconstrained)" , Free );

	if ( choice == DivX )
	{
		profileBox->addItem( tr("DivX Handheld") , DivxHandheld );
		profileBox->addItem( tr("DivX Portable NTSC") , DivxPortableNtsc );
		profileBox->addItem( tr("DivX Portable PAL") , DivxPortablePal );
		profileBox->addItem( tr("DivX Home Theatre NTSC") , DivxHomeTheatreNtsc );
		profileBox->addItem( tr("DivX Home Theatre PAL") , DivxHomeTheatrePal );
		profileBox->addItem( tr("DivX High Def") , DivxHighDef );
	}
	else if ( choice == MPEG4 )
	{
		profileBox->addItem( "MPEG-4 Simple @ Level 0" , Simple0 );
		profileBox->addItem( "MPEG-4 Simple @ Level 1" , Simple1 );
		profileBox->addItem( "MPEG-4 Simple @ Level 2" , Simple2 );
		profileBox->addItem( "MPEG-4 Simple @ Level 3" , Simple3 );

		profileBox->addItem( "MPEG-4 AdvancedSimple @ Level 0" , AdvancedSimple0 );
		profileBox->addItem( "MPEG-4 AdvancedSimple @ Level 1" , AdvancedSimple1 );
		profileBox->addItem( "MPEG-4 AdvancedSimple @ Level 2" , AdvancedSimple2 );
		profileBox->addItem( "MPEG-4 AdvancedSimple @ Level 3" , AdvancedSimple3 );
		profileBox->addItem( "MPEG-4 AdvancedSimple @ Level 4" , AdvancedSimple4 );
		profileBox->addItem( "MPEG-4 AdvancedSimple @ Level 5" , AdvancedSimple5 );
	}
	else if ( choice == Devices )
	{
		profileBox->addItem( tr("PDA/Smartphone") , SmallSmartphones );				// 240x260, 240x320, 320x480
		profileBox->addItem( tr("PDA/Smartphone (large)") , LargeSmartphones );		// 480x800
		profileBox->addItem( tr("Apple iPhone") , Apple_iPhone );	// 480x320 960x640
		profileBox->addItem( tr("Apple iPad") , Apple_iPad );		// 1024x768
		profileBox->addItem( tr("Apple MacBook Pro 13\"") , Apple_MacBook13 );		// MacBook 1280x800
		profileBox->addItem( tr("Apple MacBook Pro 15\"") , Apple_MacBook15 );		// MacBook 1440x900
		profileBox->addItem( tr("Apple MacBook Pro 17\"") , Apple_MacBook17 );		// MacBook 1920x1200
		profileBox->addItem( tr("Apple iMac") , Apple_iMac );
		profileBox->addItem( tr("Archos 7") , Archos7 );			// 800x480 (720p)
		profileBox->addItem( tr("Archos 10") , Archos10 );			// 1024x600 (720p)
		profileBox->addItem( tr("BlackBerry") , BlackBerry );		// 480x320, 480x360
		profileBox->addItem( tr("Netbook 10\"") , Netbook10 );		// 10" 1024x600		11" 1366x768	12" 1366x768
		profileBox->addItem( tr("Netbook 11\"-12\"") , Netbook12 );	// 10" 1024x600		11" 1366x768	12" 1366x768
		profileBox->addItem( tr("PC Laptop 13\"") , Laptop13 );		// Toshiba 1280x800
		profileBox->addItem( tr("PC Laptop 15\"") , Laptop15 );		// HP,	Toshiba 1366x768
		profileBox->addItem( tr("PC Laptop 17\"") , Laptop17 );		// Asus, Toshiba 1600x900
		profileBox->addItem( tr("PC Desktop") , Desktop );
	}
#ifdef WIN32
#pragma message (ToBeDone "store device set profiles outside source code - " __FUNCTION__)
#pragma message (ToBeDone "device set profiles import from XMediaRecode, MeGUI etc... - " __FUNCTION__)
#endif

	profileBox->addItem( tr("-") , -100 );

	if ( choice != Devices )
	{
		profileBox->addItem( tr("Devices profiles...") , Devices );
	}

	if ( choice != DivX )
	{
		profileBox->addItem( tr("DivX profiles...") , DivX );
	}

	if ( choice != MPEG4 )
	{
		profileBox->addItem( tr("MPEG-4 profiles...") , MPEG4 );
	}

	profileBox->setCurrentIndex( profileBox->findData( encSettings->xvid.profile ) );
}

void ConfVidXvidMEncoderDialog::populateQuantizationTypes( QComboBox *quantizationTypeBox )
{
	quantizationTypeBox->clear();

	quantizationTypeBox->addItem( "H.263" , H263 );
	quantizationTypeBox->addItem( "MPEG" , MPEG );

	quantizationTypeBox->setCurrentIndex( quantizationTypeBox->findData( encSettings->xvid.quant_type ) );
}

void ConfVidXvidMEncoderDialog::populateFourcc()
{
	fourccComboBox->clear();

	fourccComboBox->addItem( "XVID" , "XVID" );
	fourccComboBox->addItem( "DIVX" , "DIVX" );
	fourccComboBox->addItem( "DX50" , "DX50" );

	fourccComboBox->setCurrentIndex( fourccComboBox->findData( encSettings->xvid.fourcc ) );
}

void ConfVidXvidMEncoderDialog::populateMe_quality( QComboBox *meBox )
{
	meBox->clear();

	meBox->addItem( "0 - None" , 0 );
	meBox->addItem( "1 - Very Low" , 1 );
	meBox->addItem( "2 - Low" , 2 );
	meBox->addItem( "3 - Medium" , 3 );
	meBox->addItem( "4 - High" , 4 );
	meBox->addItem( "5 - Very High" , 5 );
	meBox->addItem( "6 - Ultra High" , 6 );

	meBox->setCurrentIndex( meBox->findData( encSettings->xvid.me_quality ) );
}
void ConfVidXvidMEncoderDialog::populateVhq( QComboBox *vhqBox, const int currentVhq )
{
	// Also works for bvhq (vhq for B-Frames)

	vhqBox->clear();

	vhqBox->addItem( "0 - Off" , 0 );
	vhqBox->addItem( "1 - Mode Decision" , 1 );
	vhqBox->addItem( "2 - Limited Search" , 2 );
	vhqBox->addItem( "3 - Medium Search" , 3 );
	vhqBox->addItem( "4 - Wide Search" , 4 );

	vhqBox->setCurrentIndex( vhqBox->findData( currentVhq ) );
}

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

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

void ConfVidXvidMEncoderDialog::bitrateSliderChanged(const int value)
{
	bitrateLineEdit->setText( QString::number(value) );
}

void ConfVidXvidMEncoderDialog::quantizerSliderChanged(const int value)
{
	quantizerSlider->setToolTip( QString("<b>%1</b>").arg( value ) );

	encSettings->xvid.fixed_quant = value;
}

void ConfVidXvidMEncoderDialog::defaultPushButtonClicked()
{
	encSettings->xvid.clear();

	encSettings->xvid.resX = encSettings->recommendation.xvid.resX;
	encSettings->xvid.resY = encSettings->recommendation.xvid.resY;
	encSettings->xvid.fps = encSettings->recommendation.xvid.fps;

	presetComboBox->setCurrentIndex( profileComboBox->findData( encSettings->xvid.preset ) );
	applyPresetOnSettings( encSettings->xvid.preset );

	profileComboBox->setCurrentIndex( profileComboBox->findData( encSettings->xvid.profile ) );
	applyProfileOnSettings( encSettings->xvid.profile );
	
	updateControls();
}

void ConfVidXvidMEncoderDialog::selectPreset(const int index)
{
	encSettings->xvid.preset = presetComboBox->itemData(index).toInt();

	applyPresetOnSettings( presetComboBox->itemData(index).toInt() );
	applyPresetOnControls( encSettings->xvid.preset );	

	if (encSettings->xvid.preset == CustomPreset) tabWidget->setCurrentWidget( qualityTab );
}

void ConfVidXvidMEncoderDialog::applyPresetOnControls(const int preset)
{
/*	// Quality preset
	int index = presetComboBox->findData( preset );
	if (index >= 0)
	{
		presetComboBox->setCurrentIndex( index );
	}
*/
	qualityGroup->setEnabled(encSettings->xvid.preset == CustomPreset);

	// Quality controls
	me_qualityComboBox->setCurrentIndex( me_qualityComboBox->findData( encSettings->xvid.me_quality ) );
	vhqComboBox->setCurrentIndex( vhqComboBox->findData( encSettings->xvid.vhq ) );
	bvhqCheckBox->setChecked( encSettings->xvid.bvhq );
	hq_acCheckBox->setChecked( encSettings->xvid.hq_ac );
	chroma_meCheckBox->setChecked( encSettings->xvid.chroma_me );
	chroma_optCheckBox->setChecked( encSettings->xvid.chroma_opt );
	trellisCheckBox->setChecked( encSettings->xvid.trellis );
}

void ConfVidXvidMEncoderDialog::applyPresetOnSettings(const int preset)
{
	encSettings->xvid.preset = preset;

	if ( encSettings->xvid.preset == CustomPreset ) return;

	// Reset quality settings 
	encoder_video_settings_t defaultSettings;  // default settings by construction
	encSettings->xvid.me_quality = defaultSettings.xvid.me_quality;
	encSettings->xvid.vhq = defaultSettings.xvid.vhq;
	encSettings->xvid.bvhq = defaultSettings.xvid.bvhq;
	encSettings->xvid.hq_ac = defaultSettings.xvid.hq_ac;
	encSettings->xvid.chroma_me = defaultSettings.xvid.chroma_me;
	encSettings->xvid.chroma_opt = defaultSettings.xvid.chroma_opt;
	encSettings->xvid.trellis = defaultSettings.xvid.trellis;
	encSettings->xvid.cartoon = defaultSettings.xvid.cartoon;
	encSettings->xvid.turbo = defaultSettings.xvid.turbo;

	// Then proceed for each preset
	switch (encSettings->xvid.preset)
	{
	case VeryHQ:
		encSettings->xvid.me_quality = 6;
		encSettings->xvid.vhq = 4;
		encSettings->xvid.bvhq = 1;
		encSettings->xvid.chroma_opt = true;
		encSettings->xvid.trellis = true;
		break;
	case HQ:
		encSettings->xvid.preset = HQ;
		encSettings->xvid.me_quality = 5;
		encSettings->xvid.vhq = 1;
		encSettings->xvid.bvhq = 1;
		encSettings->xvid.chroma_opt = true;
		encSettings->xvid.trellis = true;
		break;
	case Fast:
		encSettings->xvid.preset = Fast;
		encSettings->xvid.me_quality = 3;
		encSettings->xvid.vhq = 0;
		encSettings->xvid.trellis = false;
		if (encSettings->xvid.pass >= 1) encSettings->xvid.turbo = true;  // two-pass mode
		break;
	case VeryFast:
		encSettings->xvid.preset = VeryFast;
		encSettings->xvid.me_quality = 1;
		encSettings->xvid.vhq = 0;
		encSettings->xvid.trellis = false;
		encSettings->xvid.chroma_me = false;
		encSettings->xvid.trellis = false;
		if (encSettings->xvid.pass >= 1) encSettings->xvid.turbo = true;  // two-pass mode
		break;
	default:
		break;
	}
}

void ConfVidXvidMEncoderDialog::selectProfile(const int index)
{
//	DivX certified profiles
//	- Handheld
//		176 x 144 at 15 fps
//		1485 macroblocks per second
//		200 kbps average
//		800 kbps peak
//		32 KB buffer
//	- Portable
//		352 x 240 at 30 fps or 352 x 288 at 25 fps
//		9900 macroblocks per second
//		768 kbps average
//		4000 kbps peak
//		138 KB buffer
//		Bi-directional encoding
//	- Home Theatre
//		720 x 480 or 720 x 576
//		40500 macroblocks per second
//		4000 kbps average
//		8000 kbps peak
//		384 KB buffer
//		Bi-directional encoding
//		Optional interlaced video
//	- High Definition
//		1280 x 720
//		108000 macroblocks per second
//		8000 kbps average
//		32000 kbps peak
//		768 KB buffer
//		Bi-directional encoding
//		Optional interlaced video

	int profileChosen = profileComboBox->itemData(index).toInt();

	if ( profileChosen == -100 )
	{
		applyProfileOnControls( Free );
		updateControls();
		profileComboBox->showPopup();

		return; // separator
	}
	else if ( profileChosen < -1 )
	{
		// Choose profile set to display
		encSettings->user_defined.xvid.profile_set = profileChosen;

		populateProfiles( profileComboBox, encSettings->user_defined.xvid.profile_set );
		populateProfiles( profileComboBox_2, encSettings->user_defined.xvid.profile_set );

		int defaultProfile;

		// Select default in chosen set
		if ( profileChosen == DivX )
		{
			defaultProfile = profileComboBox->findData( encSettings->xvid.resY == 288 
									|| encSettings->xvid.resY == 576 
									|| encSettings->xvid.fps == 25.0 ? DivxHomeTheatrePal : DivxHomeTheatreNtsc );
		}
		else if ( profileChosen == MPEG4 )
		{
			defaultProfile = profileComboBox->findData( AdvancedSimple3 );
		}
		else if ( profileChosen == Devices )
		{
			defaultProfile = profileComboBox->findData( Netbook10 );
		}

		if ( defaultProfile == -1 ) defaultProfile = 0;
		
		profileComboBox->setCurrentIndex( defaultProfile );
	}
	else if (index < 0)
	{
		// Do nothing if empty or reset
		return;
	}
	
	encSettings->xvid.profile = profileComboBox->itemData(index).toInt();

	applyProfileOnSettings(encSettings->xvid.profile);
	applyProfileOnControls(encSettings->xvid.profile);

	if (encSettings->xvid.profile == Free) tabWidget->setCurrentWidget( profileTab );
}

void ConfVidXvidMEncoderDialog::selectTarget(const int index)
{
	// Do nothing if empty or reset
	if (index < 0)
	{
		return;
	}

	if (index == 0)
	{
		enterUserDefinedTarget();
	}

	QVariant s = targetComboBox->itemData(index);
	QPoint *vector = static_cast<QPoint*>( (void *)&s );
	encSettings->user_defined.xvid.split.size = vector->x();
	encSettings->user_defined.xvid.split.volumes = vector->y();
}

void ConfVidXvidMEncoderDialog::selectPass(const int index)
{
	// Do nothing if empty or reset
	if (index < 0)
	{
		return;
	}

	encSettings->xvid.pass = passComboBox->itemData(index).toInt();

	if (encSettings->xvid.pass == 0) encSettings->xvid.turbo = false;  // single pass

	updateControls();
}

void ConfVidXvidMEncoderDialog::selectFps(const int index)
{
	// Do nothing if empty or reset
	if (index < 0)
	{
		return;
	}

	if (index == 1)
	{
		enterUserDefinedFps();
	}

	encSettings->xvid.fps = fpsComboBox->itemData(index).toDouble();
}

void ConfVidXvidMEncoderDialog::selectRez(const int index)
{
	// Do nothing if empty or reset
	if (index < 0)
	{
		return;
	}

	if (index == 1)
	{
		enterUserDefinedSize();
	}

	QSize size = rezComboBox->itemData(index).toSize();

	encSettings->xvid.resX = size.width();
	encSettings->xvid.resY = size.height();
}

void ConfVidXvidMEncoderDialog::setToolTips()
{
	passLabel->setToolTip( formatedTip("pass", tr("0 (single pass)"),
		"<0|1|2>",
		tr( "Specify the pass in 2-pass mode. 'Two pass - 1st pass' creates the stats file required "
		"for the 2nd pass." )) );
	
	passComboBox->setToolTip( passLabel->toolTip() );

	profileLabel->setToolTip( formatedTip("profile", tr("(unrestricted)"),
		QString(),
		QStringList( tr( "Restricts options and VBV (peak bitrate over a short period) according to "
		"the Simple, Advanced Simple and DivX profiles.") )
		+ QStringList( tr( "The resulting videos should be playable on standalone "
		"players adhering to these profile specifications. These profiles should be used in conjunction with "
		"an appropriate -ffourcc. Generally DX50 is applicable, as some players do not recognize Xvid but most "
		"recognize DivX.") )
		+ QStringList( tr( "Device profiles are DivX or MPEG-4 profiles customized for targetted devices." ))));
	
	profileComboBox->setToolTip( profileLabel->toolTip() );
	profileLabel_2->setToolTip( profileLabel->toolTip() );
	profileComboBox_2->setToolTip( profileLabel->toolTip() );

	rezLabel->setToolTip( formatedTip("width x height", tr("keep source resolution"), 
		QString("<%1x%2-%3x%4>").arg(widthMin).arg(heightMin).arg(widthMax).arg(heightMax),
		tr( "Select 'user defined' to set resolution on your own. The proposed list is constrained "
		"by the profile and the recommendation depends on the source resolution." )) );

	rezComboBox->setToolTip( rezLabel->toolTip() );
	rezLabel_2->setToolTip( rezLabel->toolTip() );
	rezComboBox_2->setToolTip( rezLabel->toolTip() );

	fpsLabel->setToolTip( formatedTip("ofps", tr("keep source frame rate"), 
		QString(),
		tr( "Specify a frames per second (fps) value for the output file, "
		"which can be different from that of the source material. Must "
		"be set for variable fps (ASF, some MOV) and progressive "
		"(30000/1001 fps telecined MPEG) files." )) );

	fpsComboBox->setToolTip( fpsLabel->toolTip() );
	fpsLabel_2->setToolTip( fpsLabel->toolTip() );
	fpsComboBox_2->setToolTip( fpsLabel->toolTip() );

	presetLabel->setToolTip( formatedTip("preset", tr("Custom"),
		QString(),
		tr( "DVDx provides quality presets which you can apply "
		"or customize. Higher the image quality is, slower is the encoding speed." )) );
	
	presetComboBox->setToolTip( presetLabel->toolTip() );
	presetLabel_2->setToolTip( presetLabel->toolTip() );
	presetComboBox_2->setToolTip( presetLabel->toolTip() );

	setMaxBitrate( bitrateSlider->maximum() );

	quantizerRadioButton->setToolTip( formatedTip("fixed_quant", QString(), 
		"<1-31>",
		QStringList( tr("Encoding mode: constant quality.") )
		+ QStringList( tr("Switch to fixed quantizer mode and specify the quantizer to be used.") )
		+ QStringList( tr("The lower, better is the quality, 4 is fair. Only one single pass is required.") )));

	quantizerLabel->setToolTip( quantizerRadioButton->toolTip() );

	targetRadioButton->setToolTip( formatedTip("size x volumes", QString(), 
		QString(),
		QStringList( tr("Encoding mode: targeted size.") )
		+ QStringList( tr("Split the output file in N volumes of MiBytes size. Two pass are required.") )));

	targetComboBox->setToolTip( targetRadioButton->toolTip() );

	cartoonCheckBox->setToolTip( formatedTip("cartoon", tr("off"),
		QString(),
		tr( "Activate this if your encoded sequence is an anime/cartoon. It "
		"modifies some Xvid internal thresholds so Xvid takes better decisions "
		"on frame types and motion vectors for flat looking cartoons." )) );

	turboCheckBox->setToolTip( formatedTip("turbo", tr("off"),
		tr( "(two pass only)" ),
		QStringList( tr("Dramatically speeds up pass one using faster algorithms and disabling "
		"CPU-intensive options. This will probably reduce global PSNR a "
		"little bit and change individual frame type and PSNR a little bit more.") ) 
		+ QStringList( tr("It has no impact on speed as it just tunes the mode decision "
		"heuristics for this type of content.") )));

	defaultPushButton->setToolTip( tr("Reset Xvid codec settings to default values.") );

	lumi_maskCheckBox->setToolTip( formatedTip("lumi_mask", tr("off"),
		QString(),
		QStringList( tr("(a.k.a. adaptive quantization)") ) 
		+ QStringList( tr("Adaptive quantization allows the macroblock quantizers to vary "
		"inside each frame. This is a ’psychosensory’ setting that is supposed to make use "
		"of the fact that the human eye tends to notice fewer details in very bright and very "
		"dark parts of the picture. It compresses those areas more strongly than medium ones, "
		"which will save bits that can be spent again on other frames, raising overall subjective "
		"quality and possibly reducing PSNR.") )
		+ QStringList( tr("So it tries to give less bitrate to part of the picture that the human eye "
		"cannot see very well, which should allow the encoder to spend the saved bits on more "
		"important parts of the picture. The quality of the encode yielded by this option highly "
		"depends on personal preferences and on the type and monitor settings used to watch it "
		"(typically, it will not look as good if it is bright or if it is a TFT monitor).") )));

	interlacingCheckBox->setToolTip( formatedTip("interlacing", tr("off"),
		QString(),
		QStringList( tr("Encode the fields of interlaced video material. Turn this option "
		"on for interlaced content.") )
		+ QStringList( tr("NOTE: Should you rescale the video, you would need an interlace-aware "
		"resizer, which you can activate with -vf scale=<width>:<height>:1.") )));

	qpelCheckBox->setToolTip( formatedTip("qpel", tr("off"),
		QString(),
		QStringList( tr("MPEG-4 uses a half pixel precision for its motion search by default. "
		"The standard proposes a mode where encoders are allowed to use quarter pixel precision. "
		"This option usually results in a sharper image. Unfortunately it has a great impact on "
		"bitrate and sometimes the higher bitrate use will prevent it from giving a better image "
		"quality at a fixed bitrate. It is better to test with and without this option and see whether "
		"it is worth activating.") ) 
		+ QStringList( tr("Raise the number of candidate motion vectors by increasing the precision "
		"of the motion estimation from halfpel to quarterpel. The idea is to find better motion vectors "
		"which will in return reduce bitrate (hence increasing quality). However, motion vectors with "
		"quarterpel precision require a few extra bits to code, but the candidate vectors do not always "
		"give (much) better results. Quite often, the codec still spends bits on the extra precision, but "
		"little or no extra quality is gained in return. Unfortunately, there is no way to foresee the "
		"possible gains of qpel, so you need to actually encode with and without it to know for sure.") )
		+ QStringList( tr("NOTE: qpel can be almost double encoding time, and requires as much as 25% more "
		"processing power to decode. It is not supported by all standalone players.") )));

	gmcCheckBox->setToolTip( formatedTip("gmc", tr("off"),
		QString(),
		QStringList( tr("Enable Global Motion Compensation, which makes Xvid generate "
		"special frames (GMC-frames) which are well suited for Pan/Zoom/Rotating images. "
		"Whether or not the use of this option will	save bits is highly dependent on the "
		"source material.") ) 
		+ QStringList( tr("Tries to save bits on panning scenes by using a single motion "
		"vector for the whole frame. This almost always raises PSNR, but significantly slows "
		"down encoding (as well as decoding). Therefore, you should only use it when you have "
		"turned vhq to the maximum. Xvid's GMC is more sophisticated than DivX's, but is only "
		"supported by few standalone players.") )));

	quant_typeLabel->setToolTip( formatedTip("quant_type", "h263", 
		"<h263 | mpeg>",
		QStringList( tr("Sets the type of quantizer to use. For high bitrates, you will "
		"find that MPEG quantization preserves more detail. For low bitrates, the smoothing "
		"of H.263 will give you less block noise.") )
		+ QStringList( tr("NOTE: when using custom matrices, MPEG quantization must be used.") )));

	quant_typeComboBox->setToolTip( quant_typeLabel->toolTip() );

	fourccLabel->setToolTip( formatedTip("fourcc", "XVID", 
		"<XVID | DIVX | DX50>",
		QStringList( tr("The FourCC code determines the type of codec that was used to encode "
		"the video. Since DivX and XviD are similar (both based on MPEG-4), you could change "
		"the FourCC to make it playable with another codec. Normally, leave the FourCC code as "
		"it is (you can also change the code in the future if you wish).") )
		+ QStringList( tr("NOTE: use 'DX50' for DivX certified profiles.") )));

	fourccComboBox->setToolTip( fourccLabel->toolTip() );

	max_bframesLabel->setToolTip( formatedTip("max_bframes", "2", 
		"<0-4>",
		QStringList( tr("Maximum number of B-frames to put between I/P-frames.") )
		+ QStringList( tr("'Max consecutive BVOPs' defines the number of consecutive B-frames "
		"(Bidirectional frames, which improves quality) the encoder will be allowed to set. A "
		"setting of 0 turns off B-frames. A setting of 1 is standard (same as DivX 5.x), a setting "
		"of 2 will produce better results (but could cause compatibility issues), and a setting of 3 "
		"is used for maximum effect. It is not recommended to go above 3.") )));

	max_bframesLineEdit->setToolTip( max_bframesLabel->toolTip() );

	bquant_ratioLabel->setToolTip( formatedTip("bquant_ratio", "1.50", 
		"<0.00-10.00>",
		QStringList( tr("Quantizer ratio between B- and non-B-frames.") )
		+ QStringList( tr("The 'Quantizer ratio' setting determines how many more times the B-frames "
		"should be compressed compared to P-frames (the higher the value, the higher the quantizer "
		"setting and therefore the more compressed the frame is).") )));

	bquant_ratioLineEdit->setToolTip( bquant_ratioLabel->toolTip() );

	bquant_offsetLabel->setToolTip( formatedTip("bquant_offset", "1.00", 
		"<-10.00-10.00>",
		QStringList( tr("Quantizer offset between B- and non-B-frames.") )
		+ QStringList( tr("The 'Quantizer offset' setting determines the quantizer value to be added "
		"to B-frames compared to surrounding P-frames (so a setting of '1.00' would add '1.00' to the "
		"quantizer value of the B-frame, again the higher the quantizer, the more compression used).") )));

	bquant_offsetLineEdit->setToolTip( bquant_offsetLabel->toolTip() );

	profileDefaultPushButton->setToolTip( tr("Reset current profile settings to default values.") );

	me_qualityLabel->setToolTip( formatedTip("me_quality", "6", 
		"<0-6>",
		QStringList( tr("This options determines the precision of the motion search algorithm. "
		"The higher the setting, the better quality the picture will be (at the expense of encoding "
		"time). Set this to '6 - Ultra High' or '5 - Very High'.") )
		+ QStringList( tr("This option controls the motion estimation subsystem. The higher the "
		"value, the more precise the estimation should be. The more precise the motion estimation "
		"is, the more bits can be saved. Precision is gained at the expense of CPU time so decrease "
		"this setting if you need fast encoding.") )));

	me_qualityComboBox->setToolTip( me_qualityLabel->toolTip() );

	vhqLabel->setToolTip( formatedTip("vhq", "1", 
		"<0-4>",
		QStringList( tr("This option enables a more precise motion search mode, but at a serious "
		"expense of speed. It is usually not worth going higher than '1 - Mode Decision', unless "
		"you have selected the GMC option earlier on.") )
		+ QStringList( tr("The motion search algorithm is based on a search in the usual color "
		"domain and tries to find a motion vector that minimizes the difference between the reference "
		"frame and the encoded frame. With this setting activated, Xvid will also use the frequency "
		"domain (DCT) to search for a motion vector that minimizes not only the spatial difference but "
		"also the encoding length of the block.") )));

	vhqComboBox->setToolTip( vhqLabel->toolTip() );

	bvhqCheckBox->setToolTip( formatedTip("bvhq", tr("on"),
		QString(),
		QStringList( tr("Selecting this option will decrease encoding speed by applying VHQ "
		"mode to B-frames as well.") )
		+ QStringList( tr("This setting allows vector candidates for B-frames to be used "
		"for the encoding chosen using a rate distortion optimized operator, which is what "
		"is done for P-frames by the vhq option. This produces nicer-looking B-frames while "
		"incurring almost no per-formance penalty.") )));

	hq_acCheckBox->setToolTip( formatedTip("hq_ac", tr("on"),
		QString(),
		QStringList( tr("Activates high-quality prediction of AC coefficients for intra "
		"frames from neighbor blocks.") )
		+ QStringList( tr("Activates a better coefficient cost estimation method, which slightly "
		"reduces file size by around 0.15 to 0.19% (which corresponds to less than 0.01dB PSNR "
		"increase), while having a negligible impact on speed. It is therefore recommended to "
		"always leave it on.") )));

	chroma_meCheckBox->setToolTip( formatedTip("chroma_me", tr("on"),
		QString(),
		QStringList( tr("The usual motion estimation algorithm uses only the luminance "
		"information to find the best motion vector. However for some video material, using "
		"the chroma planes can help find better vectors. This setting toggles the use of "
		"chroma planes for motion estimation.") )
		+ QStringList( tr("Improves motion estimation by also taking the chroma (color) "
		"information into account, whereas me_quality alone only uses luma (grayscale). "
		"This slows down encoding by 5-10% but improves visual quality quite a bit by reducing "
		"blocking effects and reduces file size by around 1.3%. If you are looking for speed, "
		"you should disable this option before starting to consider reducing me_quality.") )));

	chroma_optCheckBox->setToolTip( formatedTip("chroma_opt", tr("off"),
		QString(),
		QStringList( tr("Is intended to increase chroma image quality around pure white/black "
		"edges, rather than improving compression. This can help to reduce the 'red stairs' effect.") )
		+ QStringList( tr("Enable a chroma optimizer prefilter. It will do some extra magic on "
		"color information to minimize the stepped-stairs effect on edges. It will improve quality "
		"at the cost of encoding speed.") )
		+ QStringList( tr("It reduces PSNR by nature, as the mathematical deviation to the original "
		"picture will get bigger, but the subjective image quality will raise. Since it works with "
		"color information, you might want to turn it off when encoding in grayscale.") )));

	trellisCheckBox->setToolTip( formatedTip("trellis", tr("on"),
		QString(),
		QStringList( tr("This option will decrease the file size, but may decrease "
		"encoding speed and quality.") )
		+ QStringList( tr("Trellis Quantization is a kind of adaptive quantization method "
		"that saves bits by modifying quantized coefficients to make them more compressible "
		"by the entropy encoder. Its impact on quality is good, and if VHQ uses too much CPU "
		"for you, this setting can be a good alternative to save a few bits (and gain quality "
		"at	fixed bitrate) at a lesser cost than with VHQ.") )));
}

QString ConfVidXvidMEncoderDialog::formatedTip( const QString name, const QString defaults, const QString range, QStringList details )
{
	QString p1, p2, p3;

	p1 = range.isEmpty() ? 
			tr("<p><b>%1</b></p>").arg(Qt::escape(name)) :
			tr("<p><b>%1</b> %2</p>").arg(Qt::escape(name)).arg(Qt::escape(range));

	p2 = defaults.isEmpty() ? 
			QString() :
			tr("<p></p><p><b>Default:</b> %1</p>").arg(Qt::escape(defaults));

	while( !details.isEmpty() ) p3 += tr("<p></p><p>%1</p>").arg(Qt::escape( details.takeFirst() ));

	return p1 + p2 + p3;
}

QString ConfVidXvidMEncoderDialog::formatedTip( const QString name, const QString defaults, const QString range, const QString details )
{
	return formatedTip( name, defaults, range, QStringList(details) );
}
