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

#include <qcolor.h>

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

// KDE
#include <kdebug.h>

ColorAttribute::ColorAttribute() : Attribute(ATTR_COLOR),
    m_type(COLORT_UNSPECIFIED)
{
  m_qColor = new QColor("black");
}

ColorAttribute::ColorAttribute( const QColor & color ) : Attribute(ATTR_COLOR),
    m_type(COLORT_RGB)
{
  m_qColor = new QColor( color );
  setValid();
}

ColorAttribute::ColorAttribute( int r, int g, int b ) : Attribute(ATTR_COLOR),
    m_type(COLORT_RGB)
{
  m_qColor = new QColor(r,g,b);
  setValid();
}

ColorAttribute::ColorAttribute( const QString & color ) : Attribute(ATTR_COLOR),
    m_type(COLORT_UNSPECIFIED)
{
  m_qColor = new QColor("black");

  // TODO: store the original definiton of the color
  // TODO: support more color definitions
  if ( color.startsWith("#") )
  {
    bool ok;
    int red = color.mid(1,2).toInt(&ok,16);
    int green = color.mid(3,2).toInt(&ok,16);
    int blue = color.mid(5,2).toInt(&ok,16);
    // kdDebug() << "ColorAttribute::ColorAttribute(" << color << ") -> RGB:" << red << "," << green << "," << blue << endl;
    m_qColor->setRgb(red,green,blue);
    m_type = COLORT_RGB;
    setValid();
  }
  else
  {
    *m_qColor = QColor(color); // FIXME: support all colors defined in SVG
    m_type = COLORT_NAME;
    setValid();
    // kdDebug() << "ColorAttribute::ColorAttribute(" << color << ") -> Named Color interpreted as " << *m_qColor << endl;
  }
}

ColorAttribute::ColorAttribute(const ColorAttribute &other)
    : Attribute(ATTR_COLOR),
    m_qColor(0),
    m_type(COLORT_UNSPECIFIED)
{
  m_qColor = new QColor("black");
  (*this) = other;
}

ColorAttribute::~ColorAttribute()
{}

ColorAttribute &ColorAttribute::operator=(const ColorAttribute &other)
{
  Attribute::operator=(other);
  *m_qColor = other.qColor();
  m_type = other.colorType();
  setValid();

  return *this;
}

bool ColorAttribute::operator ==( const ColorAttribute &other )
{
  return (*m_qColor == other.qColor());
}

bool ColorAttribute::operator!=( const ColorAttribute &other )
{
  return !(*this==other);
}

void ColorAttribute::setColor( const QColor &color )
{
  *m_qColor = color;
  m_type = COLORT_RGB;
  setValid();
}

Attribute* ColorAttribute::clone()
{
  ColorAttribute* retElem = new ColorAttribute();
  *retElem = *this;
  return ( retElem );
}

void ColorAttribute::accept( AVisitor &a )
{
  a.visit(*this);
}
