/*
 * Copyright (C) 2002 Richard Moore <rich@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 <qslider.h>
 
#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>
#include <kaction.h>
#include <kiconloader.h>

#include "actions.h"
#include "qtvision.h"
#include "qtvisionwidget.h"

#include "qtvisionactions.h"
#include "qtvisionactions.moc"

QtVisionActions::QtVisionActions( QtVision *qtvision, const char *name )
                :QObject( qtvision, name ? name : "qtvision_actions" ),
                 qtv( qtvision )
{
    connect(qtv, SIGNAL(deviceChanged(QVSourcePlugin *)),
            SLOT( deviceChanged(QVSourcePlugin *)));
}

QtVisionActions::~QtVisionActions()
{
}

void QtVisionActions::createActions( KActionCollection *acts )
{
    // Standard Actions
    showSettings = KStdAction::preferences( qtv, SLOT( settings() ), acts );

    showKeys = KStdAction::keyBindings( this, SIGNAL( launchBindings() ), acts);

    // Application Actions
    chanOpen = KStdAction::create(KStdAction::Open, "channel_open", qtv, 
                                  SLOT( openChannelFile() ), acts );

    chanImport = new KAction(i18n("Other channels..."), 0, 0,
                                  qtv, SLOT( importChannelFile() ),
                                  acts, "channel_import" );

    chanLoadDefaults = new KAction( i18n("&Local Defaults..."), 0, 0, qtv, 
                                    SLOT( importDefaultChannels() ), acts,
                                    "channel_load_defaults" );

    chanImportLegacy = new KAction( i18n("Old &KWinTV Channels..."), 0, 0, qtv,
                                    SLOT( importLegacyChannels() ), acts,
                                    "channel_import_legacy" );

    chanDown = new KAction( i18n("Channel &Down"), "down", Key_Down, qtv,
                            SLOT( channelDown() ), acts, "channel_down" );

    chanUp = new KAction( i18n("Channel &Up"), "up", Key_Up, qtv, 
                          SLOT( channelUp() ), acts, "channel_up" );

    chanWiz = new KAction( i18n("Channel &Wizard"), "wizard", 0,
                           qtv, SLOT( launchWizard() ), acts, 
                           "channel_wizard" );

    snap = new KAction( i18n("Save &Snapshot"), "ktv_snapshot", Key_S,
                        qtv, SLOT( snapshot() ), acts, "save_snapshot" );

    chngSrc = new KAction( i18n("Change &Source"), "rotate", 0,
                           qtv, SLOT( selectDevice() ), acts,
                           "change_source" );

    volMute = new KToggleAction( i18n("Mute"), "ktv_muteon", Key_Asterisk,
                                 qtv, SLOT( volumeMute() ), acts, 
                                 "volume_mute" );

    volUp = new KAction( i18n("Volume Up"), "ktv_volup", Key_Right,
                         qtv, SLOT( volumeUp() ), acts, "volume_up" );

    volDown = new KAction( i18n("Volume Down"), "ktv_voldown", Key_Left,
                           qtv, SLOT( volumeDown() ),
                           acts, "volume_down" );
                           
    prevChannel = new KAction( i18n("Previous Channel"), "back", Key_L,
                               qtv, SLOT( previousChannel() ), acts,
                               "last_channel" );

    // Custom Actions
    chan = new LCDNumberAction( i18n("Current Channel"), 0, 0, 0, acts, 
                                "channel_number" );
    chan->setNumDigits( 3 );
    
    connect( qtv, SIGNAL(channelChanged(int)), chan, SLOT(display(int)) );
    connect( qtv, SIGNAL(channelChanged(const QString &)), chan, 
             SLOT(setText(const QString &)) );

    volSlider = new SliderAction( 0, 100, 1, 50, i18n("Volume"), acts,
                                  "volume_slider" );
    
    volSlider->setTickInterval( 5 );
  
    connect(volSlider, SIGNAL(valueChanged(int)), qtv, SLOT(setVolume(int)));
    connect(qtv, SIGNAL(volumeChanged(int,int)), this, SLOT(updateSlider(int, int)));
    connect(qtv, SIGNAL(volumeMuted(bool)), this, SLOT(updateMuteButton(bool)));    
}

void QtVisionActions::createActions( QtVisionWidget *qtvw, KActionCollection *acts )
{
    createActions( acts );

    // View Modes
    showNorm = new KRadioAction( i18n("&Normal"), "kwintv", Key_Home, acts, "show_normal"  );
    showTop = new KRadioAction( i18n("T&V Mode"), "inline_image", Key_PageDown, acts, "show_top_level"  );
    showFull = new KRadioAction( i18n("&Full Screen"), "window_fullscreen", Key_PageUp, acts, "full_screen"  );
    showDesk = new KRadioAction( i18n("Video &Desktop"), "frame_image", 0, acts, "show_video_desktop"  );

    showNorm->setExclusiveGroup( "view_modes" );
    showTop->setExclusiveGroup( "view_modes" );
    showFull->setExclusiveGroup( "view_modes" );
    showDesk->setExclusiveGroup( "view_modes" );

    showNorm->setChecked( true );

    nextView = new KAction( i18n("Next View"), "next", 0, qtvw, SLOT( nextViewMode() ), acts,
	    "next_view_mode" );
    toggleFull = new KAction( i18n("Toggle FullScreen"), "window_fullscreen", Key_F,
			      qtvw, SLOT( toggleFullScreen() ),
			      acts, "toggle_fullscreen" );

    connect( showNorm, SIGNAL(toggled(bool)), qtvw, SLOT(setShowNormal(bool)) );
    connect( showTop, SIGNAL(toggled(bool)), qtvw, SLOT(setShowTopLevel(bool)) );
    connect( showFull, SIGNAL(toggled(bool)), qtvw, SLOT(setShowFullScreen(bool)) );
    connect( showDesk, SIGNAL(toggled(bool)), qtvw, SLOT(setShowVideoDesktop(bool)) );
    connect( qtvw, SIGNAL(viewModeChanged(int)), this, SLOT(updateViewMode(int)));
}

void QtVisionActions::deviceChanged( QVSourcePlugin *dev )
{
    if ( !chngSrc )
        return;

    chngSrc->setEnabled( true );
    showSettings->setEnabled( true );

    if ( dev ) {
        chan->display( qtv->channel() != NULL ? 
                       QString::number(qtv->channel()->number()) 
                       : QString("---") );
        chanDown->setEnabled( dev->isTuner() );
        chanUp->setEnabled( dev->isTuner() );
        snap->setEnabled( true );
    }
    else {
        chan->display( "---" );
        chanDown->setEnabled( false );
        chanUp->setEnabled( false );
        snap->setEnabled( false );
    }
}

void QtVisionActions::updateSlider(int volLeft, int volRight)
{    
    volUp->setEnabled(volLeft != 100);
    volDown->setEnabled(volLeft != 0);    
    volSlider->setValue (volLeft);
}

void QtVisionActions::updateMuteButton (bool muted)
{
    volMute->setChecked(muted);
}

void QtVisionActions::updateViewMode( int mode )
{
    if ( mode == QtVisionWidget::ViewFullScreen ) {
        showFull->setChecked( true );
	showFull->setIcon( "window_nofullscreen" );
    }
    else
	showFull->setIcon( "window_fullscreen" );

    if ( mode == QtVisionWidget::ViewNormal )
        showNorm->setChecked( true );
    else if ( mode == QtVisionWidget::ViewTopLevel )
        showTop->setChecked( true );
}


