/*
 *
 * Copyright (C) 2002 Rich Moore <rich@kde.org>
 * Copyright (C) 2002 George Staikos <staikos@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


#include <qcheckbox.h>
#include <qcombobox.h>
#include <qhbox.h>
#include <qmessagebox.h>
#include <qprogressbar.h>
#include <qpushbutton.h>
#include <qtabdialog.h>
#include <qslider.h>
#include <qspinbox.h>

#include <kapplication.h>
#include <kdebug.h>
#include <kdialogbase.h>
#include <klocale.h>
#include <kmessagebox.h>

#include "chwizard.h"
#include "channelscanner.h"
#include "channeleditor.h"
#include "devicewidgetimpl.h"
#include "qtvision.h"
#include "qvsrcplugin.h"
#include "pluginfactory.h"
#include "settingsdialog.h"
#include "cfgdata.h"

#include "viewmanager.h"
#include "viewmanager.moc"

ViewManager::ViewManager( QtVision *qtvision, const char *name )
    : QObject( qtvision, name ? name : "view_manager" ),
      dev(0), qtv( qtvision ), cfg( qtvision->config() ),
      settingsDialog(0L)
{
    connect( qtv, SIGNAL( deviceChanged( QVSourcePlugin * ) ), this, SLOT( update() ) );
}

ViewManager::~ViewManager()
{

}

void ViewManager::update()
{
    dev = qtv->device();
    cfg = qtv->config();
}

int ViewManager::launchSourceDialog( QWidget *parent )
{
    kdDebug() << "ViewManager::launchSourceDialog()" << endl;

    KDialogBase *dlg = new KDialogBase(parent, "ssdlg", true, i18n("Select Source"),
                                       KDialogBase::Help | KDialogBase::Ok | KDialogBase::Cancel,
                                       KDialogBase::Ok, true);

    QHBox *hbox = dlg->makeHBoxMainWidget();
    ssdlg = new DeviceWidgetImpl(hbox, cfg, qtv);
    ssdlg->setCurrentDevice();

    int rc = dlg->exec();
    if ( rc == KDialogBase::Rejected ) {
        delete dlg;
        dlg = 0;
        return rc;
    }

    ssdlg->device(qtv);

    delete dlg;
    dlg = 0;
    return(rc);
}


int ViewManager::launchWizard( QWidget* /*parent*/)
{
int result = 0;;
    if (!dev)
        return -1;   // Only works with V4L right now  FIXME

#if 0
    ChannelWizard *cw = new ChannelWizard( parent, "Wizard", true );
    cw->_encoding->setCurrentItem( dev->tunerMode() );

    ChannelScanner *scanner = new ChannelScanner( cw, "scanner" );
    scanner->setDevice( dev );
    scanner->setStore( qtv->channels() );
    // Disabled until I find the right units for the frequencies (rich)
    // scanner->loadRegionFile( "data/western-europe.xml" );

    connect( cw->_go, SIGNAL( clicked() ), scanner, SLOT( scan() ) );
    connect( cw->_encoding, SIGNAL(highlighted(int)), qtv, SLOT(setTunerMode(int)));
    connect( scanner, SIGNAL( done() ), cw, SLOT( accept() ) );
    connect( scanner, SIGNAL( progress(int) ), cw->_progress, SLOT( setProgress(int) ) );

    result = cw->exec();
    delete cw;
#endif

    return result;
}


int ViewManager::launchSettings( QWidget *parent )
{
    if (settingsDialog == 0L) {
        settingsDialog = new SettingsDialog(parent, 0, false, cfg, qtv);
    } // if

    connect(settingsDialog, SIGNAL(optionsModified(ConfigData *)), this, SLOT(setOptions(ConfigData *)));
    connect(settingsDialog, SIGNAL(hueChanged(int)), this, SLOT(slotHueChanged(int)));
    connect(settingsDialog, SIGNAL(colourChanged(int)), this, SLOT(slotColourChanged(int)));
    connect(settingsDialog, SIGNAL(brightnessChanged(int)), this, SLOT(slotBrightnessChanged(int)));
    connect(settingsDialog, SIGNAL(whitenessChanged(int)), this, SLOT(slotWhitenessChanged(int)));
    connect(settingsDialog, SIGNAL(contrastChanged(int)), this, SLOT(slotContrastChanged(int)));

    settingsDialog->setCurrentDevice();
    settingsDialog->show();
    connect(settingsDialog, SIGNAL(finished()), this, SLOT(destroySettings()));
    return(0); // success
}


void ViewManager::destroySettings() {
    if (settingsDialog) {
        settingsDialog->delayedDestruct();
        settingsDialog = 0;
    }
}


void ViewManager::slotHueChanged(int hue)
{
    qtv->setHue(hue);
    return;
} // slotHueChanged

void ViewManager::slotColourChanged(int colour)
{
    qtv->setColour(colour);
    return;
} // slotColourChanged

void ViewManager::slotBrightnessChanged(int brightness)
{
    qtv->setBrightness(brightness);
    return;
} // slotBrightnessChanged

void ViewManager::slotWhitenessChanged(int whiteness)
{
    qtv->setWhiteness(whiteness);
    return;
} // slotWhitenessChanged

void ViewManager::slotContrastChanged(int contrast)
{
    qtv->setContrast(contrast);
    return;
} // slotContrastChanged

void ViewManager::setOptions(ConfigData *newConfig)
{ 
    if (newConfig->showSelectedOnly != cfg->showSelectedOnly)
        emit showSelectedChanged(newConfig->showSelectedOnly);
    if (newConfig->fixAR != cfg->fixAR)
        emit setFixedAspectRatio(newConfig->fixAR, newConfig->ARmode);
    if (newConfig->ARmode != cfg->ARmode)
        emit setFixedAspectRatio(newConfig->fixAR, newConfig->ARmode);
    if ( (newConfig->disableScreenSaver != cfg->disableScreenSaver)
       ||(newConfig->screenSaverMode != cfg->screenSaverMode) ) {
        emit disableScreenSaver(newConfig->disableScreenSaver,
                                newConfig->screenSaverMode);
    }
    if (newConfig->stayOnTop != cfg->stayOnTop);
        emit stayOnTopChanged(newConfig->stayOnTop);
    if (newConfig->useListingsURL != cfg->useListingsURL);
        emit useListingsURL(newConfig->useListingsURL);
    if (newConfig->listingsURL != cfg->listingsURL)
        emit getNewListingsURL(&newConfig->listingsURL);
    
    // Apply the new options
    *cfg = *newConfig;
    cfg->save();    

} // setOptions

ChannelEditor *ViewManager::createChannelEditor( QWidget *parent )
{
    ChannelEditor *ce = new ChannelEditor( parent, "chedit", cfg->showSelectedOnly );
    
    QString cap;
    if ( parent )
        cap = i18n("Channels");
    else
        cap = kapp->makeStdCaption(i18n("Channels"));
    
    ce->setCaption( cap );
    
    connect(qtv, SIGNAL(channelChanged(Channel*)), ce, SLOT(ensureSelected(Channel*)));
    
    return ce;
}

ConfigData *ViewManager::getConfig(){
    return cfg;
}
