summaryrefslogtreecommitdiffstats
path: root/clients/tde/src/widgets/floatspinbox.cpp
blob: 3dbf346266f058ef5b876d3bb6114ad81f36f8cb (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Floating-point SpinBox Widget
 *
 * 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 3 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * (c) 2012 Timothy Pearson
 * Raptor Engineering
 * http://www.raptorengineeringinc.com
 *
 * Original Author: Ewald R. de Wit
 * From Qt-Interest mailing list
 * http://lists.trolltech.com/qt-interest/1999-07/thread00400-0.html
 */

#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, TQ_SIGNAL(valueChanged(int)), TQ_SLOT(acceptValueChanged(int)) );
}

FloatSpinBox::FloatSpinBox(TQWidget *parent , const char* name) : TQSpinBox(parent, name) {
	init(0, 0, 0);

	connect( this, TQ_SIGNAL(valueChanged(int)), TQ_SLOT(acceptValueChanged(int)) );
}

void FloatSpinBox::init(double fmin, double fmax, double fvalue, int precision) {
	min = fmin;
	max = fmax;
	value = fvalue;
	
	// How many decimals after the floating point?
	if (precision < 0) {
		dec = ((fmax - fmin) == 0) ? 2 : 2 - (int)( log10(fabs(fmax - fmin)) );
		if (dec < 0) dec = 0;
	}
	else {
		dec = precision;
	}
	
	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, dec);
}

void FloatSpinBox::setFloatMax(double fmax) {
	init(min, fmax, value, dec);
}

void FloatSpinBox::setPrecision(int precision) {
	init(min, max, value, precision);
}

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 )) );
}

double FloatSpinBox::floatValue() {
	return value;
}

void FloatSpinBox::setEnabled(bool enable) {
	if (enable != isEnabled()) {
		TQSpinBox::setEnabled(enable);
	}
}

void FloatSpinBox::acceptValueChanged(int ival) {
	Q_UNUSED(ival);
	emit floatValueChanged( value );
}

FloatSpinBox::~FloatSpinBox()
{
	//
}

#include "floatspinbox.moc"