/*
  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.
*/

/*
  Copyright (C) 2005 Ivor Hewitt <ivor@ivor.org>
  Copyright (C) 2005 Ismail Donmez <ismail@kde.org>
  Copyright (C) 2006 Dario Abatianni <eisfuchs@tigress.com>
  Copyright (C) 2006 John Tapsell <johnflux@gmail.com>
  Copyright (C) 2006 Peter Simonsson <psn@linux.se>
*/

#include "osd_preferences.h"
#include "osd.h"
#include "konversationapplication.h"

#include <tqgroupbox.h>
#include <tqspinbox.h>
#include <kcombobox.h>
#include <kcolorbutton.h>
#include <tqcheckbox.h>
#include <tdefontrequester.h>

#include <tdeconfigdialog.h>


OSD_Config::OSD_Config( TQWidget* parent, const char* name, WFlags fl )
    : OSD_ConfigUI( parent, name, fl )
{
    bool enableScreenChooser = false;
    TQRect screenRect;

    for(int i = 0; i < TQApplication::desktop()->numScreens(); ++i) {
        kcfg_OSDScreen->insertItem(TQString::number(i));
        screenRect = TQApplication::desktop()->screenGeometry(i);

        //Check if we're using xinerama or not
        if(screenRect.left() != 0 || screenRect.top() != 0) {
            enableScreenChooser = true;
        }
    }

    kcfg_OSDScreen->setEnabled(enableScreenChooser);

    m_pOSDPreview = new OSDPreviewWidget("Konversation");
    connect(m_pOSDPreview, TQ_SIGNAL(positionChanged()), this, TQ_SLOT(slotPositionChanged()));

    connect( kcfg_OSDFont, TQ_SIGNAL(fontSelected(const TQFont&)), this, TQ_SLOT(slotUpdateFont(const TQFont&)));

    slotOSDEnabledChanged(kcfg_UseOSD->isChecked());
    slotCustomColorsChanged(kcfg_OSDUseCustomColors->isChecked());
    slotScreenChanged(Preferences::oSDScreen());
    slotDrawShadowChanged( kcfg_OSDDrawShadow->isChecked());
    slotUpdateFont(Preferences::oSDFont());

    kcfg_OSDOffsetX->hide();
    kcfg_OSDOffsetY->hide();
    kcfg_OSDAlignment->hide();

    //Connect config page entries to control the OSDPreview
    connect ( kcfg_UseOSD,  TQ_SIGNAL( toggled( bool ) ), this, TQ_SLOT( slotOSDEnabledChanged(bool) ) );
    connect ( kcfg_OSDUseCustomColors, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(slotCustomColorsChanged(bool)));
    connect ( kcfg_OSDTextColor, TQ_SIGNAL(changed(const TQColor&)), this, TQ_SLOT(slotTextColorChanged(const TQColor&)));
    connect ( kcfg_OSDBackgroundColor, TQ_SIGNAL(changed(const TQColor&)), this, TQ_SLOT(slotBackgroundColorChanged(const TQColor&)));
    connect ( kcfg_OSDScreen, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotScreenChanged(int)));
    connect ( kcfg_OSDDrawShadow, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(slotDrawShadowChanged(bool)));
}

OSD_Config::~OSD_Config()
{
    delete m_pOSDPreview;
}

void OSD_Config::loadSettings()
{
}

void OSD_Config::restorePageToDefaults()
{
}

void OSD_Config::saveSettings()
{
    //Update the current OSD.
    KonversationApplication *konvApp=static_cast<KonversationApplication *>(TDEApplication::kApplication());

    konvApp->osd->setEnabled(kcfg_UseOSD->isChecked());
    if (kcfg_UseOSD->isChecked())
    {
        konvApp->osd->setFont(Preferences::oSDFont());
        if(kcfg_OSDUseCustomColors->isChecked())
        {
            konvApp->osd->setTextColor(kcfg_OSDTextColor->color());
            konvApp->osd->setBackgroundColor(kcfg_OSDBackgroundColor->color());
        }
        else
        {
            konvApp->osd->unsetColors();
        }

        konvApp->osd->setDuration(kcfg_OSDDuration->value());
        konvApp->osd->setScreen(kcfg_OSDScreen->currentItem());
        konvApp->osd->setShadow(kcfg_OSDDrawShadow->isChecked());

        //x is ignored anyway, but leave incase we use in future
        konvApp->osd->setOffset(kcfg_OSDOffsetX->value(), kcfg_OSDOffsetY->value());
        konvApp->osd->setAlignment((OSDWidget::Alignment)kcfg_OSDAlignment->value());
    }

}

void OSD_Config::showEvent(TQShowEvent*)
{
    //Update the preview
    m_pOSDPreview->setAlignment((OSDWidget::Alignment)( kcfg_OSDAlignment->value() ) );
    m_pOSDPreview->setOffset(kcfg_OSDOffsetX->value(),kcfg_OSDOffsetY->value());

    m_pOSDPreview->setShown(kcfg_UseOSD->isChecked());
}

void OSD_Config::hideEvent(TQHideEvent*)
{
    m_pOSDPreview->setShown(false);
}

bool OSD_Config::hasChanged()
{
  // follow the interface, no Non-TDEConfigXT settings here, so none have changed
  return false;
}

void OSD_Config::slotOSDEnabledChanged(bool on)
{
    if ( isVisible() )
        m_pOSDPreview->setShown(on);
}

void OSD_Config::slotPositionChanged()
{
    kcfg_OSDScreen->setCurrentItem(m_pOSDPreview->screen());

    kcfg_OSDAlignment->setValue( m_pOSDPreview->alignment() );
    kcfg_OSDOffsetX->setValue( m_pOSDPreview->x());
    kcfg_OSDOffsetY->setValue( m_pOSDPreview->y());
}


void OSD_Config::slotCustomColorsChanged(bool on)
{
    if(on)
    {
        m_pOSDPreview->setTextColor(kcfg_OSDTextColor->color());
        m_pOSDPreview->setBackgroundColor(kcfg_OSDBackgroundColor->color());
    }
    else
        m_pOSDPreview->unsetColors();
}
void OSD_Config::slotTextColorChanged(const TQColor& color)
{
    if(kcfg_OSDUseCustomColors->isChecked())
        m_pOSDPreview->setTextColor(color);
}

void OSD_Config::slotBackgroundColorChanged(const TQColor& color)
{
    if(kcfg_OSDUseCustomColors->isChecked())
        m_pOSDPreview->setBackgroundColor(color);
}

void OSD_Config::slotScreenChanged(int index)
{
    m_pOSDPreview->setScreen(index);
}

void OSD_Config::slotDrawShadowChanged(bool on)
{
    m_pOSDPreview->setShadow(on);
}

void OSD_Config::slotUpdateFont(const TQFont& font)
{
    m_pOSDPreview->setFont(font);
}

#include "osd_preferences.moc"

