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

// ELements
#include "animatedlengthattr.h"
#include "animatedtransformlist.h"
#include "documentelement.h"
#include "vectorpath.h"

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

// Visitors
#include "visitors/visitor.h"

// KCanvas
#include <kcanvas/KCanvas.h>
#include <kcanvas/KCanvasItem.h>
#include <kcanvas/KCanvasCreator.h>
#include <kcanvas/device/KRenderingDevice.h>
#include <kcanvas/device/KRenderingPaintServerSolid.h>
#include <kcanvas/device/KRenderingFillPainter.h>

// Qt
#include <qpoint.h>

// KDE
#include <kdebug.h>


VectorElement::VectorElement( const QString & name, State state )
    : Transformable( name, state ),
    m_context(0)
{
  m_vectorPath = new VectorPath();
}


VectorElement::~VectorElement()
{}

VectorElement::VectorElement( const VectorElement &other )
    : Transformable(),
    m_context(0)
{
  m_vectorPath = new VectorPath();
  (*this) = other;
}

VectorElement& VectorElement::operator=(const VectorElement &other)
{
  Transformable::operator=(other);
  *m_vectorPath = other.vectorPath();
  return *this;
}

ElementNode* VectorElement::clone()
{
  VectorElement* retElem = new VectorElement();
  *retElem = *this;
  return ( retElem );
}

Attribute* VectorElement::getAttrByName( const QString & name )
{
  Attribute* retAttr = 0;
  retAttr =  m_vectorPath->getAttrByName( name );
  if (retAttr)
    return retAttr;
  return Transformable::getAttrByName( name );
}

Element::Hit VectorElement::hits( const QPoint &p )
{
  if ( !m_context )
  {
    return Element::NOHIT;
  }
  if ( m_context->strokeContains(p) )
    return Element::PATHHIT;
  else if ( m_context->fillContains(p) )
    return Element::FILLHIT;
  else
    return Element::NOHIT;
}

void VectorElement::setContext( KCanvasItem* context )
{
  // kdDebug() << "Changing context from " << m_context << " to " << context << endl;
  m_context = context;
  if ( m_context )
    m_context->setUserData( this );
}


void VectorElement::accept( Visitor& v)
{
  v.visit( *this );
}

QRect VectorElement::bbox( KCBBoxFlags flags ) const
{
  if (!m_context)
    return QRect();
  return m_context->bbox(flags);
}

QString VectorElement::readAbleType( )
{
  return m_vectorPath->getReadeableShapeMetaType();
}

#include "vectorelement.moc"
