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

// Attributes
#include "animatedtransformlist.h"
#include "animatedenum.h"
#include "urireference.h"
#include "enumeration.h"

// Qt
#include <qrect.h>

// KDE
#include <kdebug.h>

Gradient::Gradient()
    : PaintServer(),
    m_context(0)
{
  m_ref = new URIReference(this, false);
  m_animTransList = new AnimatedTransformList();
  m_gradientUnits = new AnimatedEnum();
  m_spreadMethod  = new AnimatedEnum();
}


Gradient::~Gradient()
{
  delete m_ref;
  delete m_animTransList;
  delete m_gradientUnits;
  delete m_spreadMethod;
}

Gradient::Gradient( const Gradient &other ) : PaintServer(),
    m_context(0)
{
  m_ref = new URIReference(this, false);
  m_animTransList = new AnimatedTransformList();
  m_gradientUnits = new AnimatedEnum();
  m_spreadMethod  = new AnimatedEnum();
  (*this) = other;
}

Gradient& Gradient::operator=(const Gradient &other)
{
  PaintServer::operator=(other);
  *m_ref = other.ref();
  *m_animTransList = other.gradientTransformList();
  *m_gradientUnits = other.gradientUnits();
  *m_spreadMethod = other.spreadMethod();
  return *this;
}

Gradient& Gradient::operator|=(const Gradient &other)
{
  PaintServer::operator=(other);
  *m_ref = other.ref();
  *m_animTransList = other.gradientTransformList(true);
  *m_gradientUnits = other.gradientUnits(true);
  *m_spreadMethod = other.spreadMethod(true);
  return *this;
}

Attribute* Gradient::getAttrByName( const QString & name )
{
  if (name == "gradienttransform")
    return m_animTransList;
  else if (name == "gradientUnits")
    return m_gradientUnits;
  else if (name == "spreadMethod")
    return m_spreadMethod;
  return 0;
}

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

QRect Gradient::bbox(KCBBoxFlags flags) const
{
  return QRect();
}

/**
 * Set the context and set this as UserData of m_context
 * @param context 
 */
void Gradient::setContext( KRenderingPaintServer* context )
{
  m_context = context;
}

AnimatedTransformList& Gradient::gradientTransformList( bool inherited ) const
{
  if ( inherited && !m_animTransList->isValid() )
  {
    if ( m_ref->getRefElement() && m_ref->getRefElement()->inherits("Gradient") )
    {
      Gradient* ref = static_cast<Gradient*>(m_ref->getRefElement());
      AnimatedTransformList& inheritedAttr = ref->gradientTransformList(true);
      if ( inheritedAttr.isValid() )
        return inheritedAttr;
    }
  }
  return *m_animTransList;
}

AnimatedEnum& Gradient::gradientUnits( bool inherited ) const
{
  if ( inherited && !m_gradientUnits->isValid() )
  {
    if ( m_ref->getRefElement() && m_ref->getRefElement()->inherits("Gradient") )
    {
      Gradient* ref = static_cast<Gradient*>(m_ref->getRefElement());
      AnimatedEnum& inheritedAttr = ref->gradientUnits(true);
      if ( inheritedAttr.isValid() )
        return inheritedAttr;
    }
  }
  return *m_gradientUnits;
}

AnimatedEnum& Gradient::spreadMethod( bool inherited ) const
{
  if ( inherited && !m_spreadMethod->isValid() )
  {
    if ( m_ref->getRefElement() && m_ref->getRefElement()->inherits("Gradient") )
    {
      Gradient* ref = static_cast<Gradient*>(m_ref->getRefElement());
      AnimatedEnum& inheritedAttr = ref->spreadMethod(true);
      if ( inheritedAttr.isValid() )
        return inheritedAttr;
    }
  }
  return *m_spreadMethod;
}

#include "gradient.moc"
