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

#include <qstring.h>

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

// KDE
#include <kdebug.h>

QString LengthAttribute::LengthUnitText[] =
  {
    "", "", "%", "em", "ex", "px", "cm", "mm", "in", "pt", "pc"
  };

LengthAttribute::LengthAttribute() : Attribute(ATTR_LENGTH),
    m_unitType(SVG_LENGTHTYPE_UNKNOWN),
    m_valueInSpecifiedUnits(0),
    m_value(0)
{}

LengthAttribute::LengthAttribute( float value, unsigned int type) : Attribute(ATTR_LENGTH)
{
  newValueSpecifiedUnits(type, value);
}

LengthAttribute::LengthAttribute(const LengthAttribute &other) : Attribute(ATTR_LENGTH)
{
  (*this) = other;
}

LengthAttribute::~LengthAttribute()
{}

LengthAttribute &LengthAttribute::operator=(const LengthAttribute &other)
{
  Attribute::operator=(other);
  m_unitType = other.unitType();
  m_value = other.value();
  m_valueInSpecifiedUnits = other.valueInSpecifiedUnits();

  return *this;
}

bool LengthAttribute::operator==( const LengthAttribute &other )
{
  return ( m_unitType==other.unitType() &&
           m_value==other.value() &&
           m_valueInSpecifiedUnits==other.valueInSpecifiedUnits() );
}

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

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

void LengthAttribute::setValue(float value)
{
  m_value = value;

  switch( m_unitType )
  {
  case(SVG_LENGTHTYPE_PT):
          m_valueInSpecifiedUnits = value / 1.25;
    break;
  case(SVG_LENGTHTYPE_PC):
          m_valueInSpecifiedUnits = value / 15;
    break;
  case(SVG_LENGTHTYPE_MM):
          m_valueInSpecifiedUnits = value / 3.543307;
    break;
  case(SVG_LENGTHTYPE_CM):
          m_valueInSpecifiedUnits = value / 35.43307;
    break;
  case(SVG_LENGTHTYPE_IN):
          m_valueInSpecifiedUnits = value / 90;
    break;
  default:
    m_valueInSpecifiedUnits = value;
    break;
  }
}


void LengthAttribute::setValueInSpecifiedUnits(float valueInSpecifiedUnits)
{
  m_valueInSpecifiedUnits = valueInSpecifiedUnits;

  switch( m_unitType )
  {
  case(SVG_LENGTHTYPE_PT):
          m_value = valueInSpecifiedUnits * 1.25;
    break;
  case(SVG_LENGTHTYPE_PERCENTAGE):
          m_value = valueInSpecifiedUnits;
    break;
  case(SVG_LENGTHTYPE_PC):
          m_value = valueInSpecifiedUnits * 15;
    break;
  case(SVG_LENGTHTYPE_MM):
          m_value = valueInSpecifiedUnits * 3.543307;
    break;
  case(SVG_LENGTHTYPE_CM):
          m_value = valueInSpecifiedUnits * 35.43307;
    break;
  case(SVG_LENGTHTYPE_IN):
          m_value = valueInSpecifiedUnits * 90;
    break;
  default:
    m_value = valueInSpecifiedUnits;
    break;
  }
}

void LengthAttribute::newValueSpecifiedUnits(unsigned int unitType, float valueInSpecifiedUnits)
{
  if (unitType>10)
  {
    kdWarning() << "LengthAttribute::newValueSpecifiedUnits() - Invalid unitType given" << endl;
    return;
  }
  m_unitType = unitType;
  setValueInSpecifiedUnits(valueInSpecifiedUnits);
  setValid();
}

const QString LengthAttribute::readableSpecifiedUnits() const
{
  return LengthUnitText[m_unitType];
}

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