/***************************************************************************
 *   Copyright (C) 2005 by Max Howell <max.howell@methylblue.com>          *
 *             (C) 2004 Frederik Holljen <fh@ez.no>                        *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA  02110-1301, USA.          *
 ***************************************************************************/

/** WARNING this is not meant for use outside this unit! */

#ifndef AMAROK_TOGGLELABEL_H
#define AMAROK_TOGGLELABEL_H

#include "debug.h"
#include "overlayWidget.h"
#include "popupMessage.h"

#include <tdeactionclasses.h>
#include <tdeglobalsettings.h>
#include <kiconloader.h>
#include <tqiconset.h>
#include <tqlabel.h>
#include <tqtimer.h>
#include <tqtooltip.h>

class ToggleLabel : public TQLabel
{
    TQ_OBJECT
  

    TDEToggleAction const*const m_action;

    signals:
        void toggled( bool );

    public:
        ToggleLabel( TDEToggleAction const*const action, TQWidget *parent )
                : TQLabel( parent )
                , m_action( action )
                , m_tooltip( 0 )
                , m_tooltipShowing( false )
                , m_tooltipHidden( false )
        {
            connect( this,   TQ_SIGNAL(toggled( bool )), action, TQ_SLOT(setChecked( bool )) );
            connect( action, TQ_SIGNAL(toggled( bool )), this,   TQ_SLOT(setChecked( bool )) );
            connect( action, TQ_SIGNAL(enabled( bool )), this,   TQ_SLOT(setEnabled( bool )) );

            setChecked( isChecked() );
        }

        inline bool isChecked() const { return m_action->isChecked(); }
        inline bool isEnabled() const { return m_action->isEnabled(); }

    protected:
        void mousePressEvent( TQMouseEvent* )
        {
            hideToolTip();
            const bool b = !isChecked();
            if( isEnabled() )
            {
                setChecked( b );
                emit toggled( b );
            }
        }

        void enterEvent( TQEvent* )
        {
            //Show the tooltip after 1/2 second
            m_tooltipHidden = false;
            TQTimer::singleShot( 500, this, TQ_SLOT(aboutToShow()) );
        }

        void leaveEvent( TQEvent* )
        {
            hideToolTip();
        }

    public slots:
        void setChecked( bool on )
        {
            setPixmap( m_action->iconSet().pixmap( TQIconSet::Small, on ? TQIconSet::Normal : TQIconSet::Disabled ) );
        }

        void setEnabled( bool /*on*/ ) { }

    private slots:
        void aboutToShow()
        {
            if( hasMouse() && !m_tooltipHidden )
                showToolTip();
        }

    private:
        void showToolTip()
        {
            if( m_tooltipShowing )
                return;

            m_tooltipShowing = true;

            TQString tip = m_action->isChecked() ? i18n("%1: on") : i18n("%1: off");

            if( !isEnabled() )
                tip += i18n("&nbsp;<br>&nbsp;<i>Disabled</i>");

            tip += "&nbsp;";
            const TQString path = TDEGlobal::iconLoader()->iconPath( m_action->icon(), -TDEIcon::SizeHuge );


            m_tooltip = new KDE::PopupMessage( parentWidget()->parentWidget(), parentWidget(), 0 /*timeout*/ );
            m_tooltip->setShowCloseButton( false );
            m_tooltip->setShowCounter( false );
            m_tooltip->setMaskEffect( KDE::PopupMessage::Plain );
            m_tooltip->setText( tip.arg(m_action->text().remove('&') ) );
            m_tooltip->setImage( path );

            m_tooltip->reposition();
            m_tooltip->display();
        }

        void hideToolTip()
        {
            m_tooltipHidden = true;
            if( m_tooltipShowing )
            {
                m_tooltip->close();
                m_tooltipShowing = false;
            }
        }

        KDE::PopupMessage *m_tooltip;
        bool               m_tooltipShowing;
        bool               m_tooltipHidden;
};


#endif
