/***************************************************************************
 *   Copyright (C) 2004 by Trenton Carr                                    *
 *   trenton.carr@gmail.com                                                *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/

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

#include "trigviewer.h"
#include "ktrigplotwidget.h"
#include "deg_theta.h"


TrigViewer::TrigViewer(QWidget* parent, const char* name)
        : QWidget( parent, name )

{
    Q3VBoxLayout *vlay = new Q3VBoxLayout( this, 0, -1 );

    KDoubleSpinBox *spin = new KDoubleSpinBox( 0.0, 360.0, 1, 45.0, 2, this, 0);

    pw = new KTrigPlotWidget( -100.0, 100.0, -100.0, 100.0, this );

    deg = new Deg_Theta();

    pw->setShowAxes( true );
    pw->setShowTickMarks( true );
    pw->setBGColor( QColor( "black" ) );
    pw->setFGColor( QColor ( "white") );
    pw->setXAxisLabel( i18n( "X-Axis" ) );
    pw->setYAxisLabel( i18n( "Y-Axis" ) );
    pw->setShowCartTrigAxes( true );
    pw->setShowCartTrigTickMarks( true );

    vlay->addWidget( pw );
    vlay->addWidget( spin );
    resize( 500, 528 );

    connect( spin, SIGNAL(valueChanged( double )), deg, SLOT( setAngle( double )) );
    connect( deg, SIGNAL(angleChanged( double )), SLOT( updateAngle()) );

    initObjects();
}

TrigViewer::~TrigViewer()
{}

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


void TrigViewer::updateAngle()
{
    double radians = deg->angle() * M_PI / 180;
    double X_val = 100*cos(radians);
    double Y_val = 100*sin(radians);

    KPlotObject *r = pw->object( 0 );
    r->point( 1 )->setX( X_val );
    r->point( 1 )->setY( Y_val );

    KPlotObject *x = pw->object( 1 );
    x->point( 1 )->setX( X_val );

    KPlotObject *y = pw->object( 2 );
    y->point( 0 )->setX ( X_val );
    y->point( 1 )->setX ( X_val );
    y->point( 1 )->setY ( Y_val );

    update();

    //     //We now have only one plot object; modify its
    //     //three vertices to match the new angle
    //     KPlotObject *o = pw->object(0);
    //
    //     //First point is (0, 0), so no need to modify it
    //     //Second point is (X, 0)
    //     o->point(1)->setX( X_val );
    //
    //     //Third point is (X, Y)
    //     o->point(2)->setX( X_val );
    //     o->point(2)->setY( Y_val );
    //
    //     //Redraw the widget
    //     update();
}


void TrigViewer::initObjects()
{
    /*    KPlotObject *trigTriangle;*/				//Trig Triangle

    KPlotObject *r_hypotenuse;
    KPlotObject *x_adjacent;
    KPlotObject *y_opposite;

    double radians = deg->angle() * M_PI / 180;
    double X_val = 100*cos(radians);
    double Y_val = 100*sin(radians);



    //     trigTriangle = new KPlotObject( "Triangle", "red", KPlotObject::POLYGON, 1, KPlotObject::SOLID );
    //
    //     trigTriangle->addPoint( new DPoint( 0.0, 0.0 ) );
    //     trigTriangle->addPoint( new DPoint( X_val, 0.0 ) );
    //     trigTriangle->addPoint( new DPoint( X_val, Y_val ) );
    //     pw->addObject( trigTriangle );


    r_hypotenuse = new KPlotObject( "R", "red", KPlotObject::POLYGON, 1, KPlotObject::SOLID );
    r_hypotenuse->addPoint( new DPoint( 0.0, 0.0 ) );
    r_hypotenuse->addPoint( new DPoint( X_val, Y_val ) );
    pw->addObject( r_hypotenuse );			//Draw R

    x_adjacent = new KPlotObject( "X", "red", KPlotObject::POLYGON, 1, KPlotObject::SOLID );
    x_adjacent->addPoint( new DPoint( 0.0, 0.0 ) );
    x_adjacent->addPoint( new DPoint( X_val, 0.0 ) );
    pw->addObject( x_adjacent );			//Draw X

    y_opposite = new KPlotObject( "Y", "red", KPlotObject::POLYGON, 1, KPlotObject::SOLID );
    y_opposite->addPoint( new DPoint( X_val, 0.0 ) );
    y_opposite->addPoint( new DPoint( X_val, Y_val ) );
    pw->addObject( y_opposite );			//Draw Y
}




#include "trigviewer.moc"

