summaryrefslogtreecommitdiffstats
path: root/clients/tde/src/widgets/floatspinbox.cpp
blob: 266225af57a1f7ec8be52d5ac48ae9df5cf8d1b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 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()
{
	//
}