/***************************************************************************
 *   Copyright (C) 2005 by Armando Navarro-Vazquez                         *
 *   armando@linux                                                         *
 *                                                                         *
 *   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.             *
 ***************************************************************************/

#include <iostream>
#include <qpainter.h>

#include "peaktablespectrum.h"

PeakTableSpectrum::PeakTableSpectrum()
 : Spectrum()
{
   SetBaseLine(0.8);
}


PeakTableSpectrum::~PeakTableSpectrum()
{

}

void PeakTableSpectrum::UpdateDrawing(const QRect& rect, bool autoscale/*=false*/)
{

  float max=-1e12;
  float min=1e12;
  int l=m_zoom.firstpoint;
  int r=m_zoom.lastpoint;

  if(autoscale)
  {
    for(size_t i=0;i<m_data.size();i++)
    {
      if( m_data[i].imag() > l && m_data[i].imag() < r)
      {
      if( m_data[i].real() > max) max=m_data[i].real();
      if( m_data[i].real() < min ) min=m_data[i].real();
      }
    }
    float interval=max-min;
    int baseline=qRound(rect.top()+rect.height()*Baseline());
    std::cout << "max=" << max << " baseline=" << baseline << std::endl;
    SetScale(-(rect.top()+-baseline)/max);
    max=-1e12;
    min=1e12;
  }
  std::cout << "scale is " << Scale() << std::endl;

  m_points.clear();
  m_points.reserve(m_data.size());
  float xscalefactor=static_cast<float>(rect.width())/static_cast<float>(r-l);
  m_baselinepos=qRound(rect.top()+rect.height()*Baseline());
  std::cout << "l,r" << l <<  "," << r << std::endl;
  for(size_t i=0;i<m_data.size();++i)
  {
    if( m_data[i].imag() > l && m_data[i].imag() < r )
    {
    int x=(m_data[i].imag()-l)*xscalefactor;
    int y=m_baselinepos-qRound(Scale()*m_data[i].real());
    std::cout << "(x,y)=" << x << "," << y << std::endl;
    m_points.push_back(QPoint(x,y));
    }
   /* if( m_data[i].re > max) max=m_data[i].re;
    if( m_data[i].re < min ) min=m_data[i].re;
    int newx=qRound((i-l)*xscalefactor);
    if( newx!= x)
    {
      m_points.push_back(QPoint(x,baseline-qRound(m_scale*min)));
      m_points.push_back(QPoint(x,baseline-qRound(m_scale*max)));
      value=0;
      x=newx;
      max=-1e12;
      min=1e12;
    }*/

  }
}

void PeakTableSpectrum::Draw(QPainter* p, const QRect& rect)
{
#warning reimplement
  //Draw base line
/*  std::cout << "Draw from peaktable" << std::endl;
  std::vector<QPoint>::iterator it=m_points.begin();
  std::cout << m_baselinepos << std::endl;
  p->moveTo(rect.left(),m_baselinepos);
  p->lineTo(rect.right(),m_baselinepos);

  for(it=m_points.begin();it!=m_points.end();++it)
  {
    //std::cout << it->x() << " " << it->y() << std::endl;
    p->moveTo(it->x(),m_baselinepos);
    p->lineTo(it->x(),it->y());
  }*/
}


void PeakTableSpectrum::MoveZoom(float inc)
{
  if( m_zoom.fright+inc >1.0f || m_zoom.fleft+inc<0.0f )
  {//beep
    return;
  }
  m_zoom.fleft+=inc;
  m_zoom.fright+=inc;
  m_zoom.SetPoints(m_data[m_data.size()-1].imag()+10);
}

void PeakTableSpectrum::ResetZoom()
{
  m_zoom.reset(m_data[m_data.size()-1].imag()+10);
}

void PeakTableSpectrum::GetLimits(float& left,float& right)
{

      float sleft=0;
      float sright=m_data[m_data.size()-1].imag()+10;
      left=(sleft-sright)*(1-m_zoom.fleft)+sright;
      right=(sleft-sright)*(1-m_zoom.fright)+sright;

}