1 #ifndef __FREQUENCY_FORMATTER_HXX
2 #define __FREQUENCY_FORMATTER_HXX
4 /* ------------- A NAV/COMM Frequency formatter ---------------------- */
5 class FrequencyFormatterBase : public SGReferenced {
7 virtual ~FrequencyFormatterBase()
11 virtual double getFrequency() const = 0;
15 class FrequencyFormatter : public FrequencyFormatterBase, public SGPropertyChangeListener {
17 FrequencyFormatter( SGPropertyNode_ptr freqNode, SGPropertyNode_ptr fmtFreqNode, double channelSpacing, double min, double max ) :
18 _freqNode( freqNode ),
19 _fmtFreqNode( fmtFreqNode ),
20 _channelSpacing(channelSpacing),
24 _freqNode->addChangeListener( this, true );
26 virtual ~FrequencyFormatter()
28 _freqNode->removeChangeListener( this );
31 void valueChanged (SGPropertyNode * prop)
33 // format as fixed decimal "nnn.nn"
34 std::ostringstream buf;
38 << std::setprecision(2)
40 _fmtFreqNode->setStringValue( buf.str() );
43 virtual double getFrequency() const
45 double d = SGMiscd::roundToInt(_freqNode->getDoubleValue() / _channelSpacing) * _channelSpacing;
46 // strip last digit, do not round
47 double f = ((int)(d*100))/100.0;
48 if( f < _min ) return _min;
49 if( f >= _max ) return _max;
54 SGPropertyNode_ptr _freqNode;
55 SGPropertyNode_ptr _fmtFreqNode;
56 double _channelSpacing;
62 #endif //__FREQUENCY_FORMATTER_HXX