]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/frequencyformatter.hxx
Canvas: update for new bounding box getters.
[flightgear.git] / src / Instrumentation / frequencyformatter.hxx
1 #ifndef __FREQUENCY_FORMATTER_HXX
2 #define __FREQUENCY_FORMATTER_HXX
3
4 /* ------------- A NAV/COMM Frequency formatter ---------------------- */
5
6 class FrequencyFormatter : public SGPropertyChangeListener {
7 public:
8   FrequencyFormatter( SGPropertyNode_ptr freqNode, SGPropertyNode_ptr fmtFreqNode, double channelSpacing, double min, double max ) :
9     _freqNode( freqNode ),
10     _fmtFreqNode( fmtFreqNode ),
11     _channelSpacing(channelSpacing),
12     _min(min),
13     _max(max)
14   {
15     _freqNode->addChangeListener( this );
16     valueChanged(_freqNode);
17   }
18   ~FrequencyFormatter()
19   {
20     _freqNode->removeChangeListener( this );
21   }
22
23   void valueChanged (SGPropertyNode * prop)
24   {
25     // format as fixed decimal "nnn.nn"
26     std::ostringstream buf;
27     buf << std::fixed 
28         << std::setw(5) 
29         << std::setfill('0') 
30         << std::setprecision(2)
31         << getFrequency();
32     _fmtFreqNode->setStringValue( buf.str() );
33   }
34
35   double getFrequency() const 
36   {
37     double d = SGMiscd::roundToInt(_freqNode->getDoubleValue() / _channelSpacing) * _channelSpacing;
38     // strip last digit, do not round
39     double f = ((int)(d*100))/100.0;
40     if( f < _min ) return _min;
41     if( f >= _max ) return _max;
42     return f;
43   }
44
45 private:
46   SGPropertyNode_ptr _freqNode;
47   SGPropertyNode_ptr _fmtFreqNode;
48   double _channelSpacing;
49   double _min;
50   double _max;
51 };
52
53
54 #endif //__FREQUENCY_FORMATTER_HXX