/***************************************************************************
   This file is part of LinTV
   lintvpages.cpp
   All the code for the various setup pages in LinTV.

   Author: Jesse L. Zamora <xtremek2009@gmail.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
 ***************************************************************************/

/* LinTV Includes */
#include "lintvpages.h"
#include "lintvglobal.h"
#include "lintvglobal.h"
#include "lintvsettings.h"
#include "lintvmixer.h"

/* Qt Includes */
#include <QtGui/QLabel>
#include <QtGui/QSpinBox>
#include <QtGui/QListWidget>
#include <QtGui/QComboBox>
#include <QtGui/QRadioButton>
#include <QtGui/QPushButton>
#include <QtUiTools>

/* KDE Includes */
#include <kdebug.h>
#include <kurlrequester.h>
#include <kstandarddirs.h>
#include <kmessagebox.h>

RecordSettingsPage::RecordSettingsPage(QWidget *parent)
    : QWidget(parent)
{

    loadUi();
    connectSignalsSlots();

    loadSettings();

}

void RecordSettingsPage::loadUi()
{
    QUiLoader loader;
    QString uiFile;

    uiFile = KStandardDirs::locate("data", "lintv/ui/record_settings.ui");

    QFile file(uiFile);
    file.open(QFile::ReadOnly);

    formWidget = loader.load(&file, this);
    file.close();

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(formWidget);
    setLayout(layout);

    // Now let's load ALL the widgets! :-)
    recordLocation = qFindChild<KUrlRequester* >(this, "recordLocation");
    
    presetsListWidget = qFindChild<QListWidget* >(this, "presetsListWidget");
    savePresetPushButton = qFindChild<QPushButton* >(this, "savePresetPushButton");

    bitrateSpinBox = qFindChild<QSpinBox* >(this, "bitrateSpinBox");
    frameratesComboBox = qFindChild<QComboBox* >(this, "frameratesComboBox");


    largeResRadioButton = qFindChild<QRadioButton* >(this, "largeResRadioButton");
    largeResRadioButton->setToolTip(i18n("Standard DVD (NTSC) resolution."));
    mediumResRadioButton = qFindChild<QRadioButton* >(this, "mediumResRadioButton");
    mediumResRadioButton->setToolTip(i18n("Standard SVCD (NTSC) resolution."));
    smallResRadioButton = qFindChild<QRadioButton* >(this, "smallResRadioButton");
    smallResRadioButton->setToolTip(i18n("Standard VCD (NTSC) resolution."));
    customResRadioButton = qFindChild<QRadioButton* >(this, "customResRadioButton");
    widthSpinBox = qFindChild<QSpinBox* >(this, "widthSpinBox");
    xLabel = qFindChild<QLabel* >(this, "xLabel");
    heightSpinBox = qFindChild<QSpinBox* >(this, "heightSpinBox");

    formatsComboBox = qFindChild<QComboBox* >(this, "formatsComboBox");
    
    stereoRadioButton = qFindChild<QRadioButton* >(this, "stereoRadioButton");
    monoRadioButton = qFindChild<QRadioButton* >(this, "monoRadioButton");
    noSoundRadioButton = qFindChild<QRadioButton* >(this, "noSoundRadioButton");

    mp3RadioButton = qFindChild<QRadioButton* >(this, "mp3RadioButton");
    mp2RadioButton = qFindChild<QRadioButton* >(this, "mp2RadioButton");
    ac3RadioButton = qFindChild<QRadioButton* >(this, "ac3RadioButton");

    audioBitrateComboBox = qFindChild<QComboBox* >(this, "audioBitrateComboBox");
    averageSizeLabel = qFindChild<QLabel* >(this, "averageSizeLabel");

    highRateRadioButton = qFindChild<QRadioButton* >(this, "highRateRadioButton");
    mediumRateRadioButton = qFindChild<QRadioButton* >(this, "mediumRateRadioButton");
    lowRateRadioButton = qFindChild<QRadioButton* >(this, "lowRateRadioButton");

    audioBitrateGroupBox = qFindChild<QGroupBox * >(this, "audioBitrateGroupBox");
    audioFormatGroupBox = qFindChild<QGroupBox * >(this, "audioFormatGroupBox");
    audioRateGroupBox = qFindChild<QGroupBox * >(this, "audioRateGroupBox");

    /*constantBitrateRadioButton = qFindChild<QRadioButton* >(this, "constantBitrateRadioButton");
    variableBitrateRadioButton = qFindChild<QRadioButton* >(this, "variableBitrateRadioButton");

    peakBitrateSpinBox = qFindChild<QSpinBox* >(this, "peakBitrateSpinBox");
        
    jointStereoRadioButton = qFindChild<QRadioButton* >(this, "jointStereoRadioButton");
    dualRadioButton = qFindChild<QRadioButton* >(this, "dualRadioButton");*/
}

void RecordSettingsPage::connectSignalsSlots()
{
    connect(presetsListWidget, SIGNAL(currentTextChanged(const QString&)),
            this, SLOT(presetsListWidgetChanged(const QString& )));

    connect(bitrateSpinBox, SIGNAL(valueChanged(int )),
            this, SLOT(videoBitrateChanged(int )));

    connect(largeResRadioButton, SIGNAL(clicked(bool )),
            this, SLOT(resRadioButtonClicked(bool )));
    connect(mediumResRadioButton, SIGNAL(clicked(bool )),
            this, SLOT(resRadioButtonClicked(bool )));
    connect(smallResRadioButton, SIGNAL(clicked(bool )),
            this, SLOT(resRadioButtonClicked(bool )));
    connect(customResRadioButton, SIGNAL(toggled(bool )),
            this, SLOT(customRadioButtonToggled(bool )));

    connect(formatsComboBox, SIGNAL(activated(int )),
            this, SLOT(recordFormatActivated(int )));

    /*connect(peakBitrateSpinBox, SIGNAL(valueChanged(int )),
//                 this, SLOT(peakVideoBitrateChanged(int )));*/

    connect(noSoundRadioButton, SIGNAL(toggled(bool )),
            this, SLOT(noSoundToggled(bool )));
}

void RecordSettingsPage::loadSettings()
{
    int recordVideoWidth, recordVideoHeight;
    int audioFrequency;

    recordLocation->setMode(KFile::Directory);
    recordLocation->setUrl(KUrl(LinTVSettings::recordDirectory()));


    bitrateSpinBox->setValue(LinTVSettings::videoBitrate());

    int framerateIndex = 0;
    float framerate = (float)LinTVSettings::videoFramerate();
    if (framerate == 2997) {
        framerate /= 100.0;
    }

    frameratesComboBox->clear();
    for (int i = 0; i < LinTVGlobal::videoFramerates.size(); i++) {
        frameratesComboBox->addItem(floatLocale.toString(
				    LinTVGlobal::videoFramerates[i]));
        if (LinTVGlobal::videoFramerates[i] == framerate) {
            framerateIndex = i;
        }
    }
    frameratesComboBox->setCurrentIndex(framerateIndex);

    recordVideoWidth = LinTVSettings::recordVideoWidth();
    recordVideoHeight = LinTVSettings::recordVideoHeight();
    if (LinTVGlobal::isLargeResolution(recordVideoWidth, recordVideoHeight)) {
        largeResRadioButton->setChecked(true);
    } else if (LinTVGlobal::isMediumResolution(recordVideoWidth, recordVideoHeight)) {
        mediumResRadioButton->setChecked(true);
    } else if (LinTVGlobal::isSmallResolution(recordVideoWidth, recordVideoHeight)) {
        smallResRadioButton->setChecked(true);
    } else {
        customResRadioButton->setChecked(true);
        widthSpinBox->setEnabled(true);
        heightSpinBox->setEnabled(true);
        xLabel->setEnabled(true);
        widthSpinBox->setValue(recordVideoWidth);
        heightSpinBox->setValue(recordVideoHeight);
    }

    QString videoFormat = LinTVSettings::videoFormat();
    for (int i = 0; i < 5; i++) {
        if (videoFormat == LinTVGlobal::videoFormats[i]) {
            formatsComboBox->setCurrentIndex(i);
            break;
        }
    }

    //peakBitrateSpinBox->setValue(LinTVSettings::peakVideoBitrate());

    // Populate the presets list widget. We're doing this at the end because all 
    // the other settings have been loaded so now we can select the correct preset.
    presetsListWidget->clear();
    for (int i = 0; i < NR_OF_PRESETS; i++) {
        presetsListWidget->addItem(LinTVGlobal::Presets[i].presetName);
        if (LinTVGlobal::Presets[i].presetName.contains("Custom")) {
            break;
        }
    }
    checkPresetSettings();

    if (LinTVSettings::audioChannels() == 2) {
        stereoRadioButton->setChecked(true);
    } else if (LinTVSettings::audioChannels() == 1) {
        monoRadioButton->setChecked(true);
    } else if (LinTVSettings::audioChannels() == 0) {
        noSoundRadioButton->setChecked(true);
    }

    audioFrequency = LinTVSettings::audioFrequency();
    if (audioFrequency == 48000) {
        highRateRadioButton->setChecked(true);
    } else if (audioFrequency == 44100) {
        mediumRateRadioButton->setChecked(true);
    } else if (audioFrequency == 38000) {
        lowRateRadioButton->setChecked(true);
    }

    QString audioFormat = LinTVSettings::audioFormat();
    if (audioFormat == "mp3") {
        mp3RadioButton->setChecked(true);
    } else if (audioFormat == "mp2") {
        mp2RadioButton->setChecked(true);
    } else if (audioFormat == "ac3") {
        ac3RadioButton->setChecked(true);
    } else {
        mp2RadioButton->setChecked(true);
    }
}

void RecordSettingsPage::saveSettings()
{
    LinTVSettings::setRecordDirectory(recordLocation->text());

    LinTVSettings::setVideoBitrate(bitrateSpinBox->value());
    if (frameratesComboBox->currentText() == "29.97") {
        LinTVSettings::setVideoFramerate(2997);
    } else {
        LinTVSettings::setVideoFramerate(floatLocale.toInt(frameratesComboBox->currentText()));
    }

    //LinTVSettings::setPeakVideoBitrate(peakBitrateSpinBox->value());

    if (largeResRadioButton->isChecked()) {
        LinTVSettings::setRecordVideoWidth(720);
        LinTVSettings::setRecordVideoHeight(480);
    } else if (mediumResRadioButton->isChecked()) {
        LinTVSettings::setRecordVideoWidth(480);
        LinTVSettings::setRecordVideoHeight(480);
    } else if(smallResRadioButton->isChecked()) {
        LinTVSettings::setRecordVideoWidth(352);
        LinTVSettings::setRecordVideoHeight(240);
    } else if(customResRadioButton->isChecked()) {
        LinTVSettings::setRecordVideoWidth(widthSpinBox->value());
        LinTVSettings::setRecordVideoHeight(heightSpinBox->value());
    }

    LinTVSettings::setVideoFormat(LinTVGlobal::videoFormats[formatsComboBox->currentIndex()]);

    if (stereoRadioButton->isChecked()) {
        LinTVSettings::setAudioChannels(2);
    } else if (monoRadioButton->isChecked()) {
        LinTVSettings::setAudioChannels(1);
    } else if (noSoundRadioButton->isChecked()) {
        LinTVSettings::setAudioChannels(0);
    }

    if (highRateRadioButton->isChecked()) {
        LinTVSettings::setAudioFrequency(48000);
    } else if (mediumRateRadioButton->isChecked()) {
        LinTVSettings::setAudioFrequency(44100);
    } else if (lowRateRadioButton->isChecked()) {
        LinTVSettings::setAudioFrequency(38000);
    }

    if (mp3RadioButton->isChecked()) {
        LinTVSettings::setAudioFormat("mp3");
    } else if (mp2RadioButton->isChecked()) {
        LinTVSettings::setAudioFormat("mp2");
    } else if (ac3RadioButton->isChecked()) {
        LinTVSettings::setAudioFormat("ac3");
    }


    LinTVSettings::self()->writeConfig();
}

void RecordSettingsPage::noSoundToggled(bool toggled)
{
    if (toggled) {
        audioBitrateGroupBox->setEnabled(false);
        audioFormatGroupBox->setEnabled(false);
        audioRateGroupBox->setEnabled(false);
    } else {
        audioBitrateGroupBox->setEnabled(true);
        audioFormatGroupBox->setEnabled(true);
        audioRateGroupBox->setEnabled(true);
    }
}

void RecordSettingsPage::checkPresetSettings()
{
    m_checking = true;
    for (int i = 0; i < NR_OF_PRESETS; i++) {
        if ((widthSpinBox->value() == LinTVGlobal::Presets[i].videoWidth && 
             heightSpinBox->value() == LinTVGlobal::Presets[i].videoHeight) &&
            bitrateSpinBox->value() == LinTVGlobal::Presets[i].videoBitrate &&
            LinTVGlobal::videoFormats[formatsComboBox->currentIndex()] == 
	    LinTVGlobal::Presets[i].videoFormat) 
        {
            kDebug() << "Found matching..." << i;
            /*if (peakBitrateSpinBox->value() == LinTVGlobal::Presets[i].peakVideoBitrate) {
                presetsListWidget->setCurrentRow(i);
            } else {
                break;
            }*/
            presetsListWidget->setCurrentRow(i);
        } else {
            presetsListWidget->setCurrentRow(presetsListWidget->count() - 1);
        }

        if (LinTVGlobal::Presets[i].presetName == "Custom") {
            break;
        }
    }
    m_checking = false;
}

void RecordSettingsPage::presetsListWidgetChanged(const QString& text)
{
    if (m_checking) {
        return;
    }

    for (int i = 0; i < NR_OF_PRESETS; i++) {
        if (text == LinTVGlobal::Presets[i].presetName) {
            bitrateSpinBox->setValue(LinTVGlobal::Presets[i].videoBitrate);

            switch (i) {
            case 0:
            case 1:
                mediumResRadioButton->setChecked(true);
                break;
            case 2:
            case 3:
            case 4:
            case 5:
                largeResRadioButton->setChecked(true);
                break;
            };
            //setAudioBitrate(Presets[i].audioBitrate);
            return ;
        }
        if (text == "Custom") {
            return ;
        }
    }
}

void RecordSettingsPage::customRadioButtonToggled(bool checked)
{
    if (checked) {
        widthSpinBox->setEnabled(true);
        xLabel->setEnabled(true);
        heightSpinBox->setEnabled(true);
    } else {
        widthSpinBox->setEnabled(false);
        xLabel->setEnabled(false);
        heightSpinBox->setEnabled(false);
    }

    checkPresetSettings();
}

void RecordSettingsPage::videoBitrateChanged(int )
{
    checkPresetSettings();
}

void RecordSettingsPage::peakVideoBitrateChanged(int )
{
    checkPresetSettings();
}

void RecordSettingsPage::resRadioButtonClicked(bool )
{
    checkPresetSettings();
}

void RecordSettingsPage::recordFormatActivated(int )
{
    checkPresetSettings();
}

/***************************** SnapshotSettingsPage **********************************/
SnapshotSettingsPage::SnapshotSettingsPage(QWidget *parent)
  : QWidget(parent)
{
    page.setupUi(this);

    page.screenshotDirectory->setMode(KFile::Directory);
    page.screenshotDirectory->setUrl(KUrl(LinTVSettings::screenshotDirectory()));

    int width = LinTVSettings::screenshotWidth();
    int height = LinTVSettings::screenshotHeight();
    if (width == 1600 && height == 1200) {
        page._1600x1200_radioButton->setChecked(true);
    } else if (width == 1280 && height == 1024) {
        page._1280x1024_radioButton->setChecked(true);
    } else if (width == 1024 && height == 768) {
        page._1024x768_radioButton->setChecked(true);
    } else if (width == 800 && height == 600) {
        page._800x600_radioButton->setChecked(true);
    } else if (width == 640 && height == 480) {
        page._640x480_radioButton->setChecked(true);
    } else if (width == 320 && height == 240) {
        page._320x240_radioButton->setChecked(true);
    } else if (width == 0 && height == 0) {
        page.actualSizeRadioButton->setChecked(true);
    } else {
        page.customRadioButton->setChecked(true);
        page.widthSpinBox->setValue(width);
        page.heightSpinBox->setValue(height);
    }

    QString format = LinTVSettings::screenshotFormat();
    if (format == "PNG") {
        page.pngRadioButton->setChecked(true);
    } else if (format == "JPG") {
        page.jpgRadioButton->setChecked(true);
    } else if (format == "BMP") {
        page.bmpRadioButton->setChecked(true);
    }
}

void SnapshotSettingsPage::acceptSettings()
{
    LinTVSettings::setScreenshotDirectory(page.screenshotDirectory->text());

    int width = 1024;
    int height = 768;
    if (page._1600x1200_radioButton->isChecked()) {
        width = 1600; height = 1200;
    } else if (page._1280x1024_radioButton->isChecked()) {
        width = 1280; height = 1024;
    } else if (page._1024x768_radioButton->isChecked()) {
        width = 1024; height = 768;
    } else if (page._800x600_radioButton->isChecked()) {
        width = 800; height = 600;
    } else if (page._640x480_radioButton->isChecked()) {
        width = 640; height = 480;
    } else if (page._320x240_radioButton->isChecked()) {
        width = 320; height = 240;
    } else if (page.actualSizeRadioButton->isChecked()) {
        width = 0; height = 0;
    } else if (page.customRadioButton->isChecked()) {
        width = page.widthSpinBox->value();
        height = page.heightSpinBox->value();
    }
    LinTVSettings::setScreenshotWidth(width);
    LinTVSettings::setScreenshotHeight(height);

    QString format;
    if (page.pngRadioButton->isChecked()) {
        format = "PNG";
    } else if (page.jpgRadioButton->isChecked()) {
        format = "JPG";
    } else if (page.bmpRadioButton->isChecked()) {
        format = "BMP";
    }
    LinTVSettings::setScreenshotFormat(format);
}

void SnapshotSettingsPage::on_customRadioButton_toggled(bool checked)
{
    if (checked) {
        page.widthSpinBox->setEnabled(true);
        page.xLabel->setEnabled(true);
        page.heightSpinBox->setEnabled(true);
        page.pxLabel->setEnabled(true);
    } else {
        page.widthSpinBox->setEnabled(false);
        page.xLabel->setEnabled(false);
        page.heightSpinBox->setEnabled(false);
        page.pxLabel->setEnabled(false);
    }
}

AudioSettingsPage::AudioSettingsPage(QWidget *parent) 
    : QWidget(parent)
{
    page.setupUi(this);

    QStringList list;
    list << "Front Mic" << "Line" << "Mic" << "Aux";
    page.audioMixerComboBox->addItems(list);

    page.audioMixerComboBox->setCurrentIndex(page.audioMixerComboBox->findText(LinTVSettings::audioInput()));
}

void AudioSettingsPage::acceptSettings()
{
    LinTVSettings::setAudioInput(page.audioMixerComboBox->currentText());
}

VideoSettingsPage::VideoSettingsPage(QWidget *parent)
    : QWidget(parent)
{
    page.setupUi(this);

    page.resetDefaultPushButton->setIcon(KIcon("document-revert"));

    connect(page.topCropSpinBox, SIGNAL(valueChanged(int )),
	    this, SLOT(updateResolutionLabel()));
    connect(page.bottomCropSpinBox, SIGNAL(valueChanged(int )),
	    this, SLOT(updateResolutionLabel()));
    connect(page.leftCropSpinBox, SIGNAL(valueChanged(int )),
	    this, SLOT(updateResolutionLabel()));
    connect(page.rightCropSpinBox, SIGNAL(valueChanged(int )),
	    this, SLOT(updateResolutionLabel()));
    connect(page.resetDefaultPushButton, SIGNAL(clicked()),
            this, SLOT(resetDefaults()));

    connect(page.largeResRadioButton, SIGNAL(toggled(bool )),
            this, SLOT(updateResolution()));
    connect(page.mediumResRadioButton, SIGNAL(toggled(bool )),
            this, SLOT(updateResolution()));
    connect(page.smallResRadioButton, SIGNAL(toggled(bool )),
            this, SLOT(updateResolution()));
    connect(page.customResRadioButton, SIGNAL(toggled(bool )),
            this, SLOT(updateResolution()));

    loadSettings();
}

void VideoSettingsPage::acceptSettings()
{
    saveSettings();
}

void VideoSettingsPage::loadSettings()
{
    QString countryName;
    int countryRegionIndex = -1;
    // Load the country/region or standard here...
    for (int i = 0; i < 300; i++) {
        countryName = LinTVGlobal::countryVideoStandards[i].countryName;
        if (countryName == "") {
            break;
        }
    
        if (countryName == LinTVSettings::countryRegion()) {
            countryRegionIndex = i;
        }
        page.countryRegionListWidget->addItem(countryName);
    }
    
    if (LinTVSettings::countryRegion() == "") {
        page.standardRadioButton->setChecked(true); 
        setStandard(LinTVSettings::videoStandard());
    } else {
        page.countryRegionRadioButton->setChecked(true);
        page.countryRegionListWidget->setCurrentRow(countryRegionIndex);
        setStandard(QString(LinTVGlobal::countryVideoStandards[countryRegionIndex].videoStandard));
    }


    selectedStandard = LinTVSettings::videoStandard();

    if (selectedStandard == "SECAM") {
        page.secamStandardNotice->show();
    } else {
        page.secamStandardNotice->hide();
    }
  
    m_width = LinTVSettings::videoWidth();
    m_height = LinTVSettings::videoHeight();

    // Load the video crop area parameters
    page.topCropSpinBox->setValue(LinTVSettings::videoCropTop());
    page.bottomCropSpinBox->setValue(LinTVSettings::videoCropBottom());
    page.leftCropSpinBox->setValue(LinTVSettings::videoCropLeft());
    page.rightCropSpinBox->setValue(LinTVSettings::videoCropRight());

    if (LinTVGlobal::isLargeResolution(m_width, m_height)) {
        page.largeResRadioButton->setChecked(true);
    } else if (LinTVGlobal::isMediumResolution(m_width, m_height)) {
        page.mediumResRadioButton->setChecked(true);
    } else if (LinTVGlobal::isSmallResolution(m_width, m_height)) {
        page.smallResRadioButton->setChecked(true);
    } else {
        page.customResRadioButton->setChecked(true);
        page.widthSpinBox->setEnabled(true);
        page.heightSpinBox->setEnabled(true);
        page.xLabel->setEnabled(true);
        page.widthSpinBox->setValue(m_width);
        page.heightSpinBox->setValue(m_height);
    }

}

void VideoSettingsPage::saveSettings()
{
    if (page.countryRegionRadioButton->isChecked()) {
        int i = page.countryRegionListWidget->currentRow();
        LinTVSettings::setCountryRegion(page.countryRegionListWidget->currentItem()->text());
        LinTVSettings::setVideoStandard(QString(LinTVGlobal::countryVideoStandards[i].videoStandard));
    } else {
        LinTVSettings::setCountryRegion("");
        LinTVSettings::setVideoStandard(page.standardComboBox->currentText());
    }
    
    LinTVSettings::setVideoStandard(selectedStandard);

    LinTVSettings::setVideoCropTop(page.topCropSpinBox->value());
    LinTVSettings::setVideoCropBottom(page.bottomCropSpinBox->value());
    LinTVSettings::setVideoCropLeft(page.leftCropSpinBox->value());
    LinTVSettings::setVideoCropRight(page.rightCropSpinBox->value());


    if (page.customResRadioButton->isChecked()) {
        LinTVSettings::setVideoWidth(page.widthSpinBox->value());
        LinTVSettings::setVideoHeight(page.heightSpinBox->value());
    } else {
        LinTVSettings::setVideoWidth(m_width);
        LinTVSettings::setVideoHeight(m_height);
    }
}

void VideoSettingsPage::setStandard(QString standard)
{
    int index = 0;
    if (standard == NTSC) {
        index = 0;
    } else if (standard == PAL) {
        index = 1;  
    } else if (standard == SECAM) {
        index = 2;
    }

    page.standardComboBox->setCurrentIndex(index);
}

void VideoSettingsPage::updateResolutionLabel()
{
    int width;
    int height;

    int topCrop, bottomCrop, leftCrop, rightCrop;

    topCrop = page.topCropSpinBox->value();
    bottomCrop = page.bottomCropSpinBox->value();
    leftCrop = page.leftCropSpinBox->value();
    rightCrop = page.rightCropSpinBox->value();

    if (topCrop + bottomCrop >= m_height + 16) {
        return;
    }

    if (leftCrop + rightCrop >= m_width + 16) {
        return;
    }

    width = m_width - topCrop - bottomCrop;
    height = m_height - leftCrop - rightCrop;

    page.videoDimensionsLabel->setText(i18n("<b>%1x%2</b>", width, height));
}

void VideoSettingsPage::resetDefaults()
{
    page.topCropSpinBox->setValue(0);
    page.leftCropSpinBox->setValue(0);
    page.rightCropSpinBox->setValue(0);
    page.bottomCropSpinBox->setValue(0);
}

void VideoSettingsPage::updateResolution()
{
    if (page.largeResRadioButton->isChecked()) {
        m_width = 720;
        m_height = 480;
    } else if (page.mediumResRadioButton->isChecked()) {
        m_width = 480;
        m_height = 480;
    } else if(page.smallResRadioButton->isChecked()) {
        m_width = 352;
        m_height = 240;
    } else if(page.customResRadioButton->isChecked()) {
        m_width = page.widthSpinBox->value();
        m_height = page.heightSpinBox->value();
    }

    if (page.customResRadioButton->isChecked()) {
        page.widthSpinBox->setEnabled(true);
        page.xLabel->setEnabled(true);
        page.heightSpinBox->setEnabled(true);
    } else {
        page.widthSpinBox->setEnabled(false);
        page.xLabel->setEnabled(false);
        page.heightSpinBox->setEnabled(false);
    }

    updateResolutionLabel();
}

void VideoSettingsPage::on_countryRegionRadioButton_toggled(bool enabled)
{
    if (enabled) {
        page.countryRegionListWidget->setEnabled(true);
        page.standardComboBox->setEnabled(false);
    } else {
        page.countryRegionListWidget->setEnabled(false);
        page.standardComboBox->setEnabled(true);
    }
}

void VideoSettingsPage::on_countryRegionListWidget_currentRowChanged(int row)
{
    selectedStandard = QString(LinTVGlobal::countryVideoStandards[row].videoStandard);
    page.standardComboBox->setCurrentIndex(page.standardComboBox->findText(selectedStandard));
}

void VideoSettingsPage::on_standardComboBox_currentIndexChanged(const QString & text)
{
    selectedStandard = text;
    if (text == "SECAM") {
        page.secamStandardNotice->show();
    } else {
        page.secamStandardNotice->hide();
    }
}

#include "lintvpages.moc"