summaryrefslogtreecommitdiffstats
path: root/src/gui/doublespinbox.h
blob: 75f6c9016e4c0ad349801d932a10969f5b0b23b1 (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
124
125
126
127
128
129
130
131
/***************************************************************************
 *   Copyright (C) 2003-2004 by David Saxton                               *
 *   [email protected]                                                    *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

#ifndef DOUBLESPINBOX_H
#define DOUBLESPINBOX_H

#include <qspinbox.h>

/**
Where appropriate, function names with value in them should
be prefixed with "real" - e.g. realValue() - to get the value stored in the
spin box plus the SI magnitude symbol it is showing

@author David Saxton
*/
class DoubleSpinBox : public QSpinBox
{
	Q_OBJECT
	public:
		DoubleSpinBox( double lower, double upper, double minAbs, double value, const QString & unit, QWidget * parent = 0 );
		virtual ~DoubleSpinBox();
		
		/**
		 * The minimum value is the lowest number that the user can enter.
		 */
		double minValue() const { return m_minValue; }
		/**
		 * @see minValue
		 */
		void setMinValue( double minValue ) { m_minValue = minValue; }
		/**
		 * The minimum value is the lowest number that the user can enter.
		 */
		void setMinValue( int minValue ) { m_minValue = minValue; }
		/**
		 * The maximum value is the highest number that the user can enter.
		 */
		double maxValue() const { return m_maxValue; }
		/**
		 * @see maxValue
		 */
		void setMaxValue( double maxValue ) { m_maxValue = maxValue; }
		/**
		 * @see maxValue
		 */
		void setMaxValue( int maxValue ) { m_maxValue = maxValue; }
		/**
		 * The minimum absolute value is the smallest value that the user can
		 * enter before the value is considered 0.
		 */
		void setMinAbsValue( double minAbsValue ) { m_minAbsValue = minAbsValue; }
		/**
		 * The actual value that the user has entered - e.g. if the spinbox
		 * displays "100 kF", then the value returned will be 1e5.
		 */
		double value();
		/**
		 * Set the value to be displayed - e.g. if value is 1e5, then the
		 * spinbox might display "100 kF".
		 */
		void setValue( double value );
		/**
		 * Sets the unit used, e.g. "F"
		 */
		void setUnit( const QString & unit );
		
	public slots:
		virtual void stepUp();
		virtual void stepDown();
	
	signals:
		/**
		 * This value is emitted whenever the value of the spinbox changes.
		 */
		void valueChanged( double value );
		
	protected slots:
		/**
		 * Checks if the value has changed - and if so, emits a valueChanged
		 * signal.
		 */
		void checkIfChanged();
		/**
		 * Sets the suffix from m_queuedSuffix. Called from QTimer::singleShot 
		 * to avoid strange recursion problems.
		 */
		void setQueuedSuffix();
	
	protected:
		/**
		 * Updates the suffix using m_unit and value.
		 */
		void updateSuffix( double value );
		/**
		 * Returns the multiplication number from what is displayed
		 * in the box, e.g. "10 kV" will return "1000" due to the letter "k" presence
		 */
		double getMult();
		/**
		 * Returns the number currently displayed in the spin box.
		 */
		double getDisplayedNumber( bool * ok );
		/**
		 *  Overloaded the method in QSpinxBox to allow SI prefixes to be entered
		 */
		virtual int mapTextToValue( bool * ok );
		/**
		 *  Overloaded the method in QSpinxBox to allow SI prefixes to be entered
		 */
		virtual QString mapValueToText( int v );
		/**
		 * Returns value rounded off to one significant figure.
		 */
		double roundToOneSF( double value );
		
		QString m_queuedSuffix; ///< Used
		QString m_unit;
		double m_minValue;
		double m_maxValue;
		double m_minAbsValue;
		double m_lastEmittedValue;
};

#endif