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

// Elements
#include "animatedlengthattr.h"
#include "lengthattribute.h"
#include "urireference.h"

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

LinearGradient::LinearGradient()
 : Gradient()
{
  init();
}

LinearGradient::~LinearGradient()
{
  delete m_x1;
  delete m_y1;
  delete m_x2;
  delete m_y2;
}

LinearGradient::LinearGradient( const LinearGradient &other )
  : Gradient()
{
  init();
  (*this) = other;
}

LinearGradient& LinearGradient::operator=(const LinearGradient &other)
{
  Gradient::operator=(other);
  *m_x1 = other.getX1();
  *m_x2 = other.getX2();
  *m_y1 = other.getY1();
  *m_y2 = other.getY2();
  return *this;
}

LinearGradient& LinearGradient::operator|=(const LinearGradient &other)
{
  Gradient::operator|=(other);
  *m_x1 = other.getX1(true);
  *m_x2 = other.getX2(true);
  *m_y1 = other.getY1(true);
  *m_y2 = other.getY2(true);
  return *this;
}

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

/**
 * Set the SVG spec's defaults
 */
void LinearGradient::init()
{
  m_x1 = new AnimatedLength();
  m_x1->baseVal().newValueSpecifiedUnits(LengthAttribute::SVG_LENGTHTYPE_PERCENTAGE, 0);
  m_x1->baseVal().setValid(false);
  m_y1 = new AnimatedLength();
  m_y1->baseVal().newValueSpecifiedUnits(LengthAttribute::SVG_LENGTHTYPE_PERCENTAGE, 0);
  m_y1->baseVal().setValid(false);
  m_x2 = new AnimatedLength();
  m_x2->baseVal().newValueSpecifiedUnits(LengthAttribute::SVG_LENGTHTYPE_PERCENTAGE, 100);
  m_x2->baseVal().setValid(false);
  m_y2 = new AnimatedLength();
  m_y2->baseVal().newValueSpecifiedUnits(LengthAttribute::SVG_LENGTHTYPE_PERCENTAGE, 0);
  m_y2->baseVal().setValid(false);
}

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

AnimatedLength& LinearGradient::getX1( bool inherited ) const
{
  if ( inherited && !m_x1->isValid() )
  {
    if ( m_ref->getRefElement() && m_ref->getRefElement()->inherits("LinearGradient") )
    {
      LinearGradient* ref = static_cast<LinearGradient*>(m_ref->getRefElement());
      AnimatedLength& inheritedAttr = ref->getX1(true);
      if ( inheritedAttr.isValid() )
        return inheritedAttr;
    }
  }
  return *m_x1;
}


AnimatedLength& LinearGradient::getY1( bool inherited ) const
{
  if ( inherited && !m_y1->isValid() )
  {
    if ( m_ref->getRefElement() && m_ref->getRefElement()->inherits("LinearGradient") )
    {
      LinearGradient* ref = static_cast<LinearGradient*>(m_ref->getRefElement());
      AnimatedLength& inheritedAttr = ref->getY1(true);
      if ( inheritedAttr.isValid() )
        return inheritedAttr;
    }
  }
  return *m_y1;
}


AnimatedLength& LinearGradient::getX2( bool inherited ) const
{
  if ( inherited && !m_x2->isValid() )
  {
    if ( m_ref->getRefElement() && m_ref->getRefElement()->inherits("LinearGradient") )
    {
      LinearGradient* ref = static_cast<LinearGradient*>(m_ref->getRefElement());
      AnimatedLength& inheritedAttr = ref->getX2(true);
      if ( inheritedAttr.isValid() )
        return inheritedAttr;
    }
  }
  return *m_x2;
}


AnimatedLength& LinearGradient::getY2( bool inherited ) const
{
  if ( inherited && !m_y2->isValid() )
  {
    if ( m_ref->getRefElement() && m_ref->getRefElement()->inherits("LinearGradient") )
    {
      LinearGradient* ref = static_cast<LinearGradient*>(m_ref->getRefElement());
      AnimatedLength& inheritedAttr = ref->getY2(true);
      if ( inheritedAttr.isValid() )
        return inheritedAttr;
    }
  }
  return *m_y2;
}

Attribute * LinearGradient::getAttrByName( const QString & name )
{
  if (name == "x1")
    return m_x1;
  else if (name == "x2")
    return m_x2;
  else if (name == "y1")
    return m_y1;
  else if (name == "y2")
    return m_y2;
  else
    return Gradient::getAttrByName( name );
}


#include "lineargradient.moc"
