/***************************************************************************
                          timespinbox.cpp  -  description
                             -------------------
    begin                : Sun Mar 31 2002
    copyright            : (C) 2002 by Jason Harris
    email                : kstars@30doradus.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

//Time steps:
//0-9:   0 sec, 0.1, 0.25, 0.5, 1, 2, 5, 10, 20, 30 sec
//10-14: 1 minute, 2, 5, 10, 15, 30 min
//15-19: 1 hour, 2, 4, 6, 12 hours
//20-23: 1 day, 2, 3, 5 days
//24-26: 1 week, 2, 3 weeks
//27-32: 1 month, 2, 3, 4, 6, 9 months
//33-41: 1 year, 2, 3, 4, 5, 10, 25, 50, 100 years

#include <qlineedit.h>
#include <kdebug.h>
#include <klocale.h>
#include <stdlib.h>
#include <math.h>

#include "timespinbox.h"

#define SECS_PER_DAY 86400.
#define SIDEREAL_YEAR 31558149.77
#define TROPICAL_YEAR 31556925.19

TimeSpinBox::TimeSpinBox( QWidget *parent, const char *name )
	: QSpinBox ( -41, 41, 1 /* step */, parent, name )
{
	setValidator( 0 );
	setButtonSymbols( QSpinBox::PlusMinus );
	editor()->setReadOnly( true );
	setValue( 4 ); //1 second (real time)

	TimeScale[0] = 0.0;          // 0.0 sec
	TimeScale[1] = 0.1;          // 0.1 sec
	TimeScale[2] = 0.25;         // 0.25 sec
	TimeScale[3] = 0.5;          // 0.5 sec 
	TimeScale[4] = 1.0;          // 1 sec (real-time)
	TimeScale[5] = 2.0;          // 2 sec
	TimeScale[6] = 5.0;          // 5 sec
	TimeScale[7] = 10.0;         // 10 sec
	TimeScale[8] = 20.0;         // 20 sec
	TimeScale[9] = 30.0;         // 30 sec
	TimeScale[10] = 60.0;        // 1 min
	TimeScale[11] = 120.0;       // 2 min
	TimeScale[12] = 300.0;       // 5 min
	TimeScale[13] = 600.0;       // 10 min
	TimeScale[14] = 900.0;       // 15 min
	TimeScale[15] = 1800.0;      // 30 min
	TimeScale[16] = 3600.0;      // 1 hr
	TimeScale[17] = 7200.0;      // 2 hr
	TimeScale[18] = 10800.0;     // 3 hr
	TimeScale[19] = 21600.0;     // 6 hr
	TimeScale[20] = 43200.0;     // 12 hr
	TimeScale[21] = SECS_PER_DAY;     // 1 day
	TimeScale[22] = 2.*SECS_PER_DAY;  // 2 days
	TimeScale[23] = 3.*SECS_PER_DAY;  // 3 days
	TimeScale[24] = 5.*SECS_PER_DAY;  // 5 days
	TimeScale[25] = 7.*SECS_PER_DAY;  // 1 week
	TimeScale[26] = 14.*SECS_PER_DAY; //2 weeks
	TimeScale[27] = 21.*SECS_PER_DAY; //3 weeks    
//Months aren't a simple measurement of time; I'll just use fractions of a year
	TimeScale[28] = SIDEREAL_YEAR/12.0; // 1 month
	TimeScale[29] = SIDEREAL_YEAR/6.0;  // 2 months
	TimeScale[30] = 0.25*SIDEREAL_YEAR; // 3 months
	TimeScale[31] = SIDEREAL_YEAR/3.0;  // 4 months
	TimeScale[32] = 0.5*SIDEREAL_YEAR;  // 6 months
	TimeScale[33] = 0.75*SIDEREAL_YEAR; // 9 months
	TimeScale[34] = SIDEREAL_YEAR;       // 1 year
	TimeScale[35] = 2.0*SIDEREAL_YEAR;   // 2 years
	TimeScale[36] = 3.0*SIDEREAL_YEAR;   // 3 years
	TimeScale[37] = 5.0*SIDEREAL_YEAR;   // 5 years
	TimeScale[38] = 10.0*SIDEREAL_YEAR;  // 10 years
	TimeScale[39] = 25.0*SIDEREAL_YEAR;  // 25 years
	TimeScale[40] = 50.0*SIDEREAL_YEAR;  // 50 years
	TimeScale[41] = 100.0*SIDEREAL_YEAR; // 100 years

	TimeString.append( "0 " + i18n( "seconds", "secs" ));
	TimeString.append( "0.1 " + i18n( "seconds", "secs" ));
	TimeString.append( "0.25 " + i18n( "seconds", "secs" ));
	TimeString.append( "0.5 " + i18n( "seconds", "secs" ));
	TimeString.append( "1 " + i18n( "second", "sec" ));
	TimeString.append( "2 " + i18n( "seconds", "secs" ));
	TimeString.append( "5 " + i18n( "seconds", "secs" ));
	TimeString.append( "10 " + i18n( "seconds", "secs" ));
	TimeString.append( "20 " + i18n( "seconds", "secs" ));
	TimeString.append( "30 " + i18n( "seconds", "secs" ));
	TimeString.append( "1 " + i18n( "minute", "min" ));
	TimeString.append( "2 " + i18n( "minutes", "mins" ));
	TimeString.append( "5 " + i18n( "minutes", "mins" ));
	TimeString.append( "10 " + i18n( "minutes", "mins" ));
	TimeString.append( "15 " + i18n( "minutes", "mins" ));
	TimeString.append( "30 " + i18n( "minutes", "mins" ));
	TimeString.append( "1 " + i18n( "hour" ));
	TimeString.append( "2 " + i18n( "hours", "hrs" ));
	TimeString.append( "3 " + i18n( "hours", "hrs" ));
	TimeString.append( "6 " + i18n( "hours", "hrs" ));
	TimeString.append( "12 " + i18n( "hours", "hrs" ));
	TimeString.append( "1 " + i18n( "day" ));
	TimeString.append( "2 " + i18n( "days" ));
	TimeString.append( "3 " + i18n( "days" ));
	TimeString.append( "5 " + i18n( "days" ));
	TimeString.append( "1 " + i18n( "week" ));
	TimeString.append( "2 " + i18n( "weeks", "wks" ));
	TimeString.append( "3 " + i18n( "weeks", "wks" ));
	TimeString.append( "1 " + i18n( "month" ));
	TimeString.append( "2 " + i18n( "months", "mths" ));
	TimeString.append( "3 " + i18n( "months", "mths" ));
	TimeString.append( "4 " + i18n( "months", "mths" ));
	TimeString.append( "6 " + i18n( "months", "mths" ));
	TimeString.append( "9 " + i18n( "months", "mths" ));
	TimeString.append( "1 " + i18n( "year" ));
	TimeString.append( "2 " + i18n( "years", "yrs" ));
	TimeString.append( "3 " + i18n( "years", "yrs" ));
	TimeString.append( "5 " + i18n( "years", "yrs" ));
	TimeString.append( "10 " + i18n( "years", "yrs" ));
	TimeString.append( "25 " + i18n( "years", "yrs" ));
	TimeString.append( "50 " + i18n( "years", "yrs" ));
	TimeString.append( "100 " + i18n( "years", "yrs" ));

	connect( this, SIGNAL( valueChanged( int ) ), this, SLOT( reportChange() ) );
	updateDisplay();
}

int TimeSpinBox::mapTextToValue( bool *ok ) {
	*ok = true;
	for ( unsigned int i=0; i<42; ++i ) {
		if ( text() == TimeString[i] ) { return i; }
		if ( text().mid(1,text().length()) == TimeString[i] ) { return -1*i; }
	}

	*ok = false;
	return 0;
}

QString TimeSpinBox::mapValueToText( int value ) {
	QString neg("-"), result;
	int posval( abs( value ) );

	result = TimeString[ posval ];

	if ( value<0 ) { result = "-" + result; }
	return result;
}

void TimeSpinBox::changeScale( float x ) {
	//Pick the closest value
	int imin = 0;
	float dx, dxlast(10000000000.0), dxmin(10000000000.0);
	for ( unsigned int i=0; i<42; ++i ) {
		dx = fabs( TimeScale[i] - fabs(x) );
		if ( dx < dxmin ) { imin = i; dxmin = dx; }
		if ( dx > dxlast ) break; //we have passed the minimum dx
		dxlast = dx;
	}

	if ( x < 0.0 ) imin *= -1;
	setValue( imin );
}

float TimeSpinBox::timeScale( void ) const {
	return value() > 0 ? TimeScale[ value() ] : -1.*TimeScale[ abs(value()) ];
}

void TimeSpinBox::reportChange() {
	kdDebug() << i18n( "Reporting new timestep value: " ) << timeScale() << endl;
	emit scaleChanged( timeScale() );
}

#include "timespinbox.moc"
