You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.0 KiB

// Copyright: See COPYING file that comes with this distribution
//
// Original Author: Ewald R. de Wit
// From Qt-Interest mailing list
// http://lists.trolltech.com/qt-interest/1999-07/thread00400-0.html
//
// (c) 2012 Timothy Pearson <kb9vqf@pearsoncomputing.net>
#include <tqvalidator.h>
#include <math.h>
#include "floatspinbox.h"
#define ROUND(x) ((int)(0.5 + (x)))
FloatSpinBox::FloatSpinBox(double fmin, double fmax, double fvalue, TQWidget *parent) : TQSpinBox(parent)
{
init(fmin, fmax, fvalue);
connect( this, SIGNAL(valueChanged(int)), SLOT(acceptValueChanged(int)) );
}
FloatSpinBox::FloatSpinBox(TQWidget *parent , const char* name) : TQSpinBox(parent, name)
{
connect( this, SIGNAL(valueChanged(int)), SLOT(acceptValueChanged(int)) );
}
void FloatSpinBox::init(double fmin, double fmax, double fvalue) {
min = fmin;
max = fmax;
value = fvalue;
// How many decimals after the floating point?
dec = ((fmax - fmin) == 0) ? 2 : 2 - (int)( log10(fabs(fmax - fmin)) );
if (dec < 0) dec = 0;
int intmax = (int)((max - min) * pow( 10, dec ));
int intval = ROUND( (value - min) * pow( 10, dec ) );
setRange( 0, intmax );
setValue( intval );
setSteps( 10, 100 );
TQDoubleValidator *validator = new TQDoubleValidator(min, max, dec, this);
setValidator(validator);
}
void FloatSpinBox::setFloatMin(double fmin) {
init(fmin, max, value);
}
void FloatSpinBox::setFloatMax(double fmax) {
init(min, fmax, value);
}
TQString FloatSpinBox::mapValueToText(int ival)
{
TQString str;
value = min + (double)ival * pow(10, -dec);
str.sprintf("%.*f", dec, value);
return( str );
}
int FloatSpinBox::mapTextToValue (bool * ok)
{
TQString str = cleanText();
double tryValue = str.toDouble( ok );
if (*ok) {
value = tryValue;
}
return ROUND( (value - min) * pow( 10, dec ) );
}
void FloatSpinBox::setFloatValue(double d)
{
value = d;
setValue( ROUND( (value - min) * pow( 10, dec )) );
}
void FloatSpinBox::acceptValueChanged(int ival)
{
emit floatValueChanged( value );
}
FloatSpinBox::~FloatSpinBox()
{
//
}