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

#include <qwidget.h>
#include <qstring.h>
#include <kdebug.h>

// Elements
#include "animatedlengthattr.h"
#include "elementtreeiterator.h"

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

// KCanvas
#include <kcanvas/KCanvasItem.h>

DocumentElement::DocumentElement( const QString & name )
    : ElementNode( name ),
    m_context(0)
{
  m_x = new AnimatedLength();
  m_y = new AnimatedLength();
  m_width = new AnimatedLength();
  m_height = new AnimatedLength();
}

DocumentElement::~DocumentElement()
{}

DocumentElement::DocumentElement( const DocumentElement &other )
    : ElementNode( m_name, m_state )
{
  (*this) = other;
}

DocumentElement& DocumentElement::operator=(const DocumentElement &other)
{
  ElementNode::operator=(other);
  *m_width = other.width();
  *m_height = other.height();
  *m_x = other.x();
  *m_y = other.y();
  return *this;
}

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

Attribute* DocumentElement::getAttrByName( const QString & name )
{
  if (name == "x")
    return m_x;
  else if (name == "y")
    return m_y;
  else if (name == "width")
    return m_width;
  else if (name == "height")
    return m_height;
  else
    return 0;
}

void DocumentElement::accept( Visitor& visitor )
{
  visitor.visit( *this );
}

Element::Hit DocumentElement::hits( const QPoint & )
{
  return Element::NOHIT;
}

QRect DocumentElement::bbox( KCBBoxFlags flags ) const
{
  if (!m_context)
    return QRect(0,0,10,10);
  return m_context->bbox(flags);
}

ElementNode* DocumentElement::findElementByRef( const QString& ref )
{
  QString normalizedRef = ref;
  if (normalizedRef.left(1)=="#") // is this a local reference
    normalizedRef = normalizedRef.remove(0,1);
  else
  {
    kdDebug() << "DocumentElement::findElementByRef() - Non-local reference given - Not supported!" << endl;
    return 0;
  }

  // Find the referred element
  ElementTreeIterator itr( this );
  for( ; itr.current(); itr.next() )
    if ( itr.current()->id() == normalizedRef )
    {
      return itr.current();
    }
  return 0;
}


#include "documentelement.moc"
