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

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

Number::Number()
    : Attribute(ATTR_NUMBER),
    m_value(0.0),
    m_numType(NUM_UNSPECIFIED)
{}

Number::Number( double value, unsigned int type ) : Attribute(ATTR_NUMBER)
{
  m_value = value;
  m_numType = type;
  setValid();
}

Number::Number( const QString & numstr ): Attribute(ATTR_NUMBER),
    m_value(0.0),
    m_numType(NUM_UNSPECIFIED)
{
  if (numstr.endsWith("%"))
  {
    m_value = numstr.left(numstr.length()-1).toDouble();
    m_numType = Number::NUM_PERCENTAGE;
  }
  else
  {
    m_value = numstr.toDouble();
    m_numType = Number::NUM_INT;
  }
  setValid();
}

Number::Number(const Number &other) : Attribute(ATTR_NUMBER),
    m_value(0.0),
    m_numType(NUM_UNSPECIFIED)
{
  (*this) = other;
}

Number::~Number()
{}

Number &Number::operator=(const Number &other)
{
  Attribute::operator=(other);
  m_value = other.value();
  m_numType = other.numType();
  return *this;
}

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

bool Number::operator ==( const Number &other )
{
  return (m_value == other.value() && m_numType == other.numType());
}

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

void Number::setValue( double d )
{
  m_value = d;
  setValid();
}

/**
 * Converts the value to a floating point nr. and returns it
 * Especially usefull for type Percentage
 * @return 
 */
double Number::floatValue( ) const
{
  if ( m_numType == Number::NUM_PERCENTAGE )
    return m_value / 100;
  else
    return m_value;
}

/**
 * Converts the value to type percentage and returns it
 * @return 
 */
double Number::percentValue( ) const
{
  if ( m_numType != Number::NUM_PERCENTAGE )
    return m_value * 100;
  else
    return m_value;
}

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