/***************************************************************************
 *   Copyright (C) 2005 by Andreas Silberstorff   *
 *   ml@andreas-silberstorff.de   *
 *
 *   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  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 11-11, USA.
 *
 *   In addition, as a special exception, the copyright holders give
 *   permission to link the code of this program with any edition of
 *   the Qt library by Trolltech AS, Norway (or with modified versions
 *   of Qt that use the same license as Qt), and distribute linked
 *   combinations including the two.  You must obey the GNU General
 *   Public License in all respects for all of the code used other than
 *   Qt.  If you modify this file, you may extend this exception to
 *   your version of the file, but you are not obligated to do so.  If
 *   you do not wish to do so, delete this exception statement from
 *   your version.
 ***************************************************************************/

#include "kalvasidebar.h"
#include <qapplication.h>
#include <qptrlist.h>

#include <kmultitabbar.h>
#include <kdebug.h>
#include <klocale.h>
#include <kconfig.h>

KalvaSideBar::KalvaSideBar(QWidget *parent)
	: QWidget(parent)
{
	setName( "kalvaSideBar" );
	m_KalvaSideBar = new KMultiTabBar ( KMultiTabBar::Vertical, parent );
	m_KalvaSideBar->setStyle(    KMultiTabBar::VSNET );
	m_KalvaSideBar->setPosition( KMultiTabBar::Right );
	m_ButtonCount = -1;
}

KalvaSideBar::~KalvaSideBar()
{
}

/***
 * @short Adds a tab and the corresponding widget to the sidebar
 * @short and connects the signal of the button to the sidebars
 * @short "setTabVisibility" slot
 ***/
int KalvaSideBar::insertTab( QWidget* w,
			  const QPixmap & pix,
			  const QString & text)
{
	kdDebug() << "In insertTab(...)" << endl;
	w->hide();
	m_KalvaSideBarTab.append(w);
	m_ButtonCount += 1;
	kdDebug() << "Id = " << m_ButtonCount << endl;
	m_ButtonCount += m_KalvaSideBar->appendTab ( pix, m_ButtonCount, text);
	connect ( m_KalvaSideBar->tab(m_ButtonCount),
		  SIGNAL ( clicked ( int ) ),
		  this,
		  SLOT   ( setTabVisibility( int ) ) );
	return m_ButtonCount;
}

/***
 * @short This shall ensure that never more than one widget is visible.
 * @short Sets the corresponding widget's visibility. If the state of the
 * @short widget's button is true, we hide the other widgets by setting
 * @short their buttons state to false.
 ***/
void KalvaSideBar::setTabVisibility( int Id )
{
	kdDebug() << "In setTabVisibility(" << Id << ") " << endl;
	if ( m_KalvaSideBar->tab(Id)->state() == false )  {
	   m_KalvaSideBarTab.at(Id)->hide();
	   return;
	}
// 	for ( int i = 0; i < m_ButtonCount; i++ )  {
// 	   if ( i != Id )  {
// 		m_KalvaSideBar->tab(i)->setState (false);
// 	   }
// 	}
	m_KalvaSideBarTab.at(Id)->show();
	return;
}

void KalvaSideBar::readConfig( const KConfigGroup& kalvaConfig )
{
    QPtrList<KMultiTabBarTab>* tl = m_KalvaSideBar->tabs();
    QPtrListIterator<KMultiTabBarTab> it( *tl );
    kdDebug() << "sidebar::readConfig() " << endl;
    int Id = 0;
    QString Key = "";
    for ( ;it.current() != 0; ++it )
    {
        Id = it.current()->id();
        Key = QString("sidebar_%1").arg( Id );
        kdDebug() << Key << " = " 
                  << kalvaConfig.readBoolEntry( Key, false) 
                  << endl;
       m_KalvaSideBar->setTab( Id, kalvaConfig.readBoolEntry( Key, false) );
       setTabVisibility(Id);
    }
}

void KalvaSideBar::setConfig( KConfigGroup& kalvaConfig )
{
    QPtrList<KMultiTabBarTab>* tl = m_KalvaSideBar->tabs();
    QPtrListIterator<KMultiTabBarTab> it( *tl );
    kdDebug() << "sidebar::setConfig() " << endl;
    for ( ;it.current() != 0; ++it )
    {
       kdDebug() << QString("sidebar_%1").arg( it.current()->id() ) << " = " 
                 << m_KalvaSideBar->isTabRaised( it.current()->id() )
                 << endl;
       kalvaConfig.writeEntry(
             QString("sidebar_%1").arg( it.current()->id() ),
             m_KalvaSideBar->isTabRaised( it.current()->id() )
             );
    }
}

#include "kalvasidebar.moc"
