|
|
|
// 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) {
|
|
|
|
init(0, 0, 0);
|
|
|
|
|
|
|
|
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); // This can hang as 'value' may (randomly) have an insanely high precision that is very difficult to convert to text
|
|
|
|
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) {
|
|
|
|
Q_UNUSED(ival);
|
|
|
|
emit floatValueChanged( value );
|
|
|
|
}
|
|
|
|
|
|
|
|
FloatSpinBox::~FloatSpinBox()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|