/***************************************************************************
 *   Copyright (C) 2004 by Armando Navarro Vázquez                         *
 *   armando@rmntec                                                        *
 *                                                                         *
 *   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 <q3table.h>
#include <qlineedit.h>
#include <qspinbox.h>
#include <qpushbutton.h>
#include <qcombobox.h>
#include <qmessagebox.h>
#include <q3filedialog.h>

#include "quvwidget.h"
#include "qlogtable.h"

QUVWidget::QUVWidget(const QString& file, QWidget* parent)
    : QWidget(parent)
{
  setupUi(this);
  m_file=file.latin1();
  m_bshowspectrum=true;
  _uvTable->setReadOnly(true);
  _uvTable->setColumnStretchable(0,true);
  _uvTable->setColumnStretchable(1,true);
  _uvTable->setSorting(true);
  connect(showSpecButton,SIGNAL(clicked()),this,SLOT(OnShowSpectrum()));
  connect(_spectrumTypeComboBox,SIGNAL(activated(int)),this,SLOT(OnSpectrumTypeChanged(int)));

  float linewidth=LineWidth();
  _lineWidthSpinBox->setValue(linewidth);
  connect(_lineWidthSpinBox,SIGNAL(valueChanged(int)),this,SLOT(OnSetLineWidth(int)));
  connect(_resetLimitsButton,SIGNAL(clicked()),this,SLOT(OnResetLimits()));
  connect(_exportIRButton,SIGNAL(clicked()),this,SLOT(OnWriteJCampDX()));

  connect(_leftLimitLineEdit,SIGNAL(returnPressed()),this,SLOT(OnChangeLimits()));
  connect(_rightLimitLineEdit,SIGNAL(returnPressed()),this,SLOT(OnChangeLimits()));
}


QUVWidget::~QUVWidget()
{}

void QUVWidget::OnShowSpectrum()
{
  emit showspectrum(m_bshowspectrum);
  emit Type(GetType()); //to stablish the baseline
  m_bshowspectrum=!m_bshowspectrum;

  QString buttontext;
  if(!m_bshowspectrum)
  {
    buttontext=tr("Hide Spectrum");
  }
  else buttontext=tr("Show Spectrum");

  showSpecButton->setText(buttontext);
}


/*!
    \fn QUVWidget::InitTable()
 */
void QUVWidget::InitTable()
{

  QString sheader;
  switch(GetType())
  {
  case UV:
    sheader="Oscillator Strenght";
    break;
  case ECD:
    sheader="Rotatory Strenght";
    break;
  }
  _uvTable->horizontalHeader()->setLabel(1,sheader);
  //SetFrequencies(m_list);
  std::vector<spectralline>::iterator it;

  _uvTable->setNumRows(m_lines.size());
  std::cout << "number of line is " << m_lines.size() << std::endl;

  int i;
  for(it=m_lines.begin(),i=0;it!=m_lines.end();it++,i++)
  {
    QString sf, si;
    sf.setNum(it->x,'f',1);
    switch(GetType())
    {
    case ECD:
      si.setNum(it->y1,'f',3);
      break;
    default:
      si.setNum(it->y0,'f',3);
    }
    _uvTable->setText(i,0,sf);
    _uvTable->setText(i,1,si);
  }

  //initialize the irspectrum object

  SetAutoLimits();
  //write the limits
  QString max,min;
  max.setNum(Max());
  min.setNum(Min());
  _leftLimitLineEdit->setText(max);
  _rightLimitLineEdit->setText(min);
  SetLineWidth(_lineWidthSpinBox->value());
  CalculateSpectrum();

}

void QUVWidget::OnSpectrumTypeChanged(int t)
{
  SetType(static_cast<SpectrumType>(t));
  InitTable();
  std::cout << " max is " << Max() << "min is " << Min() << std::endl;
  emit data(GetData(),Max(),Min());
  emit Type(GetType());
}

void QUVWidget::OnSetLineWidth(int lw)
{
  SetLineWidth(1/(M_PI*lw));
  CalculateSpectrum();
  emit data(GetData(),Max(),Min());
  //emit Type(GetType()); //to stablish the baseline
}


void QUVWidget::OnSetLimits(float max,float min)
{
  QString smax,smin;
  smax.setNum(max);
  smin.setNum(min);
  _leftLimitLineEdit->setText(smax);
  _rightLimitLineEdit->setText(smin);
  SetLimits(max,min);
  emit data(GetData(),Max(),Min());
}

void QUVWidget::OnResetLimits()
{
  SetAutoLimits();
  //write the limits
  QString max,min;
  max.setNum(Max());
  min.setNum(Min());
  _leftLimitLineEdit->setText(max);
  _rightLimitLineEdit->setText(min);
  CalculateSpectrum();
  emit data(GetData(),Max(),Min());

}

void QUVWidget::OnChangeLimits()
{
  float min =_rightLimitLineEdit->text().toFloat();
  float max =_leftLimitLineEdit->text().toFloat();
  if( max < min)
  {
    QMessageBox mb;
    mb.setText("Sorry, limits are inverted");
    mb.exec();
    return;
  }
  SetLimits(max,min);
  emit data(GetData(),Max(),Min());
}

void QUVWidget::OnWriteJCampDX()
{

#ifdef USE_KDE
#else
  //propose a name for the file
  QString sfile(m_file.c_str());
  QString sfilename=sfile.section('.',0,-2)+".jdx"; //change out extension by jdx
  QString file= Q3FileDialog::getSaveFileName(sfilename,"JCAMP-DX (*.jdx)",
                this,
                "QryoMol",
                "Write JCAMP-DX spectrum" );
  if( file != QString::null)
  {
    m_file=file.latin1();
    WriteJCampDX();
  }
#endif

}
