/*
 * Copyright 2009-2010  Stefan Gehn <stefan@srcbox.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, see <http://www.gnu.org/licenses/>.
 */

#ifndef IRVALUE_H_
#define IRVALUE_H_

#include <qdebug.h>
#include <qrect.h>
#include <qlist.h>

namespace IrTouch
{

class Value
{
public:
  Value();
  Value(int value, int extent);

  //static QRect toRect(const Value &vHor, const Value &vVert);

  quint32 id() const
  {
    return mId;
  }

  bool isValid() const
  {
    return (mId > 0) && (mValue >= 0 && mExtent > 0);
  }

  int value() const
  {
    return (int) (mValue * mScaleFactor);
  }

  int extent() const
  {
    return (int) (mExtent * mScaleFactor);
  }

  inline int start() const
  {
    return value();
  }

  inline int center() const
  {
    return value() + (extent() / 2);
  }

  inline int end() const
  {
    return value() + extent();
  }

  void setScaleFactor(qreal factor)
  {
    if (factor > 0)
      mScaleFactor = factor;
  }

  qreal scaleFactor() const
  {
    return mScaleFactor;
  }

  void update(int start, int extent)
  {
    mValue = start;
    mExtent = extent;
  }

  void swapValues(Value &o)
  {
    int v = mValue;
    int e = mExtent;

    mValue = o.mValue;
    mExtent = o.mExtent;

    o.mValue = v;
    o.mExtent = e;
  }

  bool operator<(const Value &o) const { return start() < o.start(); }

private:
  static quint32 sId;

  int mValue;
  int mExtent;
  qreal mScaleFactor;
  quint32 mId;

public: //TODO: simple hack for now
  bool mHandled;
  bool mIsMerge;
  int mMaxMergeOverlap;
};

typedef QList<Value> ValueList;

} // namespace

QDebug operator<<(QDebug dbg, const IrTouch::Value &p);

#endif /*IRVALUE_H_*/
