]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/frequencyformatter.hxx
Reset: work with threaded OSG modes
[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 ) :
9     _freqNode( freqNode ),
10     _fmtFreqNode( fmtFreqNode ),
11     _channelSpacing(channelSpacing)
12   {
13     _freqNode->addChangeListener( this );
14     valueChanged(_freqNode);
15   }
16   ~FrequencyFormatter()
17   {
18     _freqNode->removeChangeListener( this );
19   }
20
21   void valueChanged (SGPropertyNode * prop)
22   {
23     // format as fixed decimal "nnn.nn"
24     std::ostringstream buf;
25     buf << std::fixed 
26         << std::setw(5) 
27         << std::setfill('0') 
28         << std::setprecision(2)
29         << getFrequency();
30     _fmtFreqNode->setStringValue( buf.str() );
31   }
32
33   double getFrequency() const 
34   {
35     double d = SGMiscd::roundToInt(_freqNode->getDoubleValue() / _channelSpacing) * _channelSpacing;
36     // strip last digit, do not round
37     return ((int)(d*100))/100.0;
38   }
39
40 private:
41   SGPropertyNode_ptr _freqNode;
42   SGPropertyNode_ptr _fmtFreqNode;
43   double _channelSpacing;
44 };
45
46
47 #endif //__FREQUENCY_FORMATTER_HXX