/*
 *
 * Copyright (C) 2002 Richard Moore <rich@kde.org>
 *
 *   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.
 *
 */

#include <q3accel.h>
#include <qlcdnumber.h>
#include <qpoint.h>
#include <qslider.h>
#include <q3stylesheet.h>
#include <qtooltip.h>
#include <q3vbox.h>
//Added by qt3to4:
#include <Q3Frame>
#include <QPixmap>
#include <QLabel>
#include <Q3PopupMenu>

#include <kdebug.h>
#include <klocale.h>
#include <kmainwindow.h>
#include <kmenubar.h>
#include <kstatusbar.h>
#include <ktoolbar.h>
#include <k3dockwidget.h>
#include <Q3DockWindow>

#include "kdetv.h"
#include "actions.h"
#include "actions.moc"

//
// LCD Number Action
//

LCDNumberAction::LCDNumberAction( const QString &text, int accel,
                                  QObject* receiver, const char* slot,
                                  QObject *parent, const char *name )
    :KAction( text, parent),
     numDig(3), val("  0")
{
    setShortcut( accel );
    connect( parent, SIGNAL(activate()), receiver, SLOT( slot ) );
}

int LCDNumberAction::plug( QWidget *widget, int index )
{
#warning "kde4 port it";
#if 0
    //do not call the previous implementation here

    if ( widget->inherits( "KToolBar" ) ) {

        KToolBar *tb = static_cast<KToolBar *>(widget);
        int id = KAction::getToolButtonID();

        QLCDNumber *lcd = createWidget( tb );
        tb->insertWidget( id, lcd->width(), lcd, index );
        addContainer( tb, id );

        connect( tb, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
        return containerCount() - 1;
    }

    if ( widget->inherits( "QPopupMenu" ) ) {
        Q3PopupMenu *pop = static_cast<Q3PopupMenu *>(widget);

        // Create item
        Q3HBox *box = new Q3HBox( pop );
        box->setMargin( 4 );
        box->setSpacing( 6 );

        // Icon
        QLCDNumber *num = createWidget( box );
        num->setFixedSize( QSize(18,18) );

        // Label
        QLabel *l = new QLabel( box );
        l->setText( text() );

        // Insert item
        int id = pop->insertItem( box, -1, index );
        addContainer( box, id );
        connect( box, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );

        return containerCount() - 1;
    }
#endif
    return -1;
}

void LCDNumberAction::unplug( QWidget *widget )
{
#warning "kde4 port it";
#if 0
    if ( widget->inherits( "KToolBar" ) ) {
        KToolBar *bar = static_cast<KToolBar *>(widget);
        int idx = findContainer( bar );

        if ( idx != -1 ) {
            bar->removeItem( menuId( idx ) );
            removeContainer( idx );
        }
    }
    else if ( widget->inherits( "QPopupMenu" ) ) {
        Q3PopupMenu *pop = static_cast<Q3PopupMenu *>(widget);

        int idx = findContainer( pop );
        if ( idx != -1 ) {
            pop->removeItem( menuId( idx ) );
            removeContainer( idx );
        }
    }
    #endif
}

QWidget *LCDNumberAction::createWidget( QWidget *parent )
{
    QLCDNumber *lcd = new QLCDNumber( parent );
    lcd->setFrameStyle( Q3Frame::Panel | Q3Frame::Sunken );
    lcd->setLineWidth(2);
    lcd->setMidLineWidth(1);

    lcd->setSegmentStyle( QLCDNumber::Flat );
    lcd->setBackgroundColor( Qt::black );
    lcd->setPaletteForegroundColor( Qt::green );

    lcd->setNumDigits( numDig );
    lcd->display( val );

    if ( !text().isEmpty() )
        QToolTip::add( lcd, text() );

    connect( this, SIGNAL( valueChanged(const QString &) ),
             lcd, SLOT( display(const QString &) ) );
    return lcd;
}

void LCDNumberAction::setNumDigits( int nDigits )
{
    numDig = nDigits;
}

void LCDNumberAction::display( int num )
{
    display( QString::number(num) );
}

void LCDNumberAction::display( const QString &s )
{
    val = s;
    emit valueChanged( val );
}

//
// Slider Action
//

KdetvSlider::KdetvSlider( int minValue, int maxValue, int pageStep, int value, Qt::Orientation o,
                          QWidget *parent, const char* name )
    : QSlider( minValue, maxValue, pageStep, value, o, parent, name)
{
}

KdetvSlider::~KdetvSlider()
{
}

void KdetvSlider::setOrientation( Qt::Orientation o )
{
    QSlider::setOrientation(o);
    emit orientationChanged(o);
}

void KdetvSlider::updateOrientation()
{
    if (!parent()->inherits("QDockWindow"))
        return;

    Q3DockWindow* d = static_cast<Q3DockWindow*>(parent());
    setOrientation( d->orientation() );
}


SliderAction::SliderAction( int min_, int max_, int step_, int val_,
                            const QString &text, QObject *parent,
                            const char *name )
    : KAction( parent ), inhibitRecursion(false), min(min_),
      max(max_), step(step_), val(val_), tickStep(-1)
{
    setText( text );
    setObjectName( name );
}

int SliderAction::plug( QWidget *widget, int index )
{
#warning "kde4 port it";
#if 0
    //do not call the previous implementation here
    kDebug() << "Plugging Slider into class '" << widget->className() << "'";

    if ( widget->inherits( "KToolBar" ) || widget->isA("KToolBar")) {
        KToolBar *bar = static_cast<KToolBar *>(widget);

        int id = KAction::getToolButtonID();

        KdetvSlider* s = createWidget( bar );
        s->setOrientation( bar->orientation() );

        int sz = (s->orientation() == Qt::Horizontal) ? s->width() : s->height();
        bar->insertWidget( id, sz, s, index );
        addContainer( bar, id );

        connect( bar, SIGNAL( orientationChanged(Qt::Orientation) ),
                 s, SLOT( setOrientation(Qt::Orientation) ) );

        /*
         * FIXME: This is a really bad hack to get events
         *        _after_ the GUI has been created.
         *        If I don't do this, the slider orientation
         *        is not updated at startup.
         *        It seems QToolBar does not emit orientationChanged()
         *        during startup phase.
         *        This connection creates loads of unnesscessary
         *        updateOrientation() calls...
         *        I tried lots of other signals (QToolBar::visibilityChanged(),
         *        QMainWindow::dockWindowPositionChanged() and many others)
         *        but none of them works.
         */
        if (bar->mainWindow()->inherits("K3DockMainWindow")) {
            K3DockMainWindow* kdmw = static_cast<K3DockMainWindow*>(bar->mainWindow());
            connect( kdmw->manager(), SIGNAL( change() ),
                     s, SLOT( updateOrientation() ) );
        }

        connect( bar, SIGNAL( destroyed() ),
                 this, SLOT( slotDestroyed() ) );

        return containerCount() - 1;
    }

    if ( widget->inherits( "QPopupMenu" ) ) {
        Q3PopupMenu *pop = static_cast<Q3PopupMenu *>(widget);

        // Create item
        Q3HBox *box = new Q3HBox( pop );
        box->setMargin( 4 );
        box->setSpacing( 6 );

        // Icon
        QLabel *p = new QLabel( box );
        if ( hasIconSet() ) {
            QPixmap pix = iconSet().pixmap( QIcon::Small, true );
            p->setPixmap( pix );
        }
        p->setFixedSize( QSize(18,18) );

        // Label
        QLabel *l = new QLabel( box );
        l->setText( text() );

        // Slider
        KdetvSlider* s = createWidget( box );

        // Focus handling
        s->setFocusPolicy( Qt::TabFocus );
        l->setBuddy( s );
        box->setFocusProxy( s );

        // Insert item
        int id = pop->insertItem( box, -1, index );
        addContainer( pop, id );
        connect( box, SIGNAL( destroyed() ),
                 this, SLOT( slotDestroyed() ) );

        return containerCount() - 1;
    }
#endif
    return -1;

}

void SliderAction::unplug( QWidget *widget )
{
#warning "kde4 port it";
   #if 0
    if ( widget->inherits( "KToolBar" ) )
        {
            KToolBar *bar = static_cast<KToolBar *>(widget);

            int idx = findContainer( bar );

            if ( idx != -1 ) {
                bar->removeItem( menuId( idx ) );
                removeContainer( idx );
            }

            return;
        }

    if ( widget->inherits( "QPopupMenu" ) ) {
        Q3PopupMenu *pop = static_cast<Q3PopupMenu *>(widget);

        int idx = findContainer( pop );

        if ( idx != -1 ) {
            pop->removeItem( menuId( idx ) );
            removeContainer( idx );
        }

        return;
    }
#endif
}

void SliderAction::setOrientation( Qt::Orientation o )
{
    const QObject* cobj = sender();
    QObject* obj = const_cast<QObject*>(cobj);

    if (!obj->inherits("KdetvSlider"))
        return;

    KdetvSlider* s = static_cast<KdetvSlider*>(obj);

    switch( o ) {
    case Qt::Horizontal:
        s->setTickmarks( QSlider::TicksBelow );
        disconnect( s, SIGNAL(valueChanged(int)), this, SLOT(setInverseInternal(int)) );
        connect( s, SIGNAL(valueChanged(int)), this, SLOT(setValueInternal(int)));
        s->setValue( val );
        break;
    case Qt::Vertical:
        s->setTickmarks( QSlider::Right );
        disconnect( s, SIGNAL(valueChanged(int)), this, SLOT(setValueInternal(int)));
        connect( s, SIGNAL(valueChanged(int)), this, SLOT(setInverseInternal(int)) );
        s->setValue( max-val );
        break;
    default:
        break;
    }
}

void SliderAction::setValueInternal( int num )
{
    if (inhibitRecursion)
        return;

    inhibitRecursion = true;
    setValue(num);
    inhibitRecursion = false;
    emit valueChanged(num);
}

void SliderAction::setInverseInternal( int num )
{
    setValueInternal (max - num);
}

void SliderAction::setValue( int num )
{
    if (val == num)
        return;
    val = num;

    for (Q3PtrListIterator<KdetvSlider> it(_sliders);
         it.current() != 0;
         ++it) {
        if (it.current()->orientation() == Qt::Horizontal) {
            it.current()->setValue(val);
        } else {
            it.current()->setValue(max - val);
        }
    }
}

void SliderAction::setTickInterval( int ticks )
{
    tickStep = ticks;
    for (Q3PtrListIterator<KdetvSlider> it(_sliders);
         it.current() != 0;
         ++it) {
        it.current()->setTickInterval( ticks );
    }
}

QWidget *SliderAction::createWidget( QWidget *parent )
{
    KdetvSlider *s = new KdetvSlider( min, max, step, val, Qt::Horizontal, parent );

    _sliders.append(s);
    connect( s, SIGNAL( destroyed(QObject*) ),
             this, SLOT( removeSlider(QObject*) ) );

    connect( s, SIGNAL( valueChanged(int) ),
             this, SLOT( setValueInternal(int) ) );
    connect( s, SIGNAL( orientationChanged(Qt::Orientation) ),
             this, SLOT( setOrientation(Qt::Orientation) ) );

    if ( tickStep > 0 )
        s->setTickInterval( tickStep );

    s->setTickmarks( QSlider::TicksBelow );

    if ( !text().isEmpty() )
        QToolTip::add( s, text() );

    return s;
}

void SliderAction::removeSlider( QObject* o )
{
    KdetvSlider* s = static_cast<KdetvSlider*>(o);
    _sliders.remove(s);
}

int SliderAction::value() const
{
    return val;
}

//
// Channel import/export action
//

ImpExChannelsAction::ImpExChannelsAction( const QString &text, int accel, const QString& fmt, Kdetv* ktv,
                                          int direction, QObject *parent, const char *name)
    : KAction(parent),
      _fmt(fmt),
      _ktv(ktv),
      _direction(direction)
{
    setShortcut( accel );
    setObjectName( name );
    setText( text );
    connect(this, SIGNAL( activated() ),
            this, SLOT( slotActivated() ));
}

ImpExChannelsAction::~ImpExChannelsAction()
{
}

void ImpExChannelsAction::slotActivated()
{
    if (_direction) {
        _ktv->importChannelFile(_fmt);
    } else {
        _ktv->exportChannelFile(_fmt);
    }
}

//
// Device action
//

DeviceAction::DeviceAction( const QString &text, int accel, const QString& dev,
                            Kdetv* ktv, QObject *parent, const char *name)
    : KToggleAction(text, parent),
      _dev(dev),
      _ktv(ktv)
{
    setShortcut( accel );
    setObjectName( name );
    connect(this, SIGNAL( activated() ),
            this, SLOT( slotActivated() ));
}

DeviceAction::~DeviceAction()
{
}

void DeviceAction::slotActivated()
{
    _ktv->playDevice(_dev);
}

//
// AudioMode action
//

AudioModeAction::AudioModeAction( const QString &text, int accel, const QString& mode,
                                  Kdetv* ktv, QObject *parent, const char *name)
    : KToggleAction(text, parent),
      _mode(mode),
      _ktv(ktv)
{
    setObjectName( name );
    setShortcut( accel );
    connect(this, SIGNAL( activated() ),
            this, SLOT( slotActivated() ));
}

AudioModeAction::~AudioModeAction()
{
}

void AudioModeAction::slotActivated()
{
    _ktv->setAudioMode(_mode);
}
