/**************************************************************************************
    begin                : Fri 18-06-2004
    edit 		 : Wed 1 Jun 2006
    copyright            : (C) 2004 by Jeroen Wijnhout (Jeroen.Wijnhout@kdemail.net)
                           (C) 2006 by Thomas Braun (braun@physik.fu-berlin.de)
                           (C) 2006 by Michel Ludwig (michel.ludwig@kdemail.net)
 **************************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "kilesidebar.h"

#include <tqwidgetstack.h>
#include <tqlayout.h>

#include "kiledebug.h"
#include "kilemultitabbar.h"

#include "symbolview.h"

KileSideBar::KileSideBar(int size, TQWidget *parent, const char *name, TQt::Orientation orientation /*=TQt::Vertical*/) : 
	TQFrame(parent, name),
	m_nTabs(0),
	m_nCurrent(0),
	m_bMinimized(false),
	m_nMinSize(0),
	m_nMaxSize(1000),
	m_nSize(size)
{
	setLineWidth(0);
	setSizePolicy(TQSizePolicy::Ignored, TQSizePolicy::Ignored);
 
	TQLayout *layout;

	m_tabStack = new TQWidgetStack(this);
	m_tabStack->setSizePolicy(TQSizePolicy::Ignored, TQSizePolicy::Ignored);

	KileMultiTabBar::KileMultiTabBarMode tabbarori = KileMultiTabBar::Horizontal;
	KileMultiTabBar::KileMultiTabBarPosition tabbarpos = KileMultiTabBar::Top;
	if ( orientation == TQt::Horizontal ) 
	{
		layout = new TQVBoxLayout(this);
		tabbarori = KileMultiTabBar::Horizontal;
		tabbarpos = KileMultiTabBar::Top;
	}
	else if ( orientation == TQt::Vertical ) 
	{
		layout = new TQHBoxLayout(this);
		tabbarori = KileMultiTabBar::Vertical;
		tabbarpos = KileMultiTabBar::Right;
	}

	m_tabBar = new KileMultiTabBar(tabbarori, this);
	m_tabBar->setPosition(tabbarpos);
	m_tabBar->setStyle(KileMultiTabBar::KDEV3ICON);

	if ( orientation == TQt::Horizontal )
	{
		setMinimumHeight(m_tabBar->height());
		m_nMinSize = m_tabBar->height();
		m_nMaxSize = m_tabBar->maximumHeight();
		layout->add(m_tabBar);
		layout->add(m_tabStack);
	}
	else if ( orientation == TQt::Vertical )
	{
		setMinimumWidth(m_tabBar->width());
		m_nMinSize = m_tabBar->width();
		m_nMaxSize = m_tabBar->maximumWidth();
		layout->add(m_tabStack);
		layout->add(m_tabBar);
	}
}

KileSideBar::~KileSideBar()
{
}

int KileSideBar::addTab(TQWidget *tab, const TQPixmap &pic, const TQString &text /* = TQString()*/)
{
	m_widgetToIndex[tab] = m_nTabs;

	m_tabBar->appendTab(pic, m_nTabs, text);
	m_tabStack->addWidget(tab, m_nTabs);
	connect(m_tabBar->tab(m_nTabs), TQ_SIGNAL(clicked(int)), this, TQ_SLOT(showTab(int)));

	return m_nTabs++;
}


void KileSideBar::setVisible(bool show)
{
	KILE_DEBUG() << "==KileSideBar::setVisible(" << show << ")===========" << endl;
	if (show) expand();
	else shrink();
}

void KileSideBar::shrink()
{
	if ( !isVisible() ) return;

	m_bMinimized = true;

	m_nSize = width();
	m_nMinSize = minimumWidth();
	m_nMaxSize = maximumWidth();

	m_tabStack->hide();
	setFixedWidth(m_tabBar->width());

	emit visibilityChanged(false);
}

void KileSideBar::expand()
{
	if ( isVisible() ) return;

	m_bMinimized = false;

	m_tabStack->show();

	resize(m_nSize, height());
	setMinimumWidth(m_nMinSize);
	setMaximumWidth(m_nMaxSize);

	emit visibilityChanged(true);
}

TQWidget* KileSideBar::currentPage()
{
	return m_tabStack->visibleWidget();
}

int KileSideBar::findNextShownTab(int i)
{
	if(m_nTabs <= 0)
	{
		return -1;
	}
	for(int j = 1; j < m_nTabs; ++j)
	{
		int index = (i + j) % m_nTabs;
		if(m_tabBar->tab(index)->isShown())
		{
			return index;
		}
	}
	return -1;
}

void KileSideBar::removePage(TQWidget *w) 
{
	TQMap<TQWidget*,int>::iterator it = m_widgetToIndex.find(w);
	if(it == m_widgetToIndex.end())
	{
		return;
	}
	int index = *it;
	m_tabStack->removeWidget(w);
	disconnect(m_tabBar->tab(index), TQ_SIGNAL(clicked(int)), this, TQ_SLOT(showTab(int)));
	m_tabBar->removeTab(index);
	m_widgetToIndex.remove(it);
	if(index == m_nCurrent && m_nTabs >= 2)
	{
		showTab(findNextShownTab(index));
	}
	--m_nTabs;
}

void KileSideBar::setPageVisible(TQWidget *w, bool b) {
	TQMap<TQWidget*,int>::iterator it = m_widgetToIndex.find(w);
	if(it == m_widgetToIndex.end())
	{
		return;
	}
	int index = *it;
	KileMultiTabBarTab *tab = m_tabBar->tab(index);
	if(tab->isShown() == b) {
		return;
	}
  	tab->setShown(b);
	if(!b && index == m_nCurrent && m_nTabs >= 2)
	{
		showTab(findNextShownTab(index));
	}
}

void KileSideBar::showPage(TQWidget *widget)
{
	if ( m_widgetToIndex.contains(widget) )
		switchToTab(m_widgetToIndex[widget]);
}

void KileSideBar::showTab(int id)
{
	if(id >= m_nTabs || id < 0)
	{
		return;
	}
	if ( id != m_nCurrent)
	{
		switchToTab(id);
		if (m_bMinimized) expand();
	}
	else
		toggleTab();
}

void KileSideBar::toggleTab()
{
	if (m_bMinimized) expand();
	else shrink();
}

void KileSideBar::switchToTab(int id)
{
	if(id >= m_nTabs || id < 0) {
		return;
	}
	m_tabBar->setTab(m_nCurrent, false);
	m_tabBar->setTab(id, true);

	m_tabStack->raiseWidget(id);

	m_nCurrent = id;
}

KileBottomBar::KileBottomBar(int size, TQWidget *parent, const char *name) :
	KileSideBar(size, parent, name, TQt::Horizontal)
{}

void KileBottomBar::shrink()
{
	m_bMinimized = true;

	m_nSize = height();
	m_nMinSize = minimumHeight();
	m_nMaxSize = maximumHeight();

	m_tabStack->hide();
	setFixedHeight(m_tabBar->height());

	emit visibilityChanged(false);
}

void KileBottomBar::expand()
{
	m_bMinimized = false;

	m_tabStack->show();

	resize(width(), m_nSize);
	setMinimumHeight(m_nMinSize);
	setMaximumHeight(m_nMaxSize);
	
	emit visibilityChanged(true);
}

#include "kilesidebar.moc"
