/***************************************************************************
 *   Copyright (C) 2006 by Armando Navarro Vazquez   *
 *   qoajnv@usc.es   *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef INTEGRATOR_H
#define INTEGRATOR_H

/**
@author Armando Navarro Vazquez
*/
#include <vector>
#include <list>
#include <complex>

#include <qpoint.h>

class QPainter;
class Spectrum;
class QRect;
class QPoint;
class Integrator;
class Integral
{
  friend class Integrator;
public:
  Integral(Integrator* integrator, size_t from, size_t to);
  ~Integral();
  size_t From() const { return m_from; }
  size_t To() const { return m_to; }
  float Value(bool real=true);
private:
      void CalculateValue();
  void SetupDrawingPath(const QRect& clientrect);
  void Draw(QPainter* p,const QRect& clientrect);

private:
  Integrator* m_integrator;
  std::vector<QPoint> m_path;
  size_t m_from;
  size_t m_to;
  std::complex<float> m_value;
};



class Integrator
{
  friend class Integral;
public:
  Integrator(Spectrum* sp);
  ~Integrator() {};
  void AddIntegral(const QRect& region, const QRect& clientrect);
  void DeleteIntegral(const QPoint& pos);
  void Clear() { m_integrals.clear(); }
  void Draw(QPainter* p, const QRect& clientrect);
  void UpdateDrawing(const QRect& clientrect);
  /** Set the baseline for integrals as a ratio of the client rectangle*/
  void SetBaseline(float r) { m_baseline=r; }
  float Baseline() const { return m_baseline; }
  void SetScale(float s) { m_scale=s; }
  float Scale() const { return m_scale; }
  /** Return the value for an integral region*/
  void Value(const Integral& intrg);
private:
  void CalculateScale();
 Spectrum* GetSpectrum() { return m_spectrum; }
private:
  std::vector<Integral> m_integrals;
  Spectrum* m_spectrum;
  float m_scale;
  float m_baseline;
};

#endif
