//    tonalisa - software to look at overtone-structures
//    Copyright (C) 2016  Dominik Schmidt-Philipp
//
//    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 3 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 CARTESIAN_H
#define CARTESIAN_H

#include <QtQuick/QQuickPaintedItem>
#include "frequencyview.h"

class Cartesian : public FrequencyView
{
  Q_OBJECT
  Q_PROPERTY(float rightHz READ rightHz WRITE setRightHz NOTIFY rightHzChanged)
  Q_PROPERTY(float leftHz READ leftHz WRITE setLeftHz NOTIFY leftHzChanged)
  Q_PROPERTY(bool paintDissonance READ paintDissonance WRITE setPaintDissonance)
  Q_PROPERTY(QList<double> dissonance READ dissonance WRITE setDissonance NOTIFY dissonanceChanged)
  Q_PROPERTY(float dbFloor READ dbFloor WRITE setDbFloor NOTIFY dbFloorChanged)
  Q_PROPERTY(bool logx READ logx WRITE setLogx NOTIFY logxChanged)
  Q_PROPERTY(float maxDissonance READ maxDissonance WRITE setMaxDissonance NOTIFY maxDissonanceChanged)
  
 public:
  Cartesian(QQuickItem *parent = 0);
  
  float rightHz();
  float leftHz();
  bool paintDissonance();
  QList<double> dissonance();
  float dbFloor() const;
  bool logx() const;
  float maxDissonance() const;
  
  void setRightHz( float &val);
  void setLeftHz( float &val);
  void setPaintDissonance( const bool &paint);
  void setDissonance(QList<double> &dissonance);
  void setMaxDissonance( const float maxDissonance );
  void paint(QPainter *painter);
  void setDbFloor(const float &dbFloor);
  void setLogx( const bool &logx);
  virtual void setPointerFreq(const double &pointerFreq);
  virtual void setPointerAmp(const double &pointerAmp);
  virtual void setGrid(const QList<double> &grid);
  
 signals:
  void rightHzChanged();
  void leftHzChanged();
  void dissonanceChanged();
  void dbFloorChanged();
  void logxChanged();
  void maxDissonanceChanged();
  
 private:
  void updatePointerPos();
  float m_leftHz;
  float m_rightHz;
  float m_dbFloor;
  bool m_logx;
  float m_maxDissonance;
  
  float binToHertz(long bin);
  long hertzToBin(float hertz);
  float binToX(long bin);
  float hertzToX(double hertz);
  float scaleAmp(double val);
  float ratio2cent(double ratio);
  double cent2ratio(float cent);

  bool paint_dissonance;
  QList<double>m_dissonance;
  
  virtual void calculateGrid();
  QList<double>m_gridData;
  int m_gridSteps;

  void printFFT(QPainter *painter);
  void printPeaks(QPainter *painter);
  void printDissonance(QPainter *painter);
  void printGrid(QPainter *painter);
};

#endif // CARTESIAN_H
