/***************************************************************************
 *   Copyright (C) 2004 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 QCONVWIDGET_H
#define QCONVWIDGET_H

#include <iostream>

#include <QWidget>

#include "kryomolcore_export.h"
#include "threshold.h"

/**
This class contais QwtPlot widget for info on convergence of calculations
 
@author Armando Navarro-Vazquez
*/

class QuantumPlot;
class Convergence
{
public:
  Convergence(size_t size);
  ~Convergence();
  double* Energies() { return m_energies; }
  double* S2() { return m_S2; }
  double* RMSForces()  {return m_rmsforces; }
  double* MaximumForces() { return m_maximumforces; }
  double* RMSDisplacements() { return m_rmsdisplacements; }
  double* MaximumDisplacements() { return m_maximumdisplacements; }
  double MaxEnergy();
  double MinEnergy();

  /* double MaxForce();
   double MinForce();
   double MaxDisplacement();
   double MinDisplacement();*/
  size_t size() const { return m_size; }
  friend std::ostream& operator << (std::ostream& s, Convergence& conv);
private:
  double* m_energies;
  double* m_S2;
  double* m_rmsforces;
  double* m_maximumforces;
  double* m_rmsdisplacements;
  double* m_maximumdisplacements;
  size_t m_size;
private:
  double GetMaxArray(double* array);
  double GetMinArray(double* array);

};

class QwtPlot;
class QDoubleEditBox;
class QSlider;
class KRYOMOLCORE_EXPORT QConvWidget : public QWidget
{
  Q_OBJECT
public:
  QConvWidget(QWidget *parent = 0, const char *name = 0);

  ~QConvWidget();
  void SetNData(size_t size);
  double* GetEnergies() { return m_convdata->Energies(); }
  double* GetS2() { return m_convdata->S2(); }
  double* GetRMSForces() { return m_convdata->RMSForces(); }
  double* GetMaximumForces() { return m_convdata->MaximumForces(); }
  double* GetRMSDisplacements() { return m_convdata->RMSDisplacements(); }
  double* GetMaximumDisplacements() { return m_convdata->MaximumDisplacements(); }

  void SetupCurves();
  void SetThreshold(const Threshold& thr) { m_threshold=thr; }
  void SetEnergyLevel(const QString& level) {  m_energylevel=level; }
signals:
  void selectedPoint(int );
  void forcescale(float );
public slots:
  void OnSelectedPoint(int );
private slots:
  void OnChangeLimits(int, int);
  void OnForceSliderChanged(int v);
private:
  QuantumPlot* m_energy;
  QuantumPlot* m_gradient;
  QuantumPlot* m_displacement;
  double* m_xaxis;
  double* m_yenergies;
  double* m_yrmsforces;
  double* m_ymaximumforces;
  double* m_yrmsdisplacements;
  double* m_ymaximumdisplacements;
  size_t m_ndata;
  QString m_energylevel;
  Convergence* m_convdata;
  Threshold m_threshold;
  QDoubleEditBox* m_limitsbox;
  QSlider* m_scaleslider;
protected:
  QSize sizeHint() const { return QSize(350,250); }

};

#endif
