//
// C++ Implementation: vectorfactorytool
//
// Description:
//
//
// Author: Hans Oischinger <hans.oischinger@kdemail.net>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "vectorfactorytool.h"

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

// Elements
#include "elements/vectorelement.h"
#include "elements/vectorpath.h"
#include "elements/documentelement.h"
#include "elements/animatedlengthattr.h"
#include "elements/lengthattribute.h"

// Visitors
#include "visitors/vsetanimvalues.h"
#include "visitors/vdrawelements.h"

// Commands
#include "commands/eaddelementcmd.h"

// Qt
#include <qstring.h>
#include <qcursor.h>

// KDE
#include <klocale.h>
#include <kdebug.h>

VectorFactoryTool::VectorFactoryTool( VectorElement* prototype )
    : GenericTool(),
    m_prototype(prototype)
{
  m_vSetAnimValues = new VSetAnimValues();
}

VectorFactoryTool::~VectorFactoryTool()
{
  delete m_vSetAnimValues;
}

void VectorFactoryTool::activate( )
{
  if ( Kast::self()->isGUIReady() )
    Kast::self()->docWidget().setCursor( QCursor( Qt::arrowCursor ) );
}

QString VectorFactoryTool::name() const
{
  return i18n( m_prototype->vectorPath().getReadeableShapeMetaType() + " Tool" );
}

QString VectorFactoryTool::statusText() const
{
  return i18n( "Select start point" );
}

QString VectorFactoryTool::contextHelp() const
{
  QString s = i18n( "<qt><b>" + m_prototype->vectorPath().getReadeableShapeMetaType() + " tool:</b><br>" );
  s += i18n( "<i>Creates a new vector item</i>" );
  return s;
}

void VectorFactoryTool::setCursor( ) const
{
  Kast::self()->docWidget().setCursor( QCursor( Qt::arrowCursor ) );
}

void VectorFactoryTool::mouseButtonPress( )
{
  kdDebug() << k_funcinfo << endl;
}

void VectorFactoryTool::mouseButtonRelease( )
{
  kdDebug() << k_funcinfo << endl;
  EAddElementCMD *cmd =
    new EAddElementCMD(m_prototype->clone(), &Kast::self()->elementDB().documentRoot());
  Kast::self()->addCommand(cmd);
}

void VectorFactoryTool::mouseDragRelease()
{
  kdDebug() << k_funcinfo << endl;

  // Remove the canvasItem of the prototype itself
  // (created in mouseDrag()) as we're cloning now
  KCanvas* canvas = Kast::self()->docWidget().getCanvas();
  KRenderingDevice* device = Kast::self()->docWidget().getDevice();
  VDrawElements *vDrawTree = new VDrawElements( canvas, device, true );
  m_prototype->accept( *vDrawTree );
  delete vDrawTree;

  EAddElementCMD *cmd =
    new EAddElementCMD(m_prototype->clone(), &Kast::self()->elementDB().documentRoot());
  Kast::self()->addCommand(cmd);
}

void VectorFactoryTool::mouseDrag( )
{
  switch(m_prototype->vectorPath().shapeMetaType())
  {
  case VectorPath::SHAPE_ELLIPSE:
    m_prototype->vectorPath().getX().baseVal().setValue(first().x());
    m_prototype->vectorPath().getY().baseVal().setValue(first().y());
    m_prototype->vectorPath().getRadiusX().baseVal().setValue(last().x()-first().x());
    m_prototype->vectorPath().getRadiusY().baseVal().setValue(last().x()-first().x());
  break;
  case VectorPath::SHAPE_RECT:
    m_prototype->vectorPath().getX().baseVal().setValue(first().x());
    m_prototype->vectorPath().getY().baseVal().setValue(first().y());
    m_prototype->vectorPath().getWidth().baseVal().setValue(last().x()-first().x());
    m_prototype->vectorPath().getHeight().baseVal().setValue(last().y()-first().y());
  }
  m_prototype->accept( *m_vSetAnimValues );
  m_prototype->setDirty();

  // Redraw the element and the selection rectangle
  KCanvas* canvas = Kast::self()->docWidget().getCanvas();
  KRenderingDevice* device = Kast::self()->docWidget().getDevice();
  VDrawElements *vDrawTree = new VDrawElements( canvas, device );
  m_prototype->accept( *vDrawTree );
  delete vDrawTree;
}



