/***************************************************************************
 *   Copyright (C) 2004 by Hans Oischinger                                 *
 *   hans.oischinger@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.                                   *
 *                                                                         *
 *   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.             *
 ***************************************************************************/
#include "toolboxwidget.h"

// Core
#include "core/direktorinstance.h"

// Tools
#include "tools/toolmanager.h"
#include "tools/generictool.h"

// Qt
#include <qvbuttongroup.h>
#include <qtoolbutton.h>
#include <qlayout.h>

//KDE
#include <kdebug.h>

ToolboxWidget::ToolboxWidget(QWidget *parent, const char *name)
    : QWidget(parent, name)
{
  QVBoxLayout* layout = new QVBoxLayout(this);
  buttonGroup = new QVButtonGroup( this );
  buttonGroup->setExclusive( true );
  layout->addWidget(buttonGroup);
  connect( buttonGroup, SIGNAL( pressed( int ) ), this, SLOT( slotButtonPressed( int ) ) );

  initTools();
  connect ( &Kast::self()->toolManager(), SIGNAL(activeToolChanged()), this, SLOT(slotToolChangedOutside()) );
}

ToolboxWidget::~ToolboxWidget()
{}

void ToolboxWidget::slotButtonPressed( int id )
{
  kdDebug() << k_funcinfo << endl;
  GenericTool *tool = 0;
  tool = m_tools.at(id);

  if (tool)
    Kast::self()->toolManager().setActiveTool(tool);
}

void ToolboxWidget::initTools()
{
  kdDebug() << "ToolboxWidget::initTools()" << endl;
  int id = 0;
  m_tools.clear();
  m_tools.resize( Kast::self()->toolManager().tools().count() );

  QDictIterator<GenericTool> it( Kast::self()->toolManager().tools() );
  for( ; it.current(); ++it )
  {
    kdDebug() << "ToolboxWidget::initTools() - Adding toolbox button: " << it.currentKey() << " id:" << id << endl;

    QToolButton *button = new QToolButton(buttonGroup);
    //button->setPixmap( pixmap );
    button->setToggleButton( true );
    button->setText(it.currentKey());

    m_tools.insert(id, it.current());
    ++id;
  }
}

QSize ToolboxWidget::sizeHint( ) const
{
  return buttonGroup->size();
}

QSize ToolboxWidget::minimumSizeHint( ) const
{
  return buttonGroup->size();
}

QSize ToolboxWidget::maximumSizeHint( ) const
{
  return buttonGroup->size();
}

void ToolboxWidget::slotToolChangedOutside( )
{
  // FIXME: Do this in some sane way!!!!
  kdDebug() << k_funcinfo << endl;
  GenericTool* actTool = Kast::self()->toolManager().activeTool();
  int id = 0;
  QDictIterator<GenericTool> it( Kast::self()->toolManager().tools() );
  for( ; it.current(); ++it )
  {
    QButton* button = 0;
    button = buttonGroup->find(id);
    if (button)
      if (it.current() == actTool)
        button->setDown(true);
      else
        button->setDown(false);

    ++id;
  }
}

#include "toolboxwidget.moc"
