/*
 *
 * 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., 51 Franklin Steet, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */


#include <qcheckbox.h>
#include <qfont.h>
#include <qpalette.h>
#include <qpoint.h>
#include <qslider.h>
#include <qspinbox.h>
#include <qtimer.h>
#include <qwidget.h>
//Added by qt3to4:
#include <QPixmap>

#include <kconfig.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kmenu.h>

#include "elegant.h"
#include "elegant.moc"

KdetvElegant::KdetvElegant(Kdetv *ktv, QWidget *p, const char *name)
    : KdetvOSDPlugin( ktv, "elegant-osd", p, name ),
      parent(p),
      align(Qt::AlignBottom|Qt::AlignCenter), indent(20),
      iconSize(KIconLoader::SizeLarge), fontSize(48),
      fontBold(true), fontItalic(false), fontUnderline(false), autoFontSize(false)
{
    timer = new QTimer( this );
    // popup = new KPopupTitle( p, "display" );
    popup = new KMenu( "display", p );
    popup->hideContextMenu();

    // FIXME: Don't use system colors, they may not work or look bad as OSD
    // TODO: Use new KDE OSD
    // popup->setPaletteBackgroundColor( QColor( 200, 200, 200 ) );

    readConfig();
    applySettings();

    connect( timer, SIGNAL( timeout() ), popup, SLOT( hide() ) );
    connect( parent, SIGNAL( resized(int,int) ), this, SLOT( viewResized(int,int) ) );

    viewResized(p->width(), p->height());
}

KdetvElegant::~KdetvElegant()
{
    kDebug() << "Elegant: destroying plugin.";
}

void KdetvElegant::applySettings()
{
    QFont font = popup->font();
    font.setPointSize( fontSize );
    font.setBold( fontBold );
    font.setItalic( fontItalic );
    font.setUnderline( fontUnderline );
    popup->setFont( font );

    updateGeometry();
}

void KdetvElegant::readConfig()
{
    kDebug() << "Elegant: reading config";
    
    KConfigGroup grp = _cfg->group( "Elegant OSD");
    timeout = grp.readEntry( "Timeout", 2000 );
    indent = grp.readEntry( "Indent", 20 );
    align = grp.readEntry( "Alignment", (int)(Qt::AlignBottom|Qt::AlignCenter) );
    iconSize = grp.readEntry( "IconSize", (int)KIconLoader::SizeLarge );
    fontSize = grp.readEntry( "FontSize", 48 );
    fontBold = grp.readEntry( "FontBold", true );
    fontItalic = grp.readEntry( "FontItalic", false );
    fontUnderline = grp.readEntry( "FontUnderline", false );
    autoFontSize = grp.readEntry( "AutoFontSize", true );
}

void KdetvElegant::saveConfig()
{
    applyConfigWidget();

    kDebug() << "Elegant: writing config";
    
    KConfigGroup grp = _cfg->group( "Elegant OSD");
    grp.writeEntry( "Timeout", timeout );
    grp.writeEntry( "Indent", indent );
    grp.writeEntry( "Alignment", align );
    grp.writeEntry( "IconSize", iconSize );
    grp.writeEntry( "FontSize", fontSize );
    grp.writeEntry( "FontBold", fontBold );
    grp.writeEntry( "FontItalic", fontItalic );
    grp.writeEntry( "FontUnderline", fontUnderline );
    grp.writeEntry( "AutoFontSize", autoFontSize );
    grp.sync();
}

void KdetvElegant::updateGeometry()
{
    popup->resize( popup->minimumSizeHint() );

    int x;
    if ( align & Qt::AlignLeft )
        x = indent;
    else if ( align & Qt::AlignRight )
        x = parent->width() - indent - popup->width();
    else
        x = parent->width()/2 - popup->width()/2;

    int y;
    if ( align & Qt::AlignTop )
        y = indent;
    else if ( align & Qt::AlignBottom )
        y = parent->height() - indent - popup->height();
    else
        y = parent->height()/2 - popup->height()/2;

    popup->move( QPoint(x,y) );
}

void KdetvElegant::clear()
{
    static QPixmap kwicon = KIconLoader::global()->loadIcon("kdetv", KIconLoader::NoGroup, iconSize);
    // FIXME: popup->setTitle(i18n("Elegant OSD"), &kwicon);
}

void KdetvElegant::showPopup()
{
    if ( popup->isHidden() )
        popup->show();
    else {
        popup->update();
        popup->raise();
    }

    updateGeometry();
    timer->setSingleShot(true);
    timer->start(timeout);
}

void KdetvElegant::display( const QString &text, const QPixmap &icon )
{
    // FIXME: popup->setTitle(text, &icon);
    showPopup();
}

void KdetvElegant::display( const QString &text, const QString &icon )
{
    QString i = icon.isNull() ? QString("kdetv") : icon;
    display( text, KIconLoader::global()->loadIcon( i, KIconLoader::NoGroup, iconSize ) );
}

void KdetvElegant::displayMisc( const QString &text )
{
    display( text, "kdetv" );
}

void KdetvElegant::displayChannel( int channel, const QString &name )
{
    display( i18n("%1 - %2",channel,name), "kdetv" );
}

void KdetvElegant::displayMuted( bool muted )
{
    if ( muted )
        display( i18n("Mute"), "player_mute" );
    else
        display( i18n("Unmute"), "player_volume_up" );
}

void KdetvElegant::displayVolume( int vol )
{
    display( i18n("Volume: %1%",QString::number(vol)), "player_volume_up" );
}

void KdetvElegant::displayCC( const QString &text )
{
    if ( text.isEmpty() )
        popup->hide();
    else
        display( text );
}

void KdetvElegant::viewResized( int w, int /*h*/ )
{
    if (!autoFontSize)
        return;
    QFont font = popup->font();
    font.setPixelSize(w / 15);
    popup->setFont( font );
    kDebug() << "elegant font resized to " << w / 15;
    if ( !popup->isHidden() ) {
        updateGeometry();
    }
}

ElegantConfigWidget *KdetvElegant::configWidget( QWidget *parent, const char *name )
{
    form = new ElegantConfigWidget( parent );

    if ( align & Qt::AlignLeft )
        form->HSlider->setValue(0);
    else if ( align & Qt::AlignRight )
        form->HSlider->setValue(2);
    else
        form->HSlider->setValue(1);

    if ( align & Qt::AlignTop )
        form->VSlider->setValue(0);
    else if ( align & Qt::AlignBottom )
        form->VSlider->setValue(2);
    else
        form->VSlider->setValue(1);

    form->TimeSpin->setValue( timeout );
    form->MarginSpin->setValue( indent );
    form->SizeSpin->setValue( fontSize );
    form->BoldCheck->setChecked( fontBold );
    form->ItalicCheck->setChecked( fontItalic );
    form->UnderlineCheck->setChecked( fontUnderline );
    form->AutoFontSizeCheck->setChecked( autoFontSize );

    return form;
}

void KdetvElegant::applyConfigWidget()
{
    timeout = form->TimeSpin->value();
    indent = form->MarginSpin->value();
    fontSize = form->SizeSpin->value();
    iconSize = form->SizeSpin->value();
    fontBold = form->BoldCheck->isChecked();
    fontItalic = form->ItalicCheck->isChecked();
    fontUnderline = form->UnderlineCheck->isChecked();
    autoFontSize = form->AutoFontSizeCheck->isChecked();

    switch ( form->HSlider->value() ) {
	case 0:
	    align = Qt::AlignLeft;
	    break;
	case 1:
	    align = Qt::AlignHCenter;
	    break;
	case 2:
	default:
	    align = Qt::AlignRight;
	    break;
    }
    switch ( form->VSlider->value() ) {
	case 0:
	    align = align | Qt::AlignTop;
	    break;
	case 1:
	    align = align | Qt::AlignVCenter;
	    break;
	case 2:
	default:
	    align = align | Qt::AlignBottom;
	    break;
    }

    applySettings();
}


void KdetvElegant::colourKeyChanged(QColor)
{
    // nothing in here, but this suppresses a kdetv complaint
    // about the colourKeyChanged slot missing in this plugin
}

extern "C" {
    KDETV_EXPORT KdetvElegant* create_elegant( Kdetv *ktv, QWidget *parent )
    {
        if ( !parent )
            return 0;

        return new KdetvElegant( ktv, parent );
    }
}


#ifdef INCLUDE_TESTS

static int test_chan_num = 0;
static int test_muted = false;
static int test_vol = 0;

void KdetvElegant::test_channel()
{
    displayChannel( test_chan_num, QString("Channel %1").arg(test_chan_num) );
    test_chan_num++;
}

void KdetvElegant::test_mute()
{
    displayMuted( test_muted );
    test_muted = !test_muted;
}

void KdetvElegant::test_volume()
{
    displayVolume( test_vol );
    test_vol = test_vol+10;
}

void KdetvElegant::test_config()
{
    QWidget *w = configWidget( 0, "conf" );
    w->show();
}
#endif // INCLUDE_TESTS
