//
// C++ Implementation:
//
// Description:
//
//
// Author: Trenton Carr <trenton.carr@gmail.com>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include <math.h>
#include <qlayout.h>
#include <qcombobox.h>
#include <qcheckbox.h>
//Added by qt3to4:
#include <Q3VBoxLayout>
#include <QPaintEvent>
#include <klocale.h>
#include <knuminput.h>

#include <../kplotwidget.h>

#include "triggraph.h"

TrigGraph::TrigGraph(QWidget* parent, const char* name, WFlags fl)
        : TrigGrapherD(parent,name,fl)
{

    QWidget *viewGraph = new QWidget (TrigGraphFrame, 0);

    Q3VBoxLayout *vlay1 = new Q3VBoxLayout( viewGraph, -1 );

    pw = new KPlotWidget( -720.0, 720.0, -5.0, 5.0, viewGraph);

    pw->setShowAxes( false );
    pw->setShowTickMarks( false );
    pw->setBGColor( QColor( "black" ) );
    pw->setFGColor( QColor ( "white") );
    pw->setCartesian( true );
    pw->setShowCartAxis( true );
    pw->setShowCartTickMarks( true );
    pw->setShowCartTickLabels( true );
    pw->setShowDegreeCircle( false );

    vlay1->addWidget( pw );

    ratio_box->insertItem( QString( "SIN" ) );
    ratio_box->insertItem( QString( "COS" ) );
    ratio_box->insertItem( QString( "TAN" ) );

    viewGraph->resize( 450, 450 );

    connect( spin_a_val, SIGNAL(valueChanged( double )), SLOT( slotUpdateTrigGraph() ));
    connect( spin_b_val, SIGNAL(valueChanged( double )), SLOT( slotUpdateTrigGraph() ));

    connect( showAxis,SIGNAL(toggled( bool )), SLOT( slotUpdateTrigGraph() ));
    connect( showTickMarks, SIGNAL(toggled( bool )), SLOT( slotUpdateTrigGraph() ));
    connect( showTickLabels, SIGNAL(toggled( bool )), SLOT( slotUpdateTrigGraph() ));
    connect( showDescriptions, SIGNAL(toggled( bool )), SLOT( slotUpdateTrigGraph() ));

    initObjects();
}

TrigGraph::~TrigGraph()
{}

void TrigGraph::slotUpdateTrigGraph()
{

    if ( showAxis->isChecked() )
    {
        pw->setShowCartAxis( true );
    }
    else
    {
        pw->setShowCartAxis( false );
    }

    if ( showTickMarks->isChecked() )
    {
        pw->setShowCartTickMarks( true );
        showTickLabels->setEnabled ( true );
    }
    else
    {
        pw->setShowCartTickMarks( false );
        showTickLabels->setEnabled ( false );
    }

    if ( showTickLabels->isChecked() )
    {
        pw->setShowCartTickLabels( true );
    }
    else
    {
        pw->setShowCartTickLabels( false );
    }

    pw->object( 0 )->setHidden( true );
    pw->object( 1 )->setHidden( true );
    pw->object( 2 )->setHidden( true );

    //     KPlotObject *cntrl = pw->object( 3 );
    //     DPoint *cntrl_p = new DPoint();

    switch( ratio_box->currentItem() )
    {
    case 0:
    {
        KPlotObject *sine = pw->object( 0 );

        DPoint *sp = new DPoint();
        for ( sp = sine->points()->first(); sp; sp = sine->points()->next() )
        {
            sp->setY( (sin(sp->x() * M_PI / 180)) * spin_a_val->value() );
            // 			sp->setX( sp->x() / spin_b_val->value() );	Pino, this nearly works.
        }
        //         for ( cntrl_p = cntrl->points()->first(); cntrl_p; cntrl_p = cntrl->points()->next() )
        //         {
        //             sp->setX(( cntrl_p->x() / spin_b_val->value() ));
        //         }

		/*
             spin_b_val->value() gives the period of the graph but if I use this;
        
            sp->setX( sp->x() / spin_b_val->value() );
                
                it works for 3 or 4 changes and goes crazy.
                All the x values should be devided by spin_b_val->value()
                    for the coresponding y value.
		*/
		
        sine->setHidden( false );
        break;
    }
    case 1:
    {
        KPlotObject *cosine = pw->object( 1 );

        DPoint *cp = new DPoint();
        for ( cp = cosine->points()->first(); cp; cp = cosine->points()->next() )
        {
            cp->setY( (cos(cp->x() * M_PI / 180)) * spin_a_val->value() );
        }
        cosine->setHidden( false );
        break;
    }
    case 2:
    {
        KPlotObject *tangent = pw->object( 2 );

        DPoint *tp = new DPoint();
        for ( tp = tangent->points()->first(); tp; tp = tangent->points()->next() )
        {
            tp->setY( (tan(tp->x() * M_PI / 180)) * spin_a_val->value() );
        }
        tangent->setHidden( false );
        break;
    }
    }
    update();
}

void TrigGraph::initObjects()
{
    KPlotObject *sineGraph;
    KPlotObject *cosineGraph;
    KPlotObject *tangentGraph;
    //    KPlotObject *controlGraph;

    sineGraph = new KPlotObject( "1", "green", KPlotObject::CURVE, 1, KPlotObject::SOLID );

    for( double x = -720.0; x < 720.0; x=x+0.1 )
    {
        DPoint *p = new DPoint( x, sin(x * M_PI / 180) ); //sets both X and Y coords
        sineGraph->addPoint( p );
    }
    pw->addObject( sineGraph );
    sineGraph->setHidden( false );

    cosineGraph = new KPlotObject( "2", "red", KPlotObject::CURVE, 1, KPlotObject::SOLID );

    for( double x = -720.0; x < 720.0; x=x+0.1 )
    {
        DPoint *p = new DPoint( x, cos(x * M_PI / 180) ); //sets both X and Y coords
        cosineGraph->addPoint( p );
    }
    pw->addObject( cosineGraph );
    cosineGraph->setHidden( true );

    tangentGraph = new KPlotObject( "3", "blue", KPlotObject::CURVE, 1, KPlotObject::SOLID );

    for( double x = -720.0; x < 720.0; x=x+0.1 )
    {
        DPoint *p = new DPoint( x, tan(x * M_PI / 180) ); //sets both X and Y coords
        tangentGraph->addPoint( p );
    }
    pw->addObject( tangentGraph );
    tangentGraph->setHidden( true );

    //     controlGraph = new KPlotObject( "4", "yellow", KPlotObject::CURVE, 1, KPlotObject::SOLID );
    //
    //     for( double x = -720.0; x < 720.0; x=x+0.1 )
    //     {
    //         DPoint *p = new DPoint( x, 0 ); //sets both X and Y coords
    //         controlGraph->addPoint( p );
    //     }
    //     pw->addObject( controlGraph );
    //     controlGraph->setHidden( false );
}

void TrigGraph::paintEvent( QPaintEvent* )
{
    pw->update();
}


#include "triggraph.moc"

