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

// Elements
#include "elements/vectorelement.h"
#include "elements/vectorpath.h"
#include "elements/strokeattribute.h"
#include "elements/fillattribute.h"
#include "elements/colorattribute.h"
#include "elements/animatedcolorattr.h"
#include "elements/number.h"
#include "elements/animatednumber.h"
#include "elements/animatedlengthattr.h"
#include "elements/lengthattribute.h"

// Tools
#include "selecttool.h"
#include "vectorfactorytool.h"

// KDE
#include <kdebug.h>

ToolManager::ToolManager()
    : QObject(),
    m_activeTool(0)
{
  init();
}

ToolManager::~ToolManager()
{
  delete m_selectTool;
}

void ToolManager::init()
{
  m_selectTool = new SelectTool();
  registerTool(m_selectTool);

  m_vecRectTool = new VectorFactoryTool(
                    createPrototype(VectorPath::SHAPE_RECT));
  registerTool(m_vecRectTool);
  m_vecEllipseTool = new VectorFactoryTool(
                    createPrototype(VectorPath::SHAPE_ELLIPSE));
  registerTool(m_vecEllipseTool);

  //   m_selectNodesTool = new SelectNodesTool( m_part, "" );

  // Set the default tool
  //setActiveTool( m_selectTool );  // Dont't do this here. will lead to an endless loop
  // as it calls Kast:self() internally and we're still
  // in the Kast constructor
}

VectorElement * ToolManager::createPrototype( unsigned int type  )
{
  VectorElement *vec = new VectorElement();
  vec->vectorPath().setShapeMetaType( (VectorPath::ShapeMetaType) type );
  vec->stroke().strokeWidth().baseVal().setValue(1);
  vec->stroke().color().baseVal().setColor(Qt::black);
  vec->stroke().setStrokeType(StrokeAttribute::STROKE_SOLID);
  vec->fill().color().baseVal().setColor(Qt::black);
  vec->fill().setFillType(FillAttribute::FILL_SOLID);
  return vec;
}

void ToolManager::setDefaultTool()
{
  setActiveTool( m_selectTool );
}

void ToolManager::setActiveTool( GenericTool *tool )
{
  if( m_activeTool )
    m_activeTool->deactivate();

  if( m_activeTool != tool )
  {
    m_activeTool = tool;
    m_activeTool->activate();
  }
  emit(activeToolChanged());
}

void ToolManager::registerTool( GenericTool *tool )
{
  if( !m_tools.find( tool->name() ) )
    m_tools.insert( tool->name(), tool );
  kdDebug() << "ToolManager::registerTool -  " << tool->name() << endl;
}

void ToolManager::unregisterTool( GenericTool *tool )
{
  QDictIterator<GenericTool> it( m_tools );
  for( ; it.current(); ++it )
    if (it.current() == tool)
    {
      m_tools.remove(it.currentKey());
      return;
    }
}

bool ToolManager::mouseEvent( QMouseEvent* event, const QPoint &p )
{
  if (!m_activeTool)
  {
    kdWarning() << "ToolManager::mouseEvent() - No tool selected... ignoring event" << endl;
    return false;
  }
  return m_activeTool->mouseEvent( event, p );
}

bool ToolManager::keyEvent( QEvent* event )
{
  if (!m_activeTool)
  {
    kdWarning() << "ToolManager::mouseEvent() - No tool selected... ignoring event" << endl;
    return false;
  }
  return m_activeTool->keyEvent( event );
}




#include "toolmanager.moc"
