/******************************************************************************
** 	ui.h extenion file, included from the uic-generated form implementation.
**
** 	If you wish to add, delete or rename functions or sluts use
**	 Qt Designer which will update this file, preserving your code. Create an
** 	init() function in place of a constructor, and a destroy() function in
**	 place of a destructor.
**	ultima revisione: 28/12/2004
**
**	Qtballit 0.1 beta - Qt ballistic calculation app - Copyright (C)  Stefano Jodice 2004  
**
**     	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 <qapplication.h>
#include <qpainter.h>
#include <qcanvas.h>
#include <qfiledialog.h>
#include <qstatusbar.h>
#include <qprinter.h>
#include <stdlib.h>

// #include <qfile.h>
// #include <qmessagebox.h>
// #include <qpicture.h>
// #include <iostream.h>

#define Y_COORDA 20
#define Y_COORDB 380
#define X_LENGHT 780

extern int count;
extern float *balPtr;
extern float distanza_massima;

int indx=0;
int indy=0;
 
QCanvas *canvas;
QCanvasView *vista;

void frmtraiett::init()
{ 
  
  
    /// QCanvas *canvas;
   
    //                                       QPainter *pennello;
    
canvas = new QCanvas ( this );
vista = new QCanvasView ( canvas, this );

     
     canvas->setBackgroundColor(Qt::white);
     canvas->resize(850,450);
     setCentralWidget(vista);
     vista->show();
     vista->update();
    
     
         QCanvasLine *X,*Y;
         X = new QCanvasLine(canvas);
         Y = new QCanvasLine(canvas);
       
    
    X->setPoints( 20,20,20,380 );
    Y->setPoints( 20,Y_COORDB, 800,Y_COORDB );
    
    X->setPen( QPen(QColor(Qt::black), 3) );
    Y->setPen( QPen(QColor(Qt::black), 3) );
    
    
    X->show();
    Y->show();
    
     QCanvasLine *tr_vera,*tr_figurata;
     QCanvasText *did_vera, *did_figurata;
    
      tr_vera = new QCanvasLine(canvas);
      tr_figurata = new QCanvasLine(canvas);
      
      did_vera = new QCanvasText(canvas);
      did_figurata = new QCanvasText(canvas);
       
      tr_vera->setPen( QPen(QColor(Qt::green), 1) );
      tr_figurata->setPen( QPen(QColor(Qt::red), 1) );
      
       did_vera->setText (tr("traiettoria reale"));
       did_figurata->setText (tr("traiettoria figurata"));
	
   tr_vera ->setPoints( 40,40,140,40 );
   tr_figurata->setPoints( 160,40,260,40);
   
   did_vera->move(40,20);
   did_figurata->move(160,20);
    
    tr_vera ->show();
    tr_figurata ->show();
     
      did_vera ->show();
      did_figurata ->show();
     
 
    int point_dist=0;
    int point_ord=2;
    int segm=0;    
    int X1=20;
    int Y1=Y_COORDB;
    int X2=0;
    int Y2=0;
    int Y1R=Y_COORDB ;
    int Y2R=0 ;
   //                                                                                                               pennello = new QPainter (canvas);
	
	QCanvasText *etich[count];
	QCanvasText *showord[count];
	QCanvasLine *ref[count];
	QCanvasLine *arch_real[count];
	QCanvasLine *arch[count];
	
	
	segm=abs ( X_LENGHT / (count-1) ) ;
	
	float control=0;
	  bool isTooHigh=FALSE;
	
	 for (int rep=0; rep<count; rep++)
	{
	     
	     control=*(balPtr+point_ord);
	
	     
	     if ((Y_COORDB-control)<0 ) {
		 isTooHigh=TRUE; 
	  }
	      point_ord+=10;
	 }
	 
	 point_ord=2;
	bool jump=0; 
         for (int rep=0; rep<count; rep++)
         {
            int distanza=static_cast<int>(*(balPtr+point_dist));
	int ordinata=static_cast<int> (*(balPtr+point_ord));
	
  
    //                                                                                               pennello->drawText (,(QString::number(distanza)));
	etich[rep] = new QCanvasText(canvas);
	etich[rep]->setText(QString::number(distanza));
   //                                                                                                           etich[rep]->move((75*rep)+20,380);
	
	if (point_dist==0)
	{
	            X2=20;
		}
	else
	{
	    X2+=segm;
		  
		}
	 
	etich[rep]->move(X2,Y_COORDB+5);
	etich[rep]->show();
       
	    arch[rep] = new QCanvasLine(canvas);
	    arch[rep]->setPen( QPen(QColor(Qt::red), 1) );
	    
	    arch_real[rep] = new QCanvasLine(canvas);
	    arch_real[rep]->setPen( QPen(QColor(Qt::green), 1) );
	    
	    ref[rep] = new QCanvasLine(canvas);
	      ref[rep]->setPen( QPen(QColor(Qt::lightGray), 1) );
	         ref[rep]->setPoints(X2,Y_COORDB,X2,Y_COORDA);
		 
		 switch (point_dist)
		 {
		 case 0:
	             break;		
	             default:
		 ref[rep]->show();
		 }
	             
		     
	    Y2 = Y_COORDB-ordinata;	


                     arch[rep]->setPoints(X1,Y1,X2,Y2);			// disegna la traiettoria enfatizzata
		     
		     
	 Y2R = static_cast<int>(Y_COORDB - ( (X_LENGHT / (distanza_massima*100))*ordinata)) ;								     
		     
		arch_real[rep]->setPoints(X1,Y1R,X2,Y2R); 	// disegna la traiettoria approssimata
		
		
		
		showord[rep] = new QCanvasText(canvas);
		showord[rep]->setText(QString::number(ordinata));
		
		
		switch (jump)
		{
		case 0:
		    showord[rep]->move(X2,Y2R-20);
		    jump=1;
	             break;		
	             default:
		    showord[rep]->move(X2,Y2R-80);	
		    jump=0;
		}
		
	
		if(isTooHigh==FALSE){
		    arch[rep]->show();}
	     
	     arch_real[rep]->show();
	     showord[rep]->show();
	     
	     X1=X2;
	     Y1=Y2;
	     Y1R=Y2R;
	     
	     point_dist+=10;
	     point_ord+=10;
         }
    vista->update();
    
   
}
    

void frmtraiett::destroy()
{

}




void frmtraiett::print_bal()
{
    QPrinter *printer;
  printer = new QPrinter;
    if ( printer->setup(this) ) 
                {
	QPainter pp(printer);
	canvas->drawArea(QRect(0,0,canvas->width(),canvas->height()),&pp,FALSE);
                }

}






void frmtraiett::salva_bmap()
{
QString filename = QFileDialog::getSaveFileName(
			    QString::null, "bitmap (*.bmp)",
			    this, tr("Esporta grafico come bitmap"),
			    tr("Qt Ballit - Esporta grafico come bitmap") );
    if ( QPixmap::grabWidget( vista ).save( filename,filename.mid( filename.findRev( '.' ) + 1 ).upper() ))
	
	
	statusBar()->message( QString( tr("Salvato \'%1\'") ).arg( filename ), 2000 );
    else
	statusBar()->message( QString( tr("Impossibile salvare \'%1\'") ).
				arg( filename ), 2000 );
    

}
