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

// Elements
#include "elements/lineargradient.h"
#include "elements/radialgradient.h"
#include "elements/stopelement.h"
#include "elements/urireference.h"

// KDE
#include <kdebug.h>

DeepCloneVisitor::DeepCloneVisitor()
    : Visitor(),
    m_clone(0)
{}


DeepCloneVisitor::~DeepCloneVisitor()
{}

bool DeepCloneVisitor::visit( LinearGradient & element )
{
  LinearGradient* grad = new LinearGradient();
  *grad |= element;
  m_clone = grad;
  processGradient(element);
  grad->ref().setURI(QString::null);
  grad->ref().setRefElement(0);
  return true;
}

bool DeepCloneVisitor::visit( RadialGradient & element )
{
  RadialGradient* grad = new RadialGradient();
  *grad |= element;
  m_clone = grad;
  processGradient(element);
  grad->ref().setURI(QString::null);
  grad->ref().setRefElement(0);
  return true;
}

bool DeepCloneVisitor::processGradient( Gradient & element )
{
  kdDebug() << k_funcinfo << endl;

  // Copy all subsequent Stop elements of the old gradient
  Gradient* currentElement = &element;
  while (currentElement)
  {
    ElementNode* currentChild = currentElement->getFirstChild();
    while (currentChild )
    {
      StopElement *stop = ::qt_cast<StopElement*>(currentChild);
      if ( stop )
      {
        StopElement *stopCopy = new StopElement(*stop);
        m_clone->appendChild(stopCopy);
        kdDebug() << "DeepCloneVisitor::processGradient() - Adding stop " << stopCopy << " from " << stop << endl;
      }
      currentChild = currentChild->getNext();
    }
    if ( currentElement->ref().getRefElement() )
    {
      Gradient* refGradient = ::qt_cast<Gradient*>( currentElement->ref().getRefElement() );
      if (refGradient)
        currentElement = static_cast<Gradient*>(currentElement->ref().getRefElement());
    }
    else
      currentElement = 0;
  }
  return true;
}
